AminHP / gym-anytrading

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

Forex Environment profit calculation: only sell leg is applied with 3 pips commission #43

Closed hcandra closed 2 years ago

hcandra commented 2 years ago

Hi, I notice in the forexenv, there is a commission applied at the sell leg but not at the buy leg. May I understand the reason?

Thanks in advance for your explanation

Rgds

AminHP commented 2 years ago

Hi @hcandra ,

You can see here that the commission fee is calculated based on unit_size.

hcandra commented 2 years ago

Thanks for replying my question. Maybe I would like to clarify using example. Let's say I use EURUSD pair, and I use "right" method because I start with USD 1.00. I understand if we are to start with 1 EUR, then the self.unit_side will be "left".

Based on the code below, for the "right" unit_side, trade will happen in 2 conditions:

  1. actions = sell and current position = Long -> I see that commission will apply in this situation
  2. actions = buy and current position = Short -> based on the code below, there is no commission applied, the quantity of EUR acquired is straight away calculated as "USD Capital divided by the price" and not as "USD Capital divided by (price + 0.0003)" In the real trade, shouldn't we apply commission for both legs and not just to one leg? When we sell, we will sell at lower price which is (current price - 0.0003) -> I see this one is reflected and when we buy we will need to buy at higher price which is (current price + 0.0003) -> I don't see this part.

Please advise

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

In some aspects, you are right. According to MetaTrader5, the most precise way to calculate profit is using bid/ask prices. Every time you sell/buy something, the bid/ask price is considered, and the fee is applied implicitly.

But I found my profit calculation method to be easier to implement and comprehend. I also tested it on some data and didn't find much of a difference. It is like I chose the bid price as the main price and abs(bid - ask) as the fee. It is not the most precise way but works.

Anyway, you can calculate profit in the way you explained and analyze the difference.