SuperTrend CCI ATR Trend for ThinkorSwim

tomsk

Well-known member
VIP
There was a request yesterday from @blakecmathis to add buy/sell signals on the following study which essentially is a combined study of SuperTrend together with CCI ATR Trend. That study originated from a user called DTEK on 8.10.2019 and was based on Mobius original work several years ago.

https://tos.mx/dDZh6eC
It looked really promising (as most strudies from Mobius are). However after working through the details, it appeared that the embedded logic was a bit complex and confusing. Hence I cleaned up the code, modified the logic, rewrote portions of the underlying engine, added state transitions, implemented buy/sell signals and arrows as well as gave it a new coloring structure.

Since the previous study did not have a name, as a placeholder I'm tagging this study as "Supertrend ATR CCI Trend". Here then is version 2.0 of the code.
Enjoy it folks!

Code:
# SuperTrend CCI ATR Trend
# tomsk
# 11.18.2019

# V1.0 - 08.10.2019 - dtek  - Initial release of SuperTrend CCI ATR Trend
# V2.0 - 11.18.2019 - tomsk - Modified the logic, cleaned up code for consistency

# SUPERTREND BY MOBIUS AND CCI ATR TREND COMBINED INTO ONE CHART INDICATOR,
# BOTH IN AGREEMENT IS A VERY POWERFUL SIGNAL IF TRENDING. VERY GOOD AT CATCHING
# REVERSALS. WORKS WELL ON 1 AND 5 MIN CHARTS. PLOT IS THE COMBINATION LOWEST
# FOR UPTREND AND HIGHEST OF THE DOWNTREND. DOTS COLORED IF BOTH IN AGREEMENT
# OR GREY IF NOT -  08/10/2019 DTEK

# Supertrend, extracted from Mobius original code

input ST_Atr_Mult = 1.0;    # was .70
input ST_nATR = 4;
input ST_AvgType = AverageType.HULL;

def ATR = MovingAverage(ST_AvgType, TrueRange(high, close, low), ST_nATR);
def UP = HL2 + (ST_Atr_Mult* ATR);
def DN = HL2 + (-ST_Atr_Mult * ATR);
def ST = if close < ST[1] then UP else DN;

# CCI_ATR measures distance from the mean. Calculates a trend
# line based on that distance using ATR as the locator for the line.
# Credit goes to Mobius for the underlying logic

input lengthCCI = 50;      # Was 20
input lengthATR = 21;      # Was 4
input AtrFactor = 1.0;     # Was 0.7

def ATRCCI = Average(TrueRange(high, close, low), lengthATR) * AtrFactor;
def price = close + low + high;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
          then 0
          else (price - Average(price, lengthCCI)) / linDev / 0.015;

def MT1 = if CCI > 0
          then Max(MT1[1], HL2 - ATRCCI)
          else Min(MT1[1], HL2 + ATRCCI);

# Alignment of Supertrend and CCI ATR indicators

def Pos_State = close > ST and close > MT1;
def Neg_State = close < ST and close < MT1;

# Combined Signal Approach - Supertrend and ATR CCI

plot CSA = MT1;
CSA.AssignValueColor(if Pos_State then Color.CYAN
                     else if Neg_State then Color.MAGENTA
                     else Color.YELLOW);

# Buy/Sell Signals using state transitions

def BuySignal = (!Pos_State[1] and Pos_State);
def SellSignal = !Neg_State[1] and Neg_State;

# Buy/Sell Arrows

plot BuySignalArrow = if BuySignal then 0.995 * MT1 else Double.NaN;
BuySignalArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignalArrow.SetDefaultColor(Color.CYAN);
BuySignalArrow.SetLineWeight(5);

plot SellSignalArrow = if SellSignal then 1.005 * MT1 else Double.NaN;
SellSignalArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellSignalArrow.SetDefaultColor(Color.PINK);
SellSignalArrow.SetLineWeight(5);

# Candle Colors

AssignPriceColor(if Pos_State then Color.GREEN
                 else if Neg_State then Color.RED
                 else Color.YELLOW);
# End SuperTrend CCI ATR Trend
 

HighBredCloud

Well-known member
@tomsk Anyway to add buy and sell chart bubbles like the Mobius ATR SuperTrend you enhanced? By the way...does this one repaint? Or is it in line with the Mobius ATR SuperTrend that you enhanced few weeks ago?
 

tomsk

Well-known member
VIP
Folks, given that stocks ebb in and out from time to time, there was an excellent idea on another thread where the request was just to display the current trend and ignore past historical up/down trends. This not only helps one have an instant insight into what the current trend is but also simplifies the display, is cleaner and reduces screen "clutter".

Using that approach, here is version 2.1 of the SuperTrend CCI ATR Trend study, I used this opportunity to redesign the state transition engine and tested this on uptrending stocks like AAPL as well as downtrending stocks like X. It works both on daily as well as intraday

Have fun with this!

Code:
# SuperTrend CCI ATR Trend
# tomsk
# 1.17.2020

# V1.0 - 08.10.2019 - dtek  - Initial release of SuperTrend CCI ATR Trend
# V2.0 - 11.18.2019 - tomsk - Modified the logic, cleaned up code for consistency
# V2.1 - 01.17.2020 - tomsk - Enhanced state transition engine to only display latest trend

# SUPERTREND BY MOBIUS AND CCI ATR TREND COMBINED INTO ONE CHART INDICATOR, 
# BOTH IN AGREEMENT IS A VERY POWERFUL SIGNAL IF TRENDING. VERY GOOD AT CATCHING 
# REVERSALS. WORKS WELL ON 1 AND 5 MIN CHARTS. PLOT IS THE COMBINATION LOWEST 
# FOR UPTREND AND HIGHEST OF THE DOWNTREND. DOTS COLORED IF BOTH IN AGREEMENT 
# OR GREY IF NOT -  08/10/2019 DTEK

# Supertrend, extracted from Mobius original code

input ST_Atr_Mult = 1.0;    # was .70
input ST_nATR = 4; 
input ST_AvgType = AverageType.HULL; 

def ATR = MovingAverage(ST_AvgType, TrueRange(high, close, low), ST_nATR); 
def UP = HL2 + (ST_Atr_Mult* ATR); 
def DN = HL2 + (-ST_Atr_Mult * ATR); 
def ST = if close < ST[1] then UP else DN; 

# CCI_ATR measures distance from the mean. Calculates a trend
# line based on that distance using ATR as the locator for the line.
# Credit goes to Mobius for the underlying logic

input lengthCCI = 50;      # Was 20
input lengthATR = 21;      # Was 4
input AtrFactor = 1.0;     # Was 0.7

def bar = barNumber();
def StateUp = 1;
def StateDn = 2;
def ATRCCI = Average(TrueRange(high, close, low), lengthATR) * AtrFactor;
def price = close + low + high;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0 
          then 0 
          else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
          then Max(MT1[1], HL2 - ATRCCI)
          else Min(MT1[1], HL2 + ATRCCI);

# Alignment of Supertrend and CCI ATR indicators
def State = if close > ST and close > MT1 then StateUp
            else if close < ST and close < MT1 then StateDn
            else State[1];
def newState = HighestAll(if State <> State[1] then bar else 0);

# Combined Signal Approach - Supertrend and ATR CCI

plot CSA = if bar >= newState then MT1 else Double.NaN;
CSA.AssignValueColor(if bar >= newState 
                     then if State == StateUp then Color.CYAN
                          else if State == StateDn then Color.YELLOW
                          else Color.CURRENT
                     else Color.CURRENT);
# Buy/Sell Arrows
plot BuySignalArrow = if bar >= newState and State == StateUp and State[1] <> StateUp then 0.995 * MT1 else Double.NaN;
BuySignalArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignalArrow.SetDefaultColor(Color.CYAN);
BuySignalArrow.SetLineWeight(5);

plot SellSignalArrow = if bar >= newState and State == StateDn and State[1] <> StateDn then 1.005 * MT1 else Double.NaN;
SellSignalArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellSignalArrow.SetDefaultColor(Color.YELLOW);
SellSignalArrow.SetLineWeight(5);

# Candle Colors
AssignPriceColor(if bar >= newState
                 then if State == StateUp then Color.GREEN 
                      else if State == StateDn then Color.RED 
                      else Color.YELLOW
                 else Color.CURRENT);

# End SuperTrend CCI ATR Trend
 

Alex

Active member
VIP
@tomsk Hey, I've recently ran a little back test using the indicator and I was pretty digging it, is there a way to possibly display buy / sell signals in the form of labels?
 

dougn

Member
2019 Donor
For some reason I am not able to scan with this study. If anyone has a fix for that please post the changes needed. Thx!
 
T

Thomas

Guest
Watching Hahn-Tech, and other areas, I cannot figure out how to simplify this column,.....HELP......alerting me to being "Too complex,"
 
Last edited by a moderator:

