Q-Trend, QQE, SuperTrend strategy for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
IOB1C9o.png


CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// Based on "Supertrend" by KivancOzbilgic and "QQE signals" by colinmck
#// Modified by YourUsername
#// A big thank you to KivancOzbilgic for creating and sharing the Supertrend script!
#// A big thank you to colinmck for creating and sharing the QQE signals script!
#strategy('Q-Trend', overlay=true, process_orders_on_close=true, explicit_plot_zorder=true)
# Convereted by Sam4COK@Samer800     - 05/2023

input useChartTimeframe = yes;
input Aggregation = {Default "10 Min", "15 Min", "30 Min", "60 Min"};
input ColorBars = yes;           # 'Match Candle Color to Trend'
input showTradebox = yes;        # 'Show Tradebox'
input Show_Dashboard = yes;      # 'Show Dashboard'
input Risk = 0.5;                # 'Risk (bps)'
input RiskToRewardRatio = 1.0;   # 'Risk/Reward Ratio'
input atr_period = 10;           # 'ATR Period'
input SupertrendSource = hl2;    # 'Source'
input atrMultiplier = 3.0;       # 'ATR Multiplier'
input changeAtrCalculationMethod = yes;    # 'ATR Calculation Method'
input rsiLength = 14;            # 'RSI Length'
input rsiSmoothing = 6;          # 'RSI Smoothing'
input qqeFactor = 4.238;         # 'Quantitative Factor'
input minNoOfBarsForDashboardCalc = 100;   # 'Hide the dashboard if there are less than this manu bars on screen'


def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def agg = if Aggregation==Aggregation."10 Min" then AggregationPeriod.TEN_MIN else
          if Aggregation==Aggregation."15 Min" then AggregationPeriod.FIFTEEN_MIN else
          if Aggregation==Aggregation."30 Min" then AggregationPeriod.THIRTY_MIN else
          if Aggregation==Aggregation."60 Min" then AggregationPeriod.HOUR else current;
def tf = if useChartTimeframe then current else agg;
def high_ = high(Period=tf);
def low_ = low(Period=tf);
def close_ = close(PEriod=tf);
def bar_index = AbsValue(CompoundValue(1, BarNumber(),0));
def x_show_dash = if Show_Dashboard then if bar_index > minNoOfBarsForDashboardCalc then yes else na else na;

#// SUPERTREND
def tr = TrueRange(high_, close_, low_);
def atr2 = Average(tr, atr_period);
def nATR = if changeAtrCalculationMethod then ATR(LENGTH=atr_period) else atr2;
def up;
def up_ = SupertrendSource - atrMultiplier * nATR;
def up1 = if (isNaN(up[1]) or up[1]==0) then up_ else up[1];
    up = if close_[1] > up1 then max(up_, up1) else up_;

def dn;
def dn_ = SupertrendSource + atrMultiplier * nATR;
def dn1 = if (isNaN(dn[1]) or dn[1]==0) then dn_ else dn[1];
    dn = if close_[1] < dn1 then min(dn_, dn1) else dn_;

def super_trend;
def supertrend = if (isNaN(super_trend[1]) or super_trend[1]==0) then 1 else super_trend[1];
    super_trend = if supertrend == -1 and close_ > dn1 then 1 else
                  if supertrend == 1  and close_ < up1 then -1 else supertrend;

plot upPlot = if super_trend == 1 then up else na;    # 'Bullish'
upPlot.SetDefaultColor(Color.GREEN);

def buySignal = super_trend == 1 and super_trend[1] == -1;
plot buyPoint  = if buySignal then up else na;        # 'Bull Start'
buyPoint.SetPaintingStrategy(PaintingStrategy.SQUARES);
buyPoint.SetDefaultColor(Color.GREEN);

plot dnPlot = if super_trend == 1 then na else dn;    # 'Bearish'
dnPlot.SetDefaultColor(Color.RED);

def sellSignal = super_trend == -1 and super_trend[1] == 1;
plot SellPoint = if sellSignal then dn else na;       # 'Bear Start'
SellPoint.SetPaintingStrategy(PaintingStrategy.SQUARES);
SellPoint.SetDefaultColor(Color.RED);

def mPlot = ohlc4;

AddCloud(mPlot, upPlot, Color.DARK_GREEN);    # 'Bull Trend Area'
AddCloud(dnPlot, mPlot, Color.DARK_RED);      # 'Bear Trend Area'

def changeCond = super_trend != super_trend[1];

#// Q
def src = close_;
def wild_period = rsiLength * 2 - 1;
def rsi = rsi(Price=src,Length= rsiLength);
def rsi_ma = ExpAverage(rsi, rsiSmoothing);
def atr_rsi = AbsValue(rsi_ma[1] - rsi_ma);
def atr_rsi_ma = ExpAverage(atr_rsi, wild_period);
def dar = ExpAverage(atr_rsi_ma, wild_period) * qqeFactor;

def longband;# = 0.0
def shortband;# = 0.0
def trend;# = 0
def trend_ = if (isNaN(trend[1]) or trend[1]==0) then 1 else trend[1];
def atr_rsi_delta_fast = dar;
def rsi_idx = rsi_ma;
def newshortband = rsi_idx + atr_rsi_delta_fast;
def newlongband = rsi_idx - atr_rsi_delta_fast;
longband = if rsi_idx[1] > longband[1] and rsi_idx > longband[1] then max(longband[1], newlongband) else newlongband;
shortband = if rsi_idx[1] < shortband[1] and rsi_idx < shortband[1] then min(shortband[1], newshortband) else newshortband;

def cross_1 = crosses(longband[1],rsi_idx);
def cross_2 = crosses(rsi_idx,shortband[1]);
    trend = if cross_2 then 1 else
            if cross_1 then -1 else trend_;
def atr_rsi_fast = if trend == 1 then longband else shortband;

#// Find all the QQE Crosses

def q_xlong;
def q_xshort;
def q_xlong_ = if isNaN(q_xlong[1]) then 0 else q_xlong[1];
def q_xshort_ = if isNaN(q_xshort[1]) then 0 else q_xshort[1];
    q_xlong  = if atr_rsi_fast < rsi_idx then q_xlong_ + 1 else 0;
    q_xshort = if atr_rsi_fast > rsi_idx then q_xshort_ + 1 else 0;

#//Conditions

def q_long = q_xlong == 1;
def q_short =q_xshort == 1;

#// Plotting

def is_buy = q_long and super_trend == -1;
def is_sell = q_short and super_trend == 1;
def is_buy_strong = q_long and super_trend == 1;
def is_sell_strong = q_short and super_trend == -1;
def in_trend;
if is_buy {
    in_trend = 1;
    } else
if is_sell {
    in_trend = -1;
    } else
if is_buy_strong {
    in_trend = 2;
    } else
if is_sell_strong {
    in_trend = -2;
    } else {
    in_trend = in_trend[1];
}
AssignPriceColor(if !ColorBars or isNaN(close_[-1]) then Color.CURRENT else
                  if in_trend==2 then Color.GREEN else
                  if in_trend==1 then Color.DARK_GREEN else
                  if in_trend==-2 then Color.RED else
                  if in_trend==-1 then Color.DARK_RED else Color.GRAY);

AddChartBubble(is_buy and !is_buy[1], low, "buy", Color.DARK_GREEN, no);                    # 'Quantitative Buy'
AddChartBubble(is_sell and !is_sell[1], high, "Sell", Color.DARK_RED, yes);                 # 'Quantitative Sell'

AddChartBubble(is_buy_strong and !is_buy_strong[1], low, "Strong Buy", Color.GREEN, no);    # 'Quantitative Strong Buy'
AddChartBubble(is_sell_strong and !is_sell_strong[1], high, "Strong Sell", Color.RED, yes); # 'Quantitative Strong Sell'

#// Trade
def is_trade;
def entry_price;
def entry_price_bi;
def stop_loss;
def take_profit;

#is_trade := switch
if is_buy or is_buy_strong {
     is_trade = yes;
    } else
if is_sell or is_sell_strong {
    is_trade = no;
    } else {
    is_trade = is_trade[1];
}
def trade = is_trade;
if is_buy or is_buy_strong {
    entry_price_bi = bar_index;
    } else
if is_sell or is_sell_strong {
    entry_price_bi = bar_index;
    } else {
    entry_price_bi = entry_price_bi[1];
}

def highBar = bar_index>=highestAll(entry_price_bi);
entry_price = GetValue(close_, bar_index - entry_price_bi);

stop_loss   = if trade then entry_price * (1 - Risk/100)  else
                               entry_price * (1 + Risk/100);
take_profit = if trade then entry_price + (AbsValue(entry_price - stop_loss) * RiskToRewardRatio) else
                               entry_price - (AbsValue(entry_price - stop_loss) * RiskToRewardRatio);
def entryprice = entry_price;
def stoploss = stop_loss;
def takeprofit = take_profit;
AddLabel(x_show_dash, "Entry price: " + Round(entryprice, 2), Color.WHITE);
AddLabel(x_show_dash, "Stop loss: " + Round(StopLoss, 2), Color.PINK);
AddLabel(x_show_dash, "Take profit: " + Round(takeprofit, 2), Color.LIGHT_GREEN);

AddLabel(x_show_dash, if in_trend==2 then "Trade: Strong Buy" else
                      if in_trend==1 then "Trade: Buy" else
                      if in_trend==-2 then "Trade: Strong Sell" else
                      if in_trend==-1 then "Trade: Sell" else "Trade: No Trend",
                      if in_trend==2 then Color.GREEN else
                      if in_trend==1 then Color.DARK_GREEN else
                      if in_trend==-2 then Color.RED else
                      if in_trend==-1 then Color.DARK_RED else Color.GRAY);

#--- Trade
def entry = if entryprice==entryprice[1] then entryprice else na;
def stop = if stoploss==stoploss[1] then stoploss else na;#stop[1];
def profit = if takeprofit==takeprofit[1] then takeprofit else na;

plot entryLine = if showTradebox and highBar then entry else na;
entryLine.SetStyle(Curve.SHORT_DASH);
entryLine.SetDefaultColor(Color.GRAY);

plot StopLine = if showTradebox and highBar then stop else na;
StopLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
StopLine.AssignValueColor(if in_trend>0 then Color.MAGENTA else Color.CYAN);

plot ProfitLine = if showTradebox and highBar then profit else na;
ProfitLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfitLine.AssignValueColor(if in_trend>0 then Color.CYAN else Color.MAGENTA);

AddCloud(if trade then entryLine else StopLine, if trade then StopLine  else entryLine, Color.DARK_RED);
AddCloud(if trade then ProfitLine else entryLine,if trade then entryLine else ProfitLine, Color.DARK_GREEN);

#--- END of CODE
 
Thanks for the indicator! Any way to put alerts on Buy/Sell labels?
You can add alerts to anything. Here's how: https://usethinkscript.com/resources/how-to-add-alert-script-to-thinkorswim-indicators.9/

Here are some examples to add to the bottom of your script.
You can add the alerts for the other labels, in the same manner.
# Alerts
Alert(trend==1, "Trade: Buy", Alert.Bar, Sound.Chimes);
Alert(trend==-1, "Trade: Sell", Alert.Bar, Sound.Bell);
 
View attachment 18608

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// Based on "Supertrend" by KivancOzbilgic and "QQE signals" by colinmck
#// Modified by YourUsername
#// A big thank you to KivancOzbilgic for creating and sharing the Supertrend script!
#// A big thank you to colinmck for creating and sharing the QQE signals script!
#strategy('Q-Trend', overlay=true, process_orders_on_close=true, explicit_plot_zorder=true)
# Convereted by Sam4COK@Samer800     - 05/2023

