OrderSend Error 134 (No Enough Money)

Sometimes, especially during backtesting or in case of a badly designed expert advisor, the OrderSend Error 134 (or ERR_NOT_ENOUGH_MONEY) appears in the Journal log of your MetaTrader platform. In MT5 this error is called TRADE_RETCODE_NO_MONEY and has a code 10019. The reason for this error is the same for both versions of the platform — lack of free margin to execute the OrderSend function for the given position volume. The methods to solve this error are quite different (we won’t discuss the post-development method, which is to deposit more funds to your trading account).
In MetaTrader 4, you have to code your EA to perform the following check before executing OrderSend():

if (AccountFreeMarginCheck(Symbol(), Direction, Volume) > 0)

It will check how much free margin will be available after opening a position with the volume = Volume in a given direction = Direction. But comparing it with 0 isn’t a very good solution. One should pay attention to the broker’s stop-out level to consider the margin requirements:

if (((AccountStopoutMode() == 1) && 
(AccountFreeMarginCheck(Symbol(), Direction, Volume) > AccountStopoutLevel()))
|| ((AccountStopoutMode() == 0) && 
((AccountEquity() / (AccountEquity() - AccountFreeMarginCheck(Symbol(), Direction, Volume)) * 100) > AccountStopoutLevel())))

This way we check the way your broker applies stop-out (absolute or relative level) and check if we’ll have enough margin after our next trade.
For MetaTrader 5 this condition would look like this (replace SYMBOL_MARGIN_LONG with SYMBOL_MARGIN_SHORT if you want to check margin before opening a short position):

if (((AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE) == ACCOUNT_STOPOUT_MODE_MONEY)
&& (AccountInfoDouble(ACCOUNT_FREEMARGIN) - SymbolInfoDouble(Symbol(), SYMBOL_MARGIN_INITIAL) * Lots * SymbolInfoDouble(Symbol(), SYMBOL_MARGIN_LONG) > AccountInfoDouble(ACCOUNT_MARGIN_SO_SO)))
|| ((AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE) == ACCOUNT_STOPOUT_MODE_PERCENT)
&& ((AccountInfoDouble(ACCOUNT_EQUITY)/(AccountInfoDouble(ACCOUNT_EQUITY) - (AccountInfoDouble(ACCOUNT_FREEMARGIN) - SymbolInfoDouble(Symbol(), SYMBOL_MARGIN_INITIAL) * Lots * SymbolInfoDouble(Symbol(), SYMBOL_MARGIN_LONG))) * 100 > AccountInfoDouble(ACCOUNT_MARGIN_SO_SO))))

It is quite complex compared to the MT4 version but it checks the same condition.
Registering with a Forex broker that offers a lower stop-out level is also a good option :-).
Update 2016-11-01: Thanks to Alexander Wait’s comment, I have corrected the formulas for account margin check.
If you have any thoughts, comments, or questions regarding MetaTrader OrderSend Error 134 or Return Code 10019 in MT5 and the ways to treat it, feel free to reply to this post using the form below.

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + 3 =