OrderModify error 1

 

Hi guys


I have been working hard to get an EA send Alerts and it seems to work just fine.

Now I am working to make the EA place trades but everytime I try to set an automatic Trailing Stop feature it just doesn´t work.

I receiving these codes: OrderModify error 1

I know it means that OrderModify attempts to replace the values already set with the same values. I have tried to change my code in several ways in order to correct it but I just can´t do it alone.

Your help will be really appreciated

Here is the code

if(Use_TS==true)
   {
    if(OrdersTotal()>0)
      {  
       for(i=1; i<=OrdersTotal(); i++)                     // Cycle searching in orders
          {
           if(OrderSelect(i-1,SELECT_BY_POS)==true)        // Select if the next is available
             {                                            // Analysis of orders:
              int OTicket=OrderTicket();                 // Number of selected order
              string OSymbol=OrderSymbol();              // Symbol of selected order
              int OType=OrderType();                     // Type of selected order
              double OPrice=OrderOpenPrice();            // Price of selected order
              double OSL=OrderStopLoss();                // SL of selected order
              double OTP=OrderTakeProfit();              // TP of selected order         
              if(OSymbol!=Symbol()||OType>1)continue;    // The order is not "ours" continue with next OrderSelect
              double TS=Trail_Stop*Multd*Point;          // Trail Stop x 10
              if(Use_ATR_as_Trail_Stop==true) TS=iATR(NULL,0,Count_bars,0)*ATR_factor;
              while(true)                                 // Modification cycle
                  {
                   //int Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL);    //Min. distance
                   //if (MathAbs(TS-SL)<= Point)                       // If less than allowed
                   //TS=Min_Dist;                             // New value of TS
                   bool Modify=false;                         // start with a Not to be modified instruction
                   switch(OType)                              // Evaluate By order type
                        {
                         case 0 :                             // Order Buy
                         double NewSL=NormalizeDouble(Bid-TS,Digits);                 // calculate new SL
                         if(NewSL-OSL>0.0002)                 // If Order Stop Loss is lower than what we want
                           {
                            string Text=" Buy Trade ";        // Text for Buy 
                            Modify=true;                      // To be modified
                           }
                         break;                               // Exit 'switch'
                         
                         case 1 :                             // Order Sell
                         NewSL=NormalizeDouble(Ask+TS,Digits);                        // calculate new SL
                         if(OSL-NewSL>0.0002)                 // If Order Stop Loss is higher than what we want
                           {
                            Text=" Sell Trade ";              // Text for Sell 
                            Modify=true;                      // To be modified
                           }
                         }                                    // End of 'switch'
                  if(Modify==false) break;                    // If it is not modified exit while            
                  Alert("Modification to",Text,"#",OTicket," - Awaiting response..");
                  bool Ans=OrderModify(OTicket,OPrice,NewSL,OTP,0);    //Modify it!
                  if(Ans==true)                           // Got it! :)
                    {
                     if (Alerts==true) Alert(Text,"#",OTicket," was modified ");
//                     break;                                // From modification cycle.
                    }
                 }                                    // End of modification cycle while (true)
            }                                          // End of select operator
         }                                             // End of for i=1
       }                                                // End of if OrderTotal>0
 

     
        //-------------------------------------------------------------------------------- 7 --
        // Error handling section
        //--------------------------------------------------------------------------------------
         int Error=GetLastError();                           // Failed :(
         switch(Error)                                       // Overcomable errors
            {
               case 130:Alert("Wrong stops. Retrying.");
                  RefreshRates();                         // Update data
                  break;                                  // At the next iteration
               case 136:Alert("No prices. Waiting for a new tick..");
                  while(RefreshRates()==false)            // To the new tick
                     Sleep(1);                            // Cycle delay
                  break;                                  // At the next iteration
               case 146:Alert("Trading subsystem is busy. Retrying ");
                  Sleep(500);                             // Simple solution
                  RefreshRates();                         // Update data
                  break;                                  // At the next iteration
                                                          // Critical errors
               case 2 : Alert("Common error.");
                  break;                                   // Exit 'switch'
               case 5 : Alert("Old version of the client terminal.");
                  break;                                   // Exit 'switch'
               case 64: Alert("Account is blocked.");
                  break;                                   // Exit 'switch'
               case 133:Alert("Trading is prohibited");
                  break;                                   // Exit 'switch'
               default: Alert("Occurred error ",Error);    //Other errors
             }
    }                                                   // End of if Use TS section                                                      
return(0);                                                 
}                                                          // End of start function
 

Hi,

the problem lies in vars OSL/NewSL, I think. You Need to change OSL once you determined the new level. Else you end up in an endless loop. Better leave out OSL, make it just SL, like this:

