Error Code 4051 Incorrect Params only on Sell Orders

 

 Hello:

This is my first posting on this forum, so please excuse any formatting/procedural errors.  I am having difficulty with the Order Modify function.  I use an ECN, so I must send the order with 0's in the SL and TP fields and then use the Order Modify function. 

The stop loss and take profit values are inserted perfectly with the buy order code, but fail when a sell order is input, resulting in the 4051 error code.  This refers to invalid parameters on the take profit field, but I am using the same approach (not the calcs) on my buy order.

There is obviously something pretty basic that I am missing.  I would really appreciate some help/insight on the extent of my error.

Thanks very much

wbd  

 // check for short position (SELL) possibility
           
              if (Down==true)         
        {
        SellTicket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"HA Model",16384,0,Red);
          if(SellTicket>0)
              {
              if(OrderSelect(SellTicket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
                SendMail("HA Model", "Sell Order Opened"+Symbol());
             }
               else Print("Error opening SELL order : ",GetLastError());
             {         
          if(SellTicket >0)
              {
               OrderSelect(SellTicket,SELECT_BY_TICKET);               
                             
               if(StopLoss > 0) double SellStopLoss = OpenPrice +(StopLoss * Point);
               if(TakeProfit > 0) double SellTakeProfit = OpenPrice -(TakeProfit * Point);
               if(SellStopLoss > 0 || SellTakeProfit > 0)
               
               bool TicketMod1 =  OrderModify(SellTicket,OrderOpenPrice(),SellStopLoss,SellTakeProfit,0,Blue);
                  
                  else Print("Error input of Stop Loss & Take Profit : ",GetLastError());                         
                
         
             }   
        
 
if(SellTicket >0){
   OrderSelect(SellTicket,SELECT_BY_TICKET);
   if(StopLoss > 0) double SellStopLoss = OpenPrice +(StopLoss * Point);
  1. Always check your return codes. What are Function return values ? How do I use them ? - MQL4 forum
  2. What is the value of OpenPrice?
 
WHRoeder:
  1. Always check your return codes. What are Function return values ? How do I use them ? - MQL4 forum
  2. What is the value of OpenPrice?
Sorry to have left that out.  OpenPrice was declared in the Buy order section.  This portion of the EA functions properly, which is why I am confused when I get the error message.  Thanks for the reference on the return values.

 if (Up==true)
                                
          {
          BuyTicket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"HA Model",16384,0,Green);
           if(BuyTicket>0)
             {
           if(OrderSelect(BuyTicket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
             SendMail("HA Model", "Buy Order Opened"+Symbol());
             }
            else Print("Error opening BUY order : ",GetLastError());
              if(BuyTicket >0)
               {
                OrderSelect(BuyTicket,SELECT_BY_TICKET);
                double OpenPrice = OrderOpenPrice();
              
                if(StopLoss > 0) double BuyStopLoss = OpenPrice -(StopLoss *Point);
                if(TakeProfit >0) double BuyTakeProfit = OpenPrice +(TakeProfit * Point);
                if(BuyStopLoss > 0 || BuyTakeProfit > 0)
                
                 bool TicketMod = OrderModify(BuyTicket,OrderOpenPrice(),BuyStopLoss,BuyTakeProfit,0,Yellow);
               
                    Print("Error input of Stop Loss & Take Profit : ",GetLastError());
                
              } 
 
wbdavis:
Sorry to have left that out.  OpenPrice was declared in the Buy order section.  This portion of the EA functions properly, which is why I am confused when I get the error message.  Thanks for the reference on the return values.

Open price for a Buy is Ask,  for a Sell it is Bid,  how far has price moved between placing your Buy and your Sell ?  Don't use OpenPrice use Ask if you must or use OrderOpenPrice().  Why don't you print OpenPrice when your OrdrModify() fails ?  why don't you Print Ask, Bid, etc ?  don't you think they are relevant to finding thee cause of your issue ?
 
RaptorUK:
Open price for a Buy is Ask,  for a Sell it is Bid,  how far has price moved between placing your Buy and your Sell ?  Don't use OpenPrice use Ask if you must or use OrderOpenPrice().  Why don't you print OpenPrice when your OrdrModify() fails ?  why don't you Print Ask, Bid, etc ?  don't you think they are relevant to finding thee cause of your issue ?


I always print values to my journal, but the answer was not obvious until I read the thread on function return values and trapped them.  Result was a mod to my code that corrected the problem.

Not sure about your reference to using Ask and Bid.  My Order Send uses Ask and Bid as you suggest, and OrderOpenPrice() was used to set the TP and SL.  Anyway, the mod to my code was successful.

Thank you both for your time.

WBD 

 
wbdavis:

 Hello:

This is my first posting on this forum, so please excuse any formatting/procedural errors.  I am having difficulty with the Order Modify function.  I use an ECN, so I must send the order with 0's in the SL and TP fields and then use the Order Modify function. 

The stop loss and take profit values are inserted perfectly with the buy order code, but fail when a sell order is input, resulting in the 4051 error code.  This refers to invalid parameters on the take profit field, but I am using the same approach (not the calcs) on my buy order.

There is obviously something pretty basic that I am missing.  I would really appreciate some help/insight on the extent of my error.

Thanks very much

wbd  

 


For anyone reading this thread, here is the change I made to make the Stop Loss and Take Profit modification.  My problem was based on the value of 0 being returned as the Ticket No.  Code replaced in pink and code deleted in yellow.

 

 if(SellTicket >0)
              {
               OrderSelect(SellTicket,SELECT_BY_TICKET);
               
               double entry_price = OrderOpenPrice();
               
               OrderModify(SellTicket, entry_price, (entry_price + StopLoss*Point), (entry_price - TakeProfit*Point), 0);
                                              
              // if(StopLoss > 0) double SellStopLoss = OpenPrice +(StopLoss * Point);
              // if(TakeProfit > 0) double SellTakeProfit = OpenPrice -(TakeProfit * Point);
              // if(SellStopLoss > 0 || SellTakeProfit > 0)
               
             // bool TicketMod1 =  OrderModify(SellTicket,OrderOpenPrice(),SellStopLoss,SellTakeProfit,0,Blue);
                  
                 // else Print("Error input of Stop Loss & Take Profit : ",GetLastError());
 
wbdavis:


I always print values to my journal, but the answer was not obvious until I read the thread on function return values and trapped them.  Result was a mod to my code that corrected the problem.

Not sure about your reference to using Ask and Bid.  My Order Send uses Ask and Bid as you suggest, and OrderOpenPrice() was used to set the TP and SL.  Anyway, the mod to my code was successful.

You used the OrderOpenPrice() from the Buy order  (Ask)  and used this when setting the SL and TP for the Sell order,  the Sell order is placed at Bid.  Aside from the issue that this may or may not be what you intended,  if there is time between the Buy and Sell being placed your saved OpenPrice value will be based on an old Ask price resulting in your SL and TP both being above or both being below the Bid price when you try to place the Sell order.
Reason: