Modifying Pending Order

 

I am trying to modify a pending order - I am only using Buy Stops and Sell Stops.

The pending order needs to be modified at every hourly bar close and the corresponding value of the 60 EMA. If and when the pending order is triggered, I have already implemented a trailing stop but am finding it hard to properly write the code to adjust pending orders...

Please see below - any suggestions will be appreciated. This example below is merely the buy side.

I seem to get error mesage 1 but am not entirely sure why?

void OrderEntry (int direction){
   //determine amount to risk on this trade based on RiskPercent Above.
   double LotSize=0;
   double Equity=AccountEquity();
   double RiskedAmount=Equity*RiskPercent*0.01;
   
   //Get Highest Price in our lookback range and set buy price above it.
   int iTBT= iBarShift(NULL,60, triggerBarTime, true),
        iHH= iHighest(NULL,60, MODE_HIGH, iTBT + CandlesBeforeBiasObtained, 0); 
            double buyPrice = High[iHH]+OrderPrice_PadAmount*pips;
   
   //Get Lowest Price in our lookback range and set sell price below it.
    int iTBT_1= iBarShift(NULL,60, triggerBarTime, true),
        iLL= iLowest(NULL,60, MODE_LOW, iTBT_1 + CandlesBeforeBiasObtained, 0); 
            double sellPrice = Low[iLL]-OrderPrice_PadAmount*pips;
   
   //get our buystop price from below the ma and our takeprofit based on our r:r ratio.
   double buy_stop_price = iMA(NULL,60,MA_Period,0,1,0,1)-MA_PadAmount*pips; 
   double pips_to_bsl = buyPrice-buy_stop_price;         
   double buy_takeprofit_price = (pips_to_bsl*RewardRatio)+buyPrice;   
   
   //get our sellstop price from below the ma and our takeprofit based on our r:r ratio.
   double sell_stop_price = iMA(NULL,60,MA_Period,0,1,0,1)+MA_PadAmount*pips; 
   double pips_to_ssl = sell_stop_price-sellPrice;     
   double sell_takeprofit_price = sellPrice-(pips_to_ssl*RewardRatio);
  
  
   //Place a pending buystop if no orders exists.. pending or otherwise.
   if (direction==0){ //--Buy--//
      double bsl=buy_stop_price;
      double btp=buy_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_bsl/pips))/10;
      if(OpenOrdersThisPair(Symbol())==0){
         int BuyTicketOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); 
      
      }
       if(BuyTicketOrder > 0){
            if (IsNewCandle())
            RecalculateStop();
            Print("Order Placed #", BuyTicketOrder);
         }
         else{
            Print("Order Send Failed, error # ", GetLastError());   
         }
    }


   void RecalculateStop()
   {
   
   for(int b=OrdersTotal()-1; b >=0; b--)
   
   {
    if(OpenOrdersThisPair(Symbol())>0)
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUYSTOP)
                  if(OrderStopLoss()<iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips) 
                      OrderModify(OrderTicket(),OrderOpenPrice(),iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips,OrderTakeProfit(),0,CLR_NONE);
   } 
   }

 
DomGilberto:

I am trying to modify a pending order - I am only using Buy Stops and Sell Stops.

The pending order needs to be modified at every hourly bar close and the corresponding value of the 60 EMA. If and when the pending order is triggered, I have already implemented a trailing stop but am finding it hard to properly write the code to adjust pending orders...

Please see below - any suggestions will be appreciated. This example below is merely the buy side.

I seem to get error mesage 1 but am not entirely sure why?

You NEED to check return values and print your error and any accompanying relevant variables to help you diagnose your coding error . . . if you don't then you will spend more time guessing and asking "what is wrong" . . . do it properly from the start and make your life easy . . . why wouldn't you want an easy life ? why does the OrderModify() fail ?

What are Function return values ? How do I use them ?

 
Ok i'll do that now.
 

Ok Its an OrderModify error 1 in that code...

And an OrderSend error 0 too?

I can't understand why the pending order won't allow me to adjust the stop loss, but when the order has been triggered, my trailing stop loss works perfectly...?

 
DomGilberto:

Ok Its an OrderModify error 1 in that code...

And an OrderSend error 0 too?

I can't understand why the pending order won't allow me to adjust the stop loss, but when the order has been triggered, my trailing stop loss works perfectly...?

  1. And what is error 1?
  2. And what is error 0?
  3. You
    Server
    Change the SL to X
    It is at X!
    Change the SL to XIt is at X!
    Change the SL to XYou are insane

    Print your values and find out.
 

I thought I had applied print values? There is nothing else being told in the journal?

   void RecalculateStop()
   {
   
   for(int b=OrdersTotal()-1; b >=0; b--) 
   
   {
    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)<0){
      Print("Order Select, error # ", GetLastError());
      {
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
               
               if(OrderType()==OP_BUYSTOP)
               
                  if(OrderStopLoss()<iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips)

                   
                      Result = OrderModify(OrderTicket(),OrderOpenPrice(),iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips,OrderTakeProfit(),0,CLR_NONE);
   if(Result!=TRUE) Print("LastError = ", GetLastError());
   } 
   
   }
   }
   }
 
DomGilberto:

I thought I had applied print values? There is nothing else being told in the journal?

ERR_NO_RESULT1OrderModify attempts to replace the values already set with the same values. One or more values must be changed, then modification attempt can be repeated.

Print the values you are trying to modify when you print the error, print the current value and the value you want to replace it with . . .

 
Ah I see, thanks for explaining that Raptor - I will do that now!
 

Yey! I'm happy for the time being - managed to get it working... Although I am not entirely sure how it works? All I added was Print variables?

Am I being stupid, or am I right in saying the Print function just simply prints the user defined information in the journal and nothing more...? Because, as soon as I added this in (v) it now works and trails the stop on pending orders...

   void RecalculateStop()
   {
   
   int stop = OrderStopLoss();
   for(int b=OrdersTotal()-1; b >=0; b--) 
   
   {
    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)==true){
      Print(" Stop loss value for the order is ", OrderStopLoss());
    }
    else if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)==false){
      Print(" OrderSelect failed error code is ",GetLastError());
      }
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
               
               if(OrderType()==OP_BUYSTOP)
               
                  if(OrderStopLoss()<iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips)

                   
                      Result = OrderModify(OrderTicket(),OrderOpenPrice(),iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips,OrderTakeProfit(),0,CLR_NONE);
                        if(Result!=TRUE) Print("LastError = ", GetLastError());
                  
   } 
   }

If I didn't get lucky in figuring out that the "Print" function in relation to "OrderSelect" showed me the stop loss value if it came back true, then I would have never got that in a million years - That was not clear in the manual from where I am standing. It wasn't until I F1'ed "OrderStopLoss" and read the example... Little ambiguous...

I am still getting Last Error = 1 and OrderModify Error = 1 though... I'll have a little play and report back - thanks for being patient with me!

 
For the last time, print out your values.
Result = OrderModify(OrderTicket(),OrderOpenPrice(),iMA(NULL,0,MA_Period,0,1,0,0)
                                                    -MA_PadAmount*pips,OrderTakeProfit(),0,CLR_NONE);
string   PriceToStr(double p){   return( DoubleToStr(p, Digits) );            }

double newSL = iMA(NULL,0,MA_Period,0,1,0,0) -MA_PadAmount*pips;
print("mod SL=", PriceToStr( OrderStopLoss() ), " -> ", PriceToStr( newSL );
Result = OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0,CLR_NONE);
 
How kind of you to elaborate WHRoeder - thanks for explaining that clearly.
Reason: