AccountFreeMarginCheck in MQL5 ?

 

Hi. Traders and Coders.

I used this article to convert my MQL4 code to MQL5 code. https://www.mql5.com/en/articles/81

Just found that AccountFreeMarginCheck in MQL4 is not available in MQL5.

Does anyone have some ways of checking free margin before sending order in MQL5 ?

The code look like this in MQL4:

if(AccountFreeMarginCheck(Symbol(),OP_BUY,Lots)<=0 || GetLastError()==134) return;

I need to do pretty much same for MQL5 too.

It will be really appreciated if you can share how to replace above MQL4 code in MQL5 language ? As far as I know, CTrade does not have such a functionality either. However including this function makes a lot of sense.

Thanks so much in advance for your sharing.

Kind regards.

F.E.

Migrating from MQL4 to MQL5
Migrating from MQL4 to MQL5
  • 2010.05.17
  • Sergey Pavlov
  • www.mql5.com
This article is a quick guide to MQL4 language functions, it will help you to migrate your programs from MQL4 to MQL5. For each MQL4 function (except trading functions) the description and MQL5 implementation are presented, it allows you to reduce the conversion time significantly. For convenience, the MQL4 functions are divided into groups, similar to MQL4 Reference.
 

Using the class CCAccountInfo. Not sure it works in indicators.

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/caccountinfo/caccountinfomargincheck

Documentation on MQL5: Standard Library / Trade Classes / CAccountInfo / MarginCheck
Documentation on MQL5: Standard Library / Trade Classes / CAccountInfo / MarginCheck
  • www.mql5.com
Standard Library / Trade Classes / CAccountInfo / MarginCheck - Reference on algorithmic/automated trading language for MetaTrader 5
 
blouf:

Using the class CCAccountInfo. Not sure it works in indicators.

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/caccountinfo/caccountinfomargincheck


Thanks this is great.

This is so far closest thing to AccountFreeMarginCheck in MQL4.

I just also found that they in fact have FreeMarginCheck too in MQL5 language. No wonder why the search did not come back with this. Because they called different name now.

Anyway, it is great finding.

Thanks so much blouf.

Regards.


double  FreeMarginCheck(
   const string        symbol,              // symbol
   ENUM_ORDER_TYPE     trade_operation,     // operation
   double              volume,              // volume
   double              price                // price
   ) const
 

Found the ways to check margin without using other library.

Please try this code and let me know what you think.

I think it is working fine. :)

      
      double freeMargin = AccountInfoDouble(ACCOUNT_FREEMARGIN);
      double margin = 0.0;
      
      bool ret = OrderCalcMargin(ORDER_TYPE_BUY, mSymbol, volume, mAsk, margin);


      if(freeMargin - margin <= 0.0 || ret != true)
      {
          printf("AIM: Not enougth Money to send buy order with %f lot or Margin Calculation Error", volume);
          Sleep(1000);
          return (false);
      }
 
Young Ho Seo #:

Found the ways to check margin without using other library.

Please try this code and let me know what you think.

I think it is working fine. :)

Thanks. Great approach.

Slightly adjusted it to new mql5 constant names:

double free_margin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
double margin = 0.0;
bool margin_check_result = OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, lots, ask, margin);
if(free_margin - margin <= 0.0 || margin_check_result != true)
  {
   printf("Not enough money to send order with %f lot or Margin Calculation Error", lots);
   Sleep(1000);
   return (false);   
  }