Changing Pending Order OR Getting the Trigger Price

 

Hello,

I am trying to Modify or Delete the old Order and Create a new Order for a Pending order if prices moves in my favor giving me a better entry price.

Initially I want to open a BuyStop 5pips above the current Ask. Then if price moves lower keep reducing the gap maintaining a 5 pip gap. If price moves up, then do not change the "Trigger Price"

I am unable to make it do this as reading the journal entries sometimes it is also increasing the price of the trigger when it should only ever reduce it.

   double a=Ask;
   double b=Bid;
   double TickV=MarketInfo(NULL,MODE_TICKVALUE);
   double s_l;
   double gap=PipPoints*ToEntry;                           //ToEntry is an input int defined in the top part of the code
   double Lots=NormalizeDouble(((RiskPercent/100)*AccountBalance())/(TickV*Stop_Loss*10),2);
   if(OrdersTotal()==0){
         if(RSI>50){
               s_l=b-(Stop_Loss*PipPoints)+gap;
               ticket=OrderSend(NULL,OP_BUYSTOP,Lots,a+(gap),slip,s_l,0,"BuyStop",1234,0,Blue);
         }
         if(RSI<50){
               s_l=a+(Stop_Loss*PipPoints)+gap;               
               ticket=OrderSend(NULL,OP_SELLSTOP,Lots,b-gap,slip,s_l,0,"SellStop",1234,0,Blue);
         }
   }
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true){
      if(OrderType()!=OP_BUY || OrderType()!=OP_SELL){
         if(RSI>50 &&((OrderType()==OP_BUYSTOP && OrderOpenPrice()>(a+gap))||OrderType()==OP_SELLSTOP)){
               OrderDelete(OrderTicket());
               s_l=b-(Stop_Loss*PipPoints)+gap;     
               ticket=OrderSend(NULL,OP_BUYSTOP,Lots,a+gap,slip,s_l,0,"ModBuy",1234,0,Blue);
         }
         if(RSI<50 &&((OrderType()==OP_SELLSTOP && OrderOpenPrice()<(b-gap))||OrderType()==OP_BUYSTOP)){
               OrderDelete(OrderTicket()); 
               s_l=a+(Stop_Loss*PipPoints)-gap; 
               ticket=OrderSend(NULL,OP_SELLSTOP,Lots,b-gap,slip,s_l,0,"ModSell",1234,0,Blue);
         }
       }
    }
}

Any help would be appreciated. As it seems that I am using OrderOpenPrice() for a pending order, making me think that well there isn't a an OrderOpenPrice() since it has not yet opened. If thats the case then how do i reference the Price of a Pending Order?

 
theone964:

Any help would be appreciated.

It may not be your actual problem, but I would start with the following line:

if (OrderType()!=OP_BUY || OrderType()!=OP_SELL)

This condition is always true. It is meaningless. For example, if OrderType() is "buy", then the condition means "if buy is not buy or buy is not sell...". 

I suspect that what you actually mean is a way of ignoring open trades and only looking at pending orders:

if (OrderType()!=OP_BUY && OrderType()!=OP_SELL)

For the record, because of the numeric values of OP_BUY (0) and OP_SELL (1), you will often see the same condition written as follows in MQL4 code:

if (OrderType() > OP_SELL)  // i.e. value of OrderType() must be 2 or more; one of the OP_ limit or stop values
 
theone964:

me think that well there isn't a an OrderOpenPrice() since it has not yet opened. If thats the case then how do i reference the Price of a Pending Order?

OrderOpenPrice() gives you either the future entry price of a pending order, or the fill price of an open trade, depending on the prior OrderSelect() context. You are looking at the correct value.

I don't immediately see a problem in the code which you have posted which will cause it to increase the entry price of a pending order rather than decreasing it. Your broker won't like you, because you are repeatedly deleting and replacing orders each time there is the tiniest downward movement in the price. But I'm failing to see an actual problem in the logic (assuming that "gap" is constant across each pass through this code). Someone else may do better.

Rather than always deleting and replacing, it would be preferable to do the following:

  • If RSI is above 50...
  • If the current order is a sell stop, delete and replace with a buy stop
  • If the current order is a buy stop, and the revised entry price is lower, use OrderModify() to change the entry price and s/l, rather than deleting and replacing.

The only difference between that and your current route is that it will not re-evaluate the lot size on each downward movement in the price. In effect, it will only re-evaluate the lot size when the RSI signal changes from >50 to <50.

 
theone964: Initially I want to open a BuyStop 5pips above the current Ask. Then if price moves lower keep reducing the gap maintaining a 5 pip gap. If price moves up, then do not change the "Trigger Price"


As it seems that I am using OrderOpenPrice() for a pending order, making me think that well there isn't a an OrderOpenPrice() since it has not yet opened. If thats the case then how do i reference the Price of a Pending Order?
  1. The OOP is the pending price and will become the open price when triggered.
  2. Your posted code doesn't have a OrderModify in it. Don't delete and create a new one. Find the pending price see if you want to move it lower (buy stop) modify it. Look at any trailing SL code and reverse the logic.
  3. Alternatively, Humans can't watch the screen 24/7 so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and open an order.
 
JC #:

OrderOpenPrice() gives you either the future entry price of a pending order, or the fill price of an open trade, depending on the prior OrderSelect() context. You are looking at the correct value.

I don't immediately see a problem in the code which you have posted which will cause it to increase the entry price of a pending order rather than decreasing it. Your broker won't like you, because you are repeatedly deleting and replacing orders each time there is the tiniest downward movement in the price. But I'm failing to see an actual problem in the logic (assuming that "gap" is constant across each pass through this code). Someone else may do better.

Rather than always deleting and replacing, it would be preferable to do the following:

  • If RSI is above 50...
  • If the current order is a sell stop, delete and replace with a buy stop
  • If the current order is a buy stop, and the revised entry price is lower, use OrderModify() to change the entry price and s/l, rather than deleting and replacing.

The only difference between that and your current route is that it will not re-evaluate the lot size on each downward movement in the price. In effect, it will only re-evaluate the lot size when the RSI signal changes from >50 to <50.

Very good suggestion, but please I would really appreciate if you can show a little example of how to use the order modify function to modify the pending order price and not deleting and creating a new one



Thanks in advance 
Reason: