Errror 1. Please help.

 

Hello,

I made an EA and sometimes when testing it in strategy tester it puts Error 1. It is supposed to move the pending order on next day low if it is sellstop(buystop) and next candle is bullish( bearish). If next candle is not bullish(bearish) then it stays where it is. Tradinglogic wise i think it is fine but code is prolly not flawless.
Any suggestions how to get rid of the error 1?
(there may be some missing if ending "}" because i substracted from the code error handler which im sure is not causing this error)

void ModifyOrder()
{

for(int cnt=0; cnt < OrdersTotal(); cnt++)
{
 //----
 if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
  {
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magicnumber)
    {
     if(OrderType()==OP_SELLSTOP)
       {
        if(OrderOpenTime()<iTime(Symbol(),EntrytimeframePeriode,0))
        {
            if(iOpen(Symbol(),EntrytimeframePeriode,1) > iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bullish move it
               {
                 if(OrderOpenPrice() != iLow(Symbol(),EntrytimeframePeriode,1)){
                 nsl=NormalizeDouble(high,Digits);
                 ans = OrderModify(OrderTicket(),NormalizeDouble(iLow(Symbol(),EntrytimeframePeriode,1),Digits),NormalizeDouble(iHigh(Symbol(),EntrytimeframePeriode,1),Digits),0 ,OrderExpiration(),clrBlue);
                 if (ans==true)                      // Got it! :)
                    {
                     Alert ("Sellstop #",OrderTicket()," is modified.");
                     break;                           // From modification cycle.
                    }
       }
             }
               }
if(OrderType()==OP_BUYSTOP)
       {
        if(OrderOpenTime()<iTime(Symbol(),EntrytimeframePeriode,0))
        {
            if(iOpen(Symbol(),EntrytimeframePeriode,1) < iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bearish move it
               {
                  nsl=NormalizeDouble(low,Digits);
                 ans = OrderModify(OrderTicket(),NormalizeDouble(iHigh(Symbol(),EntrytimeframePeriode,1),Digits),NormalizeDouble(iLow(Symbol(),EntrytimeframePeriode,1),Digits), 0, OrderExpiration(),clrBlue);
                 if (ans==true)                      // Got it! :)
              {
               Alert ("Buystop #",OrderTicket()," openprice is modified");
               break;                           // From modification cycle.
              }
              }}}
 
salda: Any suggestions how to get rid of the error 1?
  1. Your posted code doesn't call GetLastError/_LastError So no idea where you think you get error 1
  2. Don't call them unless you get an error What are Function return values ? How do I use them ? - MQL4 forum
  3. 1

    ERR_NO_RESULT

    No error returned, but the result is unknown

    128

    ERR_TRADE_TIMEOUT

    Trade timeout

    means communications problems between you and the broker's server. Nothing you can do in code to fix that. Log it, return and wait for the next tick, retest the conditions
 
I mentioned that i substracted error handler to have the code shorter. This is my nonshorten function:

void ModifyOrder()
{

for(int cnt=0; cnt < OrdersTotal(); cnt++)
{
 //----
 if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
  {
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magicnumber)
    {
     if(OrderType()==OP_SELLSTOP)
       {
        if(OrderOpenTime()<iTime(Symbol(),EntrytimeframePeriode,0))
        {
            if(iOpen(Symbol(),EntrytimeframePeriode,1) > iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bullish move it
               {
                 if(OrderOpenPrice() != iLow(Symbol(),EntrytimeframePeriode,1)){
                 nsl=NormalizeDouble(high,Digits);
                 ans = OrderModify(OrderTicket(),NormalizeDouble(iLow(Symbol(),EntrytimeframePeriode,1),Digits),NormalizeDouble(iHigh(Symbol(),EntrytimeframePeriode,1),Digits),0 ,OrderExpiration(),clrBlue);
                 if (ans==true)                      // Got it! :)
                    {
                     Alert ("Sellstop #",OrderTicket()," is modified.");
                     break;                           // From modification cycle.
                    }
            //------------------------------------------------------------------- 7 --
            int Error=GetLastError();           // Failed :(
            switch(Error)                       // Overcomable errors
              {
              case 1: Alert("Error #1. Sellorder #"+OrderTicket());
                  Sleep(500);                   // Simple solution
                  RefreshRates();               // Update data
                  continue; 
               case 130:Alert("Wrong stops. Retrying."+OrderType()+" #"+OrderTicket());
                  RefreshRates();               // Update data
                  continue;                     // 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
                  continue;                     // At the next iteration
               case 146:Alert("Trading subsystem is busy. Retrying ");
                  Sleep(500);                   // Simple solution
                  RefreshRates();               // Update data
                  continue;                     // 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
              }
            break;                              // From modification cycle
           }}
                 
               }
        }
         
       }
        if(OrderType()==OP_BUYSTOP)
          {
           if(OrderOpenTime()<iTime(Symbol(),EntrytimeframePeriode,0))
           {
               if(iOpen(Symbol(),EntrytimeframePeriode,1) < iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bearish move it
                  {
                     nsl=NormalizeDouble(low,Digits);
                    ans = OrderModify(OrderTicket(),NormalizeDouble(iHigh(Symbol(),EntrytimeframePeriode,1),Digits),NormalizeDouble(iLow(Symbol(),EntrytimeframePeriode,1),Digits), 0, OrderExpiration(),clrBlue);
                    if (ans==true)                      // Got it! :)
                 {
                  Alert ("Buystop #",OrderTicket()," openprice is modified");
                  break;                           // From modification cycle.
                 }
            //------------------------------------------------------------------- 7 --
            int error=GetLastError();           // Failed :(
            switch(error)                       // Overcomable errors
              {
                  case 1: Alert("Error #1. Buyorder #"+OrderTicket());
                  Sleep(500);                   // Simple solution
                  RefreshRates();               // Update data
                  continue; 
               case 130:Alert("Wrong stops. Retrying."+OrderType()+" #"+OrderTicket());
                  RefreshRates();               // Update data
                  continue;                     // 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
                  continue;                     // At the next iteration
               case 146:Alert("Trading subsystem is busy. Retrying ");
                  Sleep(500);                   // Simple solution
                  RefreshRates();               // Update data
                  continue;                     // 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
              }
            break;                          // From modification cycle
           }       
            }   
        }
         
       }
    }
  
 //----

return;
//----
}
 
salda:
I mentioned that i substracted error handler to have the code shorter. This is my nonshorten function:


Hi salda,

At a first glance, there are a couple of things you need to change in your code to make it compile without warnings and errors .

Second, if you want some sort of error description, you can include the :

#include <stdlib.mqh>

and then use the ErrorDescription() like :

Print("OrderSelect() failed with error : "+ErrorDescription(GetLastError()));

Try it, I don't think that the custom switch function provides significant better descriptions. Up to you.

So, back to the code, you want

" It is supposed to move the pending order on next day low if it is sellstop (buystop) and next candle is bullish( bearish). If next candle is not bullish(bearish) then it stays where it is."

The problem for SellStop orders is :

if(iOpen(Symbol(),EntrytimeframePeriode,1)>iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bullish move it
// if open of the previous > close of the previous is not a bullish candle , it's bearish
// you need Open < Close  or Close > Open

Same for BuyStops :

if(iOpen(Symbol(),EntrytimeframePeriode,1)<iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bearish move it
// open > close or close< open => bearish

So here it is the way I think it should work :

//---
   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      //----
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         Print("OrderSelect() failed with error : "+ErrorDescription(GetLastError()));
        }
      else
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magicnumber)
           {
            if(OrderType()==OP_SELLSTOP)
              {
               if(OrderOpenTime()<iTime(Symbol(),EntrytimeframePeriode,0))
                 {
                  if(iOpen(Symbol(),EntrytimeframePeriode,1)<iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bullish move it
                    {
                     if(OrderOpenPrice()>iLow(Symbol(),EntrytimeframePeriode,1))
                       {
                        nsl=NormalizeDouble(high,Digits);// not sure what this is ?
                        bool ans=OrderModify(OrderTicket(),NormalizeDouble(iLow(Symbol(),EntrytimeframePeriode,1),Digits),NormalizeDouble(iHigh(Symbol(),
                                             EntrytimeframePeriode,1),Digits),0,OrderExpiration(),clrBlue);
                        if(ans==true) // Got it! :)
                          {
                           Alert("Sellstop #",OrderTicket()," is modified.");
                           break;                           // From modification cycle.
                          }
                        else
                          {
                           Print("OrderModify() failed with error : "+ErrorDescription(GetLastError()));
                           break;
                          }
                       }
                    }

                 }
              }

           }
         if(OrderType()==OP_BUYSTOP)
           {
            if(OrderOpenTime()<iTime(Symbol(),EntrytimeframePeriode,0))
              {
               if(iOpen(Symbol(),EntrytimeframePeriode,1)>iClose(Symbol(),EntrytimeframePeriode,1))//if candle je bearish move it
                 {
                  nsl=NormalizeDouble(low,Digits);
                  bool ans=OrderModify(OrderTicket(),NormalizeDouble(iHigh(Symbol(),EntrytimeframePeriode,1),Digits),NormalizeDouble(iLow(Symbol(),EntrytimeframePeriode,1),Digits),0,OrderExpiration(),clrBlue);
                  if(ans==true) // Got it! :)
                    {
                     Alert("Buystop #",OrderTicket()," openprice is modified");
                     break;                           // From modification cycle.
                    }
                  else
                    {
                     Print("OrderModify() failed with error : "+ErrorDescription(GetLastError()));
                     break;
                    }
                 }
              }
           }

        }
     }

//----

Now, if you want to use a loop to try more than one time to modify it, here is an example :

                  while(int cnt<=30 && ans==false)
                    {
                     RefreshRates();
                     if(IsTradeAllowed()==false) Print("Trade is not allowed or context is busy. Please wait");
                     else      // send request to modify order
                       {
                        double open = NormalizeDouble(iLow(Symbol(),EntrytimeframePeriode,1),Digits);
                        double stop = NormalizeDouble(iHigh(Symbol(),EntrytimeframePeriode,1),Digits);
                        ans = OrderModify(OrderTicket(),open,stop,0 ,OrderExpiration(),clrBlue);
                        cnt++;
                       }
                     //---
                     if(!ans)
                       {
                        Print("Count "+(string)cnt+" ,Order modify failed  with error : "+ErrorDescription(GetLastError()));
                        Sleep(100);
                       }
                     else
                       {
                       Print("Order with ticket "+(string)OrderTicket()+" was modified ");
                       }
                    }//end while

What the loop does is to try multiple times to modify the order and take a nap in between. Just optional

Also optional, you may want to think about weekends as well.

Try this and if you need more help with it, ask.

I hope it helps

Cheers

PS. If you don't like the loop to try multiple times to modify it, you should at least normalize the open price and stop loss before you send the order.

See while loop example.