Strange result from OrderCalcMargin

 

Hello,

I try to calculate expected profit (D) and margin requirement , but I get strange result in Tester. On Demo and Real account, it is OK.

I use this code (Script) over account, and same as Expert in Tester :

void OnStart()
{
  double D=0.01 // Volume
        *SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)
        *0.0005 // == 0.05% change of price
        *SymbolInfoDouble(_Symbol,SYMBOL_ASK)/_Point;
   Print("D="+DoubleToString(D,2));
// ==========
// ==========
  double Margin;
  if ( OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,0.01,SymbolInfoDouble(_Symbol,SYMBOL_ASK),Margin ) )
    Print(" Margin="+DoubleToString(Margin,2));
  else
    Print(" ===>>> Margin NOT get <<<=== !");
}

.. and results are :

Results by Expert (true)

// 1. Demo account
// Currency USD, Leverage 1:100
OJ      0       07:35:11.526    test (EURUSD,H1)        D=0.55
DN      0       07:35:11.526    test (EURUSD,H1)         Margin=10.95
// 2. Real account
// Currency BGN (USDBGN=1.78644/1.78686) Leverage=1:30
FN      0       07:41:06.738    test (EURUSD,H1)        D=0.98
RK      0       07:41:06.738    test (EURUSD,H1)         Margin=65.20

 ... And result in Tester , on real account , totally different for Margin :
Currency by default in Tester BGN, I cant change. I change Leverage.

// test 1 : Currency BGN (USDBGN=1.78644/1.78686) Leverage=1:30
CS      0       07:47:07.616    127.0.0.1       login (build 3802)
...
CS      0       07:47:07.694    Tester  calculate profit in pips, initial deposit 1000, leverage 1:30
CS      0       07:47:07.696    Tester  successfully initialized
...
CS      0       07:47:07.865    Tester  EURUSD,M1: testing of Experts\test-start1.ex5 from 2023.05.02 00:00 to 2023.08.12 00:00 started
...
CS      0       07:47:07.882    test-start1 (EURUSD,M1) 2023.05.02 00:00:00   D=0.55
CS      0       07:47:07.882    test-start1 (EURUSD,M1) 2023.05.02 00:00:00    Margin=111.11
...
CS      3       07:47:09.903    Tester  stopped by user
CS      0       07:47:09.904    Tester  test Experts\test-start1.ex5 on EURUSD,M1 thread finished

// test 2 : Currency BGN (USDBGN=1.78644/1.78686) Leverage=1:100
CS      0       07:48:13.257    127.0.0.1       login (build 3802)
CS      0       07:48:13.258    Tester  account info found with currency BGN
...
CS      0       07:48:13.274    Tester  calculate profit in pips, initial deposit 1000, leverage 1:100
CS      0       07:48:13.274    Tester  successfully initialized
...
CS      0       07:48:13.436    test-start1 (EURUSD,M1) 2023.05.02 00:00:00   D=0.55
CS      0       07:48:13.436    test-start1 (EURUSD,M1) 2023.05.02 00:00:00    Margin=33.33
...
CS      3       07:48:16.607    Tester  stopped by user
CS      0       07:48:16.609    Tester  test Experts\test-start1.ex5 on EURUSD,M1 thread finished

It looks like Tester made calculation for LotSize in BGN, not in USD (for trade EURUSD

Is this normal , my error , or Tester error ?

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Ivan Ivanov:

Hello,

I try to calculate expected profit (D) and margin requirement , but I get strange result in Tester. On Demo and Real account, it is OK.

I use this code (Script) over account, and same as Expert in Tester :

.. and results are :

Is this normal , my error , or Tester error ?

To be able to track the actual calculations, more details are required.

SymbolInfoMarginRate and AccountInfoLeverage, and AccountCurrency are required.

Else it is not clear at all what is actually applied.
 

Here again description of the problem . Used code :

void OnStart()
{
// ==========
  double MaginInit, MarginKeep;
  SymbolInfoMarginRate(_Symbol,ORDER_TYPE_BUY,MaginInit,MarginKeep);
  Print(" => MaginInit="+DoubleToString(MaginInit,2));
  Print(" => MarginKeep="+DoubleToString(MarginKeep,2));
  Print(" => USDBGN="+DoubleToString(SymbolInfoDouble("USDBGN",SYMBOL_ASK),_Digits)
                 +"/"+DoubleToString(SymbolInfoDouble("USDBGN",SYMBOL_BID),_Digits));
  Print(" => AccLeverage="+IntegerToString(AccountInfoInteger(ACCOUNT_LEVERAGE),2));
// ==========
  double MarginCalculated;
  if ( OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,0.01,SymbolInfoDouble(_Symbol,SYMBOL_ASK),MarginCalculated ) )
    Print(" ===> MarginCalculated="+DoubleToString(MarginCalculated,2));
  else
    Print(" ===>>> Margin NOT get <<<=== !");
}

Result :

//  Script on real account :
RQ      0       14:03:32.057    test2 (EURUSD,H1)        => MaginInit=3.33
RQ      0       14:03:32.057    test2 (EURUSD,H1)        => MarginKeep=0.00
QK      0       14:03:32.057    test2 (EURUSD,H1)        => USDBGN=1.78686/1.78644
PL      0       14:03:32.057    test2 (EURUSD,H1)        => AccLeverage=100
DI      0       14:03:32.057    test2 (EURUSD,H1)        ===> MarginCalculated=65.20

// EA on TESTER :
CS      0       13:58:10.589    127.0.0.1       login (build 3802)
CS      0       13:58:10.589    Tester  account info found with currency BGN
CS      0       13:58:10.590    Tester  expert file added: Experts\test-start1.ex5. 19287 bytes loaded
CS      0       13:58:10.609    Tester  calculate profit in pips, initial deposit 1000, leverage 1:100
CS      0       13:58:10.609    Tester  successfully initialized
...
CS      0       13:58:10.786    test-start1 (EURUSD,M1) 2023.05.01 00:00:00    => MaginInit=3.33
CS      0       13:58:10.786    test-start1 (EURUSD,M1) 2023.05.01 00:00:00    => MarginKeep=0.00
...
CS      0       13:58:10.789    test-start1 (EURUSD,M1) 2023.05.01 00:00:00    => USDBGN=1.77507/1.77466
CS      0       13:58:10.789    test-start1 (EURUSD,M1) 2023.05.01 00:00:00    => AccLeverage=100
CS      0       13:58:10.789    test-start1 (EURUSD,M1) 2023.05.01 00:00:00    ===> MarginCalculated=33.33
CS      3       13:58:15.323    Tester  stopped by user
CS      0       13:58:15.323    Tester  test Experts\test-start1.ex5 on EURUSD,M1 thread finished
CS      0       13:58:15.324    MQL5 debugger   disconnected
CS      0       13:58:15.328    127.0.0.1       prepare for shutdown
CS      0       13:58:15.328    127.0.0.1       shutdown finished

Why are result of calculation different ? ... result from Tester is wrong .

... remark : Quote for USDBGN are : Friday Close for EA , Tester start date Open .

 
Ivan Ivanov #:

Here again description of the problem . Used code :

Result :

Why are result of calculation different ? ... result from Tester is wrong .

... remark : Quote for USDBGN are : Friday Close for EA , Tester start date Open .

Getting this , it looks correct (USD dep.curr)

Try and log all the values it gives in the tester with all leverage choices . 

