place arrow when bar is larger than previous bar

 

hi

so the title says it all. i would like an arrow to be placed every time this happens: Low[2] > Low[1] && High[2] < High[1]

this is what i came up with, but it only causes mt4 to crash. any help would be appreciated! thank you!

#property  indicator_buffers 1
#property  indicator_color1  White
extern int Period = 24;
double B2[];
int init()

{  

   SetIndexStyle(0,DRAW_ARROW);

   SetIndexArrow(0,234);

   SetIndexBuffer(0,B2);

   SetIndexLabel(0,"arrow");

   return(0);

}
int start()

{ 

int i;

  i = Bars - Period;
   while(i >= 0)
   {
      if(Low[i-2] > Low[i-1] && High[i-2] < High[i-1])
      {
         B2[i]=Low[i];
         i--;
      }
   }
} 
 

What about when High[2] - Low[2] < High[1] - Low[1] ? in this case bar 1 is larger than bar 2

Shouldn't this . . .

if(Low[i-2] > Low[i-1] && High[i-2] < High[i-1])

be this . . .

if(Low[i+1] > Low[i] && High[i+1] < High[i])
 
RaptorUK:

What about when High[2] - Low[2] < High[1] - Low[1] ? in this case bar 1 is larger than bar 2

Shouldn't this . . .

be this . . .


yes, that was a logical error on my part. but even after i changed that line, mt4 still freezes, so there must be a greater problem, than my own stupidity...
 
Show your code . . .
 

Add to the very first line:

#property  indicator_chart_window

Move i-- to the next outer block:

   while(i >= 0)
   {
      if(Low[i-2] > Low[i-1] && High[i-2] < High[i-1])
      {
         B2[i]=Low[i];
      }
      i--;
   }
 
Now that Raptor has pointed out the incorrectly decremented loop counter, you might like to take a look at some of the example indicators and use the IndicatorCounted function so you don't have to recalculate every bar on the chart when a new tick comes in!
 
  1. if(Low[i-2] > Low[i-1] && High[i-2] < High[i-1])
    i-n is the future. If you want to highlight the engulfing bar, your test should be:
    if(Low[i+2] > Low[i+1] && High[i+2] < High[i+1])

  2. int counted = indicatorCounted();
    if (counted < 2) counted = 2; // Looking back 2 bars
    for (int i = Bars - 1 - counted; i >= 0; i--){
    
    Arrow on i, testing candle[i+1] and candle[i+2]
  3. If you want your test to be "place arrow when bar is larger" and place it on the larger bar:
    int counted = indicatorCounted();
    if (counted < 1) counted = 1; // Looking back 1 bar
    for (int i = Bars - 1 - counted; i >= 0; i--){
       double range1 = High[i+1] - Low[i+1],
              range0 = High[i  ] - Low[i  ];
       if (range0 > range1) B2[i] = Low[i];
       else                 B2[i] = EMPTY_VALUE; // Or in init() SetIndexEmptyValue(0,0)
    }

  4. You also have to set the buffer to NOT show arrows when the condition is not true. Either explicitly set it to empty or let it default to zero and use SetIndexEmpty.