Trying to replicate OrderCalcMargin()

 

I've looked for any documentation on what's under the hood of this function with no luck so far.

I'm trying to calculate required margin for upcoming positions, and OrderCalcMargin() gives me a different value than the standard calcuation that I'm digging up everwhere else on the web.

I'm currently trying to sort it out where

double m = OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, 1.67, last_tick.ask, marginNeeded);

is returning a quite different value than

double leverage = (double)AccountInfoInteger(ACCOUNT_LEVERAGE);
double contractSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_CONTRACT_SIZE);

double calcMargin = 1.67 * contractSize * last_tick.ask / leverage;

I want to plug this formula into a spreadsheet to figure out some numbers but I don't know which version to trust, and if version A happens to be correct I don't know what the underlying calculations are anyways.

Any help appreciated. Thanks!

 
rrsch: I've looked for any documentation on what's under the hood of this function with no luck so far.

You will find it here ... https://www.mql5.com/en/docs/constants/environment_state/marketinfoconstants#enum_symbol_calc_mode

Both the OrderCalcMargin and OrderCalcProfit functions will calculate differently depending on the ENUM_SYMBOL_CALC_MODE

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
 
Fernando Carreiro #:

You will find it here ... https://www.mql5.com/en/docs/constants/environment_state/marketinfoconstants#enum_symbol_calc_mode

Both the OrderCalcMargin and OrderCalcProfit functions will calculate differently depending on the ENUM_SYMBOL_CALC_MODE

Wonderful, thanks. I'll look into that.