Supertrend Channel For ThinkOrSwim

EddieM

New member
Plus
The supertrend indicator is a technical analysis tool that can help make decisions about when to enter and exit a trade.

Everything that you want to know about Supertrends:
https://usethinkscript.com/threads/supertrend-indicator-by-mobius-for-thinkorswim.7/#post-77876

This supertrend adds channels.
Traders use bands and channels in trading to visually identify potential support and resistance levels, gauge market volatility, and assess the direction of a trend, helping them make informed decisions about when to buy or sell an asset by signaling potential entry and exit points within a price range, particularly when the price touches the upper or lower boundaries of the channel or band.

jwSTv1j.png


Original Tradingview code:
https://github.com/fktrdr/trade-ez-4/blob/main/trade-ez-4.pine

ThinkOrSwim code can be found in the next post.

More about this Tradingview OP:
https://usethinkscript.com/threads/steve-kalayjian-indicators.3265/
 
Last edited by a moderator:

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

Can this Tradingview indicator be converted?
https://github.com/fktrdr/trade-ez-4/blob/main/trade-ez-4.pine

More about this Tradingview OP:
https://usethinkscript.com/threads/steve-kalayjian-indicators.3265/
Ruby:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fktrdr

//@version=5
indicator("Supertrend Channel", "Supertrend Channel", false, timeframe="", timeframe_gaps=true)

atr_period = input.int(52, "ATR Length", 1)
atr_method = input.string("RMA", "ATR Method", ["EMA", "RMA", "SMA", "WMA"])
atr_factor = input.float(2.5, "ATR Factor", step = 0.1)
atr_source = input.source(close, "ATR Source")
atr_change = input.bool(true, "ATR Change Track", "Adjust the Stop with the ATR change")
ma_period = input.int(5, "MA Length", 1)
ma_method = input.string("RMA", "MA Method", ["EMA", "RMA", "SMA", "WMA"])
ma_source = input.source(close, "MA Source")

f_ma(source, length, method) =>
    switch method
        "EMA" => ta.ema(source, length)
        "RMA" => ta.rma(source, length)
        "SMA" => ta.sma(source, length)
        => ta.wma(source, length)

tr = math.max(high - low, math.abs(high - atr_source[1]), math.abs(low - atr_source[1]))
atr = f_ma(tr, atr_period, atr_method)

upper_band = close[1] + atr[1] * atr_factor
lower_band = close[1] - atr[1] * atr_factor

upper_band1 = nz(upper_band[1])
lower_band1 = nz(lower_band[1])

upper_band := upper_band < upper_band1 or close[1] > upper_band1 ? upper_band : atr_change ? upper_band1 + (atr[1] - atr[2]) * atr_factor : upper_band1
lower_band := lower_band > lower_band1 or close[1] < lower_band1 ? lower_band : atr_change ? lower_band1 + (atr[2] - atr[1]) * atr_factor : lower_band1

var DIRECTION_UP = 1
var DIRECTION_DN = -1

int stop_direction = na
int trend_direction = na

float st_stop = na

if na(atr[1])
    stop_direction := DIRECTION_UP
else if st_stop[1] == upper_band1
    stop_direction := close[1] > upper_band1 ? DIRECTION_DN : DIRECTION_UP
else
    stop_direction := close[1] < lower_band1 ? DIRECTION_UP : DIRECTION_DN

st_stop := stop_direction == DIRECTION_DN ? lower_band : upper_band

if na(atr[1])
    trend_direction := DIRECTION_UP
else if st_stop == upper_band
    trend_direction := close > upper_band ? DIRECTION_DN : DIRECTION_UP
else
    trend_direction := close < lower_band ? DIRECTION_UP : DIRECTION_DN

st_trend = close
st_trend := ta.crossover(trend_direction, 0) or ta.crossunder(trend_direction, 0) ? close : trend_direction == DIRECTION_DN ? math.max(st_trend[1], close) : math.min(st_trend[1], close)

