How to avoid error 1 in order modify - page 2

 

I change the order condition

from 

if(OrderStopLoss() < SupertrendUpTrend() && SupertrendDirection() == 1.0000)

to

if(SupertrendDirection() == -1.0 && OrderStopLoss() > SupertrendDownTrend())

now there's no error code 1 but it doesn't even do order modify at all

 
Keith Watford:

A simple check is

if(SupertrendUpTrend()-OrderStopLoss()>Point*0.5)

You can use point in this case as the symbol is the chart symbol. Otherwise you would need to get the point value for the symbol.

Luandre Ezra:

already adding the condition but still error occurred.

Show the code that you have used.

 
Keith Watford:

Show the code that you have used.

double SupertrendUpTrend(int shift = 0)
  {
   return iCustom(Symbol(),PERIOD_CURRENT,"MQLTA MT4 Supertrend Line",ATR_Level,50,1000,0,shift);
  }

double SupertrendDownTrend(int shift = 0)
  {
   return iCustom(Symbol(),PERIOD_CURRENT,"MQLTA MT4 Supertrend Line",ATR_Level,50,1000,1,shift);
  }

double SupertrendDirection(int shift = 0)
  {
   return iCustom(Symbol(),PERIOD_CURRENT,"MQLTA MT4 Supertrend Line",ATR_Level,50,1000,2,shift);
  }

OnTick{
if(!CheckIfOpenOrdersByMagicNumber(MagicNumber))
     {
      if(newBar && FuncMACDCrossover(1) == 1)
        {
         if(SupertrendDirection() == 1)
            stopLossPrice = SupertrendUpTrend();
         else
            stopLossPrice = NormalizeDouble((Bid - stoplossPoint),Digits);

         double lotSize       = NormalizeDouble(OptimalLotSize(Risk_Per_Trade,Ask,stopLossPrice),2);
         if(lotSize < MarketInfo(Symbol(),MODE_MINLOT))
            lotSize = MarketInfo(Symbol(),MODE_MINLOT);

            orderId              = OrderSend(Symbol(),OP_BUY,lotSize,Ask,slippage,stopLossPrice,0,"Strong BUY",MagicNumber);
        }

      if(newBar && FuncMACDCrossover(1) == 2)
        {
         if(SupertrendDirection() == -1)
            stopLossPrice = SupertrendDownTrend();
         else
            stopLossPrice = NormalizeDouble((Ask + stoplossPoint),Digits);

         double lotSize       = NormalizeDouble(OptimalLotSize(Risk_Per_Trade,Bid,stopLossPrice),2);
         if(lotSize < MarketInfo(Symbol(),MODE_MINLOT))
            lotSize = MarketInfo(Symbol(),MODE_MINLOT);        

            orderId              = OrderSend(Symbol(),OP_SELL,lotSize,Bid,slippage,stopLossPrice,0,"Strong SELL",MagicNumber);
        }
     }

else
     { 
      double lots          = OrderLots();
      if(OrderSelect(orderId,SELECT_BY_TICKET))
        {
         if(OrderSymbol() == Symbol() && OrderType()==OP_BUY)
           {
            if(SupertrendDirection() == 1.0 && OrderStopLoss() < SupertrendUpTrend() && SupertrendUpTrend()-OrderStopLoss()>Point*0.5)
              {
               double trailingStop = SupertrendUpTrend();
               bool resSL = OrderModify(orderId,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
               if(!resSL)
                 {
                  Print("Error in Order Modify. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Order modify successfully for ticket: ", OrderTicket());
                 }
              }
         if(OrderSymbol() == Symbol() && OrderType()==OP_SELL)
           {
            if(SupertrendDirection() == -1.0 && OrderStopLoss() > SupertrendDownTrend() && MathAbs(SupertrendDownTrend()-OrderStopLoss()>Point*0.5))
              {
               double trailingStop = SupertrendDownTrend()*Point();
               bool resSL = OrderModify(orderId,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
               if(!resSL)
                 {
                  Print("Error in OrderModify. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Order modify successfully for ticket: ", OrderTicket());
              }
           }
        }
     }
  }

I don't know if it's going to be helpful or not but I put the code for buy and sell plus the indicator func. Error code 1 is no longer occurred but the stoploss won't modified. it still use the initial stoploss even though OrderStopLoss is less or has more value than the Supertrend line. I noticed that the supertrend indicator has only 4 digits after decimal and my broker is 5 digits after decimal, don't know if this is related or not.

 
Keith Watford:

A simple check is

if(SupertrendUpTrend()-OrderStopLoss()>Point*0.5)

You can use point in this case as the symbol is the chart symbol. Otherwise you would need to get the point value for the symbol.

Use Ticksize not point. Code fails on metals.

 
William Roeder:

Use Ticksize not point. Code fails on metals.

if(SupertrendUpTrend()-OrderStopLoss()>Point*0.5)
As the point of this was to compare whether the 2 numbers are equal, I don't think that it will fail on metals.
 
Luandre Ezra:

I don't know if it's going to be helpful or not but I put the code for buy and sell plus the indicator func. Error code 1 is no longer occurred but the stoploss won't modified. it still use the initial stoploss even though OrderStopLoss is less or has more value than the Supertrend line. I noticed that the supertrend indicator has only 4 digits after decimal and my broker is 5 digits after decimal, don't know if this is related or not.

I have no idea what the highlighted text means!

First fix the obvious mistakes in your code.

      double lots          = OrderLots();               
      if(OrderSelect(orderId,SELECT_BY_TICKET))

You cannot get values for an order until the order has been selected.

You call the same function 3 times, it may be possible that the value is not the same for an indicator every call.

            if(SupertrendDirection() == 1.0 && OrderStopLoss() < SupertrendUpTrend() && SupertrendUpTrend()-OrderStopLoss()>Point*0.5)
               {
               double trailingStop = SupertrendUpTrend();

This way, you know that the value remains the same.

            double trailingStop = SupertrendUpTrend();
            if(SupertrendDirection() == 1.0 && OrderStopLoss() < trailingStop && trailingStop-OrderStopLoss()>Point*0.5)
               {
double trailingStop = SupertrendDownTrend()*Point();

Why do you multiply by Point??

 
Keith Watford:

I have no idea what the highlighted text means!

it is for the structure of the code, maybe the problem is not on the code but on the structure of the code.

First fix the obvious mistakes in your code.

Before the lots variable there's an else statement. My EA works by selecting the magic number first rather than ticket number.

This way, you know that the value remains the same.

I put it under the if statement because I use the code for BUY and SELL trailing stop. I never know that there's a possibility that the value can change. I will put it before if statement and change the variable name respectively. 

Why do you multiply by Point??

The indicator is 4 digits after decimal and my broker is 5 digits. Just thinking maybe that was the case with error 1 and order modify fail to modify at all. but I think that's not the case.


So I'm adding a print statement before and after if statement. If I use this code below

if(SupertrendDirection() == -1 && MathAbs(trailingStopSell-OrderStopLoss()>Point*0.5))

the print statement inside the if statement fail to be print. I don't know if this what WH Roeder mention by fail.

Meanwhile, if I use the code below. the error code 1 occur but never truly close the order when the price hit the indicator.

if(SupertrendDirection() == -1 && OrderStopLoss() != trailingStopSell;

Since the error 1 still occur but stoploss is changing, I try to let it run. I found out that even the stoploss is changing but there isn't any real stoploss, there is no stoploss line in backtesting visual mode. I never met with this kind of problem before.

 
Keith Watford: As the point of this was to compare whether the 2 numbers are equal, I don't think that it will fail on metals.
if(SupertrendUpTrend()-OrderStopLoss()>Point*0.5)

The title of this thread is “How to avoid error 1 in order modify.” “The point of this was to compare whether the 2 numbers” were different enough to send.

The two numbers will compare as not equal, but will result will still be error 1 or error 129/130 since the change is not at least a tick.

 
William Roeder:

The title of this thread is “How to avoid error 1 in order modify.” “The point of this was to compare whether the 2 numbers” were different enough to send.

The two numbers will compare as not equal, but will result will still be error 1 or error 129/130 since the change is not at least a tick.

Yes, I got the title of the thread. It is about error 1. It is not about error 129 or 130.


Luandre Ezra:

I do also put OrderStopLoss() != SupertrendUpTrend() this syntax but it gives me the same error

Keith Watford:

A simple check is

if(SupertrendUpTrend()-OrderStopLoss()>Point*0.5)

You can use point in this case as the symbol is the chart symbol. Otherwise you would need to get the point value for the symbol.

My post was simply about making sure that the New SL would not be considered equal to the old SL.

 

already debugging every section of the EA and still the error exist. Found out it is not the code that I used or any of you guys wrote, using point and ticksize has the same ability of solving the problem. The root of it is the indicator itself. I'm debugged it from the start using print statement and found out the value that the data window showed for the supertrend indicator is different with the data that I got from the print statement. I'm still trying to figure it out why the value is different between the data window and the print statement.

this is the screen capture of the chart using the supertrend indicator. The data showed on the data window is different from the alert and supertrend downtrend value should have been around 1.19100 and the value which the alert showed is 1.2000 which is almost 1000 pips away.

Reason: