How does MetaTrader 5 calculate profit?

 

Running a simple script:

//+------------------------------------------------------------------+
//|                                                       profit.mq5 |
//+------------------------------------------------------------------+
string com;
string Sy[28]={"EURGBP","EURAUD","EURNZD","EURUSD","EURCAD","EURCHF","EURJPY","GBPAUD","GBPNZD","GBPUSD",
              "GBPCAD","GBPCHF","GBPJPY","AUDNZD","AUDUSD","AUDCAD","AUDCHF","AUDJPY","NZDUSD","NZDCAD",
              "NZDCHF","NZDJPY","USDCAD","USDCHF","USDJPY","CADCHF","CADJPY","CHFJPY"};
double a[28],b[28],BuyPlus[28],BuyMinus[28],SellPlus[28],SellMinus[28];
double diff=0.001;

void OnStart()
  {com="";
   for(int i=0;i<28;i++)
      {b[i]=SymbolInfoDouble(Sy[i],SYMBOL_BID);a[i]=SymbolInfoDouble(Sy[i],SYMBOL_ASK);
       OrderCalcProfit(ORDER_TYPE_BUY,Sy[i],1.0,a[i],a[i]+diff,BuyPlus[i]);
       OrderCalcProfit(ORDER_TYPE_BUY,Sy[i],1.0,a[i],a[i]-diff,BuyMinus[i]);
       OrderCalcProfit(ORDER_TYPE_SELL,Sy[i],1.0,b[i],b[i]+diff,SellPlus[i]);
       OrderCalcProfit(ORDER_TYPE_SELL,Sy[i],1.0,b[i],b[i]-diff,SellMinus[i]);
       
       com=com+"\n"+Sy[i]+"  BuyPlus="  +DoubleToString(BuyPlus[i],4)
                         +"  BuyMinus=" +DoubleToString(BuyMinus[i],4)
                         +"  SellPlus=" +DoubleToString(SellPlus[i],4)
                         +"  SellMinus="+DoubleToString(SellMinus[i],4);
      }//for
   Comment(com);
  }//start

The error is clearly visible...

The problem must come fromSYMBOL_TRADE_TICK_VALUE_PROFIT andSYMBOL_TRADE_TICK_VALUE_LOSS.

We will needSYMBOL_TRADE_TICK_VALUE_LONG and SYMBOL_TRADE_TICK_VALUE_SHORT.

The search turned up something interesting:

Renat:

Я вчера, когда смотрел код, неверно выразился по поводу разной стоимость пункта в зависимости от направления.

Точнее сказать, что TickValue при конвертации в целевую валюту зависит от того, убыточна она или нет. То есть, если мы получили убыток в 1 пипс, то нам надо его выкупить по цене Ask, а если прибыль в 1 пипс, то продать по цене Bid.


This is wrong, of course. For a short position, price the other way round....

I really hope the mistake is involuntary! Can you correct it, please!

Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
  • www.mql5.com
Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте - Документация по MQL5
 

Unfortunately, the question is not clearly stated and there is no conclusion from the proposed example. It is not clear - what exactly is indicated as an error.

Formulate your question accurately, attach the results obtained and indicate where the error is in them, please.

For example, indicate where the error is here:

EURGBP  BuyPlus=158.40000000  BuyMinus=-158.48000000  SellPlus=-158.48000000  SellMinus=158.40000000  Profit=1.58398000  Loss=1.58482000

I added SYMBOL_TRADE_TICK_VALUE_PROFIT and SYMBOL_TRADE_TICK_VALUE_LOSS values at the end.

We can see that the profit actually takes into account different values of a tick, depending on profitability or loss of a trade. This is because there is an implicit conversion operation to the deposit currency, when one has to sell (if it is profit) or buy back (if it is loss) the obtained financial result in one currency for conversion.

Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
  • www.mql5.com
Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте - Документация по MQL5
 

 

The value of a tick does not depend on the profit or loss of a trade.

Profit and loss will close at the same price. Conversion is the same.

Only short and long trades can have a difference in the value of the tick and can be counted differently for the conversion into the deposit currency.

The BuyPlus and BuyMinus example would be equal. SellPlus and SellMinus too. You can only Buy.... is different from Sell...

You are confusing something here :

Renat:

... When you have tosell(if it's a profit) orbuy back(if it's a loss) the financial result in one currency for conversion.

Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация о счете
Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация о счете
  • www.mql5.com
Стандартные константы, перечисления и структуры / Состояние окружения / Информация о счете - Документация по MQL5
 

When you open a trade for EURGBP, and the currency of the deposit is USD,you basically have (roughly) Buy EURUSD and Sell GBPUSD. (The difference in volume does not matter, because they do not change when you close)

To open:Buy EURUSD at Ask(EURUSD) and Sell GBPUSD at Bid(GBPUSD).

At close(if it is a profit,if it is a loss) you have the same price: Bid(EURUSD) and Ask(GBPUSD).

Why is the tick value different forprofit/loss?

Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация о счете
Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация о счете
  • www.mql5.com
Стандартные константы, перечисления и структуры / Состояние окружения / Информация о счете - Документация по MQL5
 

This is the result of a long-standing misconception of the developers:

Renat:

It is more accurate to say that the TickValue when converting to the target currency depends on whether it is a loss or not. That is, if we have a loss of 1 pip, we have to buy it back at Ask price, and if we have a profit of 1 pip, we have to sell it at Bid price.

 
Manov:

The value of a tick does not depend on whether a trade is profitable or unprofitable.

Profit and loss will close at the same price. On conversion, so will the price.

That's just it does.

To do this, you need to understand the mathematics of calculations with complex cross conversions. As long as you operate with majors such as EURUSD and GBPUSD, you will not see anything.

Yes, it may seem at first sight that it does not depend on it, but if you examine the crosses in details, you will see that it does.

 
Renat:

That's the thing, it depends.

This is actually a moot point. Renat's logic is clear and seems even correct at first glance:

When you make a crossover trade, you make a profit in its base currency. For example, a EURGBP trade's profit is measured in GBP. But there is no concept of multi-currency profit in MT5, so GBP profit is converted to the account currency on the fly. And it would seem that in case of positive profit it must be converted at current exchange rate GBPUSD_Bid, and in case of negative - GBPUSD_Ask.

However, here is a counter-example:

  1. You have two independent accounts. You decided to transfer funds from one to the other.
  2. On EURGBP you set the inside spread on one account to BuyLimit. Accordingly, the Bid price becomes yours.
  3. On the other account, using a market order you execute SELL.
  4. By this simple operation you have sold to yourself.
  5. Some time passes and you decide to close the deals.
  6. On the first account you set the SellLimit inside the spread. Now your price becomes the Ask price.
  7. On the other account, you use market order to BUY.
  8. It turns out that you have now bought from your own account.
  9. Both trades in each account have been closed. You bought and sold to yourself.
  10. In one account you have positive profit and in the other negative.
  11. Do you think the amount of funds in both your accounts has changed (not including broker's commission)?
  12. According to Renat's logic, it has changed. Because the profit in one account will not equal the loss in the other account. And this despite the fact that you yourself were buying and selling.
  13. Is that correct?

We were talking about market conditions - ECN/STP broker.

Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
  • www.mql5.com
Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте - Документация по MQL5
 
hrenfx:

However, a counter-example:
  1. Is this the right thing to do?

It was about market conditions - ECN/STP broker.

Consider that there are at least two other internal conversion transactions with their own spreads.
 
Renat:

That's the thing: it depends.

To do that you need to delve deep enough into the mathematics of calculations with complex cross conversions. As long as you operate with majors like EURUSD and GBPUSD, you won't see anything.

Yes, it seems at first glance, that it should not depend, but if you examine the crosses in details, you will see that it does.

The Ask/Bid calculation may becomplicated, but in fact,for all crosses, you have 2 trades:

1. buy or sellSYMBOL_CURRENCY_BASE-ACCOUNT_CURRENCY on Ask/Bid

2) Buy or SellSYMBOL_CURRENCY_PROFIT -ACCOUNT_CURRENCY for Ask/Bid

Every trade, if we opened it with Bid, we will close it with Ask. And vice versa ...

Result sign does not matter for close prices, like it does now on MetaTrader 5 !

 
 
Renat:
Keep in mind that there are at least two more internal conversions with their own spreads.

What conversion operations are we talking about? The logic is simple, you have to convert multi-currency profits to the account currency, nothing more. In the case of the example, the profit in GBP must be converted to USD. It does not matter whether the profit is positive or negative, you have to convert it.

You have given up on the market scheme of taking multi-currency profits and converting them at the time of rollover. This is a departure from the market conditions of MT5, which is positioned as a market platform. But for the sake of simplification this departure can be understood and does not in many cases (not in all) incur serious costs.

But in the case of profit calculation, to call things by their proper names, you are, intentionally or accidentally, contributing to a fraudulent scheme to take money from clients in favour of a broker using MT5. Let me explain, at the moment a broker is making extra profit on MT5 from trading all clients equal (roughly) to the turnover of all his clients on crosses, multiplied by the corresponding spread of the majors.

You have implemented a free scheme to make money on the spread, and on any spread. For example, during the news, on the same GBPUSD, the spread can be very wide and if there is a close/open at the broker's clients, the broker earns this huge spread on a flat spot.

This is a disadvantage and the multicurrency profit waiver, because the multicurrency spread can be converted at very nasty prices during the news. And in fact during the rollover the multi-currency profit is converted at the total netting of all clients. And there is no way such imbalances can occur, as shown in the counter-example above.

Reason: