def load_strategy(__backtest_cls, func, *arg, **kwargs): '策略加载函数' # 首先判断是否能满足回测的要求` __messages = {} __backtest_cls.__init_cash_per_stock = int( float(__backtest_cls.account.init_assest) / len(__backtest_cls.strategy_stock_list)) # 策略的交易日循环 for i in range(int(__backtest_cls.start_real_id), int(__backtest_cls.end_real_id) - 1, 1): __backtest_cls.running_date = __backtest_cls.trade_list[i] QA_util_log_info( '=================daily hold list====================') QA_util_log_info('in the begining of ' + __backtest_cls.running_date) QA_util_log_info( tabulate( __backtest_cls.account.message['body']['account']['hold'])) __backtest_cls.now = __backtest_cls.running_date __backtest_cls.today = __backtest_cls.running_date # 交易前同步持仓状态 __backtest_cls.__sync_order_LM(__backtest_cls, 'init_') # 初始化事件 if __backtest_cls.backtest_type in ['day', 'd', 'index_day']: func(*arg, **kwargs) # 发委托单 __backtest_cls.__sell_from_order_queue(__backtest_cls) elif __backtest_cls.backtest_type in [ '1min', '5min', '15min', 'index_1min', 'index_5min', 'index_15min' ]: if __backtest_cls.backtest_type in ['1min', 'index_1min']: type_ = '1min' elif __backtest_cls.backtest_type in ['5min', 'index_5min']: type_ = '5min' elif __backtest_cls.backtest_type in ['15min', 'index_15min']: type_ = '15min' daily_min = QA_util_make_min_index(__backtest_cls.today, type_) # 创造分钟线index # print(daily_min) for min_index in daily_min: __backtest_cls.now = min_index QA_util_log_info( '=================Min hold list====================') QA_util_log_info('in the begining of %s' % str(min_index)) QA_util_log_info( tabulate(__backtest_cls.account.message['body'] ['account']['hold'])) func(*arg, **kwargs) # 发委托单 __backtest_cls.__sell_from_order_queue(__backtest_cls) if __backtest_cls.backtest_type in [ 'index_1min', 'index_5min', 'index_15min' ]: __backtest_cls.__sync_order_LM(__backtest_cls, 't_0') __backtest_cls.__sync_order_LM(__backtest_cls, 'daily_settle') # 每日结算 # 最后一天 __backtest_cls.__end_of_trading(__backtest_cls)
def _load_strategy(self, *arg, **kwargs): '策略加载函数' # 首先判断是否能满足回测的要求` __messages = {} self.__init_cash_per_stock = int( float(self.account.init_assest) / len(self.strategy_stock_list)) # 策略的交易日循环 for i in range(int(self.start_real_id), int(self.end_real_id)): self.running_date = self.trade_list[i] self.__QA_backtest_log_info( '=================daily hold list====================') self.__QA_backtest_log_info('in the begining of ' + self.running_date) self.__QA_backtest_log_info( tabulate(self.account.message['body']['account']['hold'])) if self.now is not None: self.last_time = self.now self.now = self.running_date self.today = self.running_date # 交易前同步持仓状态 self.__sync_order_LM(self, 'init_') # 初始化事件 if self.backtest_type in ['day', 'd', 'index_day']: _temp = self.market_data.select_time( self.today, self.today).data.set_index('code').close.to_dict() for key in _temp.keys(): self.lastest_price[key] = _temp[key] self.strategy(*arg, **kwargs) # 发委托单 self._deal_from_order_queue() elif self.backtest_type in [ '1min', '5min', '15min', '30min', '60min', 'index_1min', 'index_5min', 'index_15min', 'index_30min', 'index_60min' ]: if self.backtest_type in ['1min', 'index_1min']: type_ = '1min' elif self.backtest_type in ['5min', 'index_5min']: type_ = '5min' elif self.backtest_type in ['15min', 'index_15min']: type_ = '15min' elif self.backtest_type in ['30min', 'index_30min']: type_ = '30min' elif self.backtest_type in ['60min', 'index_60min']: type_ = '60min' daily_min = QA_util_make_min_index(self.today, type_) # 创造分钟线index for min_index in daily_min: self.now = min_index self.__QA_backtest_log_info( '=================Min hold list====================') self.__QA_backtest_log_info('in the begining of %s' % str(min_index)) self.__QA_backtest_log_info( tabulate( self.account.message['body']['account']['hold'])) _temp = self.market_data.select_time( self.now, self.now).data.set_index('code').close.to_dict() for key in _temp.keys(): self.lastest_price[key] = _temp[key] self.strategy(*arg, **kwargs) # 发委托单 self._deal_from_order_queue() if self.backtest_type in [ 'index_1min', 'index_5min', 'index_15min' ]: self.__sync_order_LM('t_0') self.__sync_order_LM('daily_settle') # 每日结算 # 最后一天 self._end_of_trading()
def load_strategy(_cls, func, *arg, **kwargs): '策略加载函数' # 首先判断是否能满足回测的要求` __messages = {} _cls.__init_cash_per_stock = int( float(_cls.account.init_assest) / len(_cls.strategy_stock_list)) # 策略的交易日循环 for i in range(int(_cls.start_real_id), int(_cls.end_real_id)): _cls.running_date = _cls.trade_list[i] _cls.__QA_backtest_log_info( _cls, '=================daily hold list====================') _cls.__QA_backtest_log_info( _cls, 'in the begining of ' + _cls.running_date) _cls.__QA_backtest_log_info( _cls, tabulate(_cls.account.message['body']['account']['hold'])) if _cls.now is not None: _cls.last_time = _cls.now _cls.now = _cls.running_date _cls.today = _cls.running_date # 交易前同步持仓状态 _cls.__sync_order_LM(_cls, 'init_') # 初始化事件 if _cls.backtest_type in ['day', 'd', 'index_day']: _temp = _cls.market_data.select_time( _cls.today, _cls.today).data.set_index('code').close.to_dict() for key in _temp.keys(): _cls.lastest_price[key] = _temp[key] func(*arg, **kwargs) # 发委托单 _cls._deal_from_order_queue(_cls) elif _cls.backtest_type in [ '1min', '5min', '15min', '30min', '60min', 'index_1min', 'index_5min', 'index_15min', 'index_30min', 'index_60min' ]: if _cls.backtest_type in ['1min', 'index_1min']: type_ = '1min' elif _cls.backtest_type in ['5min', 'index_5min']: type_ = '5min' elif _cls.backtest_type in ['15min', 'index_15min']: type_ = '15min' elif _cls.backtest_type in ['30min', 'index_30min']: type_ = '30min' elif _cls.backtest_type in ['60min', 'index_60min']: type_ = '60min' daily_min = QA_util_make_min_index(_cls.today, type_) # 创造分钟线index for min_index in daily_min: _cls.now = min_index _cls.__QA_backtest_log_info( _cls, '=================Min hold list====================') _cls.__QA_backtest_log_info( _cls, 'in the begining of %s' % str(min_index)) _cls.__QA_backtest_log_info( _cls, tabulate( _cls.account.message['body']['account']['hold'])) _temp = _cls.market_data.select_time( _cls.now, _cls.now).data.set_index('code').close.to_dict() for key in _temp.keys(): _cls.lastest_price[key] = _temp[key] func(*arg, **kwargs) # 发委托单 _cls._deal_from_order_queue(_cls) if _cls.backtest_type in [ 'index_1min', 'index_5min', 'index_15min' ]: _cls.__sync_order_LM(_cls, 't_0') _cls.__sync_order_LM(_cls, 'daily_settle') # 每日结算 # 最后一天 _cls._end_of_trading(_cls)