I assume the broker is responsible for tuning these (leverage selections and margins required0
 
Ivan Ivanov #:

Here again description of the problem . Used code :

Result :

Why are result of calculation different ? ... result from Tester is wrong .

... remark : Quote for USDBGN are : Friday Close for EA , Tester start date Open .

what are the account currencies in tester and on live/demo account?

Edit:
To me it looks like your live/demo account is in BGN and your tester account is in USD
 

On second post account is 1 (one) , Real . Account Currency is BGN . Leverage 1:30

I think that result from tester is : 

Volume ( 1000 BGN ) * Leverage ( 1/30 ) = 33.33    ... and this is false !

In description of  SYMBOL_CALC_MODE_FOREX Quote of Deposit currency not exist !

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Ivan Ivanov #:

On second post account is 1 (one) , Real . Account Currency is BGN . Leverage 1:30

I think that result from tester is : 

Volume ( 1000 BGN ) * Leverage ( 1/30 ) = 33.33    ... and this is false !

In description of  SYMBOL_CALC_MODE_FOREX Quote of Deposit currency not exist !

It is only false if the tester account currency is also BGN, which I still doubt.

Please add this to your code and show results here again.

EDIT:
The formula in SYMBOL_CALC_MODE* gives you the margin requirements in margin currency of your symbol. This needs to be converted to AccountCurrency.


 

Now I am on other computer.

Same Account, Broker, Account currency, Tester build. Only Wndows is a little older, but is 10, without updates.

Result in Tester is normal, as on Real account, ... but this computer is to slow for tests.

CS      0       07:37:50.028    127.0.0.1       login (build 3802)
CS      0       07:37:50.040    Tester  account info found with currency BGN
CS      0       07:37:50.042    Tester  expert file added: Experts\testEA.ex5. 11838 bytes loaded
CS      0       07:37:50.071    Tester  initial deposit 1000.00 BGN, leverage 1:100
CS      0       07:37:50.071    Tester  successfully initialized
...
CS      0       07:37:50.349    testEA (EURUSD,M1)      2023.05.01 00:00:00    => MaginInit=3.33
CS      0       07:37:50.349    testEA (EURUSD,M1)      2023.05.01 00:00:00    => MarginKeep=0.00
CS      0       07:37:50.353    testEA (EURUSD,M1)      2023.05.01 00:00:00    => USDBGN=1.77507/1.77466
CS      0       07:37:50.353    testEA (EURUSD,M1)      2023.05.01 00:00:00    => AccLeverage=100
CS      0       07:37:50.353    testEA (EURUSD,M1)      2023.05.01 00:00:00    ===> MarginCalculated=65.20
CS      3       07:37:54.347    Tester  stopped by user

And on this computer I can change Tester currency. On Faster, with Tester problem, I cant.


 
Ivan Ivanov #:

Now I am on other computer.

Same Account, Broker, Account currency, Tester build. Only Wndows is a little older, but is 10, without updates.

Result in Tester is normal, as on Real account, ... but this computer is to slow for tests.

And on this computer I can change Tester currency. On Faster, with Tester problem, I cant.


Interesting.

So in conclusion, it is the account currency, that has "played" you here.

Why can't you change the account currency? Is the tester maybe set to test indicators, instead of EAs, maybe?
 
Dominik Egert #:
Interesting.

So in conclusion, it is the account currency, that has "played" you here.

Why can't you change the account currency? Is the tester maybe set to test indicators, instead of EAs, maybe?

I think I find solution for OrderCalcMargin . It works normal , when "calculate profit in pips" is NOT checked.

//==========================Test1===
CS      0       16:24:46.482    127.0.0.1       login (build 3802)
CS      0       16:24:46.483    Tester  account info found with currency BGN
CS      0       16:24:46.507    Tester  initial deposit 1000.00 BGN, leverage 1:100
CS      0       16:24:46.507    Tester  successfully initialized
CS      0       16:24:46.673    ******** (EURUSD,M1)    2023.05.01 00:00:00    Margin=65.20
CS      0       16:27:41.572    Tester  final balance 1262.43 BGN

CS      0       16:27:41.577    Tester  EURUSD,M1: 422425 ticks, 106935 bars generated.
CS      0       16:27:41.577    Tester  EURUSD,M1: total time from login to stop testing 0:02:55.094
CS      0       16:27:41.932    127.0.0.1       shutdown finished
// =========================Test2===
CS      0       16:32:46.366    127.0.0.1       login (build 3802)
CS      0       16:32:46.366    Tester  account info found with currency BGN
CS      0       16:32:46.381    Tester  calculate profit in pips, initial deposit 1000, leverage 1:100
CS      0       16:32:46.381    Tester  successfully initialized
CS      0       16:32:46.415    ******** (EURUSD,M1)    2023.05.01 00:00:00    Margin=33.33
CS      0       16:36:04.245    Tester  final balance 1159.85 pips

CS      0       16:36:04.250    Tester  EURUSD,M1: 422425 ticks, 106935 bars generated.
CS      0       16:36:04.250    Tester  EURUSD,M1: total time from login to stop testing 0:03:17.884

It seems that  "The value is returned in the account currency", written in documentation, is not true, when "calculate profit in pips" is checked.

... only for info : 65.20/33.33 is approximately equal to the fixing EURBGN . It is not floating. So looks like returned value is in EUR. ???

 
Ivan Ivanov #:

I think I find solution for OrderCalcMargin . It works normal , when "calculate profit in pips" is NOT checked.

It seems that  "The value is returned in the account currency", written in documentation, is not true, when "calculate profit in pips" is checked.

... only for info : 65.20/33.33 is approximately equal to the fixing EURBGN . It is not floating. So looks like returned value is in EUR. ???

Well, I would say this is congruent.

In pips is margin currency of symbol. So in your second case the margin is represented in EUR.