Serious bug in the OrderCalcProfit() function - page 4

 
amrali #:

Ok, I see Ask price is used here. That's contrary to the tests I performed on OrderCalcProfit() and tick values.

Could you please check OrderCalcProfit() function and Tick values at the moment of position closure?

How could I do that ? This trade was closed in 2022.08.28 ? You mean with the Strategy Tester ? maybe I don't understand your question.

Edit:

I did not check the deal_profit field. I do not know if deal_profit == OrderCalcValue() == TICK_VALUE_xxx. I only checked the latter pair. If there is a mismatch, then both the function and tick_values are misleading.

I think there is big misunderstanding between us as several issues were discussed and mixed.

 

Ok, I will test it tomorrow like this:

  • open a position on demo
  • give some time, then close it using a script by OrderSend() function.
  • At the same time, the script records position profit, and profits returned by OrderCa.. and tick values.
 
amrali #:

Ok, I will test it tomorrow like this:

  • open a position on demo
  • give some time, then close it using a script by OrderSend() function.
  • At the same time, the script records position profit, and profits returned by OrderCa.. and tick values.

Ok please also record the rates at closure time if possible.

To be honest I didn't find a logic which works in all cases, I am confused now. On the real account I talked about, I checked 132 trades from history, on these 116 were matching my custom profit/loss calculations, but 16 doesn't match and seems to use the bid instead of the ask or the ask instead of the bid. Maybe I am getting wrong rates from history.

So yes it's better to check live and record the results.

Thank you.

 
amrali #:

2. Actually, all buy orders uses -> TV_LOSS, all sell use -> TV_PROFIT. (the order_type decides which tick value to use.)

5. TICK_VALUE_XXX is a shortcut to avoid all of these calculations. (the value itself is calculated internally in the software as follows):

6. So, you can calculate profit using a simple calculation like:

From my observations the tick values and tick size are not that accurate in the tester so they are not reliable in historical data, Sometimes even poor internet connection affects this values too, I had to check for that condition in my risk settings library

bool CRiskSettings::CheckforValues(void)
 {
   if (tick_size <=0)
    {
      Print("Can't perform calculation | Tick size=",tick_size," Make sure you have called the class Init function", MQLInfoInteger(MQL_TESTER) ? "and History Data is correct " : "\n---> an active internet connection is required too");
      return (false);
    }
   if (tick_value <=0)
    {
      Print("Can't perform calculation | Tick value=",tick_value," Make sure you have called the class Init function", MQLInfoInteger(MQL_TESTER) ? "and History Data is correct " : "\n---> an active internet connection is required too");
      return (false);
    }  
  return (true);
 }
 

Ok, this is a proof based on numbers from the terminal (not theories).

I ran the test on two different terminal, one with an account currency = EUR, the other's account currency is USD.

First I use a script, to open positions on all forex symbols on the market watch using a large position size as possible. I increased the demo balance to the maximum 100K to do the tests.

Then, I used a second script "2. AnalyzePositionProfits_Script.mq5" to analyze profits of all open positions, using these methods:

  • POSITION_PROFIT field
  • OrderCalcProfit() function
  • SYMBOL_TRADE_TICK_VALUE_xxxx
  • Conversions from profit currency -> account currency

The script outputs the results to csv file. I always use Ron's CSV editor to read csv files. https://www.ronsplace.ca/Products/RonsEditor/Download

Here is the result from the EUR account:


Here is the result from the USD account:


As can be seen  POSITION_PROFIT is sometimes (but not always) equal to profit calculation using  OrderCalcProfit(). The reason for this may be a timing (sync) issue between the ticks on the server used to calculate the POSITION_PROFIT  field and the later ticks reaching the terminal. So, my conclusion is that the value of the POSITION_PROFIT field is calculated on the server, independent of the quote data arriving to the terminal in that exact moment.

All the other methods are matching my previous calculations:



  1. SYMBOL_TRADE_TICK_VALUE_LOSS  is used to calculate profit of BUY orders.
  2. SYMBOL_TRADE_TICK_VALUE_PROFIT  is used to calculate profit of SELL orders.
  3. No correlation between the TICK_VALUE_xxxx names (PROFIT/LOSS) and the price type (BID/ASK) of profit/account currency conversion rate.
  4. The price type (BID/ASK) of the profit/account conversion pair depends to two things: (a) the type of the order (b) position of account currency in the pair (first or second).
  5. It was better if the names were SYMBOL_TRADE_TICK_VALUE_BUY (for SYMBOL_TRADE_TICK_VALUE_LOSS) and SYMBOL_TRADE_TICK_VALUE_SELL (for SYMBOL_TRADE_TICK_VALUE_PROFIT)
Download - Rons CSV Editor
Download - Rons CSV Editor
  • Aaron Stewart
  • www.ronsplace.ca
Download the latest version of Rons CSV Editor
 

Thanks Amrali,

I will check all of that and post my conclusions, but not in short term as I am very busy.

 

Forum on trading, automated trading systems and testing trading strategies

Serious bug in the OrderCalcProfit() function

Alain Verleyen, 2023.02.19 23:21

The problem is the profit/loss of this trade was -227.59 EUR.


So the real calculation was :

double profit = volume*contract*(close-open)*GetExchangeRate("AUD","EUR",BUY); /*Rate = 1/Ask*/

                    = 0.07*100,000*(1.70407-1.75114)*(1/1.44774);

                    = -227.59 EUR

This is a trade from a real account.

@Alain Verleyen Could you please show me now the next trade in your history after that one (the next line immediately below), because I know for sure how you did it, exactly  ;-)
 
amrali #:
@Alain Verleyen Could you please show me now the next trade in your history after that one (the next line immediately below), because I know for sure how you did it, exactly  ;-)

How I "did" what ?

 
Alain Verleyen #:
How I "did" what ?
The trade you posted was a close-by one. 
 
amrali #:
The trade you posted was a close-by one. 

Not at all.

I did nothing, it's all "normal" trades open and close in a standard way, CLOSE_BY was not used.

Reason: