order modify error 1?

 

I am getting order modify error 1 in this code - any ideas why?

MovetoBreakEven is set to 300 (30pips):


      if (MovetoBreakEven>0)
            {
         int totaltoBE = OrdersTotal();
         for(int itoBE=totaltoBE-1;itoBE>=0;itoBE--)
         {
            if( OrderSelect(itoBE,SELECT_BY_POS) && OrderCloseTime()==0 )
            {
               if( ( OrderType()==OP_BUY ) && 
                  (OrderMagicNumber() == EA_ID) &&
                  ((Bid-OrderOpenPrice()) > Point*MovetoBreakEven) &&
                  (movedflag==0)
               )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
                  movedflag=1;
               }
               else if( ( OrderType()==OP_SELL ) && 
                        (OrderMagicNumber() == EA_ID) &&
                        ((OrderOpenPrice()-Ask) > Point*MovetoBreakEven) &&
                        (movedflag==0)
                     )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
                  movedflag=1;
               } 
               
            }
      
         }
 
SanMiguel:

I am getting order modify error 1 in this code - any ideas why?

MovetoBreakEven is set to 300 (30pips):


Place your movedflag variable outside the start() function. Make it a global variable to make sure it doesn't change on new ticks.

 
robofx.org:

Place your movedflag variable outside the start() function. Make it a global variable to make sure it doesn't change on new ticks.

I have this in a separate bit of code still inside the start function so should be ok:

Still shouldn't affect the modify error or are you saying it is re-entering the code?


if (is_timetodelete == true)
{     
      if (debug==true) {Print("In deletion code");}
      int total = OrdersTotal();
      for(int i=total-1;i>=0;i--) //if no orders then it won't run through this again
      {
         OrderSelect(i, SELECT_BY_POS);
         if ( OrderMagicNumber() == EA_ID ) 
         {
            int type   = OrderType();
            bool result = false;
            if(type>1)result = OrderDelete( OrderTicket() );
    
            if(result == false)
            {
               Alert("Order " , OrderTicket() , " Error:" , GetLastError() );
               Sleep(3000);
            }  
         }
      }
      int movedflag=0;
}
 
SanMiguel wrote >>

I have this in a separate bit of code still inside the start function so should be ok:

Still shouldn't affect the modify error or are you saying it is re-entering the code?


Yes, that's what I mean, it could be re-enetering and initialized to 0 on new ticks. Put int movedflag=0; outside the start() and try again.

 
robofx.org:
SanMiguel wrote >>

I have this in a separate bit of code still inside the start function so should be ok:

Still shouldn't affect the modify error or are you saying it is re-entering the code?


Yes, that's what I mean, it could be re-enetering and initialized to 0 on new ticks. Put int movedflag=0; outside the start() and try again.

It only enters that bit of code if is_timetodelete is true and that only happens once a day.

If I move movedflag=0 to outside then it is never reset. It has to be reset once a day.

 
SanMiguel:

It only enters that bit of code if is_timetodelete is true and that only happens once a day.

If I move movedflag=0 to outside then it is never reset. It has to be reset once a day.

I'm not sure this is executed once a day. Where do you set is_timetodelete = false ?

 
robofx.org:

