How to calculate free margin left before opening an order ?

[Deleted]  

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...

Wackena
591
Wackena  
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
[Deleted]  

Hello and thank you...


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



Thanks a lot !

[Deleted]  

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 !