for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
           {
            if(OrderSelect(i-1,SELECT_BY_POS)==true) // Select if the next is available
              {                                            // Analysis of orders:
               int OTicket=OrderTicket();                 // Number of selected order
            //   string OSymbol=OrderSymbol();              // Symbol of selected order
               int OType=OrderType();                     // Type of selected order
               double OPrice=OrderOpenPrice();            // Price of selected order
               double SL=OrderStopLoss();                // SL of selected order
               double OTP=OrderTakeProfit();              // TP of selected order         
               if(OrderSymbol()!=Symbol()||OType>1)continue;    // The order is not "ours" continue with next OrderSelect
               double TS=10*5*Point;          // Trail Stop x 10
            //   if(Use_ATR_as_Trail_Stop==true) TS=iATR(NULL,0,Count_bars,0)*ATR_factor;
               while(true) // Modification cycle
                 {
                  //int Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL);    //Min. distance
                  //if (MathAbs(TS-SL)<= Point)                       // If less than allowed
                  //TS=Min_Dist;                             // New value of TS
                  bool Modify=false;                         // start with a Not to be modified instruction
                  switch(OType)                              // Evaluate By order type
                    {
                     case 0 :                             // Order Buy
                        double NewSL=NormalizeDouble(Bid-TS,Digits);                 // calculate new SL
                        if(NewSL-SL>0.00020) // If Order Stop Loss is lower than what we want
                          {
                          SL=NewSL;

                           string Text=" Buy Trade ";        // Text for Buy 
                           Modify=true;                      // To be modified
                          }
                        break;                               // Exit 'switch'

                     case 1 :                             // Order Sell
                        NewSL=NormalizeDouble(Ask+TS,Digits);                        // calculate new SL
                        if(SL-NewSL>0.00020) // If Order Stop Loss is higher than what we want
                          {
                          SL=NewSL;
                           Text=" Sell Trade ";              // Text for Sell 
                           Modify=true;                      // To be modified
                          }
                          break;
                    }               
 
  1. Don't hard code numbers. Why bother documenting them.
    case 0 :                             // Order Buy
    :
    case 1 :                             // Order Sell
    
    Just use the correct constants.
    case OP_BUY :
    :
    case OP_SELL :
    

  2. if(OrderSelect(i-1,SELECT_BY_POS)==true) // Select if the next is available
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  3.             if(OrderSelect(i-1,SELECT_BY_POS)==true) // Select if the next is available
    
    Will this EA, never be put on multiple charts, or multiple timeframes, or run in the presence of other EAs or manual trading? Filter order accounting - MQL4 forum

  4. for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
    
    In the presence of multiple orders (one EA or multiple,) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum. Get in the habit of the 1960's paradigm, i=0; i < limit{select(i. Not i=1 i <= limit{ select(i-1).
 
WHRoeder:
  1. Don't hard code numbers. Why bother documenting them.
    Just use the correct constants.

  2. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  3. Will this EA, never be put on multiple charts, or multiple timeframes, or run in the presence of other EAs or manual trading? Filter order accounting - MQL4 forum

  4. In the presence of multiple orders (one EA or multiple,) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum. Get in the habit of the 1960's paradigm, i=0; i < limit{select(i. Not i=1 i <= limit{ select(i-1).

So that is why it "should be used for educational purpose only!" : https://book.mql4.com/trading/ordermodify


Just kidding, of course you're right! ;)

 
PomeGranate:

Hi Guys

I just wanted to say thankyou for your kindly attention to my last post and your willingness to help. I finally reached a point were I was getting mad with this issue so I tried everything that I am capable of with my current programing knowledge and therefore focused on PomeGranate indications and on Dr. Jack the Reaper principles. I started chopping the code into smaller functions and inserted several Alerts in order to get more information on when and why is the OrderModify Error 1 being generated.  I learned curious things, for example on first trade that the EA took I got an Alert stating that it was going to " Modify Ticket #4 " and right after that the Order Modify Error1 showed up and the program crashed. It was clear that with just one trade generated the Ticket # 4 mesage was showing somekind of inconsistency that in my belief was on the For i=1 ; i<=OrdersTotal(); i++ function , the Select function and / or on the While(true) loop.  

After changing the code on different parts to narrow the possibilities I finally reached to the conclusion that the while loop was not consistent so I changed it completely to an If construction, identified my trades with the OrderMagicNumber() function and solved the issue.

I have not slept well for the past two days so I am crashing out right now but finally with a big smile on my face because now I have left my MT4 platform with an optimization process running that is finally generating interesting results.

thanks for your help again.

Roberto