Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StocksEnv profit calculation does not consider short ? #3

Closed
vincentfortin opened this issue Feb 8, 2020 · 2 comments
Closed

StocksEnv profit calculation does not consider short ? #3

vincentfortin opened this issue Feb 8, 2020 · 2 comments

Comments

@vincentfortin
Copy link

Not sure if here is the best place to ask a question.

In the _calculate_reward function, the reward does not seem to consider when shorting. Trade is true, but it does not add reward when we short and price goes down or remove reward if price goes up when shorting.

if trade:
    current_price = self.prices[self._current_tick]
    last_trade_price = self.prices[self._last_trade_tick]
    price_diff = current_price - last_trade_price

    if self._position == Positions.Long:
        step_reward += price_diff

Shouldn't it be changed to:

if trade:
    current_price = self.prices[self._current_tick]
    last_trade_price = self.prices[self._last_trade_tick]
    price_diff = current_price - last_trade_price

    if self._position == Positions.Long:
        step_reward += price_diff
    else:
        step_reward -= price_diff # Change here to account for shorting

@AminHP
Copy link
Owner

AminHP commented Feb 8, 2020

Basically in the Stocks Market, we don't trade short. We buy a stock at a low price and sell it at a high price. So I implemented the reward function as you see. But you can inherit the StocksEnv and change the reward function in the way you have mentioned and maybe you get better results.

Or you can do it in a simpler way if you don't want to use inheritance:

def _calculate_reward(self, action):
        step_reward = 0

        trade = False
        if ((action == Actions.Buy.value and self._position == Positions.Short) or
            (action == Actions.Sell.value and self._position == Positions.Long)):
            trade = True

        if trade:
            current_price = self.prices[self._current_tick]
            last_trade_price = self.prices[self._last_trade_tick]
            price_diff = current_price - last_trade_price

            if self._position == Positions.Long:
                step_reward += price_diff
            else:
                step_reward -= price_diff

        return step_reward

StocksEnv._calculate_reward = _calculate_reward

@vincentfortin
Copy link
Author

Thanks, also changed the profit function to account for shorting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants