Skip to content

Commit

Permalink
#update for backtest_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yutiansut committed Oct 14, 2018
1 parent 20211e2 commit 5f763e0
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 34 deletions.
18 changes: 12 additions & 6 deletions EXAMPLE/test_backtest/simplebacktest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ def simple_backtest(AC, code, start, end):
code=item.code[0], time=item.date[0], amount=1000, towards=QA.ORDER_DIRECTION.BUY, price=0, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT
)
if order:
Broker.receive_order(QA.QA_Event(order=order, market_data=item))
trade_mes = Broker.query_orders(AC.account_cookie, 'filled')
res = trade_mes.loc[order.account_cookie, order.realorder_id]
Broker.receive_order(QA.QA_Event(
order=order, market_data=item))
trade_mes = Broker.query_orders(
AC.account_cookie, 'filled')
res = trade_mes.loc[order.account_cookie,
order.realorder_id]
order.trade(res.trade_id, res.trade_price,
res.trade_amount, res.trade_time)

Expand All @@ -84,9 +87,12 @@ def simple_backtest(AC, code, start, end):
code=item.code[0], time=item.date[0], amount=1000, towards=QA.ORDER_DIRECTION.SELL, price=0, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT
)
if order:
Broker.receive_order(QA.QA_Event(order=order, market_data=item))
trade_mes = Broker.query_orders(AC.account_cookie, 'filled')
res = trade_mes.loc[order.account_cookie, order.realorder_id]
Broker.receive_order(QA.QA_Event(
order=order, market_data=item))
trade_mes = Broker.query_orders(
AC.account_cookie, 'filled')
res = trade_mes.loc[order.account_cookie,
order.realorder_id]
order.trade(res.trade_id, res.trade_price,
res.trade_amount, res.trade_time)
AC.settle()
Expand Down
6 changes: 4 additions & 2 deletions QUANTAXIS/QACmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@
import subprocess
import requests


from QUANTAXIS.QACmd.runner import run_backtest, run
from QUANTAXIS.QAApplication.QAAnalysis import QA_backtest_analysis_backtest
from QUANTAXIS.QAUtil import QA_util_log_info, QA_Setting, QA_util_mongo_initial
from QUANTAXIS.QASU.main import (QA_SU_save_stock_list, QA_SU_save_stock_min, QA_SU_save_stock_xdxr,
QA_SU_save_stock_block, QA_SU_save_stock_info, QA_SU_save_stock_info_tushare,
QA_SU_save_stock_day, QA_SU_save_index_day, QA_SU_save_index_min, QA_SU_save_future_list, QA_SU_save_index_list,
QA_SU_save_etf_day, QA_SU_save_etf_min, QA_SU_save_financialfiles,
QA_SU_save_option_day, QA_SU_save_option_min, QA_SU_save_option_commodity_day, QA_SU_save_option_commodity_min,
QA_SU_save_future_day, QA_SU_save_future_min,QA_SU_save_report_calendar_day,
QA_SU_save_report_calendar_his,QA_SU_save_stock_divyield_day,
QA_SU_save_future_day, QA_SU_save_future_min, QA_SU_save_report_calendar_day,
QA_SU_save_report_calendar_his, QA_SU_save_stock_divyield_day,
QA_SU_save_stock_divyield_his)
from QUANTAXIS.QASU.save_binance import QA_SU_save_binance_symbol, QA_SU_save_binance_1hour, \
QA_SU_save_binance_1day, QA_SU_save_binance_1min, QA_SU_save_binance
Expand Down
35 changes: 35 additions & 0 deletions QUANTAXIS/QACmd/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import shlex
import subprocess
import sys

from QUANTAXIS.QAUtil.QALogs import QA_util_log_info


def run_backtest(shell_cmd):
shell_cmd = 'python "{}"'.format(shell_cmd)
cmd = shlex.split(shell_cmd)
p = subprocess.Popen(
cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while p.poll() is None:
line = p.stdout.readline()
line = line.strip()
if line:

QA_util_log_info(line)
#print('QUANTAXIS: [{}]'.format(line))
if p.returncode == 0:
QA_util_log_info('backtest run success')

else:
QA_util_log_info('Subprogram failed')
return p.returncode


def run():
shell_cmd = sys.argv[1]
print(shell_cmd)
return run_backtest(shell_cmd)


if __name__ == "__main__":
print(run())
30 changes: 29 additions & 1 deletion QUANTAXIS/QAWeb/commandhandler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

import json
import tornado
import os
import shlex
import subprocess

import tornado
from tornado.web import Application, RequestHandler, authenticated
from tornado.websocket import WebSocketHandler

from QUANTAXIS.QAWeb.basehandles import QABaseHandler, QAWebSocketHandler


Expand All @@ -20,6 +23,31 @@ def get(self):
self.write({'result': 'wrong'})


class RunnerHandler(QAWebSocketHandler):

def on_message(self, shell_cmd):
shell_cmd = 'python "{}"'.format(shell_cmd)
cmd = shlex.split(shell_cmd)
p = subprocess.Popen(
cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while p.poll() is None:
line = p.stdout.readline()
line = line.strip()
if line:

self.write_message(line)
#print('QUANTAXIS: [{}]'.format(line))
if p.returncode == 0:
self.write_message('backtest run success')

else:
self.write_message('Subprogram failed')
# return p.returncode

def on_close(self):
self.write_message('close')


if __name__ == "__main__":

app = Application(
Expand Down
17 changes: 9 additions & 8 deletions QUANTAXIS/QAWeb/quotationhandles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import pandas as pd
import pymongo
import tornado
from tornado.iostream import StreamClosedError
from tornado.web import Application, RequestHandler, authenticated
from tornado.websocket import WebSocketClosedError

import QUANTAXIS as QA
from QUANTAXIS.QAWeb.basehandles import QABaseHandler,QAWebSocketHandler
from tornado.websocket import WebSocketClosedError
from tornado.iostream import StreamClosedError
from QUANTAXIS.QAWeb.basehandles import QABaseHandler, QAWebSocketHandler

"""
要实现2个api
Expand All @@ -44,30 +44,31 @@
2. REALTIME WEBSOCKET
"""
client = set()
client = set()


class INDEX(QABaseHandler):
def get(self):
self.render("./index.html")


class RealtimeSocketHandler(QAWebSocketHandler):
client = set()
client = set()

def open(self):
self.client.add(self)
self.write_message('realtime socket start')


def on_message(self, message):
#assert isinstance(message,str)

try:

database = QA.DATABASE.get_collection(
'realtime_{}'.format(datetime.date.today()))
current = [QA.QA_util_dict_remove_key(item, '_id') for item in database.find({'code': message}, limit=1, sort=[
('datetime', pymongo.DESCENDING)])]

self.write_message(current[0])

except Exception as e:
Expand Down
28 changes: 13 additions & 15 deletions QUANTAXIS/QAWeb/strategyhandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@
# SOFTWARE.

import json
import tornado
import os

import tornado
from tornado.web import Application, RequestHandler, authenticated
from tornado.websocket import WebSocketHandler

from QUANTAXIS.QAFetch.QAQuery import QA_fetch_account, QA_fetch_risk, QA_fetch_strategy
from QUANTAXIS.QASU.save_account import save_account
from QUANTAXIS.QAARP.QAAccount import QA_Account
from QUANTAXIS.QAARP.QARisk import QA_Performance, QA_Risk
from QUANTAXIS.QAFetch.QAQuery import (QA_fetch_account, QA_fetch_risk,
QA_fetch_strategy)
from QUANTAXIS.QASetting.QALocalize import cache_path
from QUANTAXIS.QASU.save_account import save_account
from QUANTAXIS.QASU.user import QA_user_sign_in, QA_user_sign_up
from QUANTAXIS.QAUtil.QASetting import DATABASE
from QUANTAXIS.QAUtil.QARandom import QA_util_random_with_topic
from QUANTAXIS.QAUtil.QASetting import DATABASE
from QUANTAXIS.QAUtil.QASql import QA_util_sql_mongo_setting
from QUANTAXIS.QAWeb.basehandles import QABaseHandler
from QUANTAXIS.QAWeb.util import CJsonEncoder
from QUANTAXIS.QASetting.QALocalize import cache_path


class StrategyHandler(QABaseHandler):
Expand All @@ -60,23 +62,19 @@ def get(self):
self.write('WRONG')






class BacktestHandler(QABaseHandler):
def get(self):
"""[summary]
Arguments:
QABaseHandler {[type]} -- [description]
"""
backtest_name = self.get_argument('strategy_name','all')
if backtest_name =='all':
res=os.listdir(cache_path)
#print(res)
res = [item[0:-3] for item in res if item[-2:]=='py' ]
self.write({'result':res})
backtest_name = self.get_argument('strategy_name', 'all')
if backtest_name == 'all':
res = os.listdir(cache_path)
# print(res)
res = [item[0:-3] for item in res if item[-2:] == 'py']
self.write({'result': res})
return
try:
with open('{}{}{}.py'.format(cache_path, os.sep, backtest_name), 'r') as f:
Expand Down
18 changes: 18 additions & 0 deletions QUANTAXIS/QAWeb/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import shlex
import subprocess

if __name__ == '__main__':
shell_cmd = 'python "E:\\quantaxis\\EXAMPLE\\test_backtest\\simplebacktest.py"'
cmd = shlex.split(shell_cmd)
p = subprocess.Popen(
cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while p.poll() is None:
line = p.stdout.readline()
line = line.strip()
if line:
print('Subprogram output: [{}]'.format(line))
if p.returncode == 0:
print('Subprogram success')
else:
print('Subprogram failed')
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def read(fname):
"""
名字,一般放你包的名字即可
"""
PACKAGES = ["QUANTAXIS", "QUANTAXIS.QAFetch", "QUANTAXIS.QACmd", "QUANTAXIS.QAMarket", 'QUANTAXIS.QAWeb', 'QUANTAXIS.QASetting',
PACKAGES = ["QUANTAXIS", "QUANTAXIS.QAFetch", "QUANTAXIS.QACmd", "QUANTAXIS.QAMarket", 'QUANTAXIS.QAWeb', 'QUANTAXIS.QASetting',"QUANTAXIS.QACmd",
"QUANTAXIS.QAApplication", "QUANTAXIS.QAEngine", "QUANTAXIS.QAData", 'QUANTAXIS.QAData.proto', "QUANTAXIS.QAAnalysis", 'QUANTAXIS.QASelector',
"QUANTAXIS.QASU", "QUANTAXIS.QAUtil", "QUANTAXIS.QAARP", "QUANTAXIS.QAIndicator", "QUANTAXIS_CRAWLY"]
"""
Expand Down Expand Up @@ -115,7 +115,7 @@ def read(fname):
'quantaxis=QUANTAXIS.QACmd:QA_cmd',
'quantaxisd=QUANTAXIS.QAWeb.QA_Web:main',
'quantaxisq=QUANTAXIS.QAFetch.QATdx_adv:bat',
'quantaxisv=QUANTAXIS.QAView.QAWindow:view'
'qarun=QUANTAXIS.QACmd.runner:run'
]
},
# install_requires=requirements,
Expand Down

0 comments on commit 5f763e0

Please sign in to comment.