#// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
#// © Mango2Juice
#
https://www.tradingview.com/script/27DO5DG7-Range-Identifier/
#study("Range Identifier")
# converted by SAM4COK 07/2022
declare lower;
#// Inputs
input Source = close; # "Source", input.source)
input maType = {default WMA, SMA, EMA, DEMA, SMMA, HMA, LSMA, TEMA, Kijun, McGinley};
input maFastLength = 3; # "MA Fast Length", input.integer)
input maSlowLength = 24; # "MA Slow Length", input.integer)
input atrLen1 = 3; # "ATR Period 1", input.integer)
input atrLen2 = 24; # "ATR Period 2", input.integer)
input CustomTreshold = no; # "Custom Treshold ? Default is Auto.", input.bool)
input TresholdValue = 0.1; # "Custom Treshold", input.float, minval = 0.001)
input showBG = yes; # "Show Background Color?")
def na = Double.nan;
def agg = GetAggregationPeriod();
#// Moving Averages
#McGinley(src, len)
script McGinley {
input src = close;
input len = 0;
def McGinley = McGinley[1] + (src - McGinley[1]) / (len * Power(src / McGinley[1], 4));
def MDI = ExpAverage(src, len)[1];
def mg = if IsNaN(MDI) then ExpAverage(src, len) else
MDI[1] + (src - MDI[1]) / (len * Power(src / MDI[1], 4));
plot result = mg;
}
#ma(type, src, len) =>
script ma {
input Type = "WMA";
input src = close;
input len = 0;
def mg;
def e = ExpAverage(src, len);
def w = WMA(src, len);
mg =
if Type == "SMA" then SimpleMovingAvg(src, len) else
if Type == "EMA" then ExpAverage(src, len) else
if Type == "DEMA" then 2 * e - ExpAverage(e, len) else
if Type == "TEMA" then 3 * (e - ExpAverage(e, len)) + ExpAverage(ExpAverage(e, len), len) else
if Type == "WMA" then WMA(src, len) else
if Type == "SMMA" then if IsNaN(w[1]) then SimpleMovingAvg(src, len) else (w[1] * (len - 1) + src) / len else
if Type == "HMA" then WMA(2 * WMA(src, len / 2) - WMA(src, len), Round(Sqrt(len))) else
if Type == "LSMA" then Inertia(src, len) else
if Type == "Kijun" then (Lowest(src,len) + Highest(src,len)) / 2 else McGinley(src, len);
# if IsNaN(mg[1]) then ExpAverage(src, len) else mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4));
plot result = mg;
}
def treshold = if agg == AggregationPeriod.MONTH or agg == AggregationPeriod.WEEK then 0.5 else
if agg == AggregationPeriod.DAY then 0.4 else
if agg == AggregationPeriod.FOUR_HOURS then 0.14 else
if agg == AggregationPeriod.HOUR then 0.08 else
if agg == AggregationPeriod.THIRTY_MIN then 0.05 else
if agg == AggregationPeriod.FIFTEEN_MIN then 0.04 else
if agg == AggregationPeriod.FIVE_MIN then 0.02 else
if agg == AggregationPeriod.MIN then 0.01 else na;
def ma1 = 100 * (ma(maType, Source, maFastLength) - ma(maType, Source, maSlowLength)) * atr(atrLen1) + 0.00001;
def ma2 = ma1 / ma(maType, Source, maSlowLength) / atr(atrLen2);
def range = (exp(2.0*ma2) - 1.0) / (exp(2.0 * ma2) + 1.0);
#// Plots
def Color = if range >= (if CustomTreshold then TresholdValue else treshold) then 1 else if range <= -(if CustomTreshold then TresholdValue else treshold) then -1 else 0;
plot Line = range; #, "Range", c_range, 4, plot.style_line)
plot LineUp = if CustomTreshold then TresholdValue else treshold; #), "Upper Treshold", color.gray, 1, plot.style_circles)
plot LineDn = -(if CustomTreshold then TresholdValue else treshold); #), "Lower Treshold", color.gray, 1, plot.style_circles)
#bgcolor(i_showBG ? c_background : na)
Line.SetStyle(Curve.FIRM);
Line.SetLineWeight(2);
Line.AssignValueColor(if Color > 0 then color.LIME else
if Color < 0 then color.RED else Color.GRAY);
Line.SetdefaultColor(Color.GRAY);
LineUp.SetStyle(Curve.SHORT_DASH);
LineUp.SetDefaultColor(Color.GRAY);
LineUp.SetLineWeight(1);
LineDn.SetStyle(Curve.SHORT_DASH);
LineDn.SetDefaultColor(Color.GRAY);
LineDn.SetLineWeight(1);
addcloud(if showBG then if Color > 0 then Double.POSITIVE_INFINITY else na else na, Double.NEGATIVE_INFINITY, Color.DARK_GREEN);
addcloud(if showBG then if Color < 0 then Double.POSITIVE_INFINITY else na else na, Double.NEGATIVE_INFINITY, Color.DARK_RED);
#### END CODE ####