Close order automatically after a given period and/or SL - page 4

 

Please use the SRC button to post code . . . .

 

It maybe that you are trying to close an order twice . .

if (OrderType() == OP_BUY || OP_SELL) 
{
bool Closed = OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), UseSlippage, Red); 
openbuy = 0;
opensell = 0;
}


if (OrderType() == OP_SELL) 
{
Closed = OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), UseSlippage, Red); 
opensell = 0;
openbuy = 0; 
} 
}

You only need one of these . . . and use . . .

if (OrderType() <= OP_SELL) 

that covers OP_SELL and OP_BUY read this: https://docs.mql4.com/constants/trading

 

I changed the code like this:

for(int i = OrdersTotal() - 1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
       {
       if((OrderOpenTime()+3600) < TimeCurrent())
          { 
           if (OrderType() == OP_BUY)          
              {
               bool Closed = OrderClose(OrderTicket() ,OrderLots(), OrderClosePrice(), UseSlippage, Red);           
               openbuy = 0;
              }
           if (OrderType() == OP_SELL)          
              {
               Closed = OrderClose(OrderTicket() ,OrderLots(), OrderClosePrice(), UseSlippage, Red);  
               opensell = 0;
              }              
          }
       }
    }

Now I need to find out how I let the code count opensell and openbuy to 0 when hitting the StopLoss or TakeProfit

 
nirvanamac:

I changed the code like this:

Now I need to find out how I let the code count opensell and openbuy to 0 when hitting the StopLoss or TakeProfit


opensell = 0;
openbuy =0;
for(int i = OrdersTotal() - 1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))   //==> select also on magicnumber and symbol
       {
       if(OrderMagicnumber()== magicnumber && OrderSymbol()== Symbol() && OrderType() == OP_BUY){openbuy++;}
       if(OrderMagicnumber()== magicnumber && OrderSymbol()== Symbol() && OrderType() == OP_SELL){opensell++;}
       if((OrderOpenTime()+3600) < TimeCurrent() && OrderMagicnumber()== magicnumber && OrderSymbol()== Symbol())
          { 
           if (OrderType() == OP_BUY)          
              {
               bool Closed = OrderClose(OrderTicket() ,OrderLots(), OrderClosePrice(), UseSlippage, Red);           
               openbuy = openbuy-1;    //openbuy--;
              }
           if (OrderType() == OP_SELL)          
              {
               Closed = OrderClose(OrderTicket() ,OrderLots(), OrderClosePrice(), UseSlippage, Red);  
               opensell = opensell-1;    //opensell--;
              }              
          }
       }
    }

Select directly on magicnumber and symbol this code will then be written in a better way

If you use it the way you had made it then you will close all open trades longer running then a hour (also not from this one)

 
deVries:

Select directly on magicnumber and symbol this code will then be written in a better way

If you use it the way you had made it then you will close all open trades longer running then a hour (also not from this one)


Hi deVries...I've tried it and will check it now with TP and SL...thanks a lot...
 

Wow now it looks good :) not the performance but the EA with SL and TP