Range Identifier

sam4cok

Member
Platform
  1. Thinkorswim
hope can be useful :)

#// 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 ####
 

barbaros

Administrator
Staff member
I like this...added bar coloring

Code:
#// 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
# added bar color by barbaros 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?")
input colorBars = yes; # "Color candlesticks with the trend"

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);

AssignPriceColor(if !colorBars then Color.CURRENT else if Color > 0 then color.LIME else if Color < 0 then color.RED else Color.GRAY);
### END Code ####
 
I like this...added bar coloring

Code:
#// 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
# added bar color by barbaros 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?")
input colorBars = yes; # "Color candlesticks with the trend"

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);

AssignPriceColor(if !colorBars then Color.CURRENT else if Color > 0 then color.LIME else if Color < 0 then color.RED else Color.GRAY);
### END Code ####
thank you very much @sam4cok and @barbaros
 

DeepThinker

New member
@sam4cok and @barbaros
Thank you for your contribution.
Sharing few thoughts based on the collected observations,
a) could benefit from the "zero" line
b) since it is a lower indicator, coloring of the curve, would make it more efficient, vs trying to overload "upper frame" (that is busy enough)

Happy to hear your thoughts and test further changes.
 

barbaros

Administrator
Staff member
Since the bar coloring is an option, you can turn it off, so #b is not an issue.

Although zero line might make sense since it oscillates, the range band is more important. It tries to filter trades out when the value is in between the bands. Zero line can be added for visual purposes of course.
 

barbaros

Administrator
Staff member
@DeepThinker there is a predetermined timeframe selection in the script. If the chart timeframe is not in the selection, you will need to turn on CustomTreshold and set it manually.
 

DeepThinker

New member
@DeepThinker there is a predetermined timeframe selection in the script. If the chart timeframe is not in the selection, you will need to turn on CustomTreshold and set it manually.
Thank you for pointing me in the right direction :) 3min AggregationPeriod (that is common for B4 systems) was not available.

Could you suggest the most appropriate value (my guess 0.02?) for the threshold when agg is 3 min ?

I see few close options in the script,

if agg == AggregationPeriod.FIVE_MIN then 0.02 else
if agg == AggregationPeriod.MIN then 0.01 else na;
 

sam4cok

Member
Thank you for pointing me in the right direction :) 3min AggregationPeriod (that is common for B4 systems) was not available.

Could you suggest the most appropriate value (my guess 0.02?) for the threshold when agg is 3 min ?

I see few close options in the script,

if agg == AggregationPeriod.FIVE_MIN then 0.02 else
if agg == AggregationPeriod.MIN then 0.01 else na;
I just converted the script as it is. You can change the "na" value with any number and it will work fine with all timeframe as per the set number.
or you can set it manually as mentioned by @barbaros .

* change na with any value. ie. 0.015 or 0.2..etc

if agg == AggregationPeriod.MIN then 0.01 else na;
 
Top