// CandleRange.mq4 — bar range, coloured against its own average #property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 clrSteelBlue #property indicator_color2 clrIndianRed input int AvgPeriod = 20; double normal[], elevated[]; int OnInit() { SetIndexBuffer(0, normal); SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, 2); SetIndexBuffer(1, elevated); SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 2); IndicatorShortName("Range(" + IntegerToString(AvgPeriod) + ")"); return(INIT_SUCCEEDED); } 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 = (prev_calculated == 0) ? rates_total - AvgPeriod - 1 : rates_total - prev_calculated; for(int i = limit; i >= 0; i--) // 0 = current bar, counting back in time { double range = (High[i] - Low[i]) / Point; double sum = 0; for(int k = 1; k <= AvgPeriod; k++) sum += (High[i+k] - Low[i+k]) / Point; double avg = sum / AvgPeriod; normal[i] = (range <= avg) ? range : EMPTY_VALUE; elevated[i] = (range > avg) ? range : EMPTY_VALUE; } return(rates_total); }