input useChartTimeframe = yes;
input Aggregation = {Default "10 Min", "15 Min", "30 Min", "60 Min"};
input ColorBars = yes;           # 'Match Candle Color to Trend'
input showTradebox = yes;        # 'Show Tradebox'
input Show_Dashboard = yes;      # 'Show Dashboard'
input Risk = 0.5;                # 'Risk (bps)'
input RiskToRewardRatio = 1.0;   # 'Risk/Reward Ratio'
input atr_period = 10;           # 'ATR Period'
input SupertrendSource = hl2;    # 'Source'
input atrMultiplier = 3.0;       # 'ATR Multiplier'
input changeAtrCalculationMethod = yes;    # 'ATR Calculation Method'
input rsiLength = 14;            # 'RSI Length'
input rsiSmoothing = 6;          # 'RSI Smoothing'
input qqeFactor = 4.238;         # 'Quantitative Factor'
input minNoOfBarsForDashboardCalc = 100;   # 'Hide the dashboard if there are less than this manu bars on screen'


def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def agg = if Aggregation==Aggregation."10 Min" then AggregationPeriod.TEN_MIN else
          if Aggregation==Aggregation."15 Min" then AggregationPeriod.FIFTEEN_MIN else
          if Aggregation==Aggregation."30 Min" then AggregationPeriod.THIRTY_MIN else
          if Aggregation==Aggregation."60 Min" then AggregationPeriod.HOUR else current;
def tf = if useChartTimeframe then current else agg;
def high_ = high(Period=tf);
def low_ = low(Period=tf);
def close_ = close(PEriod=tf);
def bar_index = AbsValue(CompoundValue(1, BarNumber(),0));
def x_show_dash = if Show_Dashboard then if bar_index > minNoOfBarsForDashboardCalc then yes else na else na;

#// SUPERTREND
def tr = TrueRange(high_, close_, low_);
def atr2 = Average(tr, atr_period);
def nATR = if changeAtrCalculationMethod then ATR(LENGTH=atr_period) else atr2;
def up;
def up_ = SupertrendSource - atrMultiplier * nATR;
def up1 = if (isNaN(up[1]) or up[1]==0) then up_ else up[1];
    up = if close_[1] > up1 then max(up_, up1) else up_;

def dn;
def dn_ = SupertrendSource + atrMultiplier * nATR;
def dn1 = if (isNaN(dn[1]) or dn[1]==0) then dn_ else dn[1];
    dn = if close_[1] < dn1 then min(dn_, dn1) else dn_;

def super_trend;
def supertrend = if (isNaN(super_trend[1]) or super_trend[1]==0) then 1 else super_trend[1];
    super_trend = if supertrend == -1 and close_ > dn1 then 1 else
                  if supertrend == 1  and close_ < up1 then -1 else supertrend;

plot upPlot = if super_trend == 1 then up else na;    # 'Bullish'
upPlot.SetDefaultColor(Color.GREEN);

def buySignal = super_trend == 1 and super_trend[1] == -1;
plot buyPoint  = if buySignal then up else na;        # 'Bull Start'
buyPoint.SetPaintingStrategy(PaintingStrategy.SQUARES);
buyPoint.SetDefaultColor(Color.GREEN);

plot dnPlot = if super_trend == 1 then na else dn;    # 'Bearish'
dnPlot.SetDefaultColor(Color.RED);

def sellSignal = super_trend == -1 and super_trend[1] == 1;
plot SellPoint = if sellSignal then dn else na;       # 'Bear Start'
SellPoint.SetPaintingStrategy(PaintingStrategy.SQUARES);
SellPoint.SetDefaultColor(Color.RED);

def mPlot = ohlc4;

AddCloud(mPlot, upPlot, Color.DARK_GREEN);    # 'Bull Trend Area'
AddCloud(dnPlot, mPlot, Color.DARK_RED);      # 'Bear Trend Area'

def changeCond = super_trend != super_trend[1];

