using Account information functions

 

Hello everyone,

Would like to know, how to properly use AccountFreeMargin function. I want my expert to not open orders when lets say the margin is less than 100, so I write,

if(AccountFreemargin()>=100)

{...logic to open orders...}

but this is not working since some orders still open even when the available margin is less than 100 hence i receive a margin call.

Am i approaching the problem in the right way?

thanks

 
if(AccountFreemargin()>=100)
   {
    Print("AccountFreemargin  = ",AccountFreemargin());
    ...logic to open orders...
    OrderSend(.....                 //Placing trade...
    Print("New AccountFreemargin  = ",AccountFreemargin());         // and check again.....
   }
 
deVries:



thanks for the reply, I see the only different thing here is that I print out the Account free margin before and after an order has been made. what effect does printing this out have?
 
nduru22:


thanks for the reply, I see the only different thing here is that I print out the Account free margin before and after an order has been made. what effect does printing this out have?

Your check      "I want my expert to not open orders when lets say the margin is less than 100"
 
nduru22:

Hello everyone,

Would like to know, how to properly use AccountFreeMargin function. I want my expert to not open orders when lets say the margin is less than 100, so I write,

if(AccountFreemargin()>=100)

{...logic to open orders...}

but this is not working since some orders still open even when the available margin is less than 100 hence i receive a margin call. 

How do you know what the free margin was at the time that the trade was opened ?  how do you know the margin requirement for the trade you are placing ?
 
RaptorUK:
How do you know what the free margin was at the time that the trade was opened ?  how do you know the margin requirement for the trade you are placing ?



i have no way of knowing, the only way to know is by e.g printing out as iv been shown. But Id like my advisor to be able to read the margin level and avoid if let say the margin goes below 100
 
nduru22:Would like to know, how to properly use AccountFreeMargin function. I want my expert to not open orders when lets say the margin is less than 100, so I write, 

You want to prevent free margin from reaching zero (stop out, margin call) and the broker from closing the trades.

Not just when you open it, but at the most adverse excursion (i.e. the stop loss,) for all open orders on all pairs. See my code

 

I think you are approaching the problem in the right way. 

Maybe check AccountFreeMarginMode( ), the value will tell you if floating profit/losses are affecting your free margin number.

0 - floating profit/loss is not used for calculation;
1 - both floating profit and loss on open positions on the current account are used for free margin calculation;
2 - only profit value is used for calculation, the current loss on open positions is not considered;

3 - only loss value is used for calculation, the current loss on open positions is not considered.  

 

Besides that, I can't see any problems in the code you posted.

Maybe 100 is a little small, are you using only micro and mini lots?

 
WHRoeder:

You want to prevent free margin from reaching zero (stop out, margin call) and the broker from closing the trades.

Not just when you open it, but at the most adverse excursion (i.e. the stop loss,) for all open orders on all pairs. See my code



thanks. having a look at it
 
alladir:

I think you are approaching the problem in the right way. 

Maybe check AccountFreeMarginMode( ), the value will tell you if floating profit/losses are affecting your free margin number.

0 - floating profit/loss is not used for calculation;
1 - both floating profit and loss on open positions on the current account are used for free margin calculation;
2 - only profit value is used for calculation, the current loss on open positions is not considered;

3 - only loss value is used for calculation, the current loss on open positions is not considered.  

 

Besides that, I can't see any problems in the code you posted.

Maybe 100 is a little small, are you using only micro and mini lots?



okay, let me look at AccountFreeMarginMode( ), thanks. Im using mini lots so i thought 100 is a good number
 
nduru22:


okay, let me look at AccountFreeMarginMode( ), thanks. Im using mini lots so i thought 100 is a good number


What kinds of profits/loss are you making?

If your total loss is >100, and floating losses are included in the calculation of AccountFreeMargin()   (i.e. AccountFreeMarginMode() = 1 or 4)  then I guess this will cause a margin call even though no new trades were opened while free margin was <100.

Fundamentally, I think 100 is too small an amount. It depends on how much you are trading but if it's any decent amount, leaving only 100 is just too risky.

Reason: