xtquant.qmttools.stgframe
1#coding:utf-8 2 3from . import contextinfo 4from . import functions 5 6import os 7import uuid 8 9from xtquant import xtdata 10from xtquant import xtbson as bson 11from xtquant import xtdata_config 12 13class StrategyLoader: 14 def __init__(this): 15 this.C = None 16 this.main_quote_subid = 0 17 return 18 19 def init(this): 20 C = this.C 21 22 C.guid = C._param.get('guid', str(uuid.uuid4())) 23 C.request_id = C._param.get('requestid', C.guid) 24 C.quote_mode = C._param.get('quote_mode', 'history') #'realtime' 'history' 'all' 25 C.trade_mode = C._param.get('trade_mode', 'backtest') #'simulation' 'trading' 'backtest' 26 C.title = C._param.get('title', '') 27 28 C.stock_code = C._param.get('stock_code', '') 29 C.period = C._param.get('period', '') 30 if type(C.period) == int: 31 C.period = { 32 0 :'tick' 33 , 60000 :'1m' 34 , 180000 :'3m' 35 , 300000 :'5m' 36 , 600000 :'10m' 37 , 900000 :'15m' 38 , 1800000 :'30m' 39 , 3600000 :'1h' 40 , 86400000 :'1d' 41 , 604800000 :'1w' 42 , 2592000000 :'1mon' 43 , 7776000000 :'1q' 44 , 15552000000 :'1hy' 45 , 31536000000 :'1y' 46 }.get(C.period, '') 47 C.dividend_type = C._param.get('dividend_type', 'none') 48 49 functions._request_id = C.request_id 50 xtdata_config.client_guid = C._param.get('clientguid') 51 52 if 1: #register 53 this.create_formula() 54 55 C.init() 56 57 if 1: #fix param 58 if '.' in C.stock_code: 59 pos = C.stock_code.rfind('.') 60 C.stockcode = C.stock_code[0:pos] 61 C.market = C.stock_code[pos + 1:].upper() 62 63 if C.stockcode and C.market: 64 C.stock_code = C.stockcode + '.' + C.market 65 C.period = C.period.lower() 66 67 if 1: #create view 68 if not C._param.get('requestid'): 69 if not C.title: 70 C.title = os.path.basename(os.path.abspath(C.user_script).replace('.py','')) 71 this.create_view(C.title) 72 73 if 1: #post initcomplete 74 init_result = {} 75 76 config_ar = ['request_id', 'quote_mode', 'trade_mode'] 77 init_result['config'] = {ar: C.__getattribute__(ar) for ar in config_ar} 78 79 quote_ar = [ 80 'stock_code', 'stockcode', 'market', 'period' 81 , 'start_time', 'end_time', 'dividend_type' 82 ] 83 init_result['quote'] = {ar: C.__getattribute__(ar) for ar in quote_ar} 84 85 trade_ar = [] 86 init_result['trade'] = {ar: C.__getattribute__(ar) for ar in trade_ar} 87 88 backtest_ar = [ 89 'asset', 'margin_ratio', 'slippage_type', 'slippage' 90 , 'max_vol_rate', 'comsisson_type', 'open_tax', 'close_tax' 91 , 'min_commission', 'open_commission', 'close_commission' 92 , 'close_today_commission', 'benchmark' 93 ] 94 init_result['backtest'] = {ar: C.__getattribute__(ar) for ar in backtest_ar} 95 96 this.call_formula('initcomplete', init_result) 97 98 if 1: 99 this.C.register_callback(0) 100 return 101 102 def shutdown(this): 103 return 104 105 def start(this): 106 C = this.C 107 108 if C.quote_mode in ['history', 'all']: 109 this.load_main_history() 110 111 C.after_init() 112 this.run_bar() 113 114 if C.quote_mode in ['realtime', 'all']: 115 this.load_main_realtime() 116 return 117 118 def stop(this): 119 if this.main_quote_subid: 120 xtdata.unsubscribe_quote(this.main_quote_subid) 121 122 this.C.stop() 123 return 124 125 def run(this): 126 C = this.C 127 128 if C.quote_mode in ['realtime', 'all']: 129 xtdata.run() 130 return 131 132 def load_main_history(this): 133 C = this.C 134 135 data = xtdata.get_market_data_ex( 136 field_list = ['time'], stock_list = [C.stock_code], period = C.period 137 , start_time = '', end_time = '', count = -1 138 , fill_data = False 139 ) 140 141 C.timelist = list(data[C.stock_code]['time']) 142 return 143 144 def load_main_realtime(this): 145 C = this.C 146 147 def on_data(data): 148 data = data.get(C.stock_code, []) 149 if data: 150 tt = data[-1]['time'] 151 this.on_main_quote(tt) 152 return 153 154 this.main_quote_subid = xtdata.subscribe_quote( 155 stock_code = C.stock_code, period = C.period 156 , start_time = '', end_time = '', count = 0 157 , callback = on_data 158 ) 159 return 160 161 def on_main_quote(this, timetag): 162 if not this.C.timelist or this.C.timelist[-1] < timetag: 163 this.C.timelist.append(timetag) 164 this.run_bar() 165 return 166 167 def run_bar(this): 168 C = this.C 169 170 push_timelist = [] 171 for i in range(max(C.lastrunbarpos, 0), len(C.timelist)): 172 C.barpos = i 173 bartime = C.timelist[i] 174 push_timelist.append(bartime) 175 176 this.call_formula('runbar', {'timelist': [bartime]}) 177 178 C.handlebar() 179 C.lastrunbarpos = i 180 181 if 1: 182 push_result = {} 183 push_result['timelist'] = push_timelist 184 push_result['outputs'] = C.push_result 185 C.push_result = {} 186 this.call_formula('index', push_result) 187 return 188 189 def create_formula(this): 190 C = this.C 191 client = xtdata.get_client() 192 data = {'formulaname': C.guid} 193 client.createFormula(C.request_id, bson.BSON.encode(data)) 194 return 195 196 def call_formula(this, func, data): 197 C = this.C 198 client = xtdata.get_client() 199 bresult = client.callFormula(C.request_id, func, bson.BSON.encode(data)) 200 return bson.BSON.decode(bresult) 201 202 def create_view(this, title): 203 C = this.C 204 client = xtdata.get_client() 205 data = {'viewtype': 0,'title':title, 'groupid':-1,'stockcode':C.market + C.stockcode,'period':C.period} 206 client.createView(C.request_id, bson.BSON.encode(data)) 207 return
class
StrategyLoader:
14class StrategyLoader: 15 def __init__(this): 16 this.C = None 17 this.main_quote_subid = 0 18 return 19 20 def init(this): 21 C = this.C 22 23 C.guid = C._param.get('guid', str(uuid.uuid4())) 24 C.request_id = C._param.get('requestid', C.guid) 25 C.quote_mode = C._param.get('quote_mode', 'history') #'realtime' 'history' 'all' 26 C.trade_mode = C._param.get('trade_mode', 'backtest') #'simulation' 'trading' 'backtest' 27 C.title = C._param.get('title', '') 28 29 C.stock_code = C._param.get('stock_code', '') 30 C.period = C._param.get('period', '') 31 if type(C.period) == int: 32 C.period = { 33 0 :'tick' 34 , 60000 :'1m' 35 , 180000 :'3m' 36 , 300000 :'5m' 37 , 600000 :'10m' 38 , 900000 :'15m' 39 , 1800000 :'30m' 40 , 3600000 :'1h' 41 , 86400000 :'1d' 42 , 604800000 :'1w' 43 , 2592000000 :'1mon' 44 , 7776000000 :'1q' 45 , 15552000000 :'1hy' 46 , 31536000000 :'1y' 47 }.get(C.period, '') 48 C.dividend_type = C._param.get('dividend_type', 'none') 49 50 functions._request_id = C.request_id 51 xtdata_config.client_guid = C._param.get('clientguid') 52 53 if 1: #register 54 this.create_formula() 55 56 C.init() 57 58 if 1: #fix param 59 if '.' in C.stock_code: 60 pos = C.stock_code.rfind('.') 61 C.stockcode = C.stock_code[0:pos] 62 C.market = C.stock_code[pos + 1:].upper() 63 64 if C.stockcode and C.market: 65 C.stock_code = C.stockcode + '.' + C.market 66 C.period = C.period.lower() 67 68 if 1: #create view 69 if not C._param.get('requestid'): 70 if not C.title: 71 C.title = os.path.basename(os.path.abspath(C.user_script).replace('.py','')) 72 this.create_view(C.title) 73 74 if 1: #post initcomplete 75 init_result = {} 76 77 config_ar = ['request_id', 'quote_mode', 'trade_mode'] 78 init_result['config'] = {ar: C.__getattribute__(ar) for ar in config_ar} 79 80 quote_ar = [ 81 'stock_code', 'stockcode', 'market', 'period' 82 , 'start_time', 'end_time', 'dividend_type' 83 ] 84 init_result['quote'] = {ar: C.__getattribute__(ar) for ar in quote_ar} 85 86 trade_ar = [] 87 init_result['trade'] = {ar: C.__getattribute__(ar) for ar in trade_ar} 88 89 backtest_ar = [ 90 'asset', 'margin_ratio', 'slippage_type', 'slippage' 91 , 'max_vol_rate', 'comsisson_type', 'open_tax', 'close_tax' 92 , 'min_commission', 'open_commission', 'close_commission' 93 , 'close_today_commission', 'benchmark' 94 ] 95 init_result['backtest'] = {ar: C.__getattribute__(ar) for ar in backtest_ar} 96 97 this.call_formula('initcomplete', init_result) 98 99 if 1: 100 this.C.register_callback(0) 101 return 102 103 def shutdown(this): 104 return 105 106 def start(this): 107 C = this.C 108 109 if C.quote_mode in ['history', 'all']: 110 this.load_main_history() 111 112 C.after_init() 113 this.run_bar() 114 115 if C.quote_mode in ['realtime', 'all']: 116 this.load_main_realtime() 117 return 118 119 def stop(this): 120 if this.main_quote_subid: 121 xtdata.unsubscribe_quote(this.main_quote_subid) 122 123 this.C.stop() 124 return 125 126 def run(this): 127 C = this.C 128 129 if C.quote_mode in ['realtime', 'all']: 130 xtdata.run() 131 return 132 133 def load_main_history(this): 134 C = this.C 135 136 data = xtdata.get_market_data_ex( 137 field_list = ['time'], stock_list = [C.stock_code], period = C.period 138 , start_time = '', end_time = '', count = -1 139 , fill_data = False 140 ) 141 142 C.timelist = list(data[C.stock_code]['time']) 143 return 144 145 def load_main_realtime(this): 146 C = this.C 147 148 def on_data(data): 149 data = data.get(C.stock_code, []) 150 if data: 151 tt = data[-1]['time'] 152 this.on_main_quote(tt) 153 return 154 155 this.main_quote_subid = xtdata.subscribe_quote( 156 stock_code = C.stock_code, period = C.period 157 , start_time = '', end_time = '', count = 0 158 , callback = on_data 159 ) 160 return 161 162 def on_main_quote(this, timetag): 163 if not this.C.timelist or this.C.timelist[-1] < timetag: 164 this.C.timelist.append(timetag) 165 this.run_bar() 166 return 167 168 def run_bar(this): 169 C = this.C 170 171 push_timelist = [] 172 for i in range(max(C.lastrunbarpos, 0), len(C.timelist)): 173 C.barpos = i 174 bartime = C.timelist[i] 175 push_timelist.append(bartime) 176 177 this.call_formula('runbar', {'timelist': [bartime]}) 178 179 C.handlebar() 180 C.lastrunbarpos = i 181 182 if 1: 183 push_result = {} 184 push_result['timelist'] = push_timelist 185 push_result['outputs'] = C.push_result 186 C.push_result = {} 187 this.call_formula('index', push_result) 188 return 189 190 def create_formula(this): 191 C = this.C 192 client = xtdata.get_client() 193 data = {'formulaname': C.guid} 194 client.createFormula(C.request_id, bson.BSON.encode(data)) 195 return 196 197 def call_formula(this, func, data): 198 C = this.C 199 client = xtdata.get_client() 200 bresult = client.callFormula(C.request_id, func, bson.BSON.encode(data)) 201 return bson.BSON.decode(bresult) 202 203 def create_view(this, title): 204 C = this.C 205 client = xtdata.get_client() 206 data = {'viewtype': 0,'title':title, 'groupid':-1,'stockcode':C.market + C.stockcode,'period':C.period} 207 client.createView(C.request_id, bson.BSON.encode(data)) 208 return
def
init(this):
20 def init(this): 21 C = this.C 22 23 C.guid = C._param.get('guid', str(uuid.uuid4())) 24 C.request_id = C._param.get('requestid', C.guid) 25 C.quote_mode = C._param.get('quote_mode', 'history') #'realtime' 'history' 'all' 26 C.trade_mode = C._param.get('trade_mode', 'backtest') #'simulation' 'trading' 'backtest' 27 C.title = C._param.get('title', '') 28 29 C.stock_code = C._param.get('stock_code', '') 30 C.period = C._param.get('period', '') 31 if type(C.period) == int: 32 C.period = { 33 0 :'tick' 34 , 60000 :'1m' 35 , 180000 :'3m' 36 , 300000 :'5m' 37 , 600000 :'10m' 38 , 900000 :'15m' 39 , 1800000 :'30m' 40 , 3600000 :'1h' 41 , 86400000 :'1d' 42 , 604800000 :'1w' 43 , 2592000000 :'1mon' 44 , 7776000000 :'1q' 45 , 15552000000 :'1hy' 46 , 31536000000 :'1y' 47 }.get(C.period, '') 48 C.dividend_type = C._param.get('dividend_type', 'none') 49 50 functions._request_id = C.request_id 51 xtdata_config.client_guid = C._param.get('clientguid') 52 53 if 1: #register 54 this.create_formula() 55 56 C.init() 57 58 if 1: #fix param 59 if '.' in C.stock_code: 60 pos = C.stock_code.rfind('.') 61 C.stockcode = C.stock_code[0:pos] 62 C.market = C.stock_code[pos + 1:].upper() 63 64 if C.stockcode and C.market: 65 C.stock_code = C.stockcode + '.' + C.market 66 C.period = C.period.lower() 67 68 if 1: #create view 69 if not C._param.get('requestid'): 70 if not C.title: 71 C.title = os.path.basename(os.path.abspath(C.user_script).replace('.py','')) 72 this.create_view(C.title) 73 74 if 1: #post initcomplete 75 init_result = {} 76 77 config_ar = ['request_id', 'quote_mode', 'trade_mode'] 78 init_result['config'] = {ar: C.__getattribute__(ar) for ar in config_ar} 79 80 quote_ar = [ 81 'stock_code', 'stockcode', 'market', 'period' 82 , 'start_time', 'end_time', 'dividend_type' 83 ] 84 init_result['quote'] = {ar: C.__getattribute__(ar) for ar in quote_ar} 85 86 trade_ar = [] 87 init_result['trade'] = {ar: C.__getattribute__(ar) for ar in trade_ar} 88 89 backtest_ar = [ 90 'asset', 'margin_ratio', 'slippage_type', 'slippage' 91 , 'max_vol_rate', 'comsisson_type', 'open_tax', 'close_tax' 92 , 'min_commission', 'open_commission', 'close_commission' 93 , 'close_today_commission', 'benchmark' 94 ] 95 init_result['backtest'] = {ar: C.__getattribute__(ar) for ar in backtest_ar} 96 97 this.call_formula('initcomplete', init_result) 98 99 if 1: 100 this.C.register_callback(0) 101 return
def
load_main_history(this):
133 def load_main_history(this): 134 C = this.C 135 136 data = xtdata.get_market_data_ex( 137 field_list = ['time'], stock_list = [C.stock_code], period = C.period 138 , start_time = '', end_time = '', count = -1 139 , fill_data = False 140 ) 141 142 C.timelist = list(data[C.stock_code]['time']) 143 return
def
load_main_realtime(this):
145 def load_main_realtime(this): 146 C = this.C 147 148 def on_data(data): 149 data = data.get(C.stock_code, []) 150 if data: 151 tt = data[-1]['time'] 152 this.on_main_quote(tt) 153 return 154 155 this.main_quote_subid = xtdata.subscribe_quote( 156 stock_code = C.stock_code, period = C.period 157 , start_time = '', end_time = '', count = 0 158 , callback = on_data 159 ) 160 return
def
run_bar(this):
168 def run_bar(this): 169 C = this.C 170 171 push_timelist = [] 172 for i in range(max(C.lastrunbarpos, 0), len(C.timelist)): 173 C.barpos = i 174 bartime = C.timelist[i] 175 push_timelist.append(bartime) 176 177 this.call_formula('runbar', {'timelist': [bartime]}) 178 179 C.handlebar() 180 C.lastrunbarpos = i 181 182 if 1: 183 push_result = {} 184 push_result['timelist'] = push_timelist 185 push_result['outputs'] = C.push_result 186 C.push_result = {} 187 this.call_formula('index', push_result) 188 return