iHighest and iLowest buffer values not saved

 

Hi everyone,

I am a long time reader, new poster, so please be gentle.

This is the first indi that I have tried to write from scratch as I always pay for the service and then tinker with the code after, but I figured that it can't be that difficult for such a simple indi.

I am try to create a momentum/range indicator by plotting the price difference between the iHighest and iLowest of X bars back and if the price is over a specific value then to mark it as trending and if not then the market is ranging.  (This works rather well on renko charts.)

What I am getting is that the same value (the latest value) is plotted for the entire indi.  I have looked at different indicators to try and figure this out, but I must either be missing something very small which I do not see, or something fundamental is amiss.


Any help would be highly appreciated.


//+------------------------------------------------------------------+
//|                                                        Range.mq4 |
//|                                                 Rhy Bezuidenhout |
//|                                           https://www.beztec.com |
//+------------------------------------------------------------------+
#property copyright "Rhy Bezuidenhout"
#property link      "https://www.beztec.com"
#property version   "1.00"
#property strict

//--- indicator settings
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_color1 Silver
//--- plot Trend
#property indicator_label1  "Trending"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrLime
#property indicator_style1  STYLE_SOLID
#property indicator_width1  4
#property indicator_label2  "Ranging"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  4

//--- input parameter
input int      BarsBack       = 200; // Bars back
input double   RangingLimit   = 1;   // Ranging below
input int      ColorRange     = clrRed;
input int      ColorTrend     = clrLime;

//--- buffers
double RangingBuffer[];
double TrendBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit(void)
  {
   IndicatorBuffers(2);
   IndicatorDigits(Digits);
   
//--- indicator line
   SetIndexBuffer(0,RangingBuffer);
   SetIndexBuffer(1,TrendBuffer);
   
//--- name for DataWindow and indicator subwindow label
   string short_name="Range ("+IntegerToString(BarsBack)+","+DoubleToString(RangingLimit)+")";
   IndicatorShortName(short_name);
  }
//+------------------------------------------------------------------+
//| Ranging Market                                                   |
//+------------------------------------------------------------------+
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 limit=rates_total-prev_calculated;

   if(rates_total<=BarsBack)
      return(0);

   if(prev_calculated>0)
      limit++;
      
   for(int i=0; i<limit; i++)
     {      
      int highest = iHighest(NULL,0,MODE_HIGH,BarsBack,1);
      int lowest = iLowest(NULL,0,MODE_LOW,BarsBack,1);
                    
      double diff = High[highest] - Low[lowest];
         
      if(diff > RangingLimit){
         TrendBuffer[i]= diff*1000; // x1000 to make it easier to see 
         RangingBuffer[i]= 0;
      }else{
         TrendBuffer[i]= 0;
         RangingBuffer[i]= diff*1000; // x1000 to make it easier to see
      }
     }
     
   return(rates_total);   
}
//+------------------------------------------------------------------+
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
  1.       int highest = iHighest(NULL,0,MODE_HIGH,BarsBack,1);
          int lowest = iLowest(NULL,0,MODE_LOW,BarsBack,1);
    Why are you looking at bar one, and storing the result in all bars (i).
  2. Your lookback is BarsBack. Do your lookbacks correctly.
 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
Thanks Keith.  I do apologize and thanks for moving it.
 
William Roeder:
  1. Why are you looking at bar one, and storing the result in all bars (i).
  2. Your lookback is BarsBack. Do your lookbacks correctly.

Hi William,

Thanks very much for your time. I suspected it was something small that I was overlooking.