Order does not close

 

Hi, it's me again. 

 

I searched through the forum but did not find anything similiar.

 For some reason, some of my Orders do not close even though the conditions are met. In this case, it affects long orders and the SL does not trigger even when the price is far below the SL level. Allowed slippage was on 10, I changed it to 50, but this did not help (by the way, does 10 mean 10 or 1 pip with a 5-digit broker?).

 The code is not complete, but it should fit the requirements of understanding concerning the content. However I know, this code is not a masterpiece. I am not a programmer, I am currently learning it. So sorry for that.

 

//general extern parameters

extern double SLPips=15,TPPips=15,Handelsvolumen=0.1;
extern int MagicNumber=99994;
extern int LongOrder,ShortOrder;
extern double Spread=1.2;
extern bool LongErlauben,ShortErlauben;
//allgemeine Variable zur Bestimmung der Orderspezifika
double ShortStoppLoss=0;
bool ShortOrderAngepasst;
double ShortTakeProfit=0;
bool ShortOrderAngepasst2;
double LongStoppLoss=0;
bool LongOrderAngepasst;
double LongTakeProfit=0;
bool LongOrderAngepasst2;
double ShortOrder_N,LongOrder_N;

//... further variables


//functions
//buying an Order when close price is below Bollinger on M5 and close price is above moving average on D1

BBupper_5 = iBands(NULL,5,Periode,Standarddev,0,PRICE_CLOSE,MODE_UPPER,1);
BBlower_5 = iBands(NULL,5,Periode,Standarddev,0,PRICE_CLOSE,MODE_LOWER,1);
SMA_D1 = iMA(NULL,1440,AVG_Period,0,MODE_SMA,PRICE_CLOSE,1);

if(Close[1]  < BBlower_5 && iClose(NULL,1440,1) > SMA_D1 && NeuePeriodeBegonnen == true)
{
   LongSignal = true;
}
else LongSignal = false;


//+------------------------------------------------------------------+
//| Martingale für Long                                              |
//+------------------------------------------------------------------+

// allows more than one trade but only if it is cheaper than the first one opened with an option to use martingale strategies.

if(verbilligen == true && Close[1] < ReferenceLevel)
{
               LongMartingaleErlauben = true;  
}
else LongMartingaleErlauben = false;




if(LongOrder > 0)
{
               LongMartingaleOrder = true;
}
else LongMartingaleOrder = false;

//opens further trades, optionally with martingale.

if(LongSignal==true && LongErlauben == true && TimeCurrent() >= TradingStartTime && TimeCurrent() < TradingStopTime && LongMartingaleOrder == true && LongMartingaleErlauben == true)
{
    //says when martingale stops  

   if(MartingaleLevel <= MaxMartingaleSteps*MartingaleMultiplier)
   {
               MartingaleLevel = MartingaleMultiplier*MartingaleLevel;
   }
   else MartingaleLevel = 1;
                  
               LongOrder_N = OrderSend(Symbol(),OP_BUY,Handelsvolumen*MartingaleLevel,Ask,10,0,0,"BB_Martingale",MagicNumber,0,Green);
               ReferenceLevel=Ask;

              
              
               if(OrderSelect(LongOrder_N,SELECT_BY_TICKET)==true)
               {
                  if(OrderCloseTime()==0 && OrderStopLoss()==0)
                  {
                      
                      
                              
                              bool LongOrderAngepasst_N=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(SLPips/10000)-(Spread/10000),OrderTakeProfit(),0,Yellow);
                                   LongOrderAngepasst_N=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(TPPips/10000)-(Spread/10000),0,Orange);
                              
                              
                  }
               }              
              
              
}



//+------------------------------------------------------------------+
//| Long Trades                                                      |
//+------------------------------------------------------------------+

// opens the first order


if(LongSignal==true && LongErlauben==true && TimeCurrent() >= TradingStartTime && TimeCurrent() < TradingStopTime)
{

   while(LongOrder<=0)
   {

  
  
               LongOrder=OrderSend(Symbol(),OP_BUY,Handelsvolumen,Ask,10,0,0,"HighBreak",MagicNumber,0,Green);
               ReferenceLevel=Ask;
   }              

}


//set Sl for long
if(OrderSelect(LongOrder,SELECT_BY_TICKET)==true)
{
   if(OrderCloseTime()==0 && OrderStopLoss()==0)
   {


                LongStoppLoss=NormalizeDouble(OrderOpenPrice()-(SLPips/10000)-Spread/10000,Digits);
               LongOrderAngepasst=OrderModify(OrderTicket(),OrderOpenPrice(),LongStoppLoss,OrderTakeProfit(),0,Yellow);
              
              
   }
}

//set TP for long
if(OrderSelect(LongOrder,SELECT_BY_TICKET)==true)
{
   if(OrderCloseTime()==0 && OrderTakeProfit()==0)
   {
  
  
               LongTakeProfit=NormalizeDouble(OrderOpenPrice()+(TPPips/10000)-Spread/10000,Digits);
             LongOrderAngepasst2=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),LongTakeProfit,0,Orange);
              
              
   }
}




//garantiert, dass nur eine Position (pro Paar) gleichzeitig geöffnet ist
if(OrderSelect(LongOrder,SELECT_BY_TICKET)==true)
{
   if(OrderTicket()>0 && OrderCloseTime()>0)
   {
  
              
               LongOrder=0;
               MartingaleLevel=1;
              
              
   }
}        
 
I know a broker (Cityforex) where on Demo-Account for GBPUSD not stop (neither takeprofit not stoploss) is executed by the server. So their server isn't set correctly and they didn't correct it even if I have complained about it. :(
 

My broker is Axiory.eu. I use external price data on an offline chart in the strategy tester. Interestingly, many orders' SL/TP are triggered. Some are not. I did not account for every order. I just realised, some of the are closed by the end of the tester. Also, it happens only in this one EA, where I allow a really wide SL of 250 pips and so the order remains open for a couple of days. Normally, in other EAs I allow like 50 pips and most of the orders are closed by the end of the day.

However, as I get your intention right, you think my code is not the reason? 

 
Chris.h:

Hi, it's me again. 

 

I searched through the forum but did not find anything similiar.

 For some reason, some of my Orders do not close even though the conditions are met. In this case, it affects long orders and the SL does not trigger even when the price is far below the SL level. Allowed slippage was on 10, I changed it to 50, but this did not help (by the way, does 10 mean 10 or 1 pip with a 5-digit broker?).

 The code is not complete, but it should fit the requirements of understanding concerning the content. However I know, this code is not a masterpiece. I am not a programmer, I am currently learning it. So sorry for that.

  • Slippage is in points not pips. Please read the documentation.
  • Your stoploss / takprofit are correctly set ? Is there any message in Journal log ?
  • Can you show a screenshot of an order not close while SL/TP is reached ?
 
bool LongOrderAngepasst_N=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-(SLPips/10000)-(Spread/10000), OrderTakeProfit(), 0, Yellow);
  1. Hard coded for a 5 digit broker and non-JPY pair.
  2. Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Reason: