# How to scan for SuperTrend

#### ZRX9000

##### New member
Hey guys I've searched this forum but cant seem to find the answer. I'm trying to figure out how set up a scan using this Supertrend strategy.
https://github.com/sureshja/ThinkOrSwim/blob/master/SuperTrend.ts

I've tried everything I can think of but cant figure it out. Hoping someone here can help me figure out how to do it. Thank you

Code:
``````declare upper;
input ST_Coeff = 3; # Between 1 - 100
input ST_Period = 7; #Between 1 - 100

def iATR = ATR(ST_Period);
def tmpUp = hl2 - (ST_Coeff * iATR);
def tmpDn = hl2 + (ST_Coeff * iATR);
def finalUp = If(close[1] > finalUp[1], Max(tmpUp, finalUp[1]), tmpUp);
def finalDn = If(close[1] < finalDn[1], Min(tmpDn, finalDn[1]), tmpDn);
def trendDir = If( close > finalDn[1], 1, If( close < finalUp[1], -1, If(!IsNaN(trendDir[1]), trendDir[1], 1) ) );
def trendLine = If(trendDir == 1, finalUp, finalDn);

plot SuperTrend = trendLine;

SuperTrend.DefineColor( "up", Color.GREEN );
SuperTrend.DefineColor( "dn", Color.RED );
SuperTrend.AssignValueColor(SuperTrend.Color("up"));
SuperTrend.AssignValueColor( if close[1] > SuperTrend[1] then SuperTrend.Color( "up" ) else SuperTrend.Color( "dn" ) );
SuperTrend.SetLineWeight( 2 );

def entryPrice = open[-1];
def exitPrice = open[-1];
def tSize = 100;

def sma200 = MovingAverage(AverageType.SIMPLE, close, 200);
def bullMarket = open[-1] > sma200;
def bearMarket = open[-1] < sma200;

input price = close;
input length200 = 200;
input averageType200 = AverageType.EXPONENTIAL;

plot Avg200 = MovingAverage(averageType200, price, length200);
Avg200.SetDefaultColor(GetColor(4));

# Bull Market BUY
AddOrder(OrderType.BUY_TO_OPEN, price is greater than Avg200 and
close[-1] > SuperTrend[-1] and close[0] < SuperTrend[0]
, price = entryPrice, tradeSize = tSize, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "BUY");
# Bull Market SELL
AddOrder(OrderType.SELL_TO_CLOSE, close[-1] < SuperTrend[-1]
and close[0] > SuperTrend[0], price = exitPrice, tradeSize = tSize, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "SELL");``````

Last edited by a moderator:
Solution
Let me know if this works

Code:
``````declare lower;

input ST_Coeff = 3; # Between 1 - 100
input ST_Period = 7; # Between 1 - 100

def iATR = ATR(ST_Period);
def tmpUp = hl2 - (ST_Coeff * iATR);
def tmpDn = hl2 + (ST_Coeff * iATR);
def finalUp = If(close[1] > finalUp[1], Max(tmpUp, finalUp[1]), tmpUp);
def finalDn = If(close[1] < finalDn[1], Min(tmpDn, finalDn[1]), tmpDn);
def trendDir = If( close > finalDn[1], 1, If( close < finalUp[1], -1, If(!IsNaN(trendDir[1]), trendDir[1], 1) ) );
def trendLine = If(trendDir == 1, finalUp, finalDn);

input length200 = 200;
input averageType200 = AverageType.EXPONENTIAL;

def Avg200 = MovingAverage(averageType200, close, length200);

# Scan Condition
def BullMarketBuy = close > Avg200 and close[1] >...``````
Any luck with this?

Hey guys I've searched this forum but cant seem to find the answer. I'm trying to figure out how set up a scan using this Supertrend strategy.
https://github.com/sureshja/ThinkOrSwim/blob/master/SuperTrend.ts

I've tried everything I can think of but cant figure it out. Hoping someone here can help me figure out how to do it. Thank you

Code:
``````declare upper;
input ST_Coeff = 3; # Between 1 - 100
input ST_Period = 7; #Between 1 - 100

def iATR = ATR(ST_Period);
def tmpUp = hl2 - (ST_Coeff * iATR);
def tmpDn = hl2 + (ST_Coeff * iATR);
def finalUp = If(close[1] > finalUp[1], Max(tmpUp, finalUp[1]), tmpUp);
def finalDn = If(close[1] < finalDn[1], Min(tmpDn, finalDn[1]), tmpDn);
def trendDir = If( close > finalDn[1], 1, If( close < finalUp[1], -1, If(!IsNaN(trendDir[1]), trendDir[1], 1) ) );
def trendLine = If(trendDir == 1, finalUp, finalDn);

plot SuperTrend = trendLine;

SuperTrend.DefineColor( "up", Color.GREEN );
SuperTrend.DefineColor( "dn", Color.RED );
SuperTrend.AssignValueColor(SuperTrend.Color("up"));
SuperTrend.AssignValueColor( if close[1] > SuperTrend[1] then SuperTrend.Color( "up" ) else SuperTrend.Color( "dn" ) );
SuperTrend.SetLineWeight( 2 );

def entryPrice = open[-1];
def exitPrice = open[-1];
def tSize = 100;

def sma200 = MovingAverage(AverageType.SIMPLE, close, 200);
def bullMarket = open[-1] > sma200;
def bearMarket = open[-1] < sma200;

input price = close;
input length200 = 200;
input averageType200 = AverageType.EXPONENTIAL;

plot Avg200 = MovingAverage(averageType200, price, length200);
Avg200.SetDefaultColor(GetColor(4));

# Bull Market BUY
AddOrder(OrderType.BUY_TO_OPEN, price is greater than Avg200 and
close[-1] > SuperTrend[-1] and close[0] < SuperTrend[0]
, price = entryPrice, tradeSize = tSize, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "BUY");
# Bull Market SELL
AddOrder(OrderType.SELL_TO_CLOSE, close[-1] < SuperTrend[-1]
and close[0] > SuperTrend[0], price = exitPrice, tradeSize = tSize, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "SELL");``````

that is a chart strategy, that has 2 outputs.
you didn't specify what you want to scan for?

it uses negative offsets, so may not work in a scan.
ex.
def bullMarket = open[-1] > sma200;

a scan uses data from the last bar.
future bars don't exist.

many variables would have to have the offsets changed.

Let me know if this works

Code:
``````declare lower;

input ST_Coeff = 3; # Between 1 - 100
input ST_Period = 7; # Between 1 - 100

def iATR = ATR(ST_Period);
def tmpUp = hl2 - (ST_Coeff * iATR);
def tmpDn = hl2 + (ST_Coeff * iATR);
def finalUp = If(close[1] > finalUp[1], Max(tmpUp, finalUp[1]), tmpUp);
def finalDn = If(close[1] < finalDn[1], Min(tmpDn, finalDn[1]), tmpDn);
def trendDir = If( close > finalDn[1], 1, If( close < finalUp[1], -1, If(!IsNaN(trendDir[1]), trendDir[1], 1) ) );
def trendLine = If(trendDir == 1, finalUp, finalDn);

input length200 = 200;
input averageType200 = AverageType.EXPONENTIAL;

def Avg200 = MovingAverage(averageType200, close, length200);

# Scan Condition
def BullMarketBuy = close > Avg200 and close[1] > trendLine[1] and close < trendLine;
def BullMarketSell = close[1] < trendLine[1] and close > trendLine;

plot Scan = BullMarketBuy or BullMarketSell;``````

Last edited by a moderator:

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
343 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?