Error #0 MT4

 

hello,

I took a few months to see EA's using two, testing on demo and real, and I will work without problem until yesterday, I suddenly goes out, only in real, the demos are working.

The command failed with error # 0

And I have not changed anything is a robot position in eur / usd at different times, and use magic number, someone you know might be going?

I reinstalled the MT4 on my VPS but nothing
 

Perhaps you are clearing the error stored by GetLastErrorr() and then calling it when you actually do have an error and it is already cleared . . . hard to say without seeing your code.

 
Bellagio:
The command failed with error # 0

The return value 0 from trading functions is not an error.

https://docs.mql4.com/trading/errors

Show us the function and the code reporting the error and somebody may be able to help you.

 
int start()
{
ganancias = (AccountEquity() - AccountBalance());

if (cierre_automatico == true && ganancias >= valor_cierre) Trade = false;

if (cierre_automatico == true && ganancias >= valor_cierre) CerrarPosiciones = true;

if (CerrarPosiciones==true && Trade==false)
{
CerrarTodasPosiciones();
return (0);
}

double precio_orden;
double precio_SL;
double precio_TP;

double dob_lotes;
double prog_tp;
double prog_limit;


int existe_buy = 0;
int existe_sell = 0;

int existe_buylimit = 0;
int existe_selllimit = 0;

double ord_lotes_buy = 0;
double ord_lotes_sell = 0;

double ord_abierta_buy = 0;
double ord_abierta_sell = 0;

double ord_tp_sell = 0;
double ord_tp_buy = 0;

double nu_1 = 0;
double nu_2 = 0;

double ini_buy_sl = 0;
double ini_sell_sl = 0;

RefreshRates();

for (int cont_ord = 0; cont_ord < OrdersTotal(); cont_ord++)
{
OrderSelect(cont_ord, SELECT_BY_POS, MODE_TRADES);

if (OrderSymbol() == Symbol() && OrderMagicNumber() == numero_magico)
{
if (OrderType() == OP_BUY && existe_buy < OrderTicket())
{
ord_lotes_buy = OrderLots();
ord_abierta_buy = OrderOpenPrice();
existe_buy = OrderTicket();
ord_tp_buy = OrderTakeProfit();

if (OrderLots() == volumen) ini_buy_sl = OrderOpenPrice() - sl * puntos*Point;
}

if (OrderType() == OP_SELL && existe_sell < OrderTicket())
{
ord_lotes_sell = OrderLots();
ord_abierta_sell = OrderOpenPrice();
existe_sell = OrderTicket();
ord_tp_sell = OrderTakeProfit();

if (OrderLots() == volumen) ini_sell_sl = OrderOpenPrice() + sl *puntos* Point;
}

if (OrderType() == OP_BUYLIMIT)
{
existe_buylimit = OrderTicket();
nu_2 = OrderLots();
}

if (OrderType() == OP_SELLLIMIT)
{
existe_selllimit = OrderTicket();
nu_2 = OrderLots();
}



perdidas = (AccountBalance() - AccountEquity());
if (perdidas >= Aviso_Perdidas)
{
if (NuevaBarra())SendMail (Symbol() + " Aviso: Alcanzada perdida especificada.",Symbol ()+ "");
}
}
}

int H_Level =-40;
int L_Level =50;

double WPR1 = iCustom(Symbol(),0,"WPR"",10,9,0.5,-0.5,200,0);
double WPR2 = iCustom(Symbol(),0,"WPR2",10,9,0.5,-0.5,200,1);

if (existe_buy == 0 && solocompra == true && hedging == false)
{
if (existe_buylimit != 0) OrderDelete(existe_buylimit);

precio_SL2 = Ask - sl *puntos* Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);

if (sl <= 0) precio_SL = 0;
precio_TP2 = Ask + tp1 * puntos*Point;
precio_TP = NormalizeDouble (precio_TP2,decimales);
orden_entrada = NormalizeDouble (Ask, decimales);

if(TimeHour(TimeCurrent()) >= l1 && TimeHour(TimeCurrent()) < grafico && Trade && ((WPR1>H_Level && WPR2<H_Level)))
{
ticket = OrderSend(Symbol(), OP_BUY, volumen, orden_entrada,0,0,0, tradecomment + "B " + valorlote, numero_magico, 0, CLR_NONE);
}

if(ticket>0)
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderModify(OrderTicket(),OrderOpenPrice(),precio_SL,precio_TP,0,CLR_NONE);
}
else
{
Print("La orden ha fallado con error #",GetLastError());
}
}

if (existe_buy == 0 && hedging == true && existe_sell==0)
{
if (existe_buylimit != 0) OrderDelete(existe_buylimit);

precio_SL2 = Ask - sl * puntos*Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);

if (sl <= 0) precio_SL = 0;
precio_TP2 = Ask + tp1 *puntos* Point;
precio_TP = NormalizeDouble (precio_TP2,decimales);
orden_entrada = NormalizeDouble (Ask, decimales);

if(TimeHour(TimeCurrent()) >= l1 && TimeHour(TimeCurrent()) < grafico && Trade && ((WPR1>H_Level && WPR2<H_Level)))
{
ticket = OrderSend(Symbol(), OP_BUY, volumen, orden_entrada,0,0,0, tradecomment + "B " + valorlote, numero_magico, 0, CLR_NONE);
}

if(ticket>0)
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderModify(OrderTicket(),OrderOpenPrice(),precio_SL,precio_TP,0,CLR_NONE);
}
else
{
Print("La orden ha fallado con error #",GetLastError());
}
}

if (existe_sell == 0 && solocompra == false && hedging == false)
{
if (existe_selllimit != 0) OrderDelete(existe_selllimit);


precio_SL2 = Bid + sl * puntos*Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);

if (sl <= 0) precio_SL = 0;
precio_TP2 = Bid - tp1 * puntos*Point;
precio_TP = NormalizeDouble (precio_TP2,decimales);
orden_entrada = NormalizeDouble (Bid, decimales);

if(TimeHour(TimeCurrent()) >= l1 && TimeHour(TimeCurrent()) < grafico && Trade && ((WPR1<L_Level && WPR2>L_Level)))
{
ticket = OrderSend(Symbol(), OP_SELL, volumen, orden_entrada,0,0,0, tradecomment + "S " + valorlote, numero_magico, 0, CLR_NONE);
}

if(ticket>0)
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderModify(OrderTicket(),OrderOpenPrice(),precio_SL,precio_TP,0,CLR_NONE);
}
else
{
Print("La orden ha fallado con error #",GetLastError());
}
}

if (existe_sell == 0 && hedging == true && existe_buy==0)
{
if (existe_selllimit != 0) OrderDelete(existe_selllimit);


precio_SL2 = Bid + sl *puntos* Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);

if (sl <= 0) precio_SL = 0;
precio_TP2 = Bid - tp1 * puntos*Point;
precio_TP = NormalizeDouble (precio_TP2,decimales);
orden_entrada = NormalizeDouble (Bid, decimales);

if(TimeHour(TimeCurrent()) >= l1 && TimeHour(TimeCurrent()) < grafico && Trade && ((WPR1<L_Level && WPR2>L_Level)))
{
ticket = OrderSend(Symbol(), OP_SELL, volumen, orden_entrada,0,0,0, tradecomment + "S " + valorlote, numero_magico, 0, CLR_NONE);
}

if(ticket>0)
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderModify(OrderTicket(),OrderOpenPrice(),precio_SL,precio_TP,0,CLR_NONE);
}
else
{
Print("La orden ha fallado con error #",GetLastError());
}
}
//---------------------------------------------------------------------------------------

//-----

if (existe_buy != 0 && existe_buylimit == 0)
{
simlot=MathCeil(volumen*100)/100;
for (int cont=1; cont<50; cont++)
{
if (ord_lotes_buy==simlot)
{
progresion=cont;
break;
}
simlot=MathCeil(Lotexponent * simlot*100)/100;
}

prog_limit = buscarlimit(progresion);
prog_tp = buscartp(progresion);


//---------------------------------------------------------------------------------



precio_orden2 = ord_abierta_buy - prog_limit *puntos* Point;
precio_orden = NormalizeDouble(precio_orden2,decimales);
if (l2 == FALSE)
{
precio_SL2 = precio_orden - sl *puntos* Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);
}

else {
precio_SL = ini_buy_sl;
}

if (sl <= 0) precio_SL = 0;
precio_TP2 = precio_orden + prog_tp * puntos*Point;
precio_TP = NormalizeDouble (precio_TP2,decimales);

dob_lotes = MathCeil(Lotexponent * ord_lotes_buy*100)/100;

//---------------------------------------------------------------------------------

if (Trade)
{
valorlote2 = DoubleToStr (dob_lotes, 2);
if (maxlote==true)
{
if (dob_lotes == MaximoLote)
{
if (NuevaBarra())SendMail(Symbol()+ " Lote Maximo",Symbol()+" Atencion");
}
if (dob_lotes <= MaximoLote && solocompra == true && hedging == false)
{
OrderSend(Symbol(), OP_BUYLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "BLT " + valorlote2, numero_magico, 0, CLR_NONE);
}
if (dob_lotes <= MaximoLote && hedging == true)
{
OrderSend(Symbol(), OP_BUYLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "BLT " + valorlote2, numero_magico, 0, CLR_NONE);
}
}

if (maxlote==false && solocompra == true && hedging == false) OrderSend(Symbol(), OP_BUYLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "BLT " + valorlote2, numero_magico, 0, CLR_NONE);
if (maxlote==false && hedging == true) OrderSend(Symbol(), OP_BUYLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "BLT " + valorlote2, numero_magico, 0, CLR_NONE);
}
}


//-----

if (existe_sell != 0 && existe_selllimit == 0)
{
simlot=MathCeil(volumen*100)/100;
for (cont=1; cont<50; cont++)
{
if (ord_lotes_sell==simlot)
{
progresion=cont;
break;
}
simlot=MathCeil(Lotexponent * simlot*100)/100;
}

prog_limit = buscarlimit(progresion);
prog_tp = buscartp(progresion);

//---------------------------------------------------------------------------------

precio_orden2 = ord_abierta_sell + prog_limit * puntos*Point;
precio_orden = NormalizeDouble(precio_orden2,decimales);
if (l2 == FALSE)
{
precio_SL2 = precio_orden + sl * puntos*Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);
}
else
{
precio_SL = ini_sell_sl;
}


if (sl <= 0) precio_SL = 0;
precio_TP2 = precio_orden - prog_tp * puntos*Point;
precio_TP = NormalizeDouble (precio_TP2,decimales);

dob_lotes = MathCeil(Lotexponent * ord_lotes_sell*100)/100;

//---------------------------------------------------------------------------------

if (Trade)
{
valorlote2 = DoubleToStr (dob_lotes, 2);
if (maxlote==true)
{
if (dob_lotes == MaximoLote)
{
if (NuevaBarra())SendMail(Symbol()+ " Lote Maximo",Symbol()+" Atencion");
}

if (dob_lotes <= MaximoLote && solocompra == false && hedging == false)
{
OrderSend(Symbol(), OP_SELLLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "SLT " + valorlote2, numero_magico, 0, CLR_NONE);
}
if (dob_lotes <= MaximoLote && hedging == true)
{
OrderSend(Symbol(), OP_SELLLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "SLT " + valorlote2, numero_magico, 0, CLR_NONE);
}
}

if (maxlote==false && solocompra == false && hedging == false) OrderSend(Symbol(), OP_SELLLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "SLT " + valorlote2, numero_magico, 0, CLR_NONE);
if (maxlote==false && hedging == true) OrderSend(Symbol(), OP_SELLLIMIT, dob_lotes, precio_orden, margen, precio_SL, precio_TP, tradecomment + "SLT " + valorlote2, numero_magico, 0, CLR_NONE);
}
}

//-----

for (cont_ord = 0; cont_ord < OrdersTotal(); cont_ord++)
{
OrderSelect(cont_ord, SELECT_BY_POS, MODE_TRADES);

if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && existe_buy != 0 && OrderMagicNumber() == numero_magico )
{
if (l2 == FALSE)
{
precio_SL2 = ord_abierta_buy - sl * puntos*Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);
}

else
{
precio_SL = ini_buy_sl;
}

if (sl <= 0) precio_SL = 0;

if (existe_buy != OrderTicket()) precio_TP = ord_tp_buy;
else precio_TP = OrderTakeProfit();

if (precio_TP != OrderTakeProfit() || precio_SL != OrderStopLoss()) OrderModify(OrderTicket(), OrderOpenPrice(), precio_SL, precio_TP, 0, CLR_NONE);

}

