Author Message:
The Range Detector indicator aims to detect and highlight intervals where prices are ranging. The extremities of the ranges are highlighted in real-time, with breakouts being indicated by the color changes of the extremities.
More Details : https://www.tradingview.com/v/QOuZIuvH/
CODE:
CSS:
#https://www.tradingview.com/v/QOuZIuvH/
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International
#// © LuxAlgo
#indicator("Range Detector [LuxAlgo]", "LuxAlgo - Range Detector", overlay = true, m
# Converted by Sam4Cok@Samer800 - 10/2023 - Not Typical
#//Settings
input highlightRangeStart = yes;
input RangeDetectMethod = {Default "Simple Average", "Median Average"};
input source = close;
input MinimumRangeLength = 20;
input RangeWidth = 1.0; # 'Range Width'
input atrLength = 500; # 'ATR Length'
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def len = MinimumRangeLength;
def last = isNaN(close);
def sma = RangeDetectMethod==RangeDetectMethod."Simple Average";
def sMTF = source;
def hMTF = high;
def lMTF = low;
def cMTF = close;
#-- Colors
DefineGlobalColor("up", CreateColor(0,165,255));
DefineGlobalColor("dn", CreateColor(255,0,148));
DefineGlobalColor("dup", CreateColor(0, 89, 137));
DefineGlobalColor("ddn", CreateColor(137, 0, 80));
DefineGlobalColor("No", CreateColor(254,165,0));
DefineGlobalColor("dNo", CreateColor(136,89,0));
#//Detect and highlight ranges
#//Ranges drawings
def bxTop;# = na
def bxBot;
def lvl;# = na
def max;# = na
def min;# = na
def detect_css;
def os;# = 0
def os1;
def bxRight;
def n = BarNumber();
def tr = TrueRange(hMTF, cMTF, lMTF);
def nATR = WildersAverage(tr, atrLength) * RangeWidth;
def ma1 = Average(sMTF, len);
def ma2 = Median(sMTF, len);
def MovAvg = if sma then ma1 else ma2;
def ma = MovAvg;
def count = fold i = 0 to len with p do
p + (if AbsValue(sMTF[i] - ma) > nATR then 1 else 0);
if count == 0 and count != count[1] {
if (n - len) <= bxRight[1] {
max = max(ma + nATR, max[1]);
min = min(ma - nATR, if(min[1], min[1], ma));
lvl = (max + min) / 2;
bxRight = bxRight[1];
bxTop = max;
bxBot = min;
os = 0;
detect_css = na;
} else {
max = ma + nATR;
min = ma - nATR;
lvl = ma;
bxRight = n;
bxTop = max;
bxBot = min;
detect_css = 1;
os = 0;
}
} else {
max = max[1];
min = min[1];
lvl = lvl[1];
bxRight = if count == 0 then n else bxRight[1];
bxTop = max;
bxBot = min;
os = os1[1];
detect_css = na;
}
#//Set color
if sMTF > bxTop {
os1 = 1;
} else if sMTF < bxBot {
os1 = -1;
} else {
os1 = os;
}
def Dir = os1[-len];
def Dir1 = Dir[-len];
def Rng = !isNaN(detect_css);
def RngStart = Rng and !Rng[1];
def bardetect = if RngStart then n+1 else bardetect[1];
def barEnd = bardetect + len;
def condEnd = n < barEnd;
plot UpLine = if !last and bxTop then bxTop else na;
plot LoLine = if !last and bxBot then bxBot else na;
UpLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LoLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
UpLine.AssignValueColor(if os1 > 0 then GlobalColor("up") else
if os1 < 0 then GlobalColor("dn") else GlobalColor("No"));
LoLine.AssignValueColor(if os1 > 0 then GlobalColor("up") else
if os1 < 0 then GlobalColor("dn") else GlobalColor("No"));
plot avg = if !last and lvl then lvl else na;
Avg.SetStyle(Curve.SHORT_DASH);
avg.SeTPaintingStrategy(PaintingStrategy.HORIZONTAL);
avg.AssignValueColor(if os1 > 0 then GlobalColor("dup") else
if os1 < 0 then GlobalColor("ddn") else GlobalColor("dNo"));
plot PreAvg = if !last and lvl[-len] then lvl[-len] else na;
PreAvg.SetStyle(Curve.POINTS);
PreAvg.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreAvg.AssignValueColor(if Dir1 > 0 then GlobalColor("dup") else
if Dir1 < 0 then GlobalColor("ddn") else GlobalColor("dNo"));
def PreHi = if last[len] then na else bxTop;
def PreLo = if last[len] then na else bxBot;
AddCloud(if Dir1>0 then PreHi[-len] else na,
if condEnd[-len] then PreLo[-len] else na, GlobalColor("dup"));
AddCloud(if Dir1<0 then PreHi[-len] else na,
if condEnd[-len] then PreLo[-len] else na, GlobalColor("ddn"));
AddCloud(if Dir1== 0 then PreHi[-len] else na,
if condEnd[-len] then PreLo[-len] else na, GlobalColor("dNo"));
def bgCond = if !highlightRangeStart then 0 else
if RngStart then 1 else
if RngStart[-1] then 1 else 0;
AddCloud(if bgCond then pos else na, neg, Color.GRAY);
#-- END of CODE