I'm not sure this is executed once a day. Where do you set is_timetodelete = false ?

   //does not work on any timeframe other than H1
   if (Period() != PERIOD_H1) {
      if (debug==true) {Print("The chart is not on H1, exiting code.");} 
      return;
   }
   
   // does not work on holidays.
   if(DayOfWeek()==0 || DayOfWeek()==6) return(0);

   //heartbeat code
   if(Hour()>=0 && Minute()==0 && heartbeat == 0) {Print("Time is "+Hour()+" - EA still operating");heartbeat=1;}
   if (Minute()>=1 && heartbeat ==1) {heartbeat=0;}

   //Buffers & lotsizes
   double BufferShort = 0.0006;
   double BufferLong = 0.0009; //takes into account the spread should be more than 6
   double Lotsize = GlobalVariableGet("GBPUSD_LotSize");//1;
      
   //Trading variables
   double TradeTargetPrice = 45;
   double TradeStopLoss = 22;
   int    TrailingStop = 0;
   double MovetoBreakEven = 0;
   bool checktrending = false;
   int checktrending_val = 35; 
   bool usingATR = false;
   double R1 = GlobalVariableGet("GBPUSD_R1"); //nearest resistance 0.0000 means this is turned off otherwise the value will be checked in code.
   double R2 = GlobalVariableGet("GBPUSD_R2");
   double S1 = GlobalVariableGet("GBPUSD_S1");
   double S2 = GlobalVariableGet("GBPUSD_S2");
   
   //Daily ATR calculation
   int currATR = MathRound(iATR(NULL,PERIOD_H1,14,0) * 10000);
   if (usingATR==true)
   {
         TradeTargetPrice = currATR+15;
         TradeStopLoss = currATR-5;
   }
   
   /////////////////////////////////////
   //Alpari hours because they use CET
   /////////////////////////////////////
   double StartHour = 21; //this is end of NYSE
   double EndHour = 7; //this is start of LSE
   if (DayOfYear()>=88) {StartHour=StartHour+1;EndHour=EndHour+1;} //IE monitoring 1900 to 0600, CET is always 1hr ahead apart from in summer when it's +2
   /////////////////////////////////////
  
   //Comments
   double diffh = R1-iHigh(Symbol(),PERIOD_H1,0);
   double diffl = iLow(Symbol(),PERIOD_H1,0)-S1;
   Comment("StartHour="+DoubleToStr(StartHour,2)+ "     EndHour="+DoubleToStr(EndHour,2)+
           "\nR1="+DoubleToStr(R1,5)+ 
           "\nS1="+DoubleToStr(S1,5)+
           "\nLot size="+DoubleToStr(Lotsize,2)
          );
   
   double diff = (EndHour-0)+(24-StartHour)+1;
   
   //5 decimal systems like Alpari
   if (Point==0.00001)
   {
      TradeTargetPrice=TradeTargetPrice*10;
      TradeStopLoss=TradeStopLoss*10;
      TrailingStop=TrailingStop*10;
      MovetoBreakEven=MovetoBreakEven*10;
   }

//////////////////////////////////////////////////////////
//It is now the start of the overnight monitoring, delete any existing pending orders
//////////////////////////////////////////////////////////
bool is_timetodelete=false;
//if(Hour()==StartHour && Minute()==0 && Seconds()==0) {is_timetodelete = true; }
if(Hour()==StartHour && Minute()==0) {is_timetodelete = true; }

if (is_timetodelete == true)
{     
      if (debug==true) {Print("In deletion code");}
      int total = OrdersTotal();
      for(int i=total-1;i>=0;i--) //if no orders then it won't run through this again
      {
         OrderSelect(i, SELECT_BY_POS);
         if ( OrderMagicNumber() == EA_ID ) 
         {
            int type   = OrderType();
            bool result = false;
            if(type>1)result = OrderDelete( OrderTicket() );
    
            if(result == false)
            {
               Alert("Order " , OrderTicket() , " Error:" , GetLastError() );
               Sleep(3000);
            }  
         }
      }
      int movedflag=0;
}
 
if(Hour()==StartHour && Minute()==0) {is_timetodelete = true; }

You have one minute which is enough time to set is_timetodelete = true with every new tick withing that minute. Then your movedflag becomes 0 and your modify is executed again.


 
SanMiguel:


But the ordermodify only happens when a BUY OR SELL order exists.

The above code deletes existing orders.

At the time of creation they will be BUY STOPS AND OR SELL STOPS.
 

+ their are checks in the code for existing orders in both those statements.

Strange.

I guess I could print out the order modify statement.

movedflag remains as 1 after it has been modified once and only on reaching 2200 does it enter the is_timetodelete section.

 

I changed to this...seems to work better:

if (MovetoBreakEven>0)
            {
         int totaltoBE = OrdersTotal();
         for(int itoBE=totaltoBE-1;itoBE>=0;itoBE--)
         {
            if( OrderSelect(itoBE,SELECT_BY_POS) && OrderCloseTime()==0 )
            {
               if( ( OrderType()==OP_BUY ) && 
                  (OrderMagicNumber() == EA_ID) &&
                  ((Bid-OrderOpenPrice()) > Point*MovetoBreakEven) &&
                  (OrderStopLoss()!=OrderOpenPrice())
                  //(movedflag==0)
               )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
                  //movedflag=1;
               }
               else if( ( OrderType()==OP_SELL ) && 
                        (OrderMagicNumber() == EA_ID) &&
                        ((OrderOpenPrice()-Ask) > Point*MovetoBreakEven) &&
                        (OrderStopLoss()!=OrderOpenPrice())
                        //(movedflag==0)
                     )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
                  //movedflag=1;
               } 
               
            }
      
         }
      }
Reason: