doClose : Fallo al hacer un OrderClose nºticket

 

Hola, es mi primer EA , bueno llego desde programación algo mas antigua como BASIC y TurboPascal (algo de c++) entonces me ayudó un colega y de momento quiero resolver esto si puedo con la vuestra ayuda.

El EA en si lo uso para hacer SpreadTrading , osea si abro una compra abro una venta en otro instrumento, por lo que el EA se encarga de cerrar ambos trades al mismo momento si se verifican 2 condiciones

1 spread>TP (si puesto) entonces cierra ambas ordenes

2 spread<SL (si puesto) "     "     "    "      "         "

 

Ayer lo probé y hoy cuando lo spread llegó a ser > TP (unos 30€, cuenta en EURO)  me salió el mensaje que decía de haber cerrada ambas ordenes (lo que hace el EA) pero de enseguida seguido por mensajes del tipo doClose : fallo al hacer un OrderClose

Agrego Captura que hice mas tarde asi que no hacer caso al mensaje de las 12:11:49 porque cerré manualmente.

error doClose 

 

y aqui abajo dejo el código por si conseguís encontrar donde esta el problema:

 

 

//+------------------------------------------------------------------+
//|                                               SimoSpreadStop.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- input parameters
input int      ticketid1; 
input int      ticketid2;
input double   stopLoss;
input double   takeProfit;

bool globalEnable = true;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
        int res;
        
        
        res = checkParam(ticketid1);
        if (res != INIT_SUCCEEDED)
        {
                return(res);
        }
        
        res = checkParam(ticketid2);
        if (res != INIT_SUCCEEDED)
        {
                return(res);
        }
        
        if (stopLoss < 0)
        {
                Alert("Stop Loss debe ser un numero positivo");
                return(INIT_PARAMETERS_INCORRECT);
        }
        
        if (takeProfit < 0)
        {
                Alert("Stop Loss debe ser un numero positivo");
                return(INIT_PARAMETERS_INCORRECT);
        }
        
        if (stopLoss == 0 && takeProfit == 0)
        {
                Alert("Debe establecer al menos el Stop Loss o el Take Profit");
                return(INIT_PARAMETERS_INCORRECT);
        }       

   return(INIT_SUCCEEDED);
}

int checkParam(int ticket)
{
        if (OrderSelect(ticket, SELECT_BY_TICKET))
        {
                if (OrderCloseTime() > 0)
                {
                        Alert("Ticket id = ", IntegerToString(ticket), " esta cerrado. Seleccione un ticket valido.");
                        return(INIT_PARAMETERS_INCORRECT);
                }
        }
        else
        {
                Alert("Ticket id = ", IntegerToString(ticket), " desconocido. Seleccione un ticket valido.");
                return(INIT_PARAMETERS_INCORRECT);
        }
        
        return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
        double spreadResult;    
        
        if (!globalEnable)
                return;
        
        spreadResult = 0;
        
        if (OrderSelect(ticketid1, SELECT_BY_TICKET))
        {
                spreadResult = spreadResult + OrderProfit();
        }
        else
        {
                Alert("Error selecting Order ", IntegerToString(ticketid1));
        }
        
        if (OrderSelect(ticketid2, SELECT_BY_TICKET))
        {
                spreadResult = spreadResult + OrderProfit();
        }
        else
        {
                Alert("Error selecting Order ", IntegerToString(ticketid2));
        }
        
        if (stopLoss > 0)
        {
                if (stopLoss + spreadResult < 0)
                {
                        doClose(ticketid1);
                        doClose(ticketid2);
                        globalEnable = false; 
                        Alert("Cerradas ordenes por Stop Loss");
                }               
        }
        
        if (takeProfit > 0)
        {
                if (takeProfit - spreadResult < 0)
                {
                        doClose(ticketid1);
                        doClose(ticketid2);
                        globalEnable = false; 
                        Alert("Cerradas ordenes por Take Profit");
                }               
        }
   
}

void doClose(int ticket)
{
        double price;
        
        if (OrderSelect(ticket, SELECT_BY_TICKET))
        {
                if (OrderType() == OP_BUY)
                {
                        price = Ask;
                }
                else if (OrderType() == OP_SELL)
                {
                        price = Bid;
                }
                else
                {
                        return;
                }
                
                if (!OrderClose(ticket, OrderLots(), price, 4))
                {
                        Alert(__FUNCTION__,": Fallo al hacer un OrderClose ", IntegerToString(ticket));
                }
        }
        else
        {
                Alert(__FUNCTION__,": Error selecting Order ", IntegerToString(ticket));
        }
}

//+------------------------------------------------------------------+
 

De momento creo que el fallo es debido que no doy tiempo entre un doClose (OrderClose) y el otro asi que les he agregado despues un doClose y el otro la siguiente orden;

 

 Sleep(1500);

 RefreshRates(); 

 

de esta manera espera 1,5 sec. antes de cerrar la otra orden,

voy añadendo que si una orden esta cerrada deba cerrar la otra.