plot(st_trend, "Trend", color.aqua, 3, plot.style_line)
plot(st_stop, "Stop", #ff8c00, 3, plot.style_line)

ma = f_ma(ma_source, ma_period, ma_method)
plot(ma, "Average", #8d8d8d, 1, plot.style_line)
plot(atr_source, "Strength", color.yellow, 1, plot.style_circles)
check the below:

CSS:
#// Indicator for TOS
#// © fktrdr
#indicator("Supertrend Channel", "Supertrend Channel", false, timeframe="", timeframe_gaps=true)
# Converted by Sam4Cok@Samer800    - 10/2024

input timeframe = AggregationPeriod.MIN;
input atrChangeTrack = yes;     # "ATR Change Track", "Adjust the Stop with the ATR change"
input atr_period = 52;          # "ATR Length"
input atrMovAvgType = AverageType.WILDERS; # "ATR Method"
input atrSource = FundamentalType.CLOSE; #, "ATR Source"
input atr_factor = 2.5; #, "ATR Factor"
input movAvgType = AverageType.WILDERS; #("RMA", "MA Method", ["EMA", "RMA", "SMA", "WMA"])
input movAvgSource = FundamentalType.CLOSE; #, "MA Source")
input ma_period = 5; #, "MA Length", 1)


def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def tf = Max(current, timeframe);
def atr_source = Fundamental(atrSource, Period = tf);
def ma_source = Fundamental(movAvgSource, Period = tf);
def ma = MovingAverage(movAvgType, ma_source, ma_period);
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def atr = MovingAverage(atrMovAvgType, tr, atr_period);
def up = close(Period = tf)[1] + atr[1] * atr_factor;
def lo = close(Period = tf)[1] - atr[1] * atr_factor;
def upper_band;
def lower_band;
def upper_band1 = CompoundValue(1, upper_band[1], 0);
def lower_band1 = CompoundValue(1, lower_band[1], 0);
upper_band = if up < upper_band1 or close(Period = tf)[1] > upper_band1 then up else
             if atrChangeTrack then upper_band1 + (atr[1] - atr[2]) * atr_factor else upper_band1;
lower_band = if lo > lower_band1 or close(Period = tf)[1] < lower_band1 then lo else
             if atrChangeTrack then lower_band1 + (atr[2] - atr[1]) * atr_factor else lower_band1;
def st_stop;
def stop_direction = if !atr[1] then 1 else
                     if st_stop[1] == upper_band1 then
                     if close(Period = tf)[1] > upper_band1 then -1 else  1 else
                     if close(Period = tf)[1] < lower_band1 then  1 else -1;

st_stop = if stop_direction == -1 then lower_band else upper_band;

def trend_direction = if !atr[1] then 1 else
                      if st_stop == upper_band then
                      if close(Period = tf) > upper_band then -1 else  1 else
                      if close(Period = tf) < lower_band then  1 else -1;
def crossUp = (trend_direction > 0) and (trend_direction[1] <= 0);
def crossDn = (trend_direction < 0) and (trend_direction[1] >= 0);
def st_trend;
def st_trend1 = CompoundValue(1, st_trend[1], close(period = tf));
    st_trend = if crossUp or crossDn then close(Period = tf) else
               if trend_direction == -1 then max(st_trend1, close(Period = tf)) else min(st_trend1, close(Period = tf));

plot Trend = if !last and st_trend then st_trend else na;
plot stop = if !last  and st_stop then st_stop else na;
plot avgLine = if !last and ma then ma else na; #, "Average"

avgLine.SetDefaultColor(Color.GRAY);
Trend.SetDefaultColor(Color.CYAN);
stop.SetDefaultColor(Color.RED);
Trend.SetLineWeight(2);
Stop.SetLineWeight(2);

AddCloud(Trend, stop, Color.DARK_GREEN, Color.DARK_RED);

#-- END of CODE
 
check the below:

CSS:
#// Indicator for TOS
#// © fktrdr
#indicator("Supertrend Channel", "Supertrend Channel", false, timeframe="", timeframe_gaps=true)
# Converted by Sam4Cok@Samer800    - 10/2024

input timeframe = AggregationPeriod.MIN;
input atrChangeTrack = yes;     # "ATR Change Track", "Adjust the Stop with the ATR change"
input atr_period = 52;          # "ATR Length"
input atrMovAvgType = AverageType.WILDERS; # "ATR Method"
input atrSource = FundamentalType.CLOSE; #, "ATR Source"
input atr_factor = 2.5; #, "ATR Factor"
input movAvgType = AverageType.WILDERS; #("RMA", "MA Method", ["EMA", "RMA", "SMA", "WMA"])
input movAvgSource = FundamentalType.CLOSE; #, "MA Source")
input ma_period = 5; #, "MA Length", 1)


def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def tf = Max(current, timeframe);
def atr_source = Fundamental(atrSource, Period = tf);
def ma_source = Fundamental(movAvgSource, Period = tf);
def ma = MovingAverage(movAvgType, ma_source, ma_period);
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def atr = MovingAverage(atrMovAvgType, tr, atr_period);
def up = close(Period = tf)[1] + atr[1] * atr_factor;
def lo = close(Period = tf)[1] - atr[1] * atr_factor;
def upper_band;
def lower_band;
def upper_band1 = CompoundValue(1, upper_band[1], 0);
def lower_band1 = CompoundValue(1, lower_band[1], 0);
upper_band = if up < upper_band1 or close(Period = tf)[1] > upper_band1 then up else
             if atrChangeTrack then upper_band1 + (atr[1] - atr[2]) * atr_factor else upper_band1;
lower_band = if lo > lower_band1 or close(Period = tf)[1] < lower_band1 then lo else
             if atrChangeTrack then lower_band1 + (atr[2] - atr[1]) * atr_factor else lower_band1;
def st_stop;
def stop_direction = if !atr[1] then 1 else
                     if st_stop[1] == upper_band1 then
                     if close(Period = tf)[1] > upper_band1 then -1 else  1 else
                     if close(Period = tf)[1] < lower_band1 then  1 else -1;

st_stop = if stop_direction == -1 then lower_band else upper_band;

def trend_direction = if !atr[1] then 1 else
                      if st_stop == upper_band then
                      if close(Period = tf) > upper_band then -1 else  1 else
                      if close(Period = tf) < lower_band then  1 else -1;
def crossUp = (trend_direction > 0) and (trend_direction[1] <= 0);
def crossDn = (trend_direction < 0) and (trend_direction[1] >= 0);
def st_trend;
def st_trend1 = CompoundValue(1, st_trend[1], close(period = tf));
    st_trend = if crossUp or crossDn then close(Period = tf) else
               if trend_direction == -1 then max(st_trend1, close(Period = tf)) else min(st_trend1, close(Period = tf));

plot Trend = if !last and st_trend then st_trend else na;
plot stop = if !last  and st_stop then st_stop else na;
plot avgLine = if !last and ma then ma else na; #, "Average"

avgLine.SetDefaultColor(Color.GRAY);
Trend.SetDefaultColor(Color.CYAN);
stop.SetDefaultColor(Color.RED);
Trend.SetLineWeight(2);
Stop.SetLineWeight(2);

AddCloud(Trend, stop, Color.DARK_GREEN, Color.DARK_RED);

#-- END of CODE
Ok thank you I have it running to see how it works out.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
449 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