self.events.put(order) self.bought[symbol] = "SHORT" elif self.bought[symbol] == "SHORT" and past_return > 0: # 空头翻转 # 先平仓 order = OrderEvent(symbol, "EXIT") self.events.put(order) self.bought[symbol] = "OUT" # 再开仓做多 order = OrderEvent(symbol, "ALLBUY") self.events.put(order) self.bought[symbol] = "LONG" if __name__ == "__main__": csv_dir = '../csv/' start_date = '2005-01-01' end_date = '2017-01-20' symbol_list = ['000300.SH'] fetch_from_wind(csv_dir, symbol_list, start_date, end_date) initial_capital = 100000.0 start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d") heartbeat = 0.0 backtest = Backtest(csv_dir, symbol_list, initial_capital, heartbeat, start_date, HistoricCSVDataHandler, Portfolio, Average_Momentum) backtest.simulate_trading(plot=True)
def get_args(): parser = argparse.ArgumentParser() parser.add_argument("--symbols", help="securities", default="000300.SH", type=str) parser.add_argument("--s", help="start date", default="2002-01-01", type=str) parser.add_argument("--e", help="end date", default="2017-01-20", type=str) args = parser.parse_args() return args if __name__ == "__main__": csv_dir = "../csv/" args = get_args() symbol_list = args.symbols.split(',') fetch_from_wind(csv_dir, symbol_list, args.s, args.e) start_date = datetime.strptime(args.s, "%Y-%m-%d") initial_capital = 100000.0 heartbeat = 1.0 backtest = Backtest(csv_dir, symbol_list, initial_capital, heartbeat, start_date, HistoricCSVDataHandler, Portfolio, LSTM_momentum) backtest.simulate_trading(plot=True)