ModifyOrder Error 4051 & 4108

 

Hi Guys,

I'm trying to add to my code that once price has reached exactly halfway between my entry point and TP, then SL will move to where the entry position was, to add some downside protection on my code.

I looked around to find a basic trending SL for a starting point for me to work from but I am getting ModifyOrder Error 4051 & 4108 the whole time. Does anyone know where I might be missing something?

Thanks in advance! 

//************************************************************  
//EXECUTE SELL CONDITION
//************************************************************
                    
        {                              
        if( sell_condition_1 && sell_condition_2 && sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && sell_condition_7)
        {
        
         double ShortStopLoss =  Bid + (StopLoss * Point*10);
         double ShortTakeProfit = Bid - (TakeProfit * Point*10);
                                      
      //-------------------------------------------------------------------------------------------------------------------    
      //-------------------------------------------------------------------------------------------------------------------
       ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,ShortStopLoss,ShortTakeProfit,"Sell Order",MagicNumber,0,Red);
      //-------------------------------------------------------------------------------------------------------------------    
      //-------------------------------------------------------------------------------------------------------------------            
        }
        }      
                        
//************************************************************  
  
      
       OrderSelect(OrderTicket(), SELECT_BY_TICKET);              
       if (OrderType() == OP_BUY)
           {    
           if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop*10)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop*10)
                    {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(Point*TrailingStop*10),OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
            
        else
                        
           if(TrailingStop>0)            
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop*10))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop*10)) || (OrderStopLoss()==0))
                    {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(Point*TrailingStop*10),OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }    
 
       OrderSelect(OrderTicket(), SELECT_BY_TICKET);              
  1. You can't use any Trade Functions - MQL4 Reference unless you select a order first. Everything after that select failure is nonsense.
  2. You would have known this had you checked your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 
whroeder1:
       OrderSelect(OrderTicket(), SELECT_BY_TICKET);              
  1. You can't use any Trade Functions - MQL4 Reference unless you select a order first.
  2. You would have known this had you checked your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Thanks for the advice! I made some adjustments, I am not getting the errors now but my SL is still not moving. Any Thoughts?
//************************************************************  
//EXECUTE SELL CONDITION
//************************************************************
                    
           {                              
           if( sell_condition_1 && sell_condition_2 && sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && sell_condition_7)
           {
        
            double ShortStopLoss =  Bid + (StopLoss * Point*10);
            double ShortTakeProfit = Bid - (TakeProfit * Point*10);
                                      
      //-------------------------------------------------------------------------------------------------------------------    
      //-------------------------------------------------------------------------------------------------------------------
      ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,ShortStopLoss,ShortTakeProfit,"Sell Order",MagicNumber,0,Red);
      //-------------------------------------------------------------------------------------------------------------------    
      //-------------------------------------------------------------------------------------------------------------------            
           }
           }    
                        
//************************************************************  
  
      
   for(cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderType()<=OP_SELL &&   // check for opened position
         OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {

                     if(!OrderModify(LongTicket,OrderOpenPrice(),Bid-(Point*TrailingStop),OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else // go to short position
           {
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     if(!OrderModify(ShortTicket,OrderOpenPrice(),Ask+(Point*TrailingStop),OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }  
 
  1. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
  2. Print out your variables, and find out why.
  3. Don't duplicate code
    if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
      {
       if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
         {
          if(!OrderModify(ShortTicket, OrderOpenPrice(), Ask+(Point*TrailingStop), OrderTakeProfit(), 0, Red))
    Simplify
    double tsl = Ask + Point*TrailingStop;
    if(tsl < MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
       if(!OrderModify(ShortTicket, OrderOpenPrice(), tsl, OrderTakeProfit(), 0, Red))

 

First of all you should make your code more efficient

This condition

if(TrailingStop>0)


should be checked first as the whole block of code is irrelevant if it <=0

for(cnt=0;cnt<total;cnt++)


Where is total assigned a value?

if(!OrderModify(LongTicket,OrderOpenPrice(),Bid-(Point*TrailingStop),OrderTakeProfit(),0,Green))


Why are you looping through the orders when you are trying to modify  a specific order. OrderOpenPrice() etc are the values for the selected ticket, not necessarily LongTicket's

return;


Why the return?
 
Keith Watford:

First of all you should make your code more efficient

This condition

if(TrailingStop>0)


should be checked first as the whole block of code is irrelevant if it <=0

for(cnt=0;cnt<total;cnt++)


Where is total assigned a value?

if(!OrderModify(LongTicket,OrderOpenPrice(),Bid-(Point*TrailingStop),OrderTakeProfit(),0,Green))


Why are you looping through the orders when you are trying to modify  a specific order. OrderOpenPrice() etc are the values for the selected ticket, not necessarily LongTicket's

return;


Why the return?
Thanks for the response guys! I've made more changes and still can't seem to find why the code won't initiate the Trailing SL, why doesn't this work?
//************************************************************  

   for(cnt=0 ; cnt < OrdersTotal(); cnt++)
  
     {
      if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false )
      continue;
      
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////        
         if(OrderType()==OP_BUY)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////          
              {
                  double tslL = Bid - (Point*TrailingStop*10);
                  if(tslL > MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
                  if(!OrderModify(OrderTicket(), OrderOpenPrice(), tslL, OrderTakeProfit(), 0, Red))
                  Print("OrderModify error ",GetLastError());
                    }
              }                    
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////        
         else if (OrderType() == OP_SELL)      
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////          
              {
                  double tslS = Ask + (Point*TrailingStop*10);
                  if(tslS < MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
                  if(!OrderModify(OrderTicket(), OrderOpenPrice(), tslS, OrderTakeProfit(), 0, Red))
                  Print("OrderModify error ",GetLastError());

                    }
              }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                  
    
     }
            
   }

 
 
  1. if(tslL > MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
    This will trail continuously not just above break even.
  2. Print out your variables, and find out why.
 
whroeder1:
  1. if(tslL > MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
    This will trail continuously not just above break even.
  2. Print out your variables, and find out why.

Thanks for the help whroeder, I'm still not getting my code to work though. I've made some more changes and tried to print every variable.

It seems that the Bid/Ask and OpenPrice difference is always 10.. which is the spread that I use to backtest. So this means that it is not looping and I have tried shifting this all over my code and it still won't loop.

I have changed my code again. All I want it to do is once the price moves past my TrailingStop level, then the StopLoss should move to where I opened the order originally. Could anyone please help me figure out why this is not working?

//************************************************************  
//EXECUTE SELL CONDITION
//************************************************************
                    
        {                              
        if( sell_condition_1 && sell_condition_2 && sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && sell_condition_7)
        {
        
            double ShortStopLoss =  Bid + (StopLoss * Point*10);
            double ShortTakeProfit = Bid - (TakeProfit * Point*10);
                                      
      //-------------------------------------------------------------------------------------------------------------------    
      //-------------------------------------------------------------------------------------------------------------------
      ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,ShortStopLoss,ShortTakeProfit,"Sell Order",MagicNumber,0,Red);
      //-------------------------------------------------------------------------------------------------------------------    
      //-------------------------------------------------------------------------------------------------------------------            
                          
                          
        OrderSelect(ShortTicket,SELECT_BY_TICKET);
        RefreshRates();
        if((OrderOpenPrice()-Ask)>TrailingStop)
            
        {
        bool res=OrderModify(ShortTicket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
        if(!res)
              Print("Error in OrderModify. Error code=",GetLastError());
        else
              Print("Order modified successfully.");
        }
                      
        }
 
C0mput3r:

I'm still not getting my code to work though.

I've made some more changes and tried to print every variable.

I have tried shifting this all over my code and it still won't loop.

I have changed my code again.

All I want it to do is once the price moves past my TrailingStop level, then the StopLoss should move to where I opened the order originally.

  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Then everything we did above to try an help you has been a waste of our time. You need to post the new code, there are no mind readers here. What you did post has no print statements, so that is not your code.
  3. There are no loops in your code. Why are you babbling about loops
  4. #2
  5. Where is your code that checks for that? All I see is you open an order, then a do nothing modify.
    bool res=OrderModify(ShortTicket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
    What do you expect that to do? Do you expect
    if((OrderOpenPrice()-Ask)>TrailingStop)
    to be true the millisecond after you open the order? You must have a check for an open order, otherwise you would be opening many orders. The modify you posted is inside that check.
Post all your code.
 
whroeder1:
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Then everything we did above to try an help you has been a waste of our time. You need to post the new code, there are no mind readers here. What you did post has no print statements, so that is not your code.
  3. There are no loops in your code. Why are you babbling about loops
  4. #2
  5. Where is your code that checks for that? All I see is you open an order, then a do nothing modify.
    bool res=OrderModify(ShortTicket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
    What do you expect that to do? Do you expect
    if((OrderOpenPrice()-Ask)>TrailingStop)
    to be true the millisecond after you open the order? You must have a check for an open order, otherwise you would be opening many orders. The modify you posted is inside that check.
Post all your code.

I removed the prints to make the code shorter to attach on here for you guys, otherwise I feel it would've been too cluttered.

Sorry if my previous comment wasn't clear, I couldn't get the code to engage the Trailing stop loss with a loop so I removed the loop and that is the code (or portion of) that I copied for you.

I figured out what the original issue was though, for some reason it did not pick up OrderOpenPrice() as anything other than 0.0001, even if I assigned an Int variable to it. I changed Int to Double and then it finally worked.

The whole goal of the code that I am trying to add is for the stoploss to move to breakeven(OrderOpenPrice), as soon as the price has moved 50% of the distance between the entry point and my TP.

I have managed to get the code to engage the ModifyOrder, although now once TP is reached it just doubles the distance of the TP for my next trade but the distance of my next trade's SL stays the same.

I am attaching my entire code for you. If you could give me any advice on this last issue, I would be extremely grateful.

Kind regards,

Anton 

Files:
Test.mq4  10 kb
 

I have deleted your new topic.

If you can't resolve your problems here, starting a new topic will not bring any different results.

Reason: