error=1 when take profit

 

Hi, I am struggling with my first EA and I am getting error=1 when calling function (take profit/modify SL/scale out)

Attached is the code for taking profit/scale out

the Alert shows funny digits for SL, and I cannot understand why it fails

It appear to be some major issues I need to learn with my code, any advise is greatly appreciated

 

this is the other part for MoveTrailingProfit

int MoveTrailingProfit()
{ total=OrdersTotal(); 
  double SL_BUY=NormalizeDouble(Bid-TrailingProfit*pips2double,Digits); //Ask will be tighter
  double SL_SELL=NormalizeDouble(Ask+TrailingProfit*pips2double,Digits);
   
        for (int cnt=total-1; cnt>=0; cnt--)
        {
         if  (
                 OrderSelect(cnt, SELECT_BY_POS)                    // select Only my orders with
                 &&  OrderMagicNumber()  == Magic                   // my magic number
                 &&  OrderSymbol()       == Symbol()            // and my pair.
                 )
                {
                 if(OrderType()==OP_BUY)                                                //and is buy order
                        {
                         if(OrderStopLoss()>=OrderOpenPrice())          //meaning already scaled once
                          {
                                if(NormalizeDouble(OrderStopLoss(),Digits)<SL_BUY)      //ToDo
                 {
                  //Print(lazy,OrderTicket()," BUY TrailingProfit Activated.");
                                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),SL_BUY,0/*OrderTakeProfit()*/,0,TPColor))
                                  Alert(lazy,OrderTicket()," BUY TrailingProfit err=", GetLastError(),", ",SL_BUY);
                             }
                          }
                        }
                
                 if(OrderType()==OP_SELL)
                        {
                         if(OrderStopLoss()<=OrderOpenPrice())          //meaning already scaled once
                          {
                                if(NormalizeDouble(OrderStopLoss(),Digits)>SL_SELL)
                 {
                                  //Print(lazy,OrderTicket(), " SELL TrailingProfit Activated.");
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),SL_SELL,0/*OrderTakeProfit()*/,0,TPColor))
                                  Alert(lazy,OrderTicket()," SELL TrailingProfit err=", GetLastError(),", ",SL_SELL);
                             }
                          }
                        }
                }
        }
return(0);
}
 

Can you explain what this line of code is meant to do ?

if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Black))

and what it does when you call it the next time for the same Order ?

 
holdenmcgorin:

the Alert shows funny digits for SL, and I cannot understand why it fails

Your Alert uses SL+ to output the SL value . . . not aware of the use of the + in that situation, what does it do ?
 

sorry for the stupid mistake and I shall correct my code

thanks for your time

 
holdenmcgorin:

sorry for the stupid mistake and I shall correct my code

thanks for your time

LOL, please, no apologies . . . make mistakes, learn from them . . . it makes you better. It also helps others who read your posts and the replies . .
 
RaptorUK:
Your Alert uses SL+ to output the SL value . . . not aware of the use of the + in that situation, what does it do ?
Forgot to ask, what is the reason when MoveTrailingProfit() function also returns error=1
 if(!OrderModify(OrderTicket(),OrderOpenPrice(),SL_BUY,0/*OrderTakeProfit()*/,0,TPColor))
 
RaptorUK:

Can you explain what this line of code is meant to do ?

and what it does when you call it the next time for the same Order ?


if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Black))
is this not move SL to BEP?
 

holdenmcgorin:
Forgot to ask, what is the reason when MoveTrailingProfit() function also returns error=1
OK, you are trying to change the SL, looking at the output from your Alerts, at 14:42 you try to modify ticket no. 58141432 with a SL of 1.5457, then you try to do the same thing at 17:12 . . hence Error 1 "OrderModify attempts to replace the values already set with the same values. One or more values must be changed, then modification attempt can be repeated."

All you need to do is something like this . . .

if (SL_Sell > OrderStopLoss())

    //  do the order modify . . . .
 
holdenmcgorin:

is this not move SL to BEP?
Yes it is . . . the first time you use it for a particular Order . . . the next time you use it on the same Order it does nothing and you WILL get an Error 1
 

oh, that is the reason for error=1, thanks for the explanation

I truly appreciate your reply as I am learning something every time

Reason: