diff --git a/EXAMPLE/test_backtest/simplebacktest.py b/EXAMPLE/test_backtest/simplebacktest.py index d112bdaff..e33e66c5c 100644 --- a/EXAMPLE/test_backtest/simplebacktest.py +++ b/EXAMPLE/test_backtest/simplebacktest.py @@ -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) @@ -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() diff --git a/QUANTAXIS/QACmd/__init__.py b/QUANTAXIS/QACmd/__init__.py index 99ab6f425..42825c1e5 100755 --- a/QUANTAXIS/QACmd/__init__.py +++ b/QUANTAXIS/QACmd/__init__.py @@ -32,6 +32,8 @@ 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, @@ -39,8 +41,8 @@ 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 diff --git a/QUANTAXIS/QACmd/runner.py b/QUANTAXIS/QACmd/runner.py new file mode 100644 index 000000000..7f1c02800 --- /dev/null +++ b/QUANTAXIS/QACmd/runner.py @@ -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()) diff --git a/QUANTAXIS/QAWeb/commandhandler.py b/QUANTAXIS/QAWeb/commandhandler.py index 508b6a6ca..d41cbe0a1 100644 --- a/QUANTAXIS/QAWeb/commandhandler.py +++ b/QUANTAXIS/QAWeb/commandhandler.py @@ -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 @@ -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( diff --git a/QUANTAXIS/QAWeb/quotationhandles.py b/QUANTAXIS/QAWeb/quotationhandles.py index 55d03a8df..20e09a3d5 100644 --- a/QUANTAXIS/QAWeb/quotationhandles.py +++ b/QUANTAXIS/QAWeb/quotationhandles.py @@ -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 @@ -44,7 +44,8 @@ 2. REALTIME WEBSOCKET """ -client = set() +client = set() + class INDEX(QABaseHandler): def get(self): @@ -52,22 +53,22 @@ def get(self): 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: diff --git a/QUANTAXIS/QAWeb/strategyhandlers.py b/QUANTAXIS/QAWeb/strategyhandlers.py index c2bf7c921..8281c0770 100644 --- a/QUANTAXIS/QAWeb/strategyhandlers.py +++ b/QUANTAXIS/QAWeb/strategyhandlers.py @@ -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): @@ -60,10 +62,6 @@ def get(self): self.write('WRONG') - - - - class BacktestHandler(QABaseHandler): def get(self): """[summary] @@ -71,12 +69,12 @@ def get(self): 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: diff --git a/QUANTAXIS/QAWeb/test.py b/QUANTAXIS/QAWeb/test.py new file mode 100644 index 000000000..0aa29d9a9 --- /dev/null +++ b/QUANTAXIS/QAWeb/test.py @@ -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') diff --git a/setup.py b/setup.py index 3ac1d2cf3..b681d8174 100644 --- a/setup.py +++ b/setup.py @@ -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"] """ @@ -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,