MehranTaghian / DQN-Trading

A Deep Reinforcement Learning Framework for Stock Market Trading
MIT License
141 stars 48 forks source link

def get_reward(self, action) #2

Open formidiable opened 2 years ago

formidiable commented 2 years ago
def get_reward(self, action):
    """
    @param action: based on the action taken it returns the reward
    @return: reward
    """

    reward_index_first = self.current_state_index + self.start_index_reward
    reward_index_last = self.current_state_index + self.start_index_reward + self.n_step \
        if self.current_state_index + self.n_step < len(self.states) else len(self.close_price) - 1

    p1 = self.close_price[reward_index_first]
    p2 = self.close_price[reward_index_last]

    reward = 0
    if action == 0 or (action == 1 and self.own_share):  # Buy Share or Hold Share
        reward = ((1 - self.trading_cost_ratio) ** 2 * p2 / p1 - 1) * 100  # profit in percent
    elif action == 2 or (action == 1 and not self.own_share):  # Sell Share or No Share
        # consider the transaction in the reverse order
        reward = ((1 - self.trading_cost_ratio) ** 2 * p1 / p2 - 1) * 100

    return reward

Sorry, the function is true.