def forward_v1(self, action_keys): # Check trader. self.trader.remove_invalid_positions() self.trader.reset_reward() # Init current prices. stocks_price = [] # Here, action_sheet is like: [-1, 1, ..., -1, 0] for index, code in enumerate(self.codes): # Get Stock for current date with code. action_code = action_keys[index] action = self.trader.action_dic[ActionCode(action_code)] try: stock = self._get_origin_stock_data(code, self.current_date) stock_next = self._get_origin_stock_data(code, self.next_date) action(code, stock, 100, stock_next) stocks_price.append(stock.close) except KeyError: stock_market_logger.info( "Current date cannot trade for code: {}.".format(code)) # Update and return the next state. self.trader.history_baseline_profits.append( np.sum(np.multiply(self.stocks_holding_baseline, stocks_price))) self.trader.history_profits.append(self.trader.profits + self.trader.initial_cash) try: self.current_date, self.next_date = next(self.iter_dates), next( self.iter_dates) state_next = self._get_scaled_stock_data_as_state( self.current_date) return state_next, self.trader.reward, self.Running, self.trader.cur_action_status except StopIteration: state_next = self._get_scaled_stock_data_as_state( self.current_date) return state_next, self.trader.reward, self.Done, self.trader.cur_action_status
def buy(self, code, stock, amount, stock_next): # Check if amount is valid. amount = amount if self.cash > stock.close * amount else int( math.floor(self.cash / stock.close)) # If amount > 0, means cash is enough. if amount > 0: # Check if position exists. if not self._exist_position(code): # Build position if possible. position = Position(code, stock.close, amount, stock_next.close) self.positions.append(position) else: # Get position and update if possible. position = self._get_position(code) position.add(stock.close, amount, stock_next.close) # Update cash and holding price. self.cash -= amount * stock.close self._update_reward(ActionCode.Buy, ActionStatus.Success, position) stock_market_logger.info("Code: {0}," " buy success," " cash: {1:.2f}," " holding value:{2:.2f}".format( code, self.cash, self.holdings_value)) else: stock_market_logger.info( "Code: {}, not enough cash, cannot buy.".format(code)) if self._exist_position(code): # If position exists, update status. position = self._get_position(code) position.update_status(stock.close, stock_next.close) self._update_reward(ActionCode.Buy, ActionStatus.Failed, position)
def hold(self, code, stock, _, stock_next): if not self._exist_position(code): stock_market_logger.info( "Code: {}, not exists in Positions, hold failed.") return self._update_reward(ActionCode.Hold, ActionStatus.Failed, None) position = self._get_position(code) position.update_status(stock.close, stock_next.close) self._update_reward(ActionCode.Hold, ActionStatus.Success, position)
def hold(self, code, stock, _, stock_next): if not self._exist_position(code): stock_market_logger.info( "Code: {}, not exists in Positions, hold failed.".format(code)) return self._update_reward(ActionCode.Hold, ActionStatus.Failed, None) position = self._get_position(code) position.update_status(stock.close, stock_next.close) self._update_reward(ActionCode.Hold, ActionStatus.Success, position) stock_market_logger.info("Code: {0}," " hold success," " cash: {1:.2f}," " holding value:{2:.2f}".format( code, self.cash, self.holdings_value))
def sell(self, code, stock, amount, stock_next): # Check if position exists. if not self._exist_position(code): stock_market_logger.info( "Code: {}, not exists in Positions, sell failed.".format(code)) return self._update_reward(ActionCode.Sell, ActionStatus.Failed, None) # Sell position if possible. position = self._get_position(code) amount = amount if amount < position.amount else position.amount position.sub(stock.close, amount, stock_next.close) # Update cash and holding price. self.cash += amount * stock.close self._update_reward(ActionCode.Sell, ActionStatus.Success, position)
def log_reward(self): stock_market_logger.info("Reward: {}".format(self.reward))