Unable to combine conditions using supertrend as a function

serendipity2020

Member
Plus
Hello

I took supertrend from https://usethinkscript.com/threads/supertrend-yahoo-finance-type-for-thinkorswim.1998/ and want to move it to function so that I can invoke it multiple times in the ToS script that I'm writing.

What I noticed is that it works fine as long as there is no other condition with "crosses" function but as soon as I add any other codition, it stops printing label values.

Appreciate if someone can point me to the error and/or provide the fix for this issue so I can combine multiple conditions by keeping supertrend as a separate script function.

Code:
script ss {
    input AtrMult = 1.00;
    input nATR = 6;
    input AvgType = AverageType.HULL;
    input PaintBars = yes;

    def ATR = ATR("length" = nATR, "average type" = AvgType);
    def UP_Band_Basic = HL2 + (AtrMult * ATR);

    def LW_Band_Basic = HL2 + (-AtrMult * ATR);

    def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];

    def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

    def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
    else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
    else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
    else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
    else LW_Band;

    plot SuperTrend = ST;
};

def supertrend = ss(1, 6, AverageType.HULL, yes);;
def smartFilter = MovAvgExponential(close, 200);

def ArrowDown = (Supertrend crosses above close) and close < smartFilter; # This does not print values and results in NaN and needs fix.
# def ArrowDown = (Supertrend crosses above close); # This works fine and below label prints both values

addlabel(yes, "Supertrend =" + supertrend + ", ArrowDown=" + ArrowDown);

Thanks
 
Hello

I took supertrend from https://usethinkscript.com/threads/supertrend-yahoo-finance-type-for-thinkorswim.1998/ and want to move it to function so that I can invoke it multiple times in the ToS script that I'm writing.

What I noticed is that it works fine as long as there is no other condition with "crosses" function but as soon as I add any other codition, it stops printing label values.

Appreciate if someone can point me to the error and/or provide the fix for this issue so I can combine multiple conditions by keeping supertrend as a separate script function.

Code:
script ss {
    input AtrMult = 1.00;
    input nATR = 6;
    input AvgType = AverageType.HULL;
    input PaintBars = yes;

    def ATR = ATR("length" = nATR, "average type" = AvgType);
    def UP_Band_Basic = HL2 + (AtrMult * ATR);

    def LW_Band_Basic = HL2 + (-AtrMult * ATR);

    def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];

    def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

    def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
    else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
    else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
    else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
    else LW_Band;

    plot SuperTrend = ST;
};

def supertrend = ss(1, 6, AverageType.HULL, yes);;
def smartFilter = MovAvgExponential(close, 200);

def ArrowDown = (Supertrend crosses above close) and close < smartFilter; # This does not print values and results in NaN and needs fix.
# def ArrowDown = (Supertrend crosses above close); # This works fine and below label prints both values

addlabel(yes, "Supertrend =" + supertrend + ", ArrowDown=" + ArrowDown);

Thanks


i tried your original code. when i pick a different stock, for a split second, it showed errors in the labels, then it shows correct labels. so i'm not sure what you are seeing.

i added,
. code to draw up and down arrows.
. inputs to choose parameters for the sub script ss().
. a different average function and inputs.


Code:
#supertrend_fcn_00

#https://usethinkscript.com/threads/unable-to-combine-conditions-using-supertrend-as-a-function.13916/
#Unable to combine conditions using supertrend as a function

script ss {
  input AtrMult = 1.00;
  input nATR = 6;
  input AvgType = AverageType.HULL;
# disable this. not used. cant draw within a script
#  input PaintBars = yes;
  def ATR = ATR("length" = nATR, "average type" = AvgType);

  def UP_Band_Basic = HL2 + (AtrMult * ATR);
  def LW_Band_Basic = HL2 + (-AtrMult * ATR);
  def UP_Band = if barnumber() == 1 then UP_Band_Basic else if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];

  def LW_Band = if barnumber() == 1 then lw_Band_Basic else if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

  def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
    else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
    else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
    else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
    else LW_Band;

  plot SuperTrend = ST;
};

#-------------------------------

def bn =  barnumber();
def na = double.nan;

input AtrMult = 1.0;
input nATR = 6;
input AvgType = AverageType.HULL;
# disable this. not used. cant draw within a script
# input PaintBars = yes;

# chg custum function to use variables
#def supertrend = ss(1, 6, AverageType.HULL, yes);
def supertrend = ss(AtrMult, natr, AvgType);

plot z = supertrend;
z.SetDefaultColor(Color.cyan);
z.hidebubble();

input ma_len = 200;
#def smartFilter = MovAvgExponential(close, ema_len);
input ma_type =  AverageType.EXPONENTIAL;
def smartFilter =  MovingAverage(ma_type, close, ma_len);
input show_average_line = yes;
plot zma = if show_average_line then smartFilter else na;
zma.SetDefaultColor(Color.yellow);
#zma.setlineweight(1);
zma.hidebubble();


input show_arrows = yes;
#def ArrowDown = (Supertrend crosses above close) and close < smartFilter;
# replace crosses with comp[aring close to previous and current st price
def ArrowDown = (Supertrend[1] < close[1] and Supertrend > close and close < smartFilter);

plot zdwn = (show_arrows and ArrowDown);
zdwn.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_DOWN);
zdwn.SetDefaultColor(Color.white);
zdwn.setlineweight(2);
zdwn.hidebubble();


#def Arrowup = (Supertrend crosses below close) and close > smartFilter;
# replace crosses with comp[aring close to previous and current st price
def Arrowup = (Supertrend[1] > close[1] and Supertrend < close and close > smartFilter);

plot zup = (show_arrows and Arrowup);
zup.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_up);
zup.SetDefaultColor(Color.white);
zup.setlineweight(2);
zup.hidebubble();


#-------------------------------

# ?? works?  This does not print values and results in NaN and needs fix.
#def ArrowDown = (Supertrend crosses above close) and close < smartFilter;
input test_labels = no;
addlabel(test_labels, "Supertrend =" + supertrend + ", ArrowDown1=" + ArrowDown);

# This works fine and below label prints both values
def ArrowDown2 = (Supertrend crosses above close); 
addlabel(test_labels, "Supertrend =" + supertrend + ", ArrowDown2=" + ArrowDown2);

#-------------------------------

input test_bubbles = no;
addchartbubble(test_bubbles, low*0.996,
 (Supertrend crosses above close) + "\n" +
 (close < smartFilter) + "\n" +
 ArrowDown
, (if ArrowDown then color.magenta else color.gray), no);

addchartbubble(test_bubbles, low*0.996,
 (Supertrend crosses below close) + "\n" +
 (close > smartFilter) + "\n" +
 Arrowup
, (if Arrowup then color.cyan else color.gray), no);
#

csco 1hr
SuperTrend and EMA200 lines
Apzc6in.jpg
 

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

Hello @halcyonguy The label Supertrend prints N/A for many stocks when I call supertrend using script function and for the same stock, it shows it correctly if I paste supertrend code as is without using script function.

I tried on multiple different stocks such as PLTR, TCNNF, SKLZ, etc and it didn't work.

It does work on few other stocks but not for all stocks. I'm not sure if its also related to timeframe but usually I run it on 20Y-1D, 1Y-1D, 3M-5min or even 3M-1min timeframe.

Appreciate your help.

UPDATE: @halcyonguy I tried your updated code as well and as I mentioned it doesn't show any arrows on many companies like ABNB, PLTR, BITO, etc on 1Y-1D and many other timeframes. I'm trying to understand why this is happening and how can it be avoided? Thanks again for your reply!
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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