hlr_histogram
Download scriptHi-Lo Range Oscillator Overview
The Hi-Lo Range Oscillator is a visual trading tool designed to help market participants understand the relationship between price action and historical volatility. Instead of simply looking at where the price is moving, this indicator measures where the current price sits relative to its recent price boundaries (the highest highs and lowest lows).
How the Logic Works
The indicator operates by analyzing a specific period of historical data (determined by your settings) to establish a "trading range." For every candle on your chart, it performs the following steps:
- Identifying Boundaries: It looks back over a set number of bars to find the highest price reached (High) and the lowest price reached (Low).
- Calculating the Midpoint: It finds the exact center point between that High and Low.
- Determining Relative Position: The script calculates where the current price sits within that range. It converts this into a percentage value:
- If the price is at the very top of the range, the value nears 100.
- If it is at the very bottom, the value nears 0.
- If it is exactly in the middle, the value is 50.
- Visualizing via Histogram: These values are plotted as a histogram in a separate window below your main price chart, allowing you to see fluctuations in "momentum" and "relative position" clearly.
Dynamic Color Coding (Trend Tracking)
The indicator does more than just provide a number; it uses a multi-color system to help identify shifts in market sentiment based on whether the price breaks out of the "High" or "Low" levels:
- Upper Zone Breakouts: When the price moves into the high end of the range (above your specified "High Level"), the histogram changes color. These colors indicate whether the momentum is strengthening or just beginning to push upward.
- Lower Zone Breakouts: When the price drops into the lower end of the range (below your "Low Level"), the colors shift again to highlight downward pressure and potential exhaustion.
- Neutral Zone: When the price stays within the middle boundaries, the indicator remains in a neutral state, signifying a period of consolidation or balanced price action.
Summary for the User
Think of this tool as a "location tracker" for price. Rather than telling you exactly where the price will go next, it tells you where the price is currently located within its recent history. By watching the histogram move toward the top or bottom and observing the color changes, you can better identify periods of overextension (when prices are stretched too far) or potential reversals when the momentum starts to slow down.
//+------------------------------------------------------------------+
//| HLR_Histogram.mq5 |
//| Copyright © 2007, Alexandre |
//| http://www.kroufr.ru/content/view/1184/124/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Alexandre"
#property link "http://www.kroufr.ru/content/view/1184/124/"
#property description "Hi-Lo Range Oscillator"
//---- íîìåð âåðñèè èíäèêàòîðà
#property version "1.00"
//---- îòðèñîâêà èíäèêàòîðà â îòäåëüíîì îêíå
#property indicator_separate_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ 3
#property indicator_buffers 3
//---- èñïîëüçîâàíî îäíî ãðàôè÷åñêîå ïîñòðîåíèå
#property indicator_plots 1
//+----------------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà |
//+----------------------------------------------+
//---- îòðèñîâêà èíäèêàòîðà â âèäå ãèñòîãðàììû
#property indicator_type1 DRAW_COLOR_HISTOGRAM2
//---- â êà÷åñòâå öâåòîâ èíäèêàòîðà èñïîëüçîâàíû
#property indicator_color1 clrLime,clrDarkTurquoise,clrMediumPurple,clrDarkOrange,clrMagenta
//---- ëèíèÿ èíäèêàòîðà - ñïëîøíàÿ
#property indicator_style1 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà ðàâíà 2
#property indicator_width1 2
//---- îòîáðàæåíèå ìåòêè èíäèêàòîðà
#property indicator_label1 "Hi-Lo Range Oscillator"
//+----------------------------------------------+
//| îáúÿâëåíèå êîíñòàíò |
//+----------------------------------------------+
#define RESET 0 // Êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷åò èíäèêàòîðà
#define MIDLLE 50 // Êîíñòàíòà ñåðèäèíû äèàïîçîíà èíäèêàòîðà èíäèêàòîðà
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà |
//+----------------------------------------------+
input uint HLR_Range=40; // Ïåðèîä óñðåäíåíèÿ èíäèêàòîðà
input uint HighLevel=80; // Óðîâåíü ïåðåêóïëåííîñòè
input uint LowLevel=20; // Óðîâåíü ïåðåïðîäàííîñòè
input int Shift=0; // Ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ
//+----------------------------------------------+
//---- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå áóäóò â
// äàëüíåéøåì èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double UpBuffer[],DnBuffer[],ColorBuffer[];
//---- Îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
int min_rates_total;
//+------------------------------------------------------------------+
//| HLR indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//---- Èíèöèàëèçàöèÿ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
min_rates_total=int(HLR_Range+1);
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
SetIndexBuffer(0,UpBuffer,INDICATOR_DATA);
//---- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà ïî ãîðèçîíòàëè íà InpKijun
PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
ArraySetAsSeries(UpBuffer,true);
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
SetIndexBuffer(1,DnBuffer,INDICATOR_DATA);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
ArraySetAsSeries(DnBuffer,true);
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â öâåòîâîé, èíäåêñíûé áóôåð
SetIndexBuffer(2,ColorBuffer,INDICATOR_COLOR_INDEX);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
ArraySetAsSeries(ColorBuffer,true);
//---- èíèöèàëèçàöèè ïåðåìåííîé äëÿ êîðîòêîãî èìåíè èíäèêàòîðà
string shortname;
StringConcatenate(shortname,"Hi-Lo Range Oscillator Histogram(",HLR_Range,", ",Shift,")");
//--- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
IndicatorSetInteger(INDICATOR_DIGITS,0);
//---- êîëè÷åñòâî ãîðèçîíòàëüíûõ óðîâíåé èíäèêàòîðà 3
IndicatorSetInteger(INDICATOR_LEVELS,3);
//---- çíà÷åíèÿ ãîðèçîíòàëüíûõ óðîâíåé èíäèêàòîðà
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,HighLevel);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,MIDLLE);
IndicatorSetDouble(INDICATOR_LEVELVALUE,2,LowLevel);
//---- â êà÷åñòâå öâåòîâ ëèíèé ãîðèçîíòàëüíûõ óðîâíåé èñïîëüçîâàíû
IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrBlue);
IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrGray);
IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrMagenta);
//---- â ëèíèè ãîðèçîíòàëüíîãî óðîâíÿ èñïîëüçîâàí êîðîòêèé øòðèõ-ïóíêòèð
IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASHDOTDOT);
IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DASHDOTDOT);
IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DASHDOTDOT);
//---- çàâåðøåíèå èíèöèàëèçàöèè
}
//+------------------------------------------------------------------+
//| HLR 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[]
)
{
//---- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷åòà
if(rates_total<min_rates_total) return(RESET);
//---- Îáúÿâëåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé
double m_pr,HH,LL,HL,res;
//---- Îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ è ïîëó÷åíèå óæå ïîñ÷èòàííûõ áàðîâ
int limit,bar,trend;
static int trend_prev;
//---- ðàñ÷åòû íåîáõîäèìîãî êîëè÷åñòâà êîïèðóåìûõ äàííûõ è
//ñòàðòîâîãî íîìåðà limit äëÿ öèêëà ïåðåñ÷åòà áàðîâ
if(prev_calculated>rates_total || prev_calculated<=0)// ïðîâåðêà íà ïåðâûé ñòàðò ðàñ÷åòà èíäèêàòîðà
{
limit=rates_total-min_rates_total-1; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà âñåõ áàðîâ
trend_prev=0;
}
else limit=rates_total-prev_calculated; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà íîâûõ áàðîâ
//---- èíäåêñàöèÿ ýëåìåíòîâ â ìàññèâàõ, êàê â òàéìñåðèÿõ
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
//---- ïåðåñ÷åò çíà÷åíèé èíäèêàòîðîâ
for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
HH=high[ArrayMaximum(high,bar,HLR_Range)];
LL=low [ArrayMinimum(low, bar,HLR_Range)];
m_pr=(high[bar]+low[bar])/2.0;
HL=HH-LL;
if(HL) res=100.0*(m_pr-LL)/(HL);
else res=MIDLLE;
UpBuffer[bar]=res;
DnBuffer[bar]=MIDLLE;
int clr=2;
trend=trend_prev;
if(res>HighLevel)
{
if(trend_prev<0) clr=0;
else clr=1;
trend=+1;
}
else if(res<LowLevel)
{
if(trend_prev>0) clr=4;
else clr=3;
trend=-1;
}
ColorBuffer[bar]=clr;
if(bar && trend) trend_prev=trend;
}
//----
return(rates_total);
}
//+------------------------------------------------------------------+
Comments