https://usethinkscript.com/threads/supertrend-indicator-by-mobius-for-thinkorswim.7/page-4#post-4925how to setup alerts in TOS using this indicator?
do you have a script for that watchlist scan?@danl
Example of how I use in a watchlist scan.
https://usethinkscript.com/threads/...y-mobius-for-thinkorswim.7/page-17#post-87548do you have a script for that watchlist scan?
Is there a MTF for this?@Talochka @BenTen I have adjusted the bubbles, fixed the coloring and added a buy/sell description on the bubble
Note that the bubbles can be further moved to your preference by changing the value of the variable "n"
You can do this either in the code directly or via the input selector in the user interface
Hope this would be aesthetically better visually.
EDIT: I have based my modifications based on @BenTen post #1 in this thread
Code:# SuperTrend # Mobius # Chat Room Request # 11.20.2019 tomsk Enhanced and adjusted bubbles with coloring and description tag input AtrMult = 1.0; input nATR = 4; input AvgType = AverageType.HULL; input PaintBars = yes; input n = 0; def n1 = n + 1; def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR); def UP = HL2 + (AtrMult * ATR); def DN = HL2 + (-AtrMult * ATR); def ST = if close < ST[1] then UP else DN; plot SuperTrend = ST; SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN); AssignPriceColor(if PaintBars and close < ST then Color.RED else if PaintBars and close > ST then Color.GREEN else Color.CURRENT); AddChartBubble(close[n] crosses below ST[n], low[n+1] + TickSize() * n, "Sell @ " + low[n1], color.Cyan, yes); AddChartBubble(close[n] crosses above ST[n], high[n+1] - TickSize() * n, "Buy @ " + high[n1], color.Yellow, no); # End Code SuperTrend
You can combine 3 supertrends, long boring job but not difficult:Wondering if anyone can help. I have 3 supertrends on my charts (10/1, 11/2, and 12/3) - i'd like to have a buy signal appear when all 3 trend lines turn green (on a 1 hr chart) or a sell signal when all 3 trend lines turn red.
# Mobius
# SuperTrend
# Chat Room Request
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
def UpperBand;
def LowerBand;
if close[1] > UpperBand[1]{
LowerBand = DN;
UpperBand = Double.NaN;
} else if Crosses(close[1], LowerBand[1], CrossingDirection.BELOW) {
LowerBand = Double.NaN;
UpperBand = UP;
} else if close[1] > LowerBand[1] and DN <= DN[1] {
LowerBand = LowerBand[1];
UpperBand = Double.NaN;
} else if close[1] > LowerBand[1] and DN >= LowerBand[1] {
LowerBand = DN;
UpperBand = Double.NaN;
} else if close[1] > LowerBand[1] and DN < LowerBand[1] {
LowerBand = LowerBand[1];
UpperBand = Double.NaN;
} else if close[1] < LowerBand[1] {
LowerBand = Double.NaN;
UpperBand = UP;
} else if Crosses(close[1], UpperBand[1], CrossingDirection.ABOVE) {
LowerBand = DN;
UpperBand = Double.NaN;
} else if close[1] < UpperBand[1] and UP >= UP[1] {
LowerBand = Double.NaN;
UpperBand = UpperBand[1];
} else if close[1] < UpperBand[1] and UP <= UpperBand[1] {
LowerBand = Double.NaN;
UpperBand = UP;
} else if close[1] < UpperBand[1] and UP > UpperBand[1] {
LowerBand = Double.NaN;
UpperBand = UpperBand[1];
} else {
LowerBand = LowerBand[1];
UpperBand = UpperBand[1];
}
plot UpperBandPlot = UpperBand;
UpperBandPlot.SetDefaultColor(GetColor(9));
UpperBandPlot.SetPaintingStrategy(PaintingStrategy.LINE);
UpperBandPlot.AssignValueColor(Color.RED);#
plot LowerBandPlot = LowerBand;
LowerBandPlot.SetDefaultColor(GetColor(9));
LowerBandPlot.SetPaintingStrategy(PaintingStrategy.LINE);
LowerBandPlot.AssignValueColor(Color.GREEN);
AssignPriceColor(if PaintBars and !IsNaN(UpperBand) then Color.RED else if PaintBars and !IsNaN(LowerBand) then Color.GREEN else Color.CURRENT);
#AddChartBubble(close crosses below UpperBand, low[1], low[1], color.Dark_Gray);
#AddChartBubble(close crosses above LowerBand, high[1], high[1], color.Dark_Gray, no);
# End Code SuperTrend
Thanks for your quick reply and clear reply
Thanks much, I was able to add a watchlist column with this exact study and I do see the count populated with green/red.@dougn As you requested, here is the very last version of the SuperTrend Watchlist that Mobius posted
Code:# SuperTrend for WatchList # Mobius # 8.8.2017 input AtrMult = .7; input nATR = 4; input AvgType = AverageType.Hull; def h = high; def l = low; def c = close; def ATR = MovingAverage(AvgType, TrueRange(h, c, l), nATR); def UP = hl2 + (ATRmult * ATR); def DN = hl2 + (-ATRmult * ATR); def ST = if c < ST[1] then Round(UP / TickSize(), 0) * TickSize() else Round(DN / TickSize(), 0) * TickSize(); def count = if c crosses below ST then 1 else if c < ST then count[1] + 1 else if c crosses above ST then 1 else if c > ST[1] then count[1] + 1 else count[1]; AddLabel(1, count, color.black); AssignBackgroundColor(if c < ST then Color.Red else Color.Green);
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-58010Thanks much, I was able to add a watchlist column with this exact study and I do see the count populated with green/red.
I'd like to setup an alert(pop-up with beep) that'll trigger on trend change(red to green & vice-versa) on any of the stocks on the watchlist.Right now, I get an alert only when the stock is loaded up on the chart actively and trend changes.
I have a static watchlist with few stocks and not interested so much in scanning.
can you pls advise, how I can do that? Thanks!
No, you don't want to "fix that".@BenTen
Thanks much for posting this. Hoping you can help with an issue I'm running into with this upper indicator.
Have noticed this issue multiple times on different tickers.. It plots RED on uptrend sometimes misleading. Attached screenshot from today on $BABA on 5 min chart.
Any ideas how we fix that?
Thanks!
@MerryDay I see..,that explains it! When I relook at the charts with this interpretation, it works great, almost predicting the next move! ..,so an a small uptrend with red plot, I can see an upcoming consolidation or a pullback. The same works when I see a green plot on a downward move...I get a consolidation or a pullback on the downward move.. Are you sure this doesnt repaint ;-)!?No, you don't want to "fix that".
What you see as an 'uptrend' has no defined supertrend trend or momentum. It is NOT a defined uptrend and therefore plots a 'red line'.
FYI, the SuperTrend as well as many other indicators are not recommended for below the 5min chart.
Read more here: https://usethinkscript.com/threads/...y-mobius-for-thinkorswim.7/page-16#post-98964
I'm getting these error messages: Expected Double at the column position for 'crosses,' and No Such Variable : ST for both lines. I can't figure out where to go from here.[Ruby:###------------------------------------------------------------------------------------------ input showSignals = yes; input showLabels = yes; input showBubbles = yes; input useStops = no; input useAlerts = no; ###------------------------------------------------------------------------------------------ ############################################ ## Create Signals - ## FILL IN THIS SECTION IF NOT DEFINED ABOVE ## ############################################ def BuySignal = close crosses above ST; # insert condition to create long position def SellSignal = close crosses below ST; # insert condition to create short position
# Mobius
# SuperTrend
# Chat Room Request
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST
then Color.RED
else if PaintBars and close > ST
then Color.GREEN
else Color.CURRENT);
AddChartBubble(close crosses below ST, low[1], low[1], color.Dark_Gray);
AddChartBubble(close crosses above ST, high[1], high[1], color.Dark_Gray, no);
# End Code SuperTrend
###------------------------------------------------------------------------------------------
# Profit and Loss Labels
#
# Fill in the 0>0 in the Create Signals section below to match your buy and sell signal conditions
#
# When using large amounts of hisorical data, P/L may take time to calculate
###------------------------------------------------------------------------------------------
input showSignals = yes; #hint showSignals: show buy and sell arrows
input LongTrades = yes; #hint LongTrades: perform long trades
input ShortTrades = yes; #hint ShortTrades: perform short trades
input showLabels = yes; #hint showLabels: show PL labels at top
input showBubbles = yes; #hint showBubbles: show PL bubbles at close of trade
input useStops = no; #hint useStops: use stop orders
input useAlerts = no; #hint useAlerts: use alerts on signals
input tradeDaytimeOnly = no; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market
def Begin = secondsfromTime(OpenTime);
def End = secondsTillTime(closetime);
# Only use market hours when using intraday timeframe
def isIntraDay = if getaggregationperiod() > 14400000 or getaggregationperiod()==0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
###------------------------------------------------------------------------------------------
######################################################
## Create Signals -
## FILL IN THIS SECTION
## replace 0>0 with your conditions for signals
######################################################
def PLBuySignal = if MarketOpen AND (close crosses above ST) then 1 else 0 ; # insert condition to create long position in place of the 0>0
def PLSellSignal = if MarketOpen AND (close crosses below ST) then 1 else 0; # insert condition to create short position in place of the 0>0
def PLBuyStop = if !useStops then 0 else if (0>0) then 1 else 0 ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if (0>0) then 1 else 0 ; # insert condition to stop in place of the 0>0
def PLMktStop = if MarketOpen[-1] == 0 then 1 else 0; # If tradeDaytimeOnly is set, then stop at end of day
#######################################
## Maintain the position of trades
#######################################
def CurrentPosition; # holds whether flat = 0 long = 1 short = -1
if (BarNumber()==1) OR isNaN(CurrentPosition[1]) {
CurrentPosition = 0;
}else{
if CurrentPosition[1] == 0 { # FLAT
if (PLBuySignal AND LongTrades) {
CurrentPosition = 1;
} else if (PLSellSignal AND ShortTrades){
CurrentPosition = -1;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == 1 { # LONG
if (PLSellSignal AND ShortTrades){
CurrentPosition = -1;
} else if ((PLBuyStop and useStops) or PLMktStop or (PLSellSignal AND ShortTrades==0)){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == -1 { # SHORT
if (PLBuySignal AND LongTrades){
CurrentPosition = 1;
} else if ((PLSellStop and useStops) or PLMktStop or (PlBuySignal and LongTrades==0)){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else {
CurrentPosition = CurrentPosition[1];
}
}
def isLong = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat = if CurrentPosition == 0 then 1 else 0;
# If not already long and get a PLBuySignal
#Plot BuySig = if (!isLong[1] and PLBuySignal and showSignals and LongTrades) then 1 else 0;
Plot BuySig = if (((isShort[1] and LongTrades) or (isFlat[1] and LongTrades)) and PLBuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(color.cyan);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
# If not already short and get a PLSellSignal
Plot SellSig = if (((isLong[1] and ShortTrades) or (isFlat[1] and ShortTrades)) and PLSellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(color.cyan);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
# If long and get a PLBuyStop
Plot BuyStpSig = if (PLBuyStop and isLong[1] and showSignals and useStops) or (isLong[1] and PLMktStop) or (isLong[1] and PLSellSignal and !ShortTrades) then 1 else 0;
BuyStpSig.AssignValueColor(color.light_gray);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);
Alert(BuyStpSig and useAlerts, "Buy Stop Signal",Alert.bar,sound.Ding);
Alert(BuyStpSig and useAlerts, "Buy Stop Signal",Alert.bar,sound.Ding);
# If short and get a PLSellStop
Plot SellStpSig = if (PLSellStop and isShort[1] and showSignals and useStops) or (isShort[1] and PLMktStop) or (isShort[1] and PLBuySignal and !LongTrades) then 1 else 0;
SellStpSig.AssignValueColor(color.light_gray);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);
Alert(SellStpSig and useAlerts, "Sell Stop Signal",Alert.bar,sound.Ding);
Alert(SellStpSig and useAlerts, "Sell Stop Signal",Alert.bar,sound.Ding);
#######################################
## Orders
#######################################
def isOrder = if ((isFlat[1] and (BuySig and LongTrades) or (SellSig and ShortTrades)) or (isLong[1] and BuyStpSig or (SellSig and ShortTrades)) or (isShort[1] and SellStpSig or (BuySig and LongTrades))) then 1 else 0 ;
def orderPrice = if (isOrder and ((BuySig and LongTrades) or (SellSig and ShortTrades))) then (if isNan(open[-1]) then close else open[-1]) else orderPrice[1];
plot OPL = if !isFlat and isLong then orderPrice else double.Nan;
OPL.AssignValueColor(color.green);
OPL.SetPaintingStrategy(PaintingStrategy.lINE);
OPL.SetStyle(Curve.SHORT_DASH);
OPL.SetLineWeight(1);
plot OPS = if !isFlat and isShort then orderPrice else double.Nan;
OPS.AssignValueColor(color.red);
OPS.SetPaintingStrategy(PaintingStrategy.lINE);
OPS.SetStyle(Curve.SHORT_DASH);
OPS.SetLineWeight(1);
def orderCount = compoundValue(1,if isNan(isOrder) or barnumber()==1 then 0 else if (BuySig or SellSig) then orderCount[1]+1 else orderCount[1],0);
#######################################
## Price and Profit
#######################################
def profitLoss;
if (!isOrder or orderPRice[1]==0){
profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)){
profitLoss = (if isNan(open[-1]) then close else open[-1]) - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
profitLoss = orderPrice[1] - (if isNan(open[-1]) then close else open[-1]);
} else {
profitLoss = 0;
}
# Total Profit or Loss
def profitLossSum = compoundValue(1, if isNaN(isOrder) or barnumber()==1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);
# How many trades won or lost
def profitWinners = compoundValue(1, if isNaN(profitWinners[1]) or barnumber()==1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = compoundValue(1, if isNaN(profitLosers[1]) or barnumber()==1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = compoundValue(1, if isNaN(profitPush[1]) or barnumber()==1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);
# Current Open Trade Profit or Loss
def TradePL = If isLong then Round(((close - orderprice)/TickSize())*TickValue()) else if isShort then Round(((orderPrice - close)/TickSize())*TickValue()) else 0;
# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPRice[1]==0 or isNaN(orderPrice[1]) then 0 else round((profitLoss/Ticksize())*Tickvalue());
# Closed Orders dollar P/L
def dollarPLSum = round((profitLossSum/Ticksize())*Tickvalue());
# Split profits or losses by long and short trades
def profitLong = compoundValue(1, if isNan(profitLong[1]) or barnumber()==1 then 0 else if isOrder and isLong[1] then profitLong[1]+dollarProfitLoss else profitLong[1],0);
def profitShort = compoundValue(1, if isNan(profitShort[1]) or barnumber()==1 then 0 else if isOrder and isShort[1] then profitShort[1]+dollarProfitLoss else profitShort[1],0);
def countLong = compoundValue(1, if isNaN(countLong[1]) or barnumber()==1 then 0 else if isOrder and isLong[1] then countLong[1]+1 else countLong[1],0);
def countShort = compoundValue(1, if isNaN(countShort[1]) or barnumber()==1 then 0 else if isOrder and isShort[1] then countShort[1]+1 else countShort[1],0);
# What was the biggest winning and losing trade
def biggestWin = compoundValue(1, if isNaN(biggestWin[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = compoundValue(1, if isNaN(biggestLoss[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);
def ClosedTradeCount = if (isLong or isShort) then orderCount-1 else orderCount;
def OpenTrades = if (isLong or isShort) then 1 else 0;
# What percent were winners
def PCTWin = if (OpenTrades and (TradePL < 0)) then round((profitWinners/(ClosedTradeCount+1))*100,2)
else if (OpenTrades and (TradePL > 0)) then round(((profitWinners+1)/(ClosedTradeCount+1))*100,2) else round(((profitWinners)/(ClosedTradeCount))*100,2) ;
# Average trade
def avgTrade = if (OpenTrades and (TradePL < 0)) then round(((dollarPLSum - TradePL)/(ClosedTradeCount+1)),2)
else if (OpenTrades and (TradePL > 0)) then round(((dollarPLSum + TradePL)/(ClosedTradeCount+1)),2) else round(((dollarPLSum)/(ClosedTradeCount)),2) ;
#######################################
## Create Labels
#######################################
AddLabel(showLabels and isIntraDay, if MarketOpen then "Market Open" else "Market Closed", color.white);
AddLabel(showLabels, GetSymbol()+" Tick Size: "+TickSize()+" Value: "+TickValue(), color.white);
AddLabel(showLabels and (LongTrades and ShortTrades), "Long+Short Trades", color.white);
AddLabel(showLabels and (LongTrades and !ShortTrades),"Long Trades Only", color.white);
AddLabel(showLabels and (!LongTrades and ShortTrades),"Short Trades Only", color.white);
AddLabel(showLabels and (tradeDaytimeOnly),"Daytime Only", color.white);
AddLabel(showLabels, "Closed Orders: " + ClosedTradeCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum< 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: "+ AsDollars(TradePL+dollarPLSum), if ((TradePL+dollarPLSum) > 0) then color.green else if ((TradePL+dollarPLSum) < 0) then color.red else color.gray);
AddLabel(showLabels, "Avg per Trade: "+ AsDollars(avgTrade), if avgTrade > 0 then Color.Green else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: "+ PCTWin +"%",if PCTWin > 50 then color.green else if PCTWin > 40 then color.yellow else color.gray);
AddLabel(showLabels, "MaxUp: "+ AsDollars(biggestWin) +" MaxDown: "+AsDollars(biggestLoss), color.white);
AddLabel(showLabels, "Long Profit: " +AsDollars(profitLong), if profitLong > 0 then color.green else if profitLong < 0 then color.red else color.gray);
AddLabel(showLabels, "Short Profit: " +AsDollars(profitShort), if profitShort > 0 then color.green else if profitShort < 0 then color.red else color.gray);
AddLabel(if !IsNan(CurrentPosition) and showLabels and OpenTrades then 1 else 0, "Open: "+ (If isLong then "Bought" else "Sold") + " @ "+orderPrice, color.white);
AddLabel(if !IsNan(orderPrice) and showLabels and OpenTrades then 1 else 0, "Open Trade P/L: "+ AsDollars(TradePL), if (TradePL > 0) then color.green else if (TradePl < 0) then color.red else color.gray);
#######################################
## Chart Bubbles for Profit/Loss
#######################################
AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 1);
#AssignPriceColor(if CurrentPosition == 1 then color.green else if CurrentPosition == -1 then color.red else color.gray);
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
Archived: Supertrend Indicator by Mobius for ThinkorSwim | Indicators | 312 | ||
SuperTrend and RSI Laguerre Indicator for ThinkorSwim | Indicators | 35 | ||
S | Smart Supertrend For ThinkOrSwim | Indicators | 13 | |
M | SuperTrend Oscillator [LUX] For ThinkOrSwim | Indicators | 7 | |
B | SuperTrend TradingView Look-A-Like For ThinkOrSwim | Indicators | 60 |
Start a new thread and receive assistance from our community.
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.
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.