xtquant.qmttools.contextinfo
1#coding:utf-8 2 3import datetime as dt 4import threading 5 6import pandas as pd 7 8from . import functions 9 10class ContextInfo: 11 def __init__(this): 12 #base 13 this.request_id = '' 14 this.quote_mode = '' #'realtime' 'history' 'all' 15 this.trade_mode = '' #'simulation' 'trading' 'backtest' 16 this.title = '' 17 this.user_script = '' 18 19 #quote 20 this.stock_code = '' 21 this.stockcode = '' 22 this.market = '' 23 this.period = '' 24 this.start_time = '' 25 this.end_time = '' 26 this.dividend_type = '' 27 28 #bar frame 29 this.timelist = [] 30 this.barpos = -1 31 this.lastrunbarpos = -1 32 this.result = {} 33 this.push_result = {} 34 35 #backtest 36 this.asset = 1000000.0 # 初始资金 37 this.margin_ratio = 0.05 # 保证金比例 38 this.slippage_type = 2 # 滑点类型 39 this.slippage = 0.0 # 滑点值 40 this.max_vol_rate = 1.0 # 最大成交比例 41 this.comsisson_type = 0 # 手续费类型 42 this.open_tax = 0.0 # 买入印花税 43 this.close_tax = 0.0 # 卖出印花税 44 this.min_commission = 0.0 # 最低佣金 45 this.open_commission = 0.0 # 买入佣金 46 this.close_commission = 0.0 # 平昨佣金 47 this.close_today_commission = 0.0 # 平今佣金 48 this.benchmark = '000300.SH' # 业绩基准 49 50 this.capital = None 51 this.do_back_test = None 52 53 #reserved 54 this.refresh_rate = None 55 this.fund_name = None 56 this.link_fund_name = None 57 this.data_info_level = None 58 this.time_tick_size = None 59 return 60 61 ### qmt strategy frame ### 62 63 def init(this): 64 return 65 66 def after_init(this): 67 return 68 69 def handlebar(this): 70 return 71 72 def stop(this): 73 return 74 75 def account_callback(this, account_info): 76 return 77 78 def order_callback(this, order_info): 79 return 80 81 def deal_callback(this, deal_info): 82 return 83 84 def position_callback(this, position_info): 85 return 86 87 def orderError_callback(this, passorder_info, msg): 88 return 89 90 ### qmt functions - bar ### 91 92 def is_last_bar(this): 93 return this.barpos >= len(this.timelist) - 1 94 95 def is_new_bar(this): 96 return this.barpos > this.lastbarpos 97 98 def get_bar_timetag(this, barpos = None): 99 try: 100 return this.timelist[barpos] if barpos is not None else this.timelist[this.barpos] 101 except Exception as e: 102 return None 103 104 ### qmt functions - graph ### 105 106 def paint(this, name, value, index = -1, drawstyle = 0, color = '', limit = ''): 107 vp = {str(this.get_bar_timetag()): value} 108 109 if name not in this.result: 110 this.result[name] = {} 111 this.result[name].update(vp) 112 113 if name not in this.push_result: 114 this.push_result[name] = {} 115 this.push_result[name].update(vp) 116 return 117 118 ### qmt functions - quote ### 119 120 def subscribe_quote(this, stock_code = '', period = '', dividend_type = '', result_type = '', callback = None): 121 if not stock_code: 122 stock_code = this.stock_code 123 if not period or period == 'follow': 124 period = this.period 125 if not dividend_type or dividend_type == 'follow': 126 dividend_type = this.dividend_type 127 return functions.subscribe_quote(stock_code, period, dividend_type, result_type, callback) 128 129 def subscribe_whole_quote(this, code_list, callback = None): 130 return functions.subscribe_whole_quote(code_list, callback) 131 132 def unsubscribe_quote(this, subscribe_id): 133 return functions.unsubscribe_quote(subscribe_id) 134 135 def get_market_data( 136 this, fields = [], stock_code = [], start_time = '', end_time = '' 137 , skip_paused = True, period = '', dividend_type = '', count = -1 138 ): 139 if not stock_code: 140 stock_code = [this.stock_code] 141 if not period or period == 'follow': 142 period = this.period 143 if not dividend_type or dividend_type == 'follow': 144 dividend_type = this.dividend_type 145 if period != 'tick' and count == -1 and len(fields) == 1: 146 if not end_time or end_time == 'follow': 147 if this.barpos >= 0: 148 end_time = functions.timetag_to_datetime(this.get_bar_timetag(this.barpos)) 149 count = -2 150 if period == 'tick' and count == -1 and len(fields) == 1 and start_time == '' and end_time == '': 151 count = -2 152 153 return functions.get_market_data( 154 fields, stock_code, start_time, end_time 155 , skip_paused, period, dividend_type, count 156 ) 157 158 def get_market_data_ex( 159 this, fields = [], stock_code = [], period = '' 160 , start_time = '', end_time = '', count = -1 161 , dividend_type = '', fill_data = True, subscribe = True 162 ): 163 if not stock_code: 164 stock_code = [this.stock_code] 165 if not period or period == 'follow': 166 period = this.period 167 if not dividend_type or dividend_type == 'follow': 168 dividend_type = this.dividend_type 169 170 return functions.get_market_data_ex( 171 fields, stock_code, period 172 , start_time, end_time, count 173 , dividend_type, fill_data, subscribe 174 ) 175 176 def get_full_tick(this, stock_code = []): 177 if not stock_code: 178 stock_code = [this.stock_code] 179 return functions.get_full_tick(stock_code) 180 181 def get_divid_factors(this, stock_code = '', date = None): 182 if not stock_code: 183 stock_code = this.stock_code 184 return functions.get_divid_factors(stock_code, date) 185 186 ### qmt functions - finance ### 187 188 def get_financial_data(this, field_list, stock_list, start_date, end_date, report_type = 'announce_time'): 189 raise 'not implemented, use get_raw_financial_data instead' 190 return 191 192 def get_raw_financial_data(this, field_list, stock_list, start_date, end_date, report_type = 'announce_time'): 193 return functions.get_raw_financial_data(field_list, stock_list, start_date, end_date, report_type) 194 195 ### qmt functions - static ### 196 197 def get_instrument_detail(this, stock_code = ''): 198 if not stock_code: 199 stock_code = this.stock_code 200 return functions.get_instrument_detail(stock_code) 201 202 get_instrumentdetail = get_instrument_detail # compat 203 204 def get_trading_dates(this, stock_code, start_date, end_date, count, period = '1d'): 205 return functions.get_trading_dates(stock_code, start_date, end_date, count, period) 206 207 def get_stock_list_in_sector(this, sector_name): 208 return functions.get_stock_list_in_sector(sector_name) 209 210 def passorder(this, opType, orderType, accountid, orderCode, prType, modelprice, volume): 211 return functions._passorder_impl(opType, orderType, accountid, orderCode, prType, modelprice, volume, this.get_bar_timetag(), this.request_id) 212 213 def set_auto_trade_callback(this, enable): 214 return functions._set_auto_trade_callback_impl(enable, this.request_id) 215 216 def set_account(this, accountid): 217 return functions.set_account(accountid, this.request_id) 218 219 ### private ### 220 221 def trade_callback(this, type, result, error): 222 class DetailData(object): 223 def __init__(self, _obj): 224 if _obj: 225 self.__dict__.update(_obj) 226 227 if type == 'accountcallback': 228 this.account_callback(DetailData(result)) 229 elif type == 'ordercallback': 230 this.order_callback(DetailData(result)) 231 elif type == 'dealcallback': 232 this.deal_callback(DetailData(result)) 233 elif type == 'positioncallback': 234 this.position_callback(DetailData(result)) 235 elif type == 'ordererrorcallback': 236 this.orderError_callback(DetailData(result.get('passorderArg')), result.get('strMsg')) 237 238 return 239 240 def register_callback(this, reqid): 241 functions.register_external_resp_callback(reqid, this.trade_callback) 242 return 243 244 def get_callback_cache(this, type): 245 return functions._get_callback_cache_impl(type, this.request_id)
class
ContextInfo:
11class ContextInfo: 12 def __init__(this): 13 #base 14 this.request_id = '' 15 this.quote_mode = '' #'realtime' 'history' 'all' 16 this.trade_mode = '' #'simulation' 'trading' 'backtest' 17 this.title = '' 18 this.user_script = '' 19 20 #quote 21 this.stock_code = '' 22 this.stockcode = '' 23 this.market = '' 24 this.period = '' 25 this.start_time = '' 26 this.end_time = '' 27 this.dividend_type = '' 28 29 #bar frame 30 this.timelist = [] 31 this.barpos = -1 32 this.lastrunbarpos = -1 33 this.result = {} 34 this.push_result = {} 35 36 #backtest 37 this.asset = 1000000.0 # 初始资金 38 this.margin_ratio = 0.05 # 保证金比例 39 this.slippage_type = 2 # 滑点类型 40 this.slippage = 0.0 # 滑点值 41 this.max_vol_rate = 1.0 # 最大成交比例 42 this.comsisson_type = 0 # 手续费类型 43 this.open_tax = 0.0 # 买入印花税 44 this.close_tax = 0.0 # 卖出印花税 45 this.min_commission = 0.0 # 最低佣金 46 this.open_commission = 0.0 # 买入佣金 47 this.close_commission = 0.0 # 平昨佣金 48 this.close_today_commission = 0.0 # 平今佣金 49 this.benchmark = '000300.SH' # 业绩基准 50 51 this.capital = None 52 this.do_back_test = None 53 54 #reserved 55 this.refresh_rate = None 56 this.fund_name = None 57 this.link_fund_name = None 58 this.data_info_level = None 59 this.time_tick_size = None 60 return 61 62 ### qmt strategy frame ### 63 64 def init(this): 65 return 66 67 def after_init(this): 68 return 69 70 def handlebar(this): 71 return 72 73 def stop(this): 74 return 75 76 def account_callback(this, account_info): 77 return 78 79 def order_callback(this, order_info): 80 return 81 82 def deal_callback(this, deal_info): 83 return 84 85 def position_callback(this, position_info): 86 return 87 88 def orderError_callback(this, passorder_info, msg): 89 return 90 91 ### qmt functions - bar ### 92 93 def is_last_bar(this): 94 return this.barpos >= len(this.timelist) - 1 95 96 def is_new_bar(this): 97 return this.barpos > this.lastbarpos 98 99 def get_bar_timetag(this, barpos = None): 100 try: 101 return this.timelist[barpos] if barpos is not None else this.timelist[this.barpos] 102 except Exception as e: 103 return None 104 105 ### qmt functions - graph ### 106 107 def paint(this, name, value, index = -1, drawstyle = 0, color = '', limit = ''): 108 vp = {str(this.get_bar_timetag()): value} 109 110 if name not in this.result: 111 this.result[name] = {} 112 this.result[name].update(vp) 113 114 if name not in this.push_result: 115 this.push_result[name] = {} 116 this.push_result[name].update(vp) 117 return 118 119 ### qmt functions - quote ### 120 121 def subscribe_quote(this, stock_code = '', period = '', dividend_type = '', result_type = '', callback = None): 122 if not stock_code: 123 stock_code = this.stock_code 124 if not period or period == 'follow': 125 period = this.period 126 if not dividend_type or dividend_type == 'follow': 127 dividend_type = this.dividend_type 128 return functions.subscribe_quote(stock_code, period, dividend_type, result_type, callback) 129 130 def subscribe_whole_quote(this, code_list, callback = None): 131 return functions.subscribe_whole_quote(code_list, callback) 132 133 def unsubscribe_quote(this, subscribe_id): 134 return functions.unsubscribe_quote(subscribe_id) 135 136 def get_market_data( 137 this, fields = [], stock_code = [], start_time = '', end_time = '' 138 , skip_paused = True, period = '', dividend_type = '', count = -1 139 ): 140 if not stock_code: 141 stock_code = [this.stock_code] 142 if not period or period == 'follow': 143 period = this.period 144 if not dividend_type or dividend_type == 'follow': 145 dividend_type = this.dividend_type 146 if period != 'tick' and count == -1 and len(fields) == 1: 147 if not end_time or end_time == 'follow': 148 if this.barpos >= 0: 149 end_time = functions.timetag_to_datetime(this.get_bar_timetag(this.barpos)) 150 count = -2 151 if period == 'tick' and count == -1 and len(fields) == 1 and start_time == '' and end_time == '': 152 count = -2 153 154 return functions.get_market_data( 155 fields, stock_code, start_time, end_time 156 , skip_paused, period, dividend_type, count 157 ) 158 159 def get_market_data_ex( 160 this, fields = [], stock_code = [], period = '' 161 , start_time = '', end_time = '', count = -1 162 , dividend_type = '', fill_data = True, subscribe = True 163 ): 164 if not stock_code: 165 stock_code = [this.stock_code] 166 if not period or period == 'follow': 167 period = this.period 168 if not dividend_type or dividend_type == 'follow': 169 dividend_type = this.dividend_type 170 171 return functions.get_market_data_ex( 172 fields, stock_code, period 173 , start_time, end_time, count 174 , dividend_type, fill_data, subscribe 175 ) 176 177 def get_full_tick(this, stock_code = []): 178 if not stock_code: 179 stock_code = [this.stock_code] 180 return functions.get_full_tick(stock_code) 181 182 def get_divid_factors(this, stock_code = '', date = None): 183 if not stock_code: 184 stock_code = this.stock_code 185 return functions.get_divid_factors(stock_code, date) 186 187 ### qmt functions - finance ### 188 189 def get_financial_data(this, field_list, stock_list, start_date, end_date, report_type = 'announce_time'): 190 raise 'not implemented, use get_raw_financial_data instead' 191 return 192 193 def get_raw_financial_data(this, field_list, stock_list, start_date, end_date, report_type = 'announce_time'): 194 return functions.get_raw_financial_data(field_list, stock_list, start_date, end_date, report_type) 195 196 ### qmt functions - static ### 197 198 def get_instrument_detail(this, stock_code = ''): 199 if not stock_code: 200 stock_code = this.stock_code 201 return functions.get_instrument_detail(stock_code) 202 203 get_instrumentdetail = get_instrument_detail # compat 204 205 def get_trading_dates(this, stock_code, start_date, end_date, count, period = '1d'): 206 return functions.get_trading_dates(stock_code, start_date, end_date, count, period) 207 208 def get_stock_list_in_sector(this, sector_name): 209 return functions.get_stock_list_in_sector(sector_name) 210 211 def passorder(this, opType, orderType, accountid, orderCode, prType, modelprice, volume): 212 return functions._passorder_impl(opType, orderType, accountid, orderCode, prType, modelprice, volume, this.get_bar_timetag(), this.request_id) 213 214 def set_auto_trade_callback(this, enable): 215 return functions._set_auto_trade_callback_impl(enable, this.request_id) 216 217 def set_account(this, accountid): 218 return functions.set_account(accountid, this.request_id) 219 220 ### private ### 221 222 def trade_callback(this, type, result, error): 223 class DetailData(object): 224 def __init__(self, _obj): 225 if _obj: 226 self.__dict__.update(_obj) 227 228 if type == 'accountcallback': 229 this.account_callback(DetailData(result)) 230 elif type == 'ordercallback': 231 this.order_callback(DetailData(result)) 232 elif type == 'dealcallback': 233 this.deal_callback(DetailData(result)) 234 elif type == 'positioncallback': 235 this.position_callback(DetailData(result)) 236 elif type == 'ordererrorcallback': 237 this.orderError_callback(DetailData(result.get('passorderArg')), result.get('strMsg')) 238 239 return 240 241 def register_callback(this, reqid): 242 functions.register_external_resp_callback(reqid, this.trade_callback) 243 return 244 245 def get_callback_cache(this, type): 246 return functions._get_callback_cache_impl(type, this.request_id)
def
paint(this, name, value, index=-1, drawstyle=0, color='', limit=''):
107 def paint(this, name, value, index = -1, drawstyle = 0, color = '', limit = ''): 108 vp = {str(this.get_bar_timetag()): value} 109 110 if name not in this.result: 111 this.result[name] = {} 112 this.result[name].update(vp) 113 114 if name not in this.push_result: 115 this.push_result[name] = {} 116 this.push_result[name].update(vp) 117 return
def
subscribe_quote( this, stock_code='', period='', dividend_type='', result_type='', callback=None):
121 def subscribe_quote(this, stock_code = '', period = '', dividend_type = '', result_type = '', callback = None): 122 if not stock_code: 123 stock_code = this.stock_code 124 if not period or period == 'follow': 125 period = this.period 126 if not dividend_type or dividend_type == 'follow': 127 dividend_type = this.dividend_type 128 return functions.subscribe_quote(stock_code, period, dividend_type, result_type, callback)
def
get_market_data( this, fields=[], stock_code=[], start_time='', end_time='', skip_paused=True, period='', dividend_type='', count=-1):
136 def get_market_data( 137 this, fields = [], stock_code = [], start_time = '', end_time = '' 138 , skip_paused = True, period = '', dividend_type = '', count = -1 139 ): 140 if not stock_code: 141 stock_code = [this.stock_code] 142 if not period or period == 'follow': 143 period = this.period 144 if not dividend_type or dividend_type == 'follow': 145 dividend_type = this.dividend_type 146 if period != 'tick' and count == -1 and len(fields) == 1: 147 if not end_time or end_time == 'follow': 148 if this.barpos >= 0: 149 end_time = functions.timetag_to_datetime(this.get_bar_timetag(this.barpos)) 150 count = -2 151 if period == 'tick' and count == -1 and len(fields) == 1 and start_time == '' and end_time == '': 152 count = -2 153 154 return functions.get_market_data( 155 fields, stock_code, start_time, end_time 156 , skip_paused, period, dividend_type, count 157 )
def
get_market_data_ex( this, fields=[], stock_code=[], period='', start_time='', end_time='', count=-1, dividend_type='', fill_data=True, subscribe=True):
159 def get_market_data_ex( 160 this, fields = [], stock_code = [], period = '' 161 , start_time = '', end_time = '', count = -1 162 , dividend_type = '', fill_data = True, subscribe = True 163 ): 164 if not stock_code: 165 stock_code = [this.stock_code] 166 if not period or period == 'follow': 167 period = this.period 168 if not dividend_type or dividend_type == 'follow': 169 dividend_type = this.dividend_type 170 171 return functions.get_market_data_ex( 172 fields, stock_code, period 173 , start_time, end_time, count 174 , dividend_type, fill_data, subscribe 175 )
def
get_financial_data( this, field_list, stock_list, start_date, end_date, report_type='announce_time'):
def
get_raw_financial_data( this, field_list, stock_list, start_date, end_date, report_type='announce_time'):
def
trade_callback(this, type, result, error):
222 def trade_callback(this, type, result, error): 223 class DetailData(object): 224 def __init__(self, _obj): 225 if _obj: 226 self.__dict__.update(_obj) 227 228 if type == 'accountcallback': 229 this.account_callback(DetailData(result)) 230 elif type == 'ordercallback': 231 this.order_callback(DetailData(result)) 232 elif type == 'dealcallback': 233 this.deal_callback(DetailData(result)) 234 elif type == 'positioncallback': 235 this.position_callback(DetailData(result)) 236 elif type == 'ordererrorcallback': 237 this.orderError_callback(DetailData(result.get('passorderArg')), result.get('strMsg')) 238 239 return