scott69

Active member
The simple truth is that this site is a group of very caring, generous people who are trying to help those TRYING TO LEARN thinkscript. The irritation comes from those coming here to find the "holy grail" FOR FREE, at other's TIME expense. Personally, I would never ask for help coding something unless I worked on it for hours and finally got stuck. Thus requesting help. Also, for all his work, I'm sure that Ben is not getting rich off this site. I give 10% or more of my yearly trading profits to charity. How many also do that? Let's all be nice to each other, and especially nice to those here who really, truly help us out.
 

scott69

Active member
@Thomas agreed, some very ingenious, successful code here. I have always thought that my own system is the holy grail, for me, LOL, and it is remarkably simple as compared to others I've seen, but some traders find comfort in complexity. I don't. I'm not even really making any tweaks to my system anymore, but I do love to learn to code a new language, thus why I'm here. It's more of a hobby, really. I would never even try to compete with any of the knowledgeable people here though. That is not my gift in life. Reading people, i.e. traders, i.e. the markets is my strength. But otherwise, I think that the whole premise of this site is wonderful. It is paramount, though, that the any new user understand what the code does before risking money with it.
 
T

Thomas

Guest
@Thomas agreed, some very ingenious, successful code here. I have always thought that my own system is the holy grail, for me, LOL, and it is remarkably simple as compared to others I've seen, but some traders find comfort in complexity. I don't. I'm not even really making any tweaks to my system anymore, but I do love to learn to code a new language, thus why I'm here. It's more of a hobby, really. I would never even try to compete with any of the knowledgeable people here though. That is not my gift in life. Reading people, i.e. traders, i.e. the markets is my strength. But otherwise, I think that the whole premise of this site is wonderful. It is paramount, though, that the any new user understand what the code does before risking money with it.
Totally agree, I'm a "Simpler," believer,...but your analysis of the situation is completely correct. If more read price, I think that would be a happier medium....Thanks for your response.....P.S. when ya develop that Grail,..please send it my way.... :)
 

Chence27

Active member
I'm looking for an alert for this version of the SuperTrend CCI indicator. I'd like an alert when a bar closes with a different color than the previous. Any help would be greatly appreciated.

Code:
# SUPERTREND BY MOBIUS AND CCI ATR TREND COMBINED INTO ONE CHART INDICATOR, BOTH IN AGREEMENT IS A VERY POWERFUL SIGNAL IF TRENDING. VERY GOOD AT CATCHING REVERSALS. WORKS WELL ON 1 AND 5 MIN CHARTS. PLOT IS THE COMBINATION LOWEST FOR UPTREND AND HIGHEST OF THE DOWNTREND. DOTS COLORED IF BOTH IN AGREEMENT OR GREY IF NOT -  08/10/19 DTEK




def c = close;
def h = high;
def l = low;
def pricedata = hl2;


#SUPERTREND
input ST_Atr_Mult = 1.0;
input ST_Length = 4;
input ST_AvgType = AverageType.HULL;


def ATR = MovingAverage(ST_AvgType, TrueRange(high, close, low), ST_Length);
def UP = HL2 + (ST_Atr_Mult * ATR);
def DN = HL2 + (-ST_Atr_Mult * ATR);
def ST = if close < ST[1] then UP else DN;


def SuperTrend = ST;




#CCI_ATR
input lengthCCI = 50;
input lengthATR = 21;
input AtrFactor = 1.0;


def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
          then 0
          else (price - Average(price, lengthCCI)) / linDev / 0.015;


def MT1 = if CCI > 0
          then Max(MT1[1], pricedata - ATRcci)
          else Min(MT1[1], pricedata + ATRcci);
def CCI_ATR_TREND = MT1;


plot ST_ATR_COMBO = if c > ST and c > CCI_ATR_TREND then Min(CCI_ATR_TREND, ST) else if c < ST and c < CCI_ATR_TREND then Max(CCI_ATR_TREND, ST) else CCI_ATR_TREND;


ST_ATR_COMBO.SetLineWeight(1);


ST_ATR_COMBO.AssignValueColor(if c < MT1 and c < ST then Color.MAGENTA else if c > MT1 and c > ST then Color.CYAN else Color.WHITE);
ST_ATR_COMBO.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);


def signal1 = c < MT1 and c < ST;




def signal2 = c > MT1 and c > ST;






AssignPriceColor(if signal1 then Color.MAGENTA else if Signal2 then Color.CYAN else Color.WHITE);
 
Last edited:

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.
Top