OrderClose functions don't get triggered

 

hi

i've put comments all over the ea just for easier and quicker understanding. so the problem is that the "control opened positions" part is not working at all. i don't get an error message or anything, thats why i'm posting to this forum. as you can see there is only a buy function in this ea as of yet and it gets triggered correctly, but the order management is not...

the ea itself is as simple as it gets... it buys when price goes over MA_High and closes if TP or SL is hit or when price goes under MA_Low. plus there is a break even function (if order is in X plus pips, the SL is moved to break even). thats all

because there are no errors while running the ea i would assume everything is working as it should be, so i hay have a "return(0)" or something like that wrong...

hope someone can help, thanks!

//---------------------------------------------------------------
// EA
//---------------------------------------------------------------

extern double Lots = 0.1;           //Lot size
extern double TP = 1000;            //Take Profit
extern double SL = 200;             //StopLoss
extern double Next = 200;           //Pips to move SL to break even
extern double Slippage = 3;         //Maxmimum allowed slippage

//---------------------------------------------------------------
// EXPERT START

int start()
{
 
   int cnt, total;
   
   int ticket_1;
  
   if(Bars<100)
   {
      Print("Bars less than 100");
      return(0);  
   }

//----
   
   double MA_High = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_HIGH, 0);
   double MA_Low = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_LOW, 0);
   
   double High_curr = iHigh(NULL,0,0);
      
//----

   //---------------------------------------------------------------
   // OPEN POSITIONS BEGIN
    
   total=OrdersTotal();
   if(total<1) 
   {
     
      //---------------------------------------------------------------
      // CHECK FOR FREE MARGIN BEGIN
      
      if(AccountFreeMargin()<(1000*Lots))
      {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
      }
      
      // CHECK FOR FREE MARGIN END
      //---------------------------------------------------------------

      //---------------------------------------------------------------
      // BUY BEGIN
      
      if(MA_High < Bid && High_curr == Ask)
      {
         ticket_1=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"BUY",1010101,0,Green);
      }
      
      // BUY END
      //---------------------------------------------------------------

      //---------------------------------------------------------------
      // SELL BEGIN
      
      // SELL END
      //---------------------------------------------------------------
   
   return(0);
   }
   
   // OPEN POSITIONS END
   //---------------------------------------------------------------
   
   //---------------------------------------------------------------
   // CONTROL OPENED POSITIONS BEGIN
   
   if(total==1)
   { 
   if(OrderSelect(ticket_1,SELECT_BY_TICKET)==True)
      {
         if(OrderOpenPrice() + Next*Point < Ask)
         {
            OrderModify(ticket_1,OrderOpenPrice(),OrderOpenPrice(),0,0,Green); //MOVE STOPLOSS TO BREAK EVEN
         }
         else if(OrderOpenPrice() - SL*Point > Bid) //STOP LOSS IS HIT
         {
            OrderClose(ticket_1,Lots,Bid,Slippage,Red);
         }
         else if(OrderOpenPrice() + TP*Point < Ask) //TAKE PROFIT IS HIT
         {
            OrderClose(ticket_1,Lots,Bid,Slippage,Red);
         }
         else if(MA_Low>Ask) //CLOSE FOR HITTING MA LOW
         {
            OrderClose(ticket_1,Lots,Bid,Slippage,Red);
         }
      }         
   return(0);
   }
   
   // CONTROL OPENED POSITIONS END
   //---------------------------------------------------------------

return(0);
}
  
// EXPERT END
//---------------------------------------------------------------
 
crashtestdummy:

i've put comments all over the ea just for easier and quicker understanding. so the problem is that the "control opened positions" part is not working at all. i don't get an error message or anything, thats why i'm posting to this forum. as you can see there is only a buy function in this ea as of yet and it gets triggered correctly, but the order management is not...

It looks as though the problem is that ticket_1 is declared as a local, non-static variable within start(). Therefore, on each call to start() its value is reset to zero. As a result, OrderSelect(ticket_1,SELECT_BY_TICKET) can only succeed immediately after the order has been placed, as part of the processing of the same tick. After that the OrderSelect() never succeeds because ticket_1 is zero, and the subsequent checks are never called.

There may be other problems as well, e.g. the assumption that there will only ever be a single order open on the entire account, not just a single order from your EA.
 

thanks for the tip! i made this change

static int ticket_1;

and now the SL is moved to break even just like it needs to and positions are closed at SL, but the TP is never triggered and trades are not closed when price goeas under lower MA.

plus now i have a bunch of this: "OrderModify error 1"

kida strange, because the ordermodify seems to work properly...

btw i'm testing on eurusd 1h

 
crashtestdummy:

and now the SL is moved to break even just like it needs to and positions are closed at SL, but the TP is never triggered and trades are not closed when price goeas under lower MA.

plus now i have a bunch of this: "OrderModify error 1"

What's happening now is that your b/e condition is being repeatedly met on every tick, with two effects. Firstly, the code keeps trying to do an OrderModify() using what is already the trade's s/l and t/p, which leads to an error. Secondly, the t/p closure condition is never called, because it is part of an if-else-if block, and the earlier if condition for the b/e is always met if the t/p condition is met. The conditions for the b/e must be true if the conditions for the t/p are true. Therefore, the if-block exits without ever getting to the t/p condition.

Going back to the earlier problem, declaring ticket_1 as static will work, but only up to a point. What you have now is an EA which will develop amnesia if MT4 has to be restarted. After a restart it will no longer track any open position.

Reason: