How to modify pending orders (BuyStop & SellStop) ?

 

Hi All

I would like to modify pending orders (BuyStop & SellStop). It only modifies once instead of several times. Please help if you know the problem.

#property strict

extern double TakeProfit=10.5;
extern double StopLoss=10.5;

int MagicNumber=10;
int DelayTime=1;
static datetime NextTradeBuy=0;
static datetime NextTradeSell=0;

int CountPenOrders()
 {
  int Pending=0;
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP)
   Pending++;
  }
   
  return(Pending);
 } 
 
double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
  else if(Digit==4||Digit==5){PipPoint=Point*10;}
  return(PipPoint);
 }
 
void Buy()
 {
  double TakeProfitBuy=Ask+TakeProfit*Pips();
  double StopLossBuy=Low[0]-(StopLoss*Pips());
  
  double Lowest=iLowest(Symbol(),0,MODE_LOW,10,1);
  double Lowest2=iLowest(Symbol(),0,MODE_LOW,10,2);
  
  datetime Expire=TimeCurrent()+(60*15);
  
  if(OrdersTotal()==0)
  if(TimeCurrent()>=NextTradeBuy)
  if(Close[1]>Lowest&&Close[1]<Lowest2)
  {
   int BuyTrade=OrderSend(Symbol(),OP_BUYSTOP,0.01,Ask+(0.5*Pips()),0,StopLossBuy,TakeProfitBuy,"JackBuda",MagicNumber,Expire,clrBlue);
   
   NextTradeBuy=TimeCurrent()+(DelayTime*20);
   
   for(int x=OrdersTotal()-1;x>=0;x--)
   {
    double TrailStep=0.2;
    if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUYSTOP)
    if(OrderOpenPrice()-Ask>TrailStep*Pips())
    {
     bool ModBuyStop=OrderModify(OrderTicket(),Ask+(TrailStep*Pips()),OrderStopLoss(),OrderTakeProfit(),0,clrNONE);
    }
   }
  } 
  
 } 
 
void Sell()
 {
  double TakeProfitSell=Bid-TakeProfit*Pips();
  double StopLossSell=High[0]+(StopLoss*Pips());
  
  double Highest=iHighest(Symbol(),0,MODE_HIGH,10,1);
  double Highest2=iHighest(Symbol(),0,MODE_HIGH,10,2);
  
  datetime Expire=TimeCurrent()+(60*15);
  
  if(OrdersTotal()==0)
  if(TimeCurrent()>=NextTradeSell)
  if(Close[1]<Highest&&Close[1]>Highest2)
  {
   int SellTrade=OrderSend(Symbol(),OP_SELLSTOP,0.01,Bid-(0.5*Pips()),0,StopLossSell,TakeProfitSell,"JackBuda",MagicNumber,Expire,clrRed);
   
   NextTradeSell=TimeCurrent()+(DelayTime*20);
   
   for(int y=OrdersTotal()-1;y>=0;y--)
   {
    double TrailStep=0.2;
    if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_SELLSTOP)
    if(Bid-OrderOpenPrice()>TrailStep*Pips())
    {
     bool ModSellStop=OrderModify(OrderTicket(),Bid-(TrailStep*Pips()),OrderStopLoss(),OrderTakeProfit(),0,clrNONE);
    }
   }
  } 
  
 }  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
 {
  double AccBalance=AccountBalance();
  
  int Figures=0;

  if(AccBalance>10)
   {
    Figures=2;
   }
   
  switch(Figures)
  {
   case 2:
   if(CountPenOrders()<1){Buy();}

   if(CountPenOrders()<1){Sell();}

   break;
  } 
   
 }
 
  double TakeProfitBuy=Ask+TakeProfit*Pips();
  double StopLossBuy=Low[0]-(StopLoss*Pips());
   int BuyTrade=OrderSend(Symbol(),OP_BUYSTOP,0.01,Ask+(0.5*Pips()),0,StopLossBuy,TakeProfitBuy
  1. There is no need to create pending orders in code.
    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. 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 just open an order.

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. If TakeProfit - 0.5 is less than the spread, your send fails.

  4. If Bid-Low[0] is less then the spread, your send fails.
 
William Roeder #:
  1. There is no need to create pending orders in code.
    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. 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 just open an order.

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. If TakeProfit - 0.5 is less than the spread, your send fails.

  4. If Bid-Low[0] is less then the spread, your send fails.

Hi William, I'm using the pending stops to scalp M1.

So what you saying is that, for Buystop should be: 

int BuyTrade=OrderSend(Symbol(),OP_BUYSTOP,0.01,Bid+(1*Pips())

For SellStop:

int SellTrade=OrderSend(Symbol(),OP_SELLSTOP,0.01,Ask-(1*Pips())

For spreads I'm using raw spreads accounts meaning zero spreads.

As for modifying pending orders it's still modifying once instead of twice.

///Buystop/////
for(int x=OrdersTotal()-1;x>=0;x--)
   {
    double TrailStep=0.1;//0.2
    if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUYSTOP)
    if(OrderOpenPrice()-Ask>TrailStep*Pips())
    {
     bool ModBuyStop=OrderModify(OrderTicket(),Ask+(TrailStep*Pips()),OrderStopLoss(),OrderTakeProfit(),0,clrNONE);
    }
   }
  }

///Sellstop/////
for(int y=OrdersTotal()-1;y>=0;y--)
   {
    double TrailStep=0.1;//0.2
    if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_SELLSTOP)
    if(Bid-OrderOpenPrice()>TrailStep*Pips())
    {
     bool ModSellStop=OrderModify(OrderTicket(),Bid-(TrailStep*Pips()),OrderStopLoss(),OrderTakeProfit(),0,clrNONE);
    }
   }
 

I figured out why it's modifying once. I just had to create a void and add to the Ontick function:

void TrailStopOrders()
 {
  for(int x=OrdersTotal()-1;x>=0;x--)
   {
    double TrailStep=1;//0.5;
    if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUYSTOP)
    if(OrderOpenPrice()-Ask>TrailStep*Pips())
    bool ModBuyStop=OrderModify(OrderTicket(),Ask+(TrailStep*Pips()),OrderStopLoss(),OrderTakeProfit(),0,clrNONE);
   }
  
  for(int y=OrdersTotal()-1;y>=0;y--)
   {
    double TrailStep=1;//0.5;
    if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_SELLSTOP)
    if(Bid-OrderOpenPrice()>TrailStep*Pips())
    bool ModSellStop=OrderModify(OrderTicket(),Bid-(TrailStep*Pips()),OrderStopLoss(),OrderTakeProfit(),0,clrNONE);
   }
  
 } 

void OnTick()
 {
  TrailStopOrders();
 }
Reason: