def __init__(self, data, strategy, account=Account(), init_opt_tau=(1000, ), opt_frequency=(1000, )): super().__init__(data, strategy, account) self.init_opt_tau = init_opt_tau self.opt_frequency = opt_frequency self.opt_count = [0] * len(self.opt_frequency)
def __init__(self, data, strategy, account=Account()): self.data = data # 回测数据 self.strategy = strategy # 回测策略 self.account = account # 账户信息 self.position_list = PositionList() # 仓位信息 self.current_data = None self.open_order_flow = [] # 策略产生的开仓订单流 self.close_order_flow = [] # 策略产生的平仓订单流 self.account_dict = []
def test(): # 获取测试数据 test_data = pd.read_csv('../Data/FutureData/rb-SHF_min.csv', nrows=10000, parse_dates=[0]) test_data['symbol_name'] = 'rb-SHF' test_data.columns = BAR_DATA_COLUMN_NAMES_10 # 回测引擎初始化 account = Account() bt_engine = BacKTestEngine2(account) # 构建策略 symbol = SymbolRB() p_boll = ParameterBoll(tau=120, delta=2, take_profit=5000, stop_days=14) s = StrategyBoll(strategy_id=1, parameter=p_boll, symbol=symbol) # 构建回测 mtt = MyTradeTest(test_data, [s], bt_engine) print('back test begin...') mtt.back_test() print('back test end...') mtt.result_analysis(need_plot=True)
print('send open_buy_order at time:', self.time) else: open_order = MarketOrder(strategy_id=self.strategy_id, symbol=self.symbol, order_type=ORDER_TYPE_SELL, send_time=self.time, lots=1) print('send open_sell_order at time:', self.time) open_order_flow.append(open_order) # 当前策略空仓无需平仓 if len(self.position_ls) == 0: return open_order_flow, close_order_flow # 平仓判断 for pos in self.position_ls: if self.close_condition(pos): order_type = ORDER_TYPE_BUY if pos.position_type == POSITION_TYPE_SHORT else ORDER_TYPE_SELL close_order = MarketOrder(strategy_id=self.strategy_id, symbol=self.symbol, order_type=order_type, send_time=self.time, lots=pos.lots) print('send close order at time:', self.time) close_order_flow.append(close_order) return open_order_flow, close_order_flow if __name__ == '__main__': data = get_future_data(path='../Data/FutureData/', nrows=100000) s = StrategyBoll(strategy_id=1, parameter=ParameterBoll(), symbol=SymbolRB()) account = Account(initial_capital=100000) bt = BackTestEngine(data=data, strategy=[s], account=account) bt.run() bt.result_analysis()
def __init__(self, account=Account()): self.account = account # 资金账户 self.position_list = PositionList() # 仓位信息 self.current_data = None # 回测引擎每次拿到的数据 self.open_order_flow = [] # 策略产生的开仓订单流 self.close_order_flow = [] # 策略产生的平仓订单流