SuperTrend CCI ATR Trend for ThinkorSwim

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
@tomsk you are a genius!
 
Last edited:
Supertrend CCI ATR Trend Enchanced

This is the script from post#1 to which I added Mobius's Supertrend Cycle Statistics Labels (many thanks to @sunnybabu, who found these on TOS OneNote). I haven't had time to see how having these statistics will affect my trading, yet. But I find them interesting.
Ruby:
# 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;
input ShowLabel = yes;
def c = close;
def v = volume;

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
def upBars = if c < ST
             then upBars[1] + 1
             else upBars[1];
def upCycles = if c < ST and c[1] > ST[1]
               then upCycles[1] + 1
               else upCycles[1];

def dnBars = if c > ST
             then dnBars[1] + 1
             else dnBars[1];
def dnCycles = if c > ST and c[1] < ST[1]
               then dnCycles[1] + 1
               else dnCycles[1];
def upCycleCount = upBars / upCycles;
def dnCycleCount = dnBars / dnCycles;

def thisCycle = if c < ST and c[1] > ST[1]
                then 1
                else if c < ST
                then thisCycle[1] + 1
                else if c > ST and c[1] < ST[1]
                     then 1
                     else if c > ST
                          then thisCycle[1] + 1
                          else thisCycle[1];
def Volup = (fold i = 0 to thisCycle
             do if i > 0
                then Volup[1] + v
                else Volup[1]) / thisCycle;
