Author Message:
The Predictive Ranges indicator aims to efficiently predict future trading ranges in real-time, providing multiple effective support & resistance levels as well as indications of the current trend direction.
Predictive Ranges was a premium feature originally released by LuxAlgo in 2020.
The feature was discontinued & made legacy, however, due to its popularity and reproduction attempts, we deemed it necessary to release it open source to the community.
CODE:
CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
# https://www.tradingview.com/v/lIdNGLiV/
#// © LuxAlgo
#indicator("Predictive Ranges [LuxAlgo]", "LuxAlgo - Predictive Ranges", overlay = true)
# Converted by Sam4Cok@Samer800 - 07/2023
input showCloud = yes;
input useChartTimeframe = {Default "Yes", "No"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN; # 'Timeframe'
input Source = close; # 'Source'
input atrLength = 200; # 'Length'
input atrMulti = 6.0; # 'Factor'
input rangeMulti = 2.0;
def na = Double.NaN;
DefineGlobalColor("pru", CreateColor(242,54,69));
DefineGlobalColor("dpru", CreateColor(201, 95, 104));
DefineGlobalColor("avg", CreateColor(91,156,246));
DefineGlobalColor("prl", CreateColor(8,153,129));
DefineGlobalColor("dprl", CreateColor(27,134,117));
def src; def hi; def lo;
Switch (useChartTimeframe) {
Case "Yes" : src = Source;
hi = high;
lo = low;
Case "No" : src = close(Period=manualTimeframe);
hi = high(Period=manualTimeframe);
lo = low(Period=manualTimeframe);
}
def tr = TrueRange(hi, src, lo);
#pred_ranges(length, mult)=>
def avg;
def ATRtf = WildersAverage(tr, atrLength);
def nATR = if(isNaN(ATRtf), 0, ATRtf) * atrMulti;
def avg_ = if (isNaN(avg[1]) or avg[1]==0) then src else avg[1];
avg = if src - avg_ > nATR then avg_ + nATR else
if avg_ - src > nATR then avg_ - nATR else avg_;
def hold_atr = if avg != avg[1] then nATR / 2 else hold_atr[1];
def prR2 = avg + hold_atr * rangeMulti;
def prR1 = avg + hold_atr;
def avg0 = avg;
def prS1 = avg - hold_atr;
def prS2 = avg - hold_atr * rangeMulti;
#//Plots
plot plot_pru2 = if avg!=avg[1] then na else prR2;#, 'PR Upper 2'
plot plot_pru1 = if avg!=avg[1] then na else prR1;#, 'PR Upper 1'
plot plot_pravg = if avg!=avg[1] then na else avg0;# , 'PR Average'
plot plot_prl1 = if avg!=avg[1] then na else prS1;#, 'PR Lower 1'
plot plot_prl2 = if avg!=avg[1] then na else prS2;#, 'PR Lower 2'
plot_pru2.SetDefaultColor(GlobalColor("pru"));
plot_pru1.SetDefaultColor(GlobalColor("pru"));
plot_pravg.SetDefaultColor(GlobalColor("avg"));
plot_prl1.SetDefaultColor(GlobalColor("prl"));
plot_prl2.SetDefaultColor(GlobalColor("prl"));
#//Fills
AddCloud(if !showCloud or avg0!=avg0[1] then na else plot_pru2, plot_pru1, GlobalColor("dpru"));
AddCloud(if !showCloud or avg0!=avg0[1] then na else plot_prl1, plot_prl2, GlobalColor("dprl"));
#-- END of CODE