class TradingEnv(gym.Env): def __init__(self, data_source, look_backward_window=1, mode="rl", starting_cash={"STOCK": 1000000}, commission_multiplier=1, min_commission=5, tax_multiplier=1) -> None: self.look_backward_window = look_backward_window self.mode = mode self.starting_cash = starting_cash self.commission_multiplier = commission_multiplier self.min_commission = min_commission self.tax_multiplier = tax_multiplier self._context = Context(look_backward_window=look_backward_window) #set data_source self._context.set_data_source(data_source) #set event_source event_source = SimulationEventSource() self._context.set_event_source(event_source) #set broker broker = SimulationBroker(self._context) self._context.set_broker(broker) # transaction_cost decider transaction_cost_decider = CNStockTransactionCostDecider( commission_multiplier=commission_multiplier, min_commission=min_commission, tax_multiplier=tax_multiplier) self._context.set_transaction_cost_decider("CS", transaction_cost_decider) # setup account and portfolio portfolio = Portfolio(starting_cash=starting_cash, init_positions={}) self._context.set_portfolio(portfolio) # setup a tracker to record key info tracker = Tracker(self._context) self._context.set_tracker(tracker) self._context.event_bus.publish_event(Event(EVENT.POST_SYSTEM_INIT)) #setUP executor if mode == "rl": self._executor = RLExecutor(self._context) else: self._executor = Executor(self._context) # user strategy user_strategy = Strategy(self._context) def reset(self): state = Context.get_instance().history_bars() return state def step(self, action): reward, is_done, info = self._executor.send(action) #pdb.set_trace() next_state = Context.get_instance().history_bars() return next_state, reward, is_done, info @property def trading_dt(self): return self._context.trading_dt def render(self): if self.mode == "rl": returns_list = self._context.tracker._portfolio_forward_bar_returns.copy( ) returns_list.insert(0, 0) else: returns_list = self._context.tracker._portfolio_current_bar_returns.copy( ) #pdb.set_trace() index = self._context.availabel_trading_dts returns = pd.DataFrame(returns_list, index=index, columns=["unit_net_value"]) unit_net_value = (returns + 1).cumprod() plot_performance(unit_net_value)
class TradingEnv(gym.Env): def __init__(self, data_source, look_backward_window=1, mode="non-rl", starting_cash={"STOCK": 1000000}, commission_multiplier=1, min_commission=5, tax_multiplier=1) -> None: self.look_backward_window = look_backward_window self.mode = mode self.starting_cash = starting_cash self.commission_multiplier = commission_multiplier self.min_commission = min_commission self.tax_multiplier = tax_multiplier self._context = Context(look_backward_window=look_backward_window) #set mode self._context.set_mode(mode) #set data_source self._context.set_data_source(data_source) #set event_source event_source = SimulationEventSource() self._context.set_event_source(event_source) #set broker broker = SimulationBroker(self._context) self._context.set_broker(broker) # transaction_cost decider transaction_cost_decider = CNStockTransactionCostDecider( commission_multiplier=commission_multiplier, min_commission=min_commission, tax_multiplier=tax_multiplier) self._context.set_transaction_cost_decider("CS", transaction_cost_decider) # setup account and portfolio portfolio = Portfolio(starting_cash=starting_cash, init_positions={}) self._context.set_portfolio(portfolio) #setUP executor if mode == "rl": self._executor = RLExecutor(self._context) else: self._executor = Executor(self._context) # user strategy user_strategy = Strategy(self._context) # setup a tracker to record key info tracker = Tracker(self._context) self._context.set_tracker(tracker) self._context.event_bus.publish_event(Event(EVENT.POST_SYSTEM_INIT)) self._context.update_time( calendar_dt=self._context.available_trading_dts[0], trading_dt=self._context.available_trading_dts[0]) # action and observation space self.action_space = gym.spaces.Box( 0, 1, shape=(len(data_source.order_book_ids_index), ), dtype=np.float32) # include cash # get the state space from the data min and max if look_backward_window == 1: self.observation_space = gym.spaces.Box( low=-np.inf, high=np.inf, shape=(len(data_source.order_book_ids_index), len(data_source.feature_list)), dtype=np.float32) else: self.observation_space = gym.spaces.Box( low=-np.inf, high=np.inf, shape=(len(data_source.order_book_ids_index), look_backward_window, len(data_source.feature_list)), dtype=np.float32) def reset(self): self._context.update_time( calendar_dt=self._context.available_trading_dts[0], trading_dt=self._context.available_trading_dts[0]) state = Context.get_instance().history_bars() return state def step(self, action): reward, is_done, info = self._executor.send(action) #pdb.set_trace() next_state = Context.get_instance().history_bars() return next_state, reward, is_done, info @property def trading_dt(self): return self._context.trading_dt @property def available_trading_dts(self): return self._context.available_trading_dts def render(self, auto_open=True): if self.mode == "rl": returns_list = self._context.tracker._portfolio_forward_bar_returns.copy( ) returns_list.insert(0, 0) else: returns_list = self._context.tracker._portfolio_current_bar_returns.copy( ) #pdb.set_trace() index = self._context.available_trading_dts returns = pd.DataFrame(returns_list, index=index, columns=["unit_net_value"]) unit_net_value = (returns + 1).cumprod() plot_performance(unit_net_value, auto_open=auto_open)