Strange "not enough money" when EA opens a position.

 

First off all, sorry for my bad english, my mother language is Spanish. 

Im programming an EA for Forex on MQL5 that opens positions when some conditions are met, the EA places a TP and SL based on some conditions and of course calculates the lotage to only risk 1% of the account. 

While doing Buy/Long positions, everything is correct and the numbers are correct 

The problem begins while doing sell/short operations: Sometimes the operation never opens and MT5 shows me the error "not enough money" i configurated the EA to show me in the log the Lots, SL and TP that result before execute an operation, the numbers are correct and i can literally place the same order manually without any problem. Like i said sometimes it happends, others don't and the operation executes correctly, i haven't found anything in common when the problem occurs, tryed forcing big positions with tons of pips and sometimes the error happends, same goes with small operations for a few pips, the lots also aren't anything crazy.

This is the part of the code that calculates for sell/short. Like i said, i tested it a lot and the numbers and calculations seems correct but shows the error "not enough money" even when i can open the exact same position manually with the same lots, SL and TP. 

(the file is just a pic of the error in the log, so you can see the lot wasnt anything crazy, i inmediatly just placed a position manually with the exact same values and was executed correctly. So i don't understand why says "not enough money")


double riesgoPorcentaje = 1.0;
double balance = AccountInfoDouble(ACCOUNT_BALANCE);

    double riesgoMaximo = balance * (riesgoPorcentaje / 100.0);
    

    double stopLossDistancia = MathAbs(precioAsk - SLVenta);
    

    double valorPunto = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
    double tamanoPunto = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
    

    double perdidaPorLote = stopLossDistancia / tamanoPunto * valorPunto;
    

    double lotaje = riesgoMaximo / perdidaPorLote;
    

    double loteMinimo = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
    double loteMaximo = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
    double pasoLote = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
        

    lotaje = MathMax(loteMinimo, MathMin(lotaje, loteMaximo));
    lotaje = NormalizeDouble(lotaje - fmod(lotaje - loteMinimo, pasoLote), 2);
    
    printf("Giro encontrado, ejecutando orden, se arriesgaran: " + DoubleToString(riesgoMaximo) + " Dolares en la operacion");
    printf("Se abre operacion con " + DoubleToString(lotaje) + " lotes con TP en " + DoubleToString(TPNivel) + " y SL en " + DoubleToString(SLVenta));
    SendNotification("Ejecutando Orden \nNivel de cierre sera: " + DoubleToString(NivelCierre) + Cambio);


   NivelGestion = MathMax(iHigh(_Symbol, PERIOD_CURRENT, 1), iHigh(_Symbol, PERIOD_CURRENT, 2));
           int ticket = trade.Sell(lotaje, _Symbol, SYMBOL_ASK, SLVenta, TPNivel, 0);
Files:
order.jpg  16 kb
 

do you print the calculated lot to the journal? you do not show any custom print message in the image.

EDIT: normally we use bid price when opening sell trades. that could be whole reason.

 

I think you are right, gonna change that right now and test it

Kinda rare that only Sells are having problems while Buys are working correctly even when have the same mistake; Buy orders have "Symbol_Bid", like this. 

int ticket = trade.Buy(lotaje, _Symbol, SYMBOL_BID, SLCompra, TPNivel, "");
 
lahu92 #:

I think you are right, gonna change that right now and test it

Kinda rare that only Sells are having problems while Buys are working correctly even when have the same mistake; Buy orders have "Symbol_Bid", like this. 

lots of possible reasons, however, the most common would be that the prices that you have incorrectly asked for, could be within the default maximum slippage allowed.

to open a sell trade we use bid.
to open a buy trade we use ask.

to close a sell trade we use ask.
to close a buy trade we use bid.

 
Michael Charles Schefe #:

lots of possible reasons, however, the most common would be that the prices that you have incorrectly asked for, could be within the default maximum slippage allowed.

to open a sell trade we use bid.
to open a buy trade we use ask.

to close a sell trade we use ask.
to close a buy trade we use bid.

Now that i changed it, im having problems with the buy orders. Sells are working as intended, now Buys are showing that error. 
Files:
error.jpg  32 kb
 

see how you have SYMBOL_ASK? I believe you can not use it that way. Instead change it to the full function or Ask or Bid instead.

that whole line is wrong. you have put the sl where the slippage should be. But it would be ok if you just followed the syntax shown above the cursor when you start typing. (amused sarcasm).

 
lol... 
Files:
syntax.jpg  19 kb
 

Hi

You must use functions for getting the correct prices, not used predefined variables from symbol properties. Also use proper prices: Ask for Buy and Bid for Sell.

trade.Buy(lotaje, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_ASK), SLCompra, TPNivel, "");
trade.Sell(lotaje, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_BID), SLVenta, TPNivel, "");

I am not sure whether you are using the same TP for Buy and Sell – but also check this out. And to be perfectly sure what the trade will be opened you should check free margin allowed for this lot size and check if SL/TP is not too close to close price (stoplevel, freezelevel) etc. But those are just additional functions. 

Have a nice day👍📊
 
Marzena Maria Szmit #:

Hi

You must use functions for getting the correct prices, not used predefined variables from symbol properties. Also use proper prices: Ask for Buy and Bid for Sell.

I am not sure whether you are using the same TP for Buy and Sell – but also check this out. And to be perfectly sure what the trade will be opened you should check free margin allowed for this lot size and check if SL/TP is not too close to close price (stoplevel, freezelevel) etc. But those are just additional functions. 

Have a nice day👍📊

Hi, is possible to have the same error "not enough money/margin" due the leverage of the demo account? Im testing with "realistic" numbers and sometimes i get the error and others don't. 

I just had this same code running in 2 MT5 instances, same broker for the demo but different quantity of money one 1000 and the other 5000. The account with 5000 created the position without any problem but the account with 1000 returned the error "Not enough money/margin". 

 
lahu92 #:

Hi, is possible to have the same error "not enough money/margin" due the leverage of the demo account? Im testing with "realistic" numbers and sometimes i get the error and others don't. 

I just had this same code running in 2 MT5 instances, same broker for the demo but different quantity of money one 1000 and the other 5000. The account with 5000 created the position without any problem but the account with 1000 returned the error "Not enough money/margin". 

You should use the functions OrderCalcProfit and OrderCalcMargin to correctly calculate your order's volume.

The function OrderCalcProfit should be used to calculate the volume based on the stop-loss risk % of your account balance, and the function OrderCalcMargin should be used to calculate the volume based on the leverage and margin requirements. Of the two volume values, you should use the lesser of the two, to make sure you never have the "not enough money" issue.

Here is another reference you should research for your EA ...

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.