DefineGlobalColor("LabelRed",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
AddLabel(ShowLabel, "Up Bars = " + upBars + "; " +
                  "  Up Cycles = " + upCycles + "; " +
                  "  Dn Bars = " + dnBars + "; " +
                  "  Dn Cycles = " + dnCycles + "; " ,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
AddLabel(ShowLabel,
                  "  Avg Up Cycle Count = " + Round(upCycleCount, 0) +
                  "  Avg Dn Cycle Count = " + Round(dnCycleCount, 0) +
                  "  This Cycle = " + thisCycle,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
# End Code SuperTrend
 
Supertrend CCI ATR Trend Enchanced

This is the script from post#1 to which I added Mobius's Supertrend Cycle Statistics Labels (many thanks to @sunnybabu, who found these on TOS OneNote). I haven't had time to see how having these statistics will affect my trading, yet. But I find them interesting.
Ruby:
# 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;
input ShowLabel = yes;
def c = close;
def v = volume;

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
def upBars = if c < ST
             then upBars[1] + 1
             else upBars[1];
def upCycles = if c < ST and c[1] > ST[1]
               then upCycles[1] + 1
               else upCycles[1];

def dnBars = if c > ST
             then dnBars[1] + 1
             else dnBars[1];
def dnCycles = if c > ST and c[1] < ST[1]
               then dnCycles[1] + 1
               else dnCycles[1];
def upCycleCount = upBars / upCycles;
def dnCycleCount = dnBars / dnCycles;

def thisCycle = if c < ST and c[1] > ST[1]
                then 1
                else if c < ST
                then thisCycle[1] + 1
                else if c > ST and c[1] < ST[1]
                     then 1
                     else if c > ST
                          then thisCycle[1] + 1
                          else thisCycle[1];
def Volup = (fold i = 0 to thisCycle
             do if i > 0
                then Volup[1] + v
                else Volup[1]) / thisCycle;
DefineGlobalColor("LabelRed",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
AddLabel(ShowLabel, "Up Bars = " + upBars + "; " +
                  "  Up Cycles = " + upCycles + "; " +
                  "  Dn Bars = " + dnBars + "; " +
                  "  Dn Cycles = " + dnCycles + "; " ,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
AddLabel(ShowLabel,
                  "  Avg Up Cycle Count = " + Round(upCycleCount, 0) +
                  "  Avg Dn Cycle Count = " + Round(dnCycleCount, 0) +
                  "  This Cycle = " + thisCycle,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
# End Code SuperTrend
@MerryDay thanks for sharing, am learning so much from you all!
 
Supertrend CCI ATR Trend Enchanced

This is the script from post#1 to which I added Mobius's Supertrend Cycle Statistics Labels (many thanks to @sunnybabu, who found these on TOS OneNote). I haven't had time to see how having these statistics will affect my trading, yet. But I find them interesting.
Ruby:
# 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;
input ShowLabel = yes;
def c = close;
def v = volume;

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
def upBars = if c < ST
             then upBars[1] + 1
             else upBars[1];
def upCycles = if c < ST and c[1] > ST[1]
               then upCycles[1] + 1
               else upCycles[1];

def dnBars = if c > ST
             then dnBars[1] + 1
             else dnBars[1];
def dnCycles = if c > ST and c[1] < ST[1]
               then dnCycles[1] + 1
               else dnCycles[1];
def upCycleCount = upBars / upCycles;
def dnCycleCount = dnBars / dnCycles;

def thisCycle = if c < ST and c[1] > ST[1]
                then 1
                else if c < ST
                then thisCycle[1] + 1
                else if c > ST and c[1] < ST[1]
                     then 1
                     else if c > ST
                          then thisCycle[1] + 1
                          else thisCycle[1];
def Volup = (fold i = 0 to thisCycle
             do if i > 0
                then Volup[1] + v
                else Volup[1]) / thisCycle;
DefineGlobalColor("LabelRed",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
AddLabel(ShowLabel, "Up Bars = " + upBars + "; " +
                  "  Up Cycles = " + upCycles + "; " +
                  "  Dn Bars = " + dnBars + "; " +
                  "  Dn Cycles = " + dnCycles + "; " ,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
AddLabel(ShowLabel,
                  "  Avg Up Cycle Count = " + Round(upCycleCount, 0) +
                  "  Avg Dn Cycle Count = " + Round(dnCycleCount, 0) +
                  "  This Cycle = " + thisCycle,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
# End Code SuperTrend
@MerryDay ... have you been able to scan with this? I've tried everything and no success.

Normally I'm pretty good working with other folks creations... especially @tomsk!

I'd appreciate some input. I've asked about this one prior but no one else has responded.

Thanks!
 
great indicator, there are some cases during the live market it will repaint and disappear if the trend changes direction. it's still one of my favorites
 
Last edited:
@dougn Posters tend to provide answers when they have answers. Given how active this forum is if you don't get an answer, generally, it is because it can't be done.
Read More Here
 
I just started playing around with this one specifically after a long break, so I haven't cut up the code yet, but has anybody run into this with a down signal/continued move up?
screenshot

*For some reason, the imgur url won't post a picture so I had to use a link
 
@dougn, I tried to complete your request but I cannot get the scan to work. I tried a very simple code and used the condition wizard but there are no matches found. Maybe somebody else has an answer to why this is, sorry I'm not much help. This is the code I used

Code:
close is greater than or equal to Supertrend_CCI_ATR("length cci" = 20, "length atr" = 4, "atr factor" = 0.7)."CSA"
or
Supertrend_CCI_ATR("length cci" = 20, "length atr" = 4, "atr factor" = 0.7)."BuySignalArrow" is true
 
Last edited:
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
is there a way to get the candle bars to change colors without having the "show plot" "show data line" check marked off?

I uncheck them and it does not repaint the candle bars. if those 2 boxes are checked off then it repaints the candle bars

just trying to clean up the lines
 
Supertrend CCI ATR Trend Enchanced

This is the script from post#1 to which I added Mobius's Supertrend Cycle Statistics Labels (many thanks to @sunnybabu, who found these on TOS OneNote). I haven't had time to see how having these statistics will affect my trading, yet. But I find them interesting.
Ruby:
# 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;
input ShowLabel = yes;
def c = close;
def v = volume;

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
def upBars = if c < ST
             then upBars[1] + 1
             else upBars[1];
def upCycles = if c < ST and c[1] > ST[1]
               then upCycles[1] + 1
               else upCycles[1];

def dnBars = if c > ST
             then dnBars[1] + 1
             else dnBars[1];
def dnCycles = if c > ST and c[1] < ST[1]
               then dnCycles[1] + 1
               else dnCycles[1];
def upCycleCount = upBars / upCycles;
def dnCycleCount = dnBars / dnCycles;

def thisCycle = if c < ST and c[1] > ST[1]
                then 1
                else if c < ST
                then thisCycle[1] + 1
                else if c > ST and c[1] < ST[1]
                     then 1
                     else if c > ST
                          then thisCycle[1] + 1
                          else thisCycle[1];
def Volup = (fold i = 0 to thisCycle
             do if i > 0
                then Volup[1] + v
                else Volup[1]) / thisCycle;
DefineGlobalColor("LabelRed",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
AddLabel(ShowLabel, "Up Bars = " + upBars + "; " +
                  "  Up Cycles = " + upCycles + "; " +
                  "  Dn Bars = " + dnBars + "; " +
                  "  Dn Cycles = " + dnCycles + "; " ,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
AddLabel(ShowLabel,
                  "  Avg Up Cycle Count = " + Round(upCycleCount, 0) +
                  "  Avg Dn Cycle Count = " + Round(dnCycleCount, 0) +
                  "  This Cycle = " + thisCycle,
if c < ST then GlobalColor("LabelRed") else GlobalColor("LabelGreen") );   #.... This portion of code modofies by jhf
# End Code SuperTrend
How can I adjust the distance of the Arrows to the Candles ?
Thanks
 
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
Hi Tom, thanks so much for sharing your work. The arrow I have is quite high up, is there a way to get it to close the price line?
 
Hi Jonas - if you remove the "0.995 *" and the "1.005 *" the arrow will be on the candle. I believe I did the same once - and found the arrows developing on the candle and didn't like it.
 
Hi Jonas - if you remove the "0.995 *" and the "1.005 *" the arrow will be on the candle. I believe I did the same once - and found the arrows developing on the candle and didn't like it.
Hi @GoldStriple Could you direct me on exactly what part of this code to remove? Thanks
# 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);
 
after the "then" is the logic for arrow placement.

This would be 'place arrow at MT1'
plot SellSignalArrow = if SellSignal then MT1 else Double.NaN;

Suggestion would be to use the high of the candle on down arrow and low of candle on Up arrows. therefore that would be this:
plot BuySignalArrow = if BuySignal then low else Double.NaN;
plot SellSignalArrow = if SellSignal then high else Double.NaN;
 
after the "then" is the logic for arrow placement.

This would be 'place arrow at MT1'
plot SellSignalArrow = if SellSignal then MT1 else Double.NaN;

Suggestion would be to use the high of the candle on down arrow and low of candle on Up arrows. therefore that would be this:
plot BuySignalArrow = if BuySignal then low else Double.NaN;
plot SellSignalArrow = if SellSignal then high else Double.NaN;
I used plot BuySignalArrow = if BuySignal then low else Double.NaN;
to replace this plot BuySignalArrow = if bar >= newState and State == StateUp and State[1] <> StateUp then 0.995 * MT1 else Double.NaN;

I got an error signal about if BuySignal where BuySignal was highlighted as an error?

Thanks!
 

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