#// Q
def src = close_;
def wild_period = rsiLength * 2 - 1;
def rsi = rsi(Price=src,Length= rsiLength);
def rsi_ma = ExpAverage(rsi, rsiSmoothing);
def atr_rsi = AbsValue(rsi_ma[1] - rsi_ma);
def atr_rsi_ma = ExpAverage(atr_rsi, wild_period);
def dar = ExpAverage(atr_rsi_ma, wild_period) * qqeFactor;

def longband;# = 0.0
def shortband;# = 0.0
def trend;# = 0
def trend_ = if (isNaN(trend[1]) or trend[1]==0) then 1 else trend[1];
def atr_rsi_delta_fast = dar;
def rsi_idx = rsi_ma;
def newshortband = rsi_idx + atr_rsi_delta_fast;
def newlongband = rsi_idx - atr_rsi_delta_fast;
longband = if rsi_idx[1] > longband[1] and rsi_idx > longband[1] then max(longband[1], newlongband) else newlongband;
shortband = if rsi_idx[1] < shortband[1] and rsi_idx < shortband[1] then min(shortband[1], newshortband) else newshortband;

def cross_1 = crosses(longband[1],rsi_idx);
def cross_2 = crosses(rsi_idx,shortband[1]);
    trend = if cross_2 then 1 else
            if cross_1 then -1 else trend_;
def atr_rsi_fast = if trend == 1 then longband else shortband;

#// Find all the QQE Crosses

def q_xlong;
def q_xshort;
def q_xlong_ = if isNaN(q_xlong[1]) then 0 else q_xlong[1];
def q_xshort_ = if isNaN(q_xshort[1]) then 0 else q_xshort[1];
    q_xlong  = if atr_rsi_fast < rsi_idx then q_xlong_ + 1 else 0;
    q_xshort = if atr_rsi_fast > rsi_idx then q_xshort_ + 1 else 0;

#//Conditions

def q_long = q_xlong == 1;
def q_short =q_xshort == 1;

#// Plotting

def is_buy = q_long and super_trend == -1;
def is_sell = q_short and super_trend == 1;
def is_buy_strong = q_long and super_trend == 1;
def is_sell_strong = q_short and super_trend == -1;
def in_trend;
if is_buy {
    in_trend = 1;
    } else
if is_sell {
    in_trend = -1;
    } else
if is_buy_strong {
    in_trend = 2;
    } else
if is_sell_strong {
    in_trend = -2;
    } else {
    in_trend = in_trend[1];
}
AssignPriceColor(if !ColorBars or isNaN(close_[-1]) then Color.CURRENT else
                  if in_trend==2 then Color.GREEN else
                  if in_trend==1 then Color.DARK_GREEN else
                  if in_trend==-2 then Color.RED else
                  if in_trend==-1 then Color.DARK_RED else Color.GRAY);

AddChartBubble(is_buy and !is_buy[1], low, "buy", Color.DARK_GREEN, no);                    # 'Quantitative Buy'
AddChartBubble(is_sell and !is_sell[1], high, "Sell", Color.DARK_RED, yes);                 # 'Quantitative Sell'

AddChartBubble(is_buy_strong and !is_buy_strong[1], low, "Strong Buy", Color.GREEN, no);    # 'Quantitative Strong Buy'
AddChartBubble(is_sell_strong and !is_sell_strong[1], high, "Strong Sell", Color.RED, yes); # 'Quantitative Strong Sell'

#// Trade
def is_trade;
def entry_price;
def entry_price_bi;
def stop_loss;
def take_profit;

#is_trade := switch
if is_buy or is_buy_strong {
     is_trade = yes;
    } else
if is_sell or is_sell_strong {
    is_trade = no;
    } else {
    is_trade = is_trade[1];
}
def trade = is_trade;
if is_buy or is_buy_strong {
    entry_price_bi = bar_index;
    } else
if is_sell or is_sell_strong {
    entry_price_bi = bar_index;
    } else {
    entry_price_bi = entry_price_bi[1];
}

def highBar = bar_index>=highestAll(entry_price_bi);
entry_price = GetValue(close_, bar_index - entry_price_bi);

stop_loss   = if trade then entry_price * (1 - Risk/100)  else
                               entry_price * (1 + Risk/100);
take_profit = if trade then entry_price + (AbsValue(entry_price - stop_loss) * RiskToRewardRatio) else
                               entry_price - (AbsValue(entry_price - stop_loss) * RiskToRewardRatio);
def entryprice = entry_price;
def stoploss = stop_loss;
def takeprofit = take_profit;
AddLabel(x_show_dash, "Entry price: " + Round(entryprice, 2), Color.WHITE);
AddLabel(x_show_dash, "Stop loss: " + Round(StopLoss, 2), Color.PINK);
AddLabel(x_show_dash, "Take profit: " + Round(takeprofit, 2), Color.LIGHT_GREEN);

AddLabel(x_show_dash, if in_trend==2 then "Trade: Strong Buy" else
                      if in_trend==1 then "Trade: Buy" else
                      if in_trend==-2 then "Trade: Strong Sell" else
                      if in_trend==-1 then "Trade: Sell" else "Trade: No Trend",
                      if in_trend==2 then Color.GREEN else
                      if in_trend==1 then Color.DARK_GREEN else
                      if in_trend==-2 then Color.RED else
                      if in_trend==-1 then Color.DARK_RED else Color.GRAY);

#--- Trade
def entry = if entryprice==entryprice[1] then entryprice else na;
def stop = if stoploss==stoploss[1] then stoploss else na;#stop[1];
def profit = if takeprofit==takeprofit[1] then takeprofit else na;

plot entryLine = if showTradebox and highBar then entry else na;
entryLine.SetStyle(Curve.SHORT_DASH);
entryLine.SetDefaultColor(Color.GRAY);

plot StopLine = if showTradebox and highBar then stop else na;
StopLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
StopLine.AssignValueColor(if in_trend>0 then Color.MAGENTA else Color.CYAN);

plot ProfitLine = if showTradebox and highBar then profit else na;
ProfitLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfitLine.AssignValueColor(if in_trend>0 then Color.CYAN else Color.MAGENTA);

AddCloud(if trade then entryLine else StopLine, if trade then StopLine  else entryLine, Color.DARK_RED);
AddCloud(if trade then ProfitLine else entryLine,if trade then entryLine else ProfitLine, Color.DARK_GREEN);

#--- END of CODE
Hello, I was wondering if it is possible to have a scanner for this one that can notify you when a stock has a buy or sell alert? Possibly a scanner that can be made into a watchlist that one could set alerts for?
 
Last edited:
Hello, I was wondering if it is possible to have a scanner for this one that can notify you when a stock has a buy or sell alert? Possibly a scanner that can be made into a watchlist that one could set alerts for?
SuperTrend Scan & Watchlist
shared scan link: http://tos.mx/gxVFu27 Click here for --> Easiest way to load shared links
AnjK6ak.png

Ruby:
# Supertrend Scan & Watchlist
#// Based on "Supertrend" by KivancOzbilgic
# Convereted by Sam4COK@Samer800     - 05/2023


input atr_period = 10;           # 'ATR Period'
input SupertrendSource = hl2;    # 'Source'
input atrMultiplier = 3.0;       # 'ATR Multiplier'
input changeAtrCalculationMethod = yes;    # 'ATR Calculation Method'

#// SUPERTREND
def tr = TrueRange(high, close, low);
def atr2 = Average(tr, atr_period);
def nATR = if changeAtrCalculationMethod then ATR(LENGTH=atr_period) else atr2;
def up;
def up_ = SupertrendSource - atrMultiplier * nATR;
def up1 = if (isNaN(up[1]) or up[1]==0) then up_ else up[1];
    up = if close[1] > up1 then max(up_, up1) else up_;

def dn;
def dn_ = SupertrendSource + atrMultiplier * nATR;
def dn1 = if (isNaN(dn[1]) or dn[1]==0) then dn_ else dn[1];
    dn = if close[1] < dn1 then min(dn_, dn1) else dn_;

def super_trend;
def supertrend = if (isNaN(super_trend[1]) or super_trend[1]==0) then 1 else super_trend[1];
    super_trend = if supertrend == -1 and close > dn1 then 1 else
                  if supertrend == 1  and close < up1 then -1 else supertrend;

def buySignal = super_trend == 1 and super_trend[1] == -1;
def sellSignal = super_trend == -1 and super_trend[1] == 1;

plot scan = buySignal or sellSignal ;
scan.hide();

AddLabel(yes, " ") ;
AssignBackgroundColor(if buySignal then color.green else if sellSignal then color.red else color.gray);
 
Last edited:
SuperTrend Scan & Watchlist
shared scan link: http://tos.mx/gxVFu27 Click here for --> Easiest way to load shared links
AnjK6ak.png

Ruby:
# Supertrend Scan & Watchlist
#// Based on "Supertrend" by KivancOzbilgic
# Convereted by Sam4COK@Samer800     - 05/2023


input atr_period = 10;           # 'ATR Period'
input SupertrendSource = hl2;    # 'Source'
input atrMultiplier = 3.0;       # 'ATR Multiplier'
input changeAtrCalculationMethod = yes;    # 'ATR Calculation Method'

#// SUPERTREND
def tr = TrueRange(high, close, low);
def atr2 = Average(tr, atr_period);
def nATR = if changeAtrCalculationMethod then ATR(LENGTH=atr_period) else atr2;
def up;
def up_ = SupertrendSource - atrMultiplier * nATR;
def up1 = if (isNaN(up[1]) or up[1]==0) then up_ else up[1];
    up = if close[1] > up1 then max(up_, up1) else up_;

def dn;
def dn_ = SupertrendSource + atrMultiplier * nATR;
def dn1 = if (isNaN(dn[1]) or dn[1]==0) then dn_ else dn[1];
    dn = if close[1] < dn1 then min(dn_, dn1) else dn_;

def super_trend;
def supertrend = if (isNaN(super_trend[1]) or super_trend[1]==0) then 1 else super_trend[1];
    super_trend = if supertrend == -1 and close > dn1 then 1 else
                  if supertrend == 1  and close < up1 then -1 else supertrend;

def buySignal = super_trend == 1 and super_trend[1] == -1;
def sellSignal = super_trend == -1 and super_trend[1] == 1;

plot scan = buySignal or sellSignal ;
scan.hide();

AddLabel(yes, " ")
AssignBackgroundColor(if buySignal then color.green else if sellSignal then color.red else color.gray);
Thanks so much Merry! I appreciate you whipping this up! I just have one question. I don't know much about coding and writing scripts. I tried to get the watchlist label to work but for some reason I'm getting an error. Any ideas what I am doing wrong? It is giving me an "Invalid statement: AddLabel at 36:1" error message. Do you have any suggestions on what I could do to get it to work so I can add the label to the watchlist? Thanks in advance!
 
Thanks so much Merry! I appreciate you whipping this up! I just have one question. I don't know much about coding and writing scripts. I tried to get the watchlist label to work but for some reason I'm getting an error. Any ideas what I am doing wrong? It is giving me an "Invalid statement: AddLabel at 36:1" error message. Do you have any suggestions on what I could do to get it to work so I can add the label to the watchlist? Thanks in advance!
Sorry about that, my error.
That's the problem with "whipping" things up on my phone!
I left off the semicolon ; at the end of the AddLabel statement.
In English, statements end with a period.
In ThinkScript, statements end with a ;

Either re-copy and paste the corrected code above or just put a semicolon ; at the end of your AddLabel statement.
 
Sorry about that, my error.
That's the problem with "whipping" things up on my phone!
I left off the semicolon ; at the end of the AddLabel statement.
In English, statements end with a period.
In ThinkScript, statements end with a ;

Either re-copy and paste the corrected code above or just put a semicolon ; at the end of your AddLabel statement.
No, worries! That did it! Thanks Merry! I do have one more question. Will this script also find the "Strong Sell" and "Strong Buy" parameters? If not, is there any way we could adjust it to include those conditions as well?
 

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
426 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