AminHP / gym-anytrading

The most simple, flexible, and comprehensive OpenAI Gym trading environment (Approved by OpenAI Gym)
MIT License
2.13k stars 467 forks source link

Forex env unit_side question #56

Closed dancydancy closed 2 years ago

dancydancy commented 2 years ago

Hi!

First off, please forgive my lack of understanding. I'm a big fan of the project and am using it almost daily.

I'm looking at the FOREX environment code and there is something I cannot get my head around. Perhaps you can help clarify the flow of what is going on.

The question:

In the forex env, how does the unit_side checking work? Specifically, what is the mechanism to calculate profit of a long position if the unit_side was set to be "left"? Or to ask is another way - if unit_side "left" is initialised, does that mean that long position profits are never added?

If they are - how is it happening?

The code from forex env for reference:


def _update_profit(self, action):
        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 or self._done:
            current_price = self.prices[self._current_tick]
            last_trade_price = self.prices[self._last_trade_tick]

            if self.unit_side == 'left':
                if self._position == Positions.Short:
                    quantity = self._total_profit * (last_trade_price - self.trade_fee)
                    self._total_profit = quantity / current_price

            elif self.unit_side == 'right':
                if self._position == Positions.Long:
                    quantity = self._total_profit / last_trade_price
                    self._total_profit = quantity * (current_price - self.trade_fee)
AminHP commented 2 years ago

Hi @dancydancy, I'm delighted this project was helpful for you :)

I'm not sure it was two years ago. But I probably wanted to keep it as simple as possible. You can add more calculations according to your need.

I highly suggest you check out my newer project gym-mtsim for daily usage. It's a real-world tool and calculates everything (almost) precisely.

dancydancy commented 2 years ago

That's a neat looking project :) Nicely done.

I'm looking it - but it's possibly overkill for my needs while I'm still training models and have a decent workflow setup on gym-anytrading. Metatrader looks like a lot to get into.

Let me know if it comes to you.

Many thanks