Range Levels

Download script
Author: Evgeniy Chumakov | © Copyright 2024

This script is designed to automatically identify and display specific price boundaries on your trading chart based on a range you define. Instead of manually guessing where key levels might be, the tool scans your market history to find significant price "zones" that meet your specific criteria.

How It Works

1. Customizable Measurement Units The tool allows you to define the "Range" in two different ways, depending on your trading style:

  • Points: You can set a fixed number of price points (e.g., a 500-point move).
  • Percentage: You can set a specific percentage of price movement (e.g., a 1% move).

2. The Scanning Process Once the script is active, it performs a dynamic search of the historical data available on your chart. It looks backward through time to find the most recent instance where the price fluctuated by the amount you specified in your settings.

For example, if you set a "Percentage" range of 5%, the script scans the history until it finds a period where the gap between the highest high and the lowest low reached at least 5%.

3. Automatic Level Plotting As soon as the script identifies this specific historical range, it automatically draws two horizontal lines on your chart:

  • Range Max: A line marking the top of that identified price zone.
  • Range Min: A line marking the bottom of that identified price zone.

These lines are visually distinct (dotted by default) and can be customized in color through the settings menu.

4. Smart Reset Logic The tool is designed to be "live." It constantly monitors current price action. If the market moves so far that the current price breaks outside of the previously identified Max or Min levels, the script recognizes this as a new market phase. It will then automatically clear the old lines and begin scanning again to find the next valid range that meets your requirements.

Summary of Benefits

  • Objective Analysis: Removes human bias by identifying levels based on hard data rather than "feeling."
  • Dynamic Updates: Automatically updates as the market moves, ensuring the levels shown are always relevant to current price action.
  • Visual Clarity: Instantly identifies the boundaries of significant historical volatility, helping you see where the market has previously reacted.
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
7 Views
0 Downloads
0 Favorites
Range Levels
ÿþ#property copyright "Evgeniy Chumakov | © Copyright 2024"

#property description "The indicator draws the nearest levels of the specified range."

#property version "1.0"

#property link "https://www.mql5.com/en/users/jack857752"

#property strict



#property indicator_chart_window



//------------------------

enum units_of_calculation

{

Points = 0,  // Points

Percent = 1  // Percent

};



input units_of_calculation type_calc = 0; // Units of Calculation

//------------------------



input double InputRange = 500; // Specified Range

input color InputColor = clrYellow; // Color Levels



string MaxStr = "Range Max" + "\n"; // Max Level label when hovering over it

string MinStr = "Range Min" + "\n"; // Min Level label when hovering over it



double Level_Max = 0; // Level Value Max

double Level_Min = 0; // Level Value Min



//+------------------------------------------------------------------+

int OnInit(){



IndicatorSetString(INDICATOR_SHORTNAME,"Range Levels");



// Let's sign the levels completely

string type_calc_str = "";



if(type_calc == 0){type_calc_str = " Points";}

if(type_calc == 1){type_calc_str = " Percent";}



MaxStr = MaxStr + DoubleToStr(InputRange,2) + type_calc_str;

MinStr = MinStr + DoubleToStr(InputRange,2) + type_calc_str;

//-----------------------------------------------------------//



// Let's reset the level values

Level_Max = 0;

Level_Min = 0;

//-----------------------------------------------------------//

    

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

void OnDeinit(const int reason){



// Remove levels from the chart when deinitializing the indicator 

ObjectDelete(MaxStr);

ObjectDelete(MinStr);

//-----------------------------------------------------------//

   

return;

}

//+------------------------------------------------------------------+

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[]){



// Let's check if the current Maximum and Minimum levels have gone beyond a certain range. 

// If so, we'll reset the level values.



if( (Level_Max != 0 || Level_Min != 0) && (high[0] > Level_Max || low[0] < Level_Min)){Level_Max = 0; Level_Min = 0;}



//-----------------------------------------------------------//

   

// Let's find the levels of the specified range.

// First, let's check if there are levels, so as not to overload the system. 

// If there aren't any, then we'll search for levels.

if(Level_Max == 0 || Level_Min == 0){



int depth = iBars(Symbol(),PERIOD_CURRENT); // Scanning depth

bool RangeFound = False; // Event trigger / Range defined



for(int period = 1; period < depth; period++){



double Max = iHigh(Symbol(),PERIOD_CURRENT,iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,period,0)); // Maximum on interval

double Min = iLow(Symbol(),PERIOD_CURRENT,iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,period,0)); // Minimum on interval



double point_value = MathAbs(Max - Min)/Point(); // The range between levels in points.

double percentage_value = ((Max - Min)/Min) * 100.0; // The range between levels in percent.



if(type_calc == 0 && point_value >= InputRange){RangeFound = True; Level_Max = Max; Level_Min = Min; break;}

if(type_calc == 1 && percentage_value >= InputRange){RangeFound = True; Level_Max = Max; Level_Min = Min; break;}

if(period > depth - 2 && RangeFound == False){return(rates_total);} // There is not enough history to determine the specified range.

}

//-----------------------------------------------------------//

   

// Let's display the levels on the chart

string LevMax_Name = MaxStr;



bool LevMax_Line = ObjectCreate(ChartID(),LevMax_Name,OBJ_HLINE,0,0,0,0,0);

ObjectSetInteger(ChartID(),LevMax_Name,OBJPROP_COLOR,InputColor);

ObjectSetInteger(ChartID(),LevMax_Name,OBJPROP_STYLE,STYLE_DOT);

ObjectSetInteger(ChartID(),LevMax_Name,OBJPROP_WIDTH,1);

ObjectSetInteger(ChartID(),LevMax_Name,OBJPROP_HIDDEN,false);

ObjectSetDouble(ChartID(),LevMax_Name,OBJPROP_PRICE1,Level_Max);



string LevMin_Name = MinStr;



bool LevMin_Line = ObjectCreate(ChartID(),LevMin_Name,OBJ_HLINE,0,0,0,0,0);

ObjectSetInteger(ChartID(),LevMin_Name,OBJPROP_COLOR,InputColor);

ObjectSetInteger(ChartID(),LevMin_Name,OBJPROP_STYLE,STYLE_DOT);

ObjectSetInteger(ChartID(),LevMin_Name,OBJPROP_WIDTH,1);

ObjectSetInteger(ChartID(),LevMin_Name,OBJPROP_HIDDEN,false);

ObjectSetDouble(ChartID(),LevMin_Name,OBJPROP_PRICE1,Level_Min);

}

//-----------------------------------------------------------//

   

return(rates_total);

}

//+------------------------------------------------------------------+

Comments