I require a clarification on the concept of Free Margin!

 

I am a bit confused. I ran a test on my EA (set deposit $10,000 on EUR/USD, every tick) and at one point the test was terminated due to a "Stop Out" error.

I checked the account balance thinking that the resulting losses wiped out the account, but there was still a $9,300 balance in the account at the moment of stop.

Apparently the combined losses of 2 open BUY position right before the "Stop Out" wiped out the Free Margin available even though equity was still at $9,300!

I don't get it... I thought margin calls only happen once you actually run out of money in your account! How come it wouldn't let me hold the positions if the resulting losses from them were way below the remaining Equity?

Thanks!

 

If you have a higher leverage you need less margin, if you have lower leverage you need more margin.

http://www.investopedia.com/articles/forex/07/forex_leverage.asp#axzz241GNVVbx

 

Thanks for your reply RaptorUK! I'm starting to understand the concept of margin better now.

I was always under the impression that a "margin call" meant that all the money in your account has been wiped out.

But I see now it only refers to the amount of free-margin remaining after opening an order, which may be FAR LESS than the amount of money in your account, depending on the broker leverage and lot size of the opened order.

 
To prevent stop out, you have to compute what will be the free margin at the MAE (i.e. SL) for all open orders and reduce the lotsize until that is positive. See my code
 

Yeah, thanks WHRoeder! I already figured as much!

Just that up until now I was sure the *free margin* you had to work with after opening a position was the account equity. Now that I know how the free margin is actually calculated I have adapted my code accordingly.

Thanks for the help and the code sample!

Reason: