Help with logic, detect touches in the rectangle

 

Hello guys, im doing this to learn and code an indicator that i want, im a newbie and im doing what i can to learn this..

Well, my problem in this code is i want to put an arrow after a touch in the rectangle, but the arrow appears everywhere. The arrow is just to see if i done the right thing.. 

My definition of a touch is, in this case i was looking at a resistance perspective, if the High enter in the area between xkl1 and xkl2 and close bellow xkl2 it would be a touch. Dont matter how many candles it takes to close bellow the area, the main thing is to not close above xkl1... In the second loop i use the number 25 as a limit just because it was just faster for me then search the bar index, because i dont know how do it properly i try it with iBarshift but doesnt work how i though it was. 

Thanks for your time and patience, sorry about my poor english.

 

//+------------------------------------------------------------------+
//|                                                        qtest.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2
double price[10000];
double date[10000];
int up=0;

double ext1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
SetIndexBuffer(0,ext1);
SetIndexStyle(0,DRAW_ARROW,clrRed);
SetIndexArrow(0,134);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int i;
   
   double buf=0;
   int counted_bars=IndicatorCounted();
   int limit=(Bars-counted_bars);
   for(i=0; i < limit; i++)
     {
      double atr=iATR(NULL,0,7,i);
      buf=iFractals(NULL,0,MODE_UPPER,i);
      if(buf)
        {
         up++;
         price[up]=iFractals(NULL,0,MODE_UPPER,i);
         date[up]=Time[i];
        }
      ObjectCreate("Box",OBJ_RECTANGLE,0,date[3],price[3],Time[i],(price[3]-atr));
      ObjectSet("Box",OBJPROP_BACK,false);
      ObjectSet("Box",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("Box",OBJPROP_WIDTH,1);
      ObjectSet("Box",OBJPROP_TIME2,Time[0]);
      ObjectSet("Box",OBJPROP_PRICE1,price[3]);
      ObjectSet("Box",OBJPROP_TIME1,date[3]);
      ObjectSet("Box",OBJPROP_PRICE2,(price[3]-atr));
      double xkl2= ObjectGet("Box",OBJPROP_PRICE2);    
      double xkl1=ObjectGet("Box",OBJPROP_PRICE1);
      Print(GetLastError());
    for (int j=0; j<25; j++)
    {  
     if((High[j+1]>xkl2 && High[j+1] < xkl1) && Close[j] < xkl2)
     {
     ext1[j]= Close[j];
     Print(GetLastError());
     }
     }
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
      buf=iFractals(NULL,0,MODE_UPPER,i);
      if(buf)
        {
         up++;
         price[up]=iFractals(NULL,0,MODE_UPPER,i);
         date[up]=Time[i];
        }

I am pretty sure that fractals returns either EMPTY_VALUE or a price

either way, double buf will have a value and if(buf) will return true

 

price[3] is not being updated and is probably EMPTY_VALUE which will take it way off the chart

up is being incremented every tick and will soon be higher than the number of bars on the chart 

To check if you have a new upper fractal, you can use something like

      if(MathAbs(High[i]-iFractals(NULL,0,MODE_UPPER,i))<Point)

Start with something like that to identify the fractals and then build on that. 

 
GumRai:

I am pretty sure that fractals returns either EMPTY_VALUE or a price

Hello GumRai, 

iFractals returns either 0 or a price

(I only know because I looked at it yesterday) 

 
honest_knave:

Hello GumRai, 

iFractals returns either 0 or a price

(I only know because I looked at it yesterday) 

I stand corrected - I should have checked first.

But what I said about price[3] not being updated stands, i believe 

 

Hello GumRai and honest_knave, thank you guys for helping me, the idea worked out perfectly. In my main code im using MathMax to compare values of the previous fractals, so the code with the rectangles worked great!

thank you guys again!! 

Reason: