Pending orders not placed when conditions are met

 

My code below places sell pending orders when certain candle pattern conditions are met on the H_1 chart. At certain points the condition is met but the pending orders are not placed, as in the encircled portions in https://imgur.com/a/3pNC7M3 . I am not sure what's causing it.


string prefix = "HG"; 
const  int    N_bars =  1;

       int    numBars = 1;
       int    numBarsArray[];
       int    tempVal = 0;
       int    NumOfDisplayBars = 300;

       int    count = 0;

extern double lotSize = 0.01;
       int    magicnumber = 1337;

void showRectangles()
{
   for (int i=NumOfDisplayBars;i>=1;i--)
   {
       if(isBearishEngulfing(i))
       {
           drawBearRectangle(i + 1,iHigh(_Symbol,0,i + 1),iOpen(_Symbol,0,i + 1));
       }
   }
}

bool isBearishEngulfing(int current)
{   
    if(  (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current    )) 
      && (iClose(_Symbol,0,current + 1) > iOpen( _Symbol,0,current + 1)) 
      && (iOpen( _Symbol,0,current    ) > iClose(_Symbol,0,current + 1)) 
      && (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current + 1))
         )
          return true;
    return false;      
}

bool drawBearRectangle(int candleInt,const double top,const double bottom)
{  
    const datetime starts = iTime(_Symbol,0,candleInt); 
    const datetime   ends = starts+PeriodSeconds()*N_bars;
    const   string   name = prefix+"_"+(candleInt>0?"DEMAND":"SUPPLY")+"_"+TimeToString(starts);

    if(!ObjectCreate(0,name,OBJ_RECTANGLE,0,0,0,0,0))
    {
        printf("%i %s: failed to create %s. error=%d",__LINE__,__FILE__,name,_LastError);
        return false;
    }
    ObjectSetInteger(0,name,OBJPROP_TIME1, starts);
    ObjectSetInteger(0,name,OBJPROP_TIME2, ends);
    ObjectSetDouble( 0,name,OBJPROP_PRICE1,bottom);
    ObjectSetDouble( 0,name,OBJPROP_PRICE2,top);
    ObjectSetInteger(0,name,OBJPROP_COLOR, clrChocolate);
    ObjectSetInteger(0,name,OBJPROP_STYLE, STYLE_DASHDOT);
    ObjectSetInteger(0,name,OBJPROP_WIDTH, 1);
    ObjectSetInteger(0,name,OBJPROP_FILL,  false);

    if(_Period == 60){
       double entryPrice=bottom-3*_Point; 
       double stopLoss=top; 
       double slDist=fabs(entryPrice-stopLoss); 
       double dTakeProfit=entryPrice-2*slDist;
       int    ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, entryPrice,0,stopLoss,dTakeProfit,"SellOrder",magicnumber,0,Red);
    }

    return true;
}

void OnDeinit(const int reason){ObjectsDeleteAll(0,prefix);}

void OnTick()
{
    if(!isNewBar())
        return;     // not necessary but waste of time to check every second        
    showRectangles();
}

bool isNewBar()
{
   static datetime lastbar;
          datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(lastbar != curbar)
   {
      lastbar = curbar;
      return true;
   }
   return false;
}
Imgur
Imgur
  • 2019.08.14
  • imgur.com
Post with 2 views.
 
Jackery:

My code below places sell pending orders when certain candle pattern conditions are met on the H_1 chart. At certain points the condition is met but the pending orders are not placed, as in the encircled portions in https://imgur.com/a/3pNC7M3 . I am not sure what's causing it.

Did you check these?

MarketInfo(_Symbol,MODE_STOPLEVEL);

When limit (price - Bid) and your stoploss/takeprofit is less than stoplevel, OrderSend() will fail.

 
Seng Joo Thio:

Did you check these?

When limit (price - Bid) and your stoploss/takeprofit is less than stoplevel, OrderSend() will fail.

Thanks. I adjusted the Period condition code in the following way and it seems like the send orders always fail. Even on a weekly period that the difference in the entry price and stop loss is very large. How can I solve this? Is there a way I could delay the pending order or try after a new bar is formed till it goes through? Or what's the recommended way of solving this kind of issue?


if(_Period == 10080){
   double entryPrice=bottom-3*_Point; 
   double stopLoss=top; 
   double slDist=fabs(entryPrice-stopLoss); 
   double dTakeProfit=entryPrice-2*slDist;
   int ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, entryPrice,0, stopLoss,dTakeProfit,"SellOrder",magicnumber,0,Red);
   
   double val = MarketInfo(_Symbol,MODE_STOPLEVEL);
   double bpDiff = entryPrice - Bid;
   
   if (bpDiff < val || slDist < val)   {
      Print("stop level caused order to fail = "+failCount);
      failCount++;
   }
 

returning both true and false seems odd

bool isBearishEngulfing(int current)
{   
    if(  (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current    )) 
      && (iClose(_Symbol,0,current + 1) > iOpen( _Symbol,0,current + 1)) 
      && (iOpen( _Symbol,0,current    ) > iClose(_Symbol,0,current + 1)) 
      && (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current + 1))
         )
          return true;////??
    return false; ////??     
}
 
Kenneth Parling:

returning both true and false seems odd

Okay, actually I think putting it in curly braces is better, but I don't think it really changes the result since true will be returned if the condition is satisfied and false if not. This seems to be doing exactly what I want it to do. Unless I don't fully understand your comment.
 
Jackery:
Okay, actually I think putting it in curly braces is better, but I don't think it really changes the result since true will be returned if the condition is satisfied and false if not. This seems to be doing exactly what I want it to do. Unless I don't fully understand your comment.

well i think using 'else' would be better. If it's not true then it needs to return false ;). Your code do not tell what to do it if it's false, it runs both,first telling it's true and then direct after it's false- it can't work out. Well this is my opinion

 
bool isBearishEngulfing(int current)
{   
    if(  (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current    )) 
      && (iClose(_Symbol,0,current + 1) > iOpen( _Symbol,0,current + 1)) 
      && (iOpen( _Symbol,0,current    ) > iClose(_Symbol,0,current + 1)) 
      && (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current + 1)))
      return(true);
      else return(false);     
}

maybe this is better

 
Kenneth Parling:

maybe this is better

Thanks. Do you have any thoughts on how to solve my pending order issues?
 
Jackery:
Thanks. Do you have any thoughts on how to solve my pending order issues?

what about them? is there an issue with entry price maybe...


for a sell limit order the entry price would be for example;

Bid + distance to price, or if the broker uses a dynamic stops level then it would be with the use of MODE_STOPLEVEL Bid + Stops Level + distance

 
Kenneth Parling:

what about them? is there an issue with entry price maybe...


for a sell limit order the entry price would be for example;

Bid + distance to price, or if the broker uses a dynamic stops level then it would be with the use of MODE_STOPLEVEL Bid + Stops Level + distance

I don't fully get this.

What should my OrderSend() precisely be in the code below

double entryPrice=bottom-3*_Point; 
   double stopLoss=top; 
   double slDist=fabs(entryPrice-stopLoss); 
   double dTakeProfit=entryPrice-2*slDist;
   int ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, entryPrice,0, stopLoss,dTakeProfit,"SellOrder",magicnumber,0,Red);
   

for both a SELLLIMIT and a SELLSTOP?

 
Jackery:

Thanks. I adjusted the Period condition code in the following way and it seems like the send orders always fail. Even on a weekly period that the difference in the entry price and stop loss is very large. How can I solve this? Is there a way I could delay the pending order or try after a new bar is formed till it goes through? Or what's the recommended way of solving this kind of issue?

Assuming that the price you're setting SellLimit is above Bid in the first place, just that the Bid and entryprice are too close to each other (in which case using higher timeframe does not necessarily widen the gap between entry price and stop loss)... then I have these two thoughts:

(1) As you mentioned, one way is to delay the pending order - you can always keep track of un-sent pending order with a global array of prices, for instance. Then you scan through this array in later ticks to set pending order for those that qualify.

or

(2) since your Bid is already so close to your entryprice, how about opening a Sell straightaway?

Reason: