Range Identifier Indicator For ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
https://www.tradingview.com/script/27DO5DG7-Range-Identifier/
//@version=4
study("Range Identifier")

// Inputs
i_maSource = input(close, "Source", input.source)
i_maType = input("WMA", "MA Type", input.string, options = ["EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMA", "SMMA", "HMA", "LSMA", "Kijun", "McGinley"])
i_maLen1 = input(3, "MA Fast Length", input.integer)
i_maLen2 = input(24, "MA Slow Length", input.integer)
i_atrLen1 = input(3, "ATR Period 1", input.integer)
i_atrLen2 = input(24, "ATR Period 2", input.integer)
i_treshold = input(false, "Custom Treshold ? Default is Auto.", input.bool)
i_custom = input(0.1, "Custom Treshold", input.float, minval = 0.001)
i_showBG = input(true,"Show Background Color?")

// Moving Averages
ma(type, src, len) =>
float result = 0
if type=="SMA" // Simple
result := sma(src, len)
if type=="EMA" // Exponential
result := ema(src, len)
if type=="DEMA" // Double Exponential
e = ema(src, len)
result := 2 * e - ema(e, len)
if type=="TEMA" // Triple Exponential
e = ema(src, len)
result := 3 * (e - ema(e, len)) + ema(ema(e, len), len)
if type=="WMA" // Weighted
result := wma(src, len)
if type=="VWMA" // Volume Weighted
result := vwma(src, len)
if type=="SMMA" // Smoothed
w = wma(src, len)
result := na(w[1]) ? sma(src, len) : (w[1] * (len - 1) + src) / len
if type == "RMA"
result := rma(src, len)
if type=="HMA" // Hull
result := wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))
if type=="LSMA" // Least Squares
result := linreg(src, len, 0)
if type=="Kijun" //Kijun-sen
kijun = avg(lowest(len), highest(len))
result :=kijun
if type=="McGinley"
mg = 0.0
mg := na(mg[1]) ? ema(src, len) : mg[1] + (src - mg[1]) / (len * pow(src/mg[1], 4))
result :=mg
result

// Auto Set Treshold
float treshold = na
if timeframe.period == "M" or timeframe.period == "W"
treshold := 0.5
else
if timeframe.period == "D"
treshold := 0.4
else
if timeframe.period == "240"
treshold := 0.14
else
if timeframe.period == "60"
treshold := 0.08
else
if timeframe.period == "30"
treshold := 0.05
else
if timeframe.period == "15"
treshold := 0.04
else
if timeframe.period == "5"
treshold := 0.02
else
if timeframe.period == "1"
treshold := 0.01

ma1 = 100 * (ma(i_maType, i_maSource, i_maLen1) - ma(i_maType, i_maSource, i_maLen2)) * atr(i_atrLen1) + 0.00001
ma2 = ma1 / ma(i_maType, i_maSource, i_maLen2) / atr(i_atrLen2)
range = (exp(2.0*ma2) - 1.0) / (exp(2.0 * ma2) + 1.0)

// Plots
c_range = range >= (i_treshold ? i_custom : treshold) ? color.lime : range <= -(i_treshold ? i_custom : treshold) ? color.red : color.gray
c_background = range >= (i_treshold ? i_custom : treshold) ? color.lime : range <= -(i_treshold ? i_custom : treshold) ? color.red : color.gray

plot(range, "Range", c_range, 4, plot.style_line)
plot((i_treshold ? i_custom : treshold), "Upper Treshold", color.gray, 1, plot.style_circles)
plot(-(i_treshold ? i_custom : treshold), "Lower Treshold", color.gray, 1, plot.style_circles)
bgcolor(i_showBG ? c_background : na)
here we go - pls test it

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

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
444 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top