if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && existe_sell != 0 && OrderMagicNumber() == numero_magico )
{
if (l2 == FALSE)
{
precio_SL2 = ord_abierta_sell + sl * puntos*Point;
precio_SL = NormalizeDouble (precio_SL2,decimales);
}
else
{
precio_SL = ini_sell_sl;
}

if (sl <= 0) precio_SL = 0;

if (existe_sell != OrderTicket()) precio_TP = ord_tp_sell;
else precio_TP = OrderTakeProfit();

if (precio_TP != OrderTakeProfit() || precio_SL != OrderStopLoss()) OrderModify(OrderTicket(), OrderOpenPrice(), precio_SL, precio_TP, 0, CLR_NONE);
}
}
return (0);
}


//------------------------------------------------ Funciones --------------------------------------------

//-----

int buscarlimit(double nd)
{
switch (nd)
{
case 1.0:
return (limit1);

case 2.0:
return (limit2);

case 3.0:
return (limit3);

case 4.0:
return (limit4);

case 5.0:
return (limit5);

case 6.0:
return (limit6);

case 7.0:
return (limit7);

case 8.0:
return (limit8);

case 9.0:
return (limit9);

case 10.0:
return (limit10);
}
return (limit10);
}


//-----

int buscartp(double nd)
{

switch (nd)
{
case 1.0:
return (tp2);

case 2.0:
return (tp3);

case 3.0:
return (tp4);

case 4.0:
return (tp5);

case 5.0:
return (tp6);

case 6.0:
return (tp7);

case 7.0:
return (tp8);

case 8.0:
return (tp9);

case 9.0:
return (tp10);

case 10.0:
return (tp11);
}
return (tp11);
}


//-----

bool NuevaBarra()
{
static datetime ti = 0;

if (ti != Time[0])
{
ti = Time[0]; Sleep(100);
return(true);
}

return(false);
}


//-----

int CerrarTodasPosiciones()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), margen, Red );
break;


case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), margen, Red );
break;


case OP_BUYLIMIT : result = OrderDelete( OrderTicket() );
case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );
}

if(result == false)
{
Print("La Orden ", OrderTicket(), " fallo al cerrar. Error:", GetLastError() );
Sleep(3000);
}
}
return(0);
}
 

Please use this to post code . . . it makes it easier to read.

If your code is too big for the SRC window please attach it as a file . . .

 
What type is ticket declared as ?
 
if(TimeHour(TimeCurrent()) >= l1 && TimeHour(TimeCurrent()) < grafico 
&& Trade && ((WPR1<L_Level && WPR2>L_Level)))
{
    ticket = OrderSend(Symbol(), OP_SELL, volumen, orden_entrada,0,0,0, 
                       tradecomment + "S " + valorlote, numero_magico, 0, CLR_NONE);
}

if(ticket>0)
{
    OrderSelect(ticket,SELECT_BY_TICKET);
    OrderModify(OrderTicket(),OrderOpenPrice(),precio_SL,precio_TP,0,CLR_NONE);
}
else
{
    Print("La orden ha fallado con error #",GetLastError());
}

If the FIRST Ordersend fails, you print the error.

Then if THIS IF is false, you do not call OrderSend.

At this point ticket is still negative and you print error #0

 

Here's what WHRoeder meant, Bellagio :

if(TimeHour(TimeCurrent()) >= l1 && TimeHour(TimeCurrent()) < grafico && Trade && ((WPR1<L_Level && WPR2>L_Level)))
   {
    ticket = OrderSend(Symbol(), OP_SELL, volumen, orden_entrada,0,0,0,tradecomment + "S " + valorlote, numero_magico, 0, CLR_NONE);
                       
    if(ticket > 0)
      {
       OrderSelect(ticket,SELECT_BY_TICKET);
       OrderModify(OrderTicket(),OrderOpenPrice(),precio_SL,precio_TP,0,CLR_NONE);
      }
      else
      {
      Print("La orden ha fallado con error #",GetLastError());
      }
   }

Put "if (ticket > 0)" inside within :)

 
onewithzachy:

Here's what WHRoeder meant, Bellagio :

Put "if (ticket > 0)" inside within :)

and you need to do that same thing on all of the if( ticket>0 ) tests of course.

 
thank you all for the moment but the error does not come out, you're great
Reason: