How to calculate free margin left before opening an order ?

 

Hello,


I would like to know the expression to calculate :


In my EA I would make something like this :

- I try 1 lot

- If the free margin is not ok, I try 0.9 lots

- If the free margin is not ok, I try 0.8 lots

- and continue until have the minimum lots to open an order


Have you got an expression to make this ?

Thank you...

 
nazoreen:

Hello,


I would like to know the expression to calculate :


In my EA I would make something like this :

- I try 1 lot

- If the free margin is not ok, I try 0.9 lots

- If the free margin is not ok, I try 0.8 lots

- and continue until have the minimum lots to open an order


Have you got an expression to make this ?

Thank you...

AccountFreeMargin()

https://docs.mql4.com/account/AccountFreeMargin


double Risk=3;  // percent of free margin to calculate lot size.
 
 
double Lots=NormalizeDouble(AccountFreeMargin()*Risk/100/1000.0,1);   // Standard lot size
 

Hello and thank you...


Could you explain numbers "100" and "1000" ?!?



Thanks a lot !

 

Hello,


I found the answer :

double MargRequired = MarketInfo ( Symbol ( ), MODE_MARGINREQUIRED ) ; // current price * 1000

double Equity = AccountEquity ( ) ;


double Max_Lots = NormalizeDouble ( Equity / MargRequired, 1 ) ;


With this data, I now know what is the maximum lots I could trade.


Thank you !

Reason: