A trend, momentum and cycle Trading System v3.0 (CSA)

@diazlaz Hi! I stumbled to this site while Googling for Heiken-Ashi charts and trading strategies. Been going thru the many many posts from the original thread and this one. So much to learn and it is kinda scary realising the huge gap in knowledge between the experts here and myself.

I was fiddling around with the scan code in Post #4 (https://usethinkscript.com/threads/...cycle-trading-system-v3-0-csa.1596/post-14870), trying to tweak it for fast execution when some indicators are disabled. I am not familiar with ThinkScript, still getting used to it. I am posting the changed version here, need some help in figuring out a NaN result in the Frema logic. Did some reading regarding possible causes but since I am not really familiar with the actual calculation, was not able to resolve it at that level. So, decided to just handle it at the end of the Frema code by checking for NaN on the EMA_Signal variable and return a not-matched condition but no matter how I construct the code, the end result is still a NaN. This then causes the final scan result to fail (regardless of other indicators) as well at the end of execution for such stock (INST, VTEX are two of the stocks having this issue due to lack of sufficient data). Hoping you can advise how best to handle such not a number conditions in ThinkScript. Note that the variable 'bodoh' actually is a local word in our national language that means 'stupid', kinda how I feel now! I tested the result by disabling all other indicators except Frema and having the value displayed in a custom column of a watchlist. Thanks for assisting.

Modified code:

Code:
# Trend v1.7 Bullish Scan
# tomsk
# 1.4.2020

# tomsk: Here is a bullish scan of the Trend V1.7 study, based on the study that @diazlaz posted
# If you'd like to configure this as a bearish scan, just replace the plot statement with
#
# plot scan = sBearish >= threshold;
#
#
#trend, a momentum and a cycle based indicator for ThinkorSwim V1.7
#@hockeycoachdoug Community Request
#
# https://usethinkscript.com/threads/i-request-combining-3-indicators-into-1-paintbar-study.1390/page-3#post-13087
#
#VERSION
# 2020.01.04 V1.7 @diazlaz - Integrated Slim Ribbon.
# 2020.01.02 V1.6 @diazlaz - Integrated DMI and BOP, restructured INPUTS to each module.
#                            threshold increased to 4 by default.
# 2020.01.01 V1.5 @diazlaz - Integrated ADX Trending States and BAR Line
# 2020.01.01 V1.4 @diazlaz - Integrated TTM Trends, modules selection
#                            and added threshold for signals
# 2019.12.31 V1.2 @diazlaz - Integrated FREMA
# 2019.12.30 V1.1 @diazlaz - Labels, Additional Dashboard, Lower Study
# 2019.12.30 V1.0 @diazlaz - Community Ask/Release

# Bullish or bearish scan
input bullish = yes;

# Minimum number of indicators that meet the scan conditions
input threshold = 1;

# The indicators to be enabled
input enableTTM = no;
input enableSuperTrend = no;
input enableFrema = yes;
input enableTMO = no;
input enableSlim = no;
input enableTOP = no;
input enableDMI = no;
input enableBOP = no;

input displace = 0;

# Count number of indicators that are enabled via configuration
def ethreshold = enableTTM + enableSuperTrend + enableFrema + enableTMO +
                 enableSlim + enableTOP + enableDMI + enableBOP;

# Final threshold count that will be used. Was thinking that threshold was number of indicators that need to be true for stock to meet scan criterion.
def cthreshold;

# Ensure that configured threshold count is not higher than the number of actual indicators that are enabled.
if threshold > ethreshold then
{
   cthreshold = ethreshold; # Reset to actual number of enabled indicators
} else {
   cthreshold = threshold; # Minimum number of indicators to meet the scan condition.
}

def h = high;
def l = low;
def o = open;
def c = close;

# TTM Trend
def sBullish_TTM;

if enableTTM then
{
  sBullish_TTM = if TTM_Trend().TrendUp == 1 then 1 else 0;
} else {
  sBullish_TTM = 0;
}

def sBearish_TTM;

if enableTTM then
{
  sBearish_TTM = if TTM_Trend().TrendDown == 1 then 1 else 0;
} else {
  sBearish_TTM = 0;
}

# SuperTrend (Mobius)
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;

def ATR;
def UP;
def DN;
def ST;
def sBullish_SuperTrend;
def sBearish_SuperTrend;

# Restructured code so that disabled indicators related code is not executed to reduce running time. The same has been done for all below.
if enableSuperTrend then
{
  ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
  UP = HL2 + (AtrMult * ATR);
  DN = HL2 + (-AtrMult * ATR);
  ST = if close < ST[1] then UP else DN;
  sBullish_SuperTrend = close >= ST;
  sBearish_SuperTrend = close < ST;
} else {
  ATR = 0;
  UP = 0;
  DN = 0;
  ST = 0;
  sBullish_SuperTrend = 0;
  sBearish_SuperTrend = 0;
}

# FREMA
input AA = .1;

def CC;
def EMA;
def RE1;
def RE2;
def RE3;
def RE4;
def RE5;
def RE6;
def RE7;
def RE8;
def EMA_Signal;
def sBullish_Frema;
def sBearish_Frema;
def bodoh;

if enableFrema then
{
  CC = if CC[1] == 0 then .9 else 1 – AA;
  EMA = AA * close + CC * EMA[1];
  RE1 = CC * EMA + EMA[1];
  RE2 = Power(CC, 2) * RE1 + RE1[1];
  RE3 = Power(CC, 4) * RE2 + RE2[1];
  RE4 = Power(CC, 8) * RE3 + RE3[1];
  RE5 = Power(CC, 16) * RE4 + RE4[1];
  RE6 = Power(CC, 32) * RE5 + RE5[1];
  RE7 = Power(CC, 64) * RE6 + RE6[1];
  RE8 = Power(CC, 128) * RE7 + RE7[1];
  EMA_Signal = EMA – AA * RE8;
  bodoh = isNaN(EMA_Signal);
  if bodoh then {
     sBullish_Frema = 0;
     sBearish_Frema = 0;
  } else {
     sBullish_Frema = EMA_Signal > 0;
     sBearish_Frema = EMA_Signal <= 0;
  }
} else {
  bodoh =0;
  CC = 0;
  EMA = 0;
  RE1 = 0;
  RE2 = 0;
  RE3 = 0;
  RE4 = 0;
  RE5 = 0;
  RE6 = 0;
  RE7 = 0;
  RE8 = 0;
  EMA_Signal = 0;
  sBullish_Frema = 0;
  sBearish_Frema = 0;
}

# TMO
input length = 14;
input calcLength = 5;
input smoothLength = 3;

def data;
def EMA5;
def Main;
def Signal;
def sBullish_TMO;
def sBearish_TMO;
if enableTMO then
{
    data = fold i = 0 to length
           with s
           do s + (if c > getValue(o, i)
                   then 1
                   else if c < getValue(o, i)
                        then - 1
                        else 0);
    EMA5 = ExpAverage(data, calcLength);
    Main = ExpAverage(EMA5, smoothLength);
    Signal = ExpAverage(Main, smoothLength);
    sBullish_TMO = Main > Signal;
    sBearish_TMO = Main <= Signal;
} else {
    data = 0;
    EMA5 = 0;
    Main = 0;
    Signal = 0;
    sBullish_TMO = 0;
    sBearish_TMO = 0;
}

# SLIM
input sSuperFast = 8;
input sFast = 13;
input sSlow = 21;

def SuperFastM1;
def FastM1;
def SlowM1;
def buyM1;
def stopbuyM1;
def buynowM1;
def buysignalM1;
def Buy_SignalM1;
def sellM1;
def stopsellM1;
def sellnowM1;
def sellsignalM1;
def Sell_SignalM1;
def sBullish_SLIM;
def sBearish_SLIM;

if enableSLIM then
{
    SuperFastM1 = ExpAverage(close[-displace], sSuperFast);
    FastM1 = ExpAverage(close[-displace], sFast);
    SlowM1 = ExpAverage(close[-displace], sSlow);
    buyM1 = SuperFastM1 > FastM1 and FastM1 > SlowM1 and low > SuperFastM1;
    stopbuyM1 = SuperFastM1 <= FastM1;
    buynowM1 = buyM1 and !buyM1[1];
    buysignalM1 = if buynowM1 and !stopbuyM1 then 1
                  else if buysignalM1[1] and stopbuyM1 then 0
                  else buysignalM1[1];
    Buy_SignalM1 = buysignalM1 and !buysignalM1[1];
    sellM1 = SuperFastM1 < FastM1 and FastM1 < SlowM1 and high < SuperFastM1;
    stopsellM1 = SuperFastM1 >= FastM1;
    sellnowM1 = sellM1 and !SellM1[1];
    sellsignalM1 = if sellnowM1 and !stopsellM1 then 1
                   else if sellsignalM1[1] and stopsellM1 then 0
                   else sellsignalM1[1];
    Sell_SignalM1 = sellsignalM1 and !sellsignalM1[1];
    sBullish_SLIM = Buy_SignalM1;
    sBearish_SLIM = Sell_SignalM1;
} else {
    SuperFastM1 = 0;
    FastM1 = 0;
    SlowM1 = 0;
    buyM1 = 0;
    stopbuyM1 = 0;
    buynowM1 = 0;
    buysignalM1 = 0;
    Buy_SignalM1 = 0;
    sellM1 = 0;
    stopsellM1 = 0;
    sellnowM1 = 0;
    sellsignalM1 = 0;
    Sell_SignalM1 = 0;
    sBullish_SLIM = 0;
    sBearish_SLIM = 0;
}

# TOP Cycle Trader
input FastCycleLength = 5;
input SlowCycleLength = 8;

def FastVar;
def SlowVar;
def DiffVar;
def sBullish_TOP;
def sBearish_TOP;

if enableTOP then
{
    FastVar = ExpAverage((H + L) / 2, FastCycleLength);
    SlowVar = Average((H + L) / 2, SlowCycleLength);
    DiffVar = FastVar - SlowVar;
    sBullish_TOP = DiffVar > 0;
    sBearish_TOP = DiffVar <= 0;
} else {
    FastVar = 0;
    SlowVar = 0;
    DiffVar = 0;
    sBullish_TOP = 0;
    sBearish_TOP = 0;
}

# DMITrend
input DMIlength = 13;

def hiDiff;
def loDiff;
def plusDM;
def minusDM;
def ATRDMI;
Def DIPlus;
Def DIMinus;
def sBullish_DMI;
def sBearish_DMI;

if enableDMI then
{
    hiDiff = high - high[1];
    loDiff = low[1] - low;
    plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
    minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;
    ATRDMI = WildersAverage(TrueRange(high, close, low), DMIlength);
    DIPlus = 100 * WildersAverage(plusDM, DMIlength) / ATRDMI;
    DIMinus = 100 * WildersAverage(minusDM, DMIlength) / ATRDMI;
    sBullish_DMI = DIPlus > DIMinus;
    sBearish_DMI = DIMinus > DIPlus;
} else {
    hiDiff = 0;
    loDiff = 0;
    plusDM = 0;
    minusDM = 0;
    ATRDMI = 0;
    DIPlus = 0;
    DIMinus = 0;
    sBullish_DMI = 0;
    sBearish_DMI = 0;
}

# BOP indicator
input averageType = {Simple, Exponential, default Weighted, Wilders, Hull,  Disabled};
input BoPlength = 16;

def rawBMP = if high != low then (close - open) / (high - low) else 1;
def BMP;
def sBullish_BOP;
def sBearish_BOP;

if enableBOP then
{
   switch (averageType) {
   case Simple:
       BMP = Average(rawBMP, BoPlength);
   case Exponential:
       BMP = ExpAverage(rawBMP, BoPlength);
   case Weighted:
       BMP = wma(rawBMP, BoPlength);
   case Wilders:
       BMP = WildersAverage(rawBMP, BoPlength);
   case Hull:
       BMP = HullMovingAvg(rawBMP, BoPlength);
   case Disabled:
       BMP = rawBMP;
   }
    sBullish_BOP = BMP > 0;
    sBearish_BOP = BMP <= 0;
} else {
    BMP = 0;
    sBullish_BOP = 0;
    sBearish_BOP = 0;
}

# SCAN

plot scan;

if bullish then
{
   scan = cthreshold <= sBullish_TTM + sBullish_SuperTrend + sBullish_Frema + sBullish_TMO + sBullish_TOP + sBullish_DMI + sBullish_BOP + sBullish_SLIM;
} else {
   scan = cthreshold <= sBearish_TTM + sBearish_SuperTrend + sBearish_Frema + sBearish_TMO + sBearish_TOP + sBearish_DMI + sBearish_BOP + sBearish_SLIM;
}

# End Trend v1.7 Bullish Scan
 
Welcome to the Forum @nsubbra There really isn't any reason to scan for all these indicators on one study. It is more standard to load each indicator separately into the scanner. This uses less resources and will cause fewer problems.
 
@MerryDay thanks for replying. I truly understand what you mean. This is just me trying to learn thinkscript by making changes to the code I find here. Too complex scans tend to take too long to run and timeout anyways, it generally is a waste of time and defeats the usability of such scans.

The main reason for the post was to get help to understand how NaN behavior is in thinkscript code. I actually got the scan code to work fine in entirety but it looks like once a NaN value is assigned to variable, the behavior of the code is not as I would expect. The Frema logic works fine if written like below. If I attempt to assign the result of EMA – AA * RE8 to a variable, and then use the variable in isNan() test, it will fail. I might have this wrong in some manner, anyways, just learning. Anyways, please disregard the earlier post.

Code:
if enableFrema then
{
  CC = if CC[1] == 0 then .9 else 1 – AA;
  EMA = AA * close + CC * EMA[1];
  RE1 = CC * EMA + EMA[1];
  RE2 = Power(CC, 2) * RE1 + RE1[1];
  RE3 = Power(CC, 4) * RE2 + RE2[1];
  RE4 = Power(CC, 8) * RE3 + RE3[1];
  RE5 = Power(CC, 16) * RE4 + RE4[1];
  RE6 = Power(CC, 32) * RE5 + RE5[1];
  RE7 = Power(CC, 64) * RE6 + RE6[1];
  RE8 = Power(CC, 128) * RE7 + RE7[1];
 
# EMA_Signal = EMA – AA * RE8;
#  bodoh = isNaN(EMA_Signal);
#  if bodoh then {
#     sBullish_Frema = 0;
#     sBearish_Frema = 0;
#  } else {
#     sBullish_Frema = EMA_Signal > 0;
#     sBearish_Frema = EMA_Signal <= 0;
#  }

EMA_Signal = if !isNaN(EMA – AA * RE8) then EMA – AA * RE8 else 0;
  sBullish_Frema = EMA_Signal > 0;
  sBearish_Frema = EMA_Signal <= 0;
} else {
  CC = 0;
  EMA = 0;
  RE1 = 0;
  RE2 = 0;
  RE3 = 0;
  RE4 = 0;
  RE5 = 0;
  RE6 = 0;
  RE7 = 0;
  RE8 = 0;
  EMA_Signal = 0;
  sBullish_Frema = 0;
  sBearish_Frema = 0;
}
 
@nsubbra IsNan stands for "is not a number", meaning no number, nor zero, has been assigned to the variable... Essentially a Null variable check... Click the link I provided to learn more...
 
are any New Update for ..
A trend, momentum and cycle Trading System v3.2 (CSA)
 
Last edited by a moderator:
Inspired by the work of the community here, this is the continuation of the CSA studies series.

mvcJnlS.png


The studies integrates and unifies a number of community studies and provides signals allowing the construction of trading strategies and the development of trading plans. It's community driven initiative. It includes studies such as TTM Trend, Super Trend, Frema, TMO, TOP, DMI , BOP, Slim Ribbon, ADX as Trending/Non-Trending, Super Trend w/CCI and ATR combo and a tri-fecta of CCI + TTM squeeze + TTM trend Combo with entry signals on LONG/SHORT/HOLD.
Pretty incredible work u did on this.
 
@diazlaz Hi! I stumbled to this site while Googling for Heiken-Ashi charts and trading strategies. Been going thru the many many posts from the original thread and this one. So much to learn and it is kinda scary realising the huge gap in knowledge between the experts here and myself.

I was fiddling around with the scan code in Post #4 (https://usethinkscript.com/threads/...cycle-trading-system-v3-0-csa.1596/post-14870), trying to tweak it for fast execution when some indicators are disabled. I am not familiar with ThinkScript, still getting used to it. I am posting the changed version here, need some help in figuring out a NaN result in the Frema logic. Did some reading regarding possible causes but since I am not really familiar with the actual calculation, was not able to resolve it at that level. So, decided to just handle it at the end of the Frema code by checking for NaN on the EMA_Signal variable and return a not-matched condition but no matter how I construct the code, the end result is still a NaN. This then causes the final scan result to fail (regardless of other indicators) as well at the end of execution for such stock (INST, VTEX are two of the stocks having this issue due to lack of sufficient data). Hoping you can advise how best to handle such not a number conditions in ThinkScript. Note that the variable 'bodoh' actually is a local word in our national language that means 'stupid', kinda how I feel now! I tested the result by disabling all other indicators except Frema and having the value displayed in a custom column of a watchlist. Thanks for assisting.

Modified code:

Code:
# Trend v1.7 Bullish Scan
# tomsk
# 1.4.2020

# tomsk: Here is a bullish scan of the Trend V1.7 study, based on the study that @diazlaz posted
# If you'd like to configure this as a bearish scan, just replace the plot statement with
#
# plot scan = sBearish >= threshold;
#
#
#trend, a momentum and a cycle based indicator for ThinkorSwim V1.7
#@hockeycoachdoug Community Request
#
# https://usethinkscript.com/threads/i-request-combining-3-indicators-into-1-paintbar-study.1390/page-3#post-13087
#
#VERSION
# 2020.01.04 V1.7 @diazlaz - Integrated Slim Ribbon.
# 2020.01.02 V1.6 @diazlaz - Integrated DMI and BOP, restructured INPUTS to each module.
#                            threshold increased to 4 by default.
# 2020.01.01 V1.5 @diazlaz - Integrated ADX Trending States and BAR Line
# 2020.01.01 V1.4 @diazlaz - Integrated TTM Trends, modules selection
#                            and added threshold for signals
# 2019.12.31 V1.2 @diazlaz - Integrated FREMA
# 2019.12.30 V1.1 @diazlaz - Labels, Additional Dashboard, Lower Study
# 2019.12.30 V1.0 @diazlaz - Community Ask/Release

# Bullish or bearish scan
input bullish = yes;

# Minimum number of indicators that meet the scan conditions
input threshold = 1;

# The indicators to be enabled
input enableTTM = no;
input enableSuperTrend = no;
input enableFrema = yes;
input enableTMO = no;
input enableSlim = no;
input enableTOP = no;
input enableDMI = no;
input enableBOP = no;

input displace = 0;

# Count number of indicators that are enabled via configuration
def ethreshold = enableTTM + enableSuperTrend + enableFrema + enableTMO +
                 enableSlim + enableTOP + enableDMI + enableBOP;

# Final threshold count that will be used. Was thinking that threshold was number of indicators that need to be true for stock to meet scan criterion.
def cthreshold;

# Ensure that configured threshold count is not higher than the number of actual indicators that are enabled.
if threshold > ethreshold then
{
   cthreshold = ethreshold; # Reset to actual number of enabled indicators
} else {
   cthreshold = threshold; # Minimum number of indicators to meet the scan condition.
}

def h = high;
def l = low;
def o = open;
def c = close;

# TTM Trend
def sBullish_TTM;

if enableTTM then
{
  sBullish_TTM = if TTM_Trend().TrendUp == 1 then 1 else 0;
} else {
  sBullish_TTM = 0;
}

def sBearish_TTM;

if enableTTM then
{
  sBearish_TTM = if TTM_Trend().TrendDown == 1 then 1 else 0;
} else {
  sBearish_TTM = 0;
}

# SuperTrend (Mobius)
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;

def ATR;
def UP;
def DN;
def ST;
def sBullish_SuperTrend;
def sBearish_SuperTrend;

# Restructured code so that disabled indicators related code is not executed to reduce running time. The same has been done for all below.
if enableSuperTrend then
{
  ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
  UP = HL2 + (AtrMult * ATR);
  DN = HL2 + (-AtrMult * ATR);
  ST = if close < ST[1] then UP else DN;
  sBullish_SuperTrend = close >= ST;
  sBearish_SuperTrend = close < ST;
} else {
  ATR = 0;
  UP = 0;
  DN = 0;
  ST = 0;
  sBullish_SuperTrend = 0;
  sBearish_SuperTrend = 0;
}

# FREMA
input AA = .1;

def CC;
def EMA;
def RE1;
def RE2;
def RE3;
def RE4;
def RE5;
def RE6;
def RE7;
def RE8;
def EMA_Signal;
def sBullish_Frema;
def sBearish_Frema;
def bodoh;

if enableFrema then
{
  CC = if CC[1] == 0 then .9 else 1 – AA;
  EMA = AA * close + CC * EMA[1];
  RE1 = CC * EMA + EMA[1];
  RE2 = Power(CC, 2) * RE1 + RE1[1];
  RE3 = Power(CC, 4) * RE2 + RE2[1];
  RE4 = Power(CC, 8) * RE3 + RE3[1];
  RE5 = Power(CC, 16) * RE4 + RE4[1];
  RE6 = Power(CC, 32) * RE5 + RE5[1];
  RE7 = Power(CC, 64) * RE6 + RE6[1];
  RE8 = Power(CC, 128) * RE7 + RE7[1];
  EMA_Signal = EMA – AA * RE8;
  bodoh = isNaN(EMA_Signal);
  if bodoh then {
     sBullish_Frema = 0;
     sBearish_Frema = 0;
  } else {
     sBullish_Frema = EMA_Signal > 0;
     sBearish_Frema = EMA_Signal <= 0;
  }
} else {
  bodoh =0;
  CC = 0;
  EMA = 0;
  RE1 = 0;
  RE2 = 0;
  RE3 = 0;
  RE4 = 0;
  RE5 = 0;
  RE6 = 0;
  RE7 = 0;
  RE8 = 0;
  EMA_Signal = 0;
  sBullish_Frema = 0;
  sBearish_Frema = 0;
}

# TMO
input length = 14;
input calcLength = 5;
input smoothLength = 3;

def data;
def EMA5;
def Main;
def Signal;
def sBullish_TMO;
def sBearish_TMO;
if enableTMO then
{
    data = fold i = 0 to length
           with s
           do s + (if c > getValue(o, i)
                   then 1
                   else if c < getValue(o, i)
                        then - 1
                        else 0);
    EMA5 = ExpAverage(data, calcLength);
    Main = ExpAverage(EMA5, smoothLength);
    Signal = ExpAverage(Main, smoothLength);
    sBullish_TMO = Main > Signal;
    sBearish_TMO = Main <= Signal;
} else {
    data = 0;
    EMA5 = 0;
    Main = 0;
    Signal = 0;
    sBullish_TMO = 0;
    sBearish_TMO = 0;
}

# SLIM
input sSuperFast = 8;
input sFast = 13;
input sSlow = 21;

def SuperFastM1;
def FastM1;
def SlowM1;
def buyM1;
def stopbuyM1;
def buynowM1;
def buysignalM1;
def Buy_SignalM1;
def sellM1;
def stopsellM1;
def sellnowM1;
def sellsignalM1;
def Sell_SignalM1;
def sBullish_SLIM;
def sBearish_SLIM;

if enableSLIM then
{
    SuperFastM1 = ExpAverage(close[-displace], sSuperFast);
    FastM1 = ExpAverage(close[-displace], sFast);
    SlowM1 = ExpAverage(close[-displace], sSlow);
    buyM1 = SuperFastM1 > FastM1 and FastM1 > SlowM1 and low > SuperFastM1;
    stopbuyM1 = SuperFastM1 <= FastM1;
    buynowM1 = buyM1 and !buyM1[1];
    buysignalM1 = if buynowM1 and !stopbuyM1 then 1
                  else if buysignalM1[1] and stopbuyM1 then 0
                  else buysignalM1[1];
    Buy_SignalM1 = buysignalM1 and !buysignalM1[1];
    sellM1 = SuperFastM1 < FastM1 and FastM1 < SlowM1 and high < SuperFastM1;
    stopsellM1 = SuperFastM1 >= FastM1;
    sellnowM1 = sellM1 and !SellM1[1];
    sellsignalM1 = if sellnowM1 and !stopsellM1 then 1
                   else if sellsignalM1[1] and stopsellM1 then 0
                   else sellsignalM1[1];
    Sell_SignalM1 = sellsignalM1 and !sellsignalM1[1];
    sBullish_SLIM = Buy_SignalM1;
    sBearish_SLIM = Sell_SignalM1;
} else {
    SuperFastM1 = 0;
    FastM1 = 0;
    SlowM1 = 0;
    buyM1 = 0;
    stopbuyM1 = 0;
    buynowM1 = 0;
    buysignalM1 = 0;
    Buy_SignalM1 = 0;
    sellM1 = 0;
    stopsellM1 = 0;
    sellnowM1 = 0;
    sellsignalM1 = 0;
    Sell_SignalM1 = 0;
    sBullish_SLIM = 0;
    sBearish_SLIM = 0;
}

# TOP Cycle Trader
input FastCycleLength = 5;
input SlowCycleLength = 8;

def FastVar;
def SlowVar;
def DiffVar;
def sBullish_TOP;
def sBearish_TOP;

if enableTOP then
{
    FastVar = ExpAverage((H + L) / 2, FastCycleLength);
    SlowVar = Average((H + L) / 2, SlowCycleLength);
    DiffVar = FastVar - SlowVar;
    sBullish_TOP = DiffVar > 0;
    sBearish_TOP = DiffVar <= 0;
} else {
    FastVar = 0;
    SlowVar = 0;
    DiffVar = 0;
    sBullish_TOP = 0;
    sBearish_TOP = 0;
}

# DMITrend
input DMIlength = 13;

def hiDiff;
def loDiff;
def plusDM;
def minusDM;
def ATRDMI;
Def DIPlus;
Def DIMinus;
def sBullish_DMI;
def sBearish_DMI;

if enableDMI then
{
    hiDiff = high - high[1];
    loDiff = low[1] - low;
    plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
    minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;
    ATRDMI = WildersAverage(TrueRange(high, close, low), DMIlength);
    DIPlus = 100 * WildersAverage(plusDM, DMIlength) / ATRDMI;
    DIMinus = 100 * WildersAverage(minusDM, DMIlength) / ATRDMI;
    sBullish_DMI = DIPlus > DIMinus;
    sBearish_DMI = DIMinus > DIPlus;
} else {
    hiDiff = 0;
    loDiff = 0;
    plusDM = 0;
    minusDM = 0;
    ATRDMI = 0;
    DIPlus = 0;
    DIMinus = 0;
    sBullish_DMI = 0;
    sBearish_DMI = 0;
}

# BOP indicator
input averageType = {Simple, Exponential, default Weighted, Wilders, Hull,  Disabled};
input BoPlength = 16;

def rawBMP = if high != low then (close - open) / (high - low) else 1;
def BMP;
def sBullish_BOP;
def sBearish_BOP;

if enableBOP then
{
   switch (averageType) {
   case Simple:
       BMP = Average(rawBMP, BoPlength);
   case Exponential:
       BMP = ExpAverage(rawBMP, BoPlength);
   case Weighted:
       BMP = wma(rawBMP, BoPlength);
   case Wilders:
       BMP = WildersAverage(rawBMP, BoPlength);
   case Hull:
       BMP = HullMovingAvg(rawBMP, BoPlength);
   case Disabled:
       BMP = rawBMP;
   }
    sBullish_BOP = BMP > 0;
    sBearish_BOP = BMP <= 0;
} else {
    BMP = 0;
    sBullish_BOP = 0;
    sBearish_BOP = 0;
}

# SCAN

plot scan;

if bullish then
{
   scan = cthreshold <= sBullish_TTM + sBullish_SuperTrend + sBullish_Frema + sBullish_TMO + sBullish_TOP + sBullish_DMI + sBullish_BOP + sBullish_SLIM;
} else {
   scan = cthreshold <= sBearish_TTM + sBearish_SuperTrend + sBearish_Frema + sBearish_TMO + sBearish_TOP + sBearish_DMI + sBearish_BOP + sBearish_SLIM;
}

# End Trend v1.7 Bullish Scan
The scan says its to complex for the scanner. Is it possible to scan for the green bars and red bars only?
 
The scan says its to complex for the scanner. Is it possible to scan for the green bars and red bars only?
A workaround that members have had success with when encountering the too complex error is to scan for each study individually. Each study is identified by name within this script. Search the forum for each of those individual studies and build them into your scan. (y)
 
A workaround that members have had success with when encountering the too complex error is to scan for each study individually. Each study is identified by name within this script. Search the forum for each of those individual studies and build them into your scan. (y)
thanks for the reply...I cut and pasted the code below and it has several errors, couldn't find a copy of his on the site...Do you have one? thanks



#ADX TRENDING
input ADXlength = 8; #ADX
input ADXTrending = 25; #ADX
input showADXBar = yes;
input DI_length = 8;

def DX = if (diplus(di_length) + diminus(di_length) > 0) then 100 * AbsValue(diplus(di_length) - diminus(di_length)) / (diplus(di_length) + diminus(di_length)) else 0;
def ADX = WildersAverage(DX, adxlength);

plot ADXCross = LowestAll(low);
ADXCross.SetPaintingStrategy(PaintingStrategy.LINE);
ADXCross.AssignValueColor(
if ADX > ADXTrending then Color.LIME
else Color.DARK_GRAY);
ADXCross.SetLineWeight(5);
ADXCross.SetHiding(!showADXBar);

#MACD Neutal Eval
input fastLength_1 = 12;
input slowLength_1 = 26;
input MACDLength_1 = 9;
input AverageType_1 = {SMA, default EMA};
def macd_Val_1 = MACD(fastLength_1, slowLength_1, MACDLength_1, AverageType_1).Value;
def macd_Avg1 = MACD(fastLength_1, slowLength_1, MACDLength_1, AverageType_1).Avg;
def MACD_trig = if MACD().value > MACD().avg then 1 else 0;

AddLabel(showlabels,
if ADX > ADXTrending then "Trending" else
"Non Trending",
if IsNan(c) then COLOR.DARK_GRAY else
if ADX > ADXTrending then COLOR.GREEN else
COLOR.DARK_GRAY);

AddLabel(showlabels,
"MACD",
if IsNan(c) then COLOR.DARK_GRAY else
if macd_Val_1 > macd_Avg1 then COLOR.DARK_GREEN else
COLOR.DARK_RED);


#STATE
def sResults = HA1_sState + HAMTF_sState + TTM_sState + ST1_ssupertrend + STHA_ssupertrend + FRE_sfrema + TMO_stmo + SL1_sstate + TOP_stop + DMI_sdmi + BOP_sbop
+ STCCI_ssupertrendcci + CTT_sccittm + DRSI_sstate + SWCA_sstate;

def sBullish = HA1_sBullish + HAMTF_sBullish + TTM_sBullish + ST1_sbullish + STHA_sbullish + FRE_sbullish + TMO_sbullish + SL1_sbullish + TOP_sbullish + DMI_sbullish + BOP_sbullish + STCCI_sbullish + CTT_sbullish + DRSI_sbullish + SWCA_sbullish;

def sBearish = HA1_sBearish + HAMTF_sBearish + TTM_sBearish + ST1_sbearish + STHA_sbearish + FRE_sbearish + TMO_sbearish + SL1_sbearish + TOP_sbearish + DMI_sbearish + BOP_sbearish + STCCI_sbearish + CTT_sbearish + DRSI_sbearish + SWCA_sbearish;

def bullish = if(sBullish >= threshold,1,0);
def bearish = if(sBearish >= threshold,1,0);
def sState = if bullish then 100 else if bearish then -100 else 0;
def sState2 = if sState != 0 then sState else sState2[1];

#COLORBARS
AssignPriceColor(
if PaintBars then
if ADXPaintBarsMode then if ADX > ADXTrending then COLOR.ORANGE else COLOR.DARK_GRAY
else
if bullish then COLOR.GREEN else if bearish then COLOR.RED
else
if enableNeutralMACDPainter then
if macd_Val_1 > macd_Avg1 then COLOR.DARK_GREEN else COLOR.DARK_RED
else
COLOR.GRAY
else COLOR.CURRENT);
 
thanks for the reply...I cut and pasted the code below and it has several errors, couldn't find a copy of his on the site...Do you have one? thanks
Are you asking about ADX and MACD indicators? Those are built-in TOS studies. You can place them individually in your scanner.
 
Are you asking about ADX and MACD indicators? Those are built-in TOS studies. You can place them individually in your scanner.
Hey Merry! So ive been backtesting this strategy and if you follow the system over time it is very profitable the only problem i have is I do not know how to set alerts when there is a buy or sell signal..... can you help me with that?
 
Hey Merry! So ive been backtesting this strategy and if you follow the system over time it is very profitable the only problem i have is I do not know how to set alerts when there is a buy or sell signal..... can you help me with that?

It's not a strategy. It's an information study that is monitoring multiple signals. So if 7 out of 10 signals are green light then its saying bullish. the threshold sets the number of signals you are accepting as valid to determine a trend bull or bear.

I do not recommend using this. As @irishgold states this is monitoring multiple signals. Any 7 of the 10 could be firing at any time; alerts could end up firing almost constantly.

But ask and thou shallt receive. Here is an alert for whenever the threshold of 7 indicators is breached. Should be added to the bottom of your study:
Code:
# Alert
Alert(bullish and !bullish[1], "Bullish", Alert.Bar, Sound.Chimes);
# Alert
Alert(!bullish and bullish[1], "Bearish", Alert.Bar, Sound.Chimes);
 
I do not recommend using this. As @irishgold states this is monitoring multiple signals. Any 7 of the 10 could be firing at any time; alerts could end up firing almost constantly.

But ask and thou shallt receive. Here is an alert for whenever the threshold of 7 indicators is breached. Should be added to the bottom of your study:
Code:
# Alert
Alert(bullish and !bullish[1], "Bullish", Alert.Bar, Sound.Chimes);
# Alert
Alert(!bullish and bullish[1], "Bearish", Alert.Bar, Sound.Chimes);
what causes the strategy to buy or sell? I checked the "show report" and it takes trades off of something.... Are you saying that whenever anyone of the 10 fires off it buys?
 
what causes the strategy to buy or sell? I checked the "show report" and it takes trades off of something.... Are you saying that whenever anyone of the 10 fires off it buys?
Sorry, I can't help you. The original code in this thread has no "addstrategy" to trigger a buy, no arrows, or anything else.
The original code is written to show trend. There is a default threshold of 7. IE: if 7 out of the 10 indicators are positive then a label states bullish.
 
@diazlaz @HighBredCloud I think you will like the solution to the sharp edges of the sharp geometry of the Schaff Wave - Shaff Trend rectangles, you can keep the standard time intervals recognized the world over for MACD just use this code you were already using here. https://usethinkscript.com/threads/price-trend-indicator-for-thinkorswim.13/page-3
Code:
input SWCA_fastlengthwave = 12;
input SWCA_slowlengthwave = 26;
Without being thrown off in odd time increments...
The solution is to use multiple Schaff time frames. Maybe 13 like the lower in this photo?
input SWCA_fastlengthwave = 12;
input SWCA_slowlengthwave = 26;
https://usethinkscript.com/threads/...swim.13/page-2#lg=_xfUid-1-1586461542&slide=0Does anyone have the code to this lower with the 13 moving averages?
Now I might have to do the subtraction step in the Schaff indicator multiple times but that shouldn't be resource intensive..
@vvcv does anybody have the code to this lower? If not I can figure it out but I would not present it in those bright neon lights I would just have a soft cloud behind the candlesticks and green & red for Bull Bear, AND yellow for narrowing & Black for close trade...
That is what I will do and then I will use some of there for prerequisites to see if bars should be colored at all or left black!
I think this will be popular. I think also require certain ADX minimum too
Did you ever get this bottom indicator figured out!?
 
Lastest Release: V3.2 - Last Updated: February 1, 2020

Release Modules

  1. Module HA1 - Heikin Ashi Candles
  2. Module HAMTF - Heikin Ashi Candles MTF (Multiple Time Frame)
  3. Module TTM - TTM Trend Module
  4. Module ST1 - SuperTrend Module
  5. Module STHA - SuperTrend HA
  6. Module SL1 - SLIM Module
  7. Module TOP - TOP Module
  8. Module DMI - DMI Trend
  9. Module BOP - BOP Module
  10. Module STCCI - Super Trend w/CCI Module
  11. Module CTT - CCI + TTM squeeze + TTM trend Module
  12. Module DRSI - Derivative RSI Oscillator Module
  13. Module SWCA - Schaff Wave CrossOver Average

Ruby:
#CSA - trend, a momentum and a cycle based indicator for ThinkorSwim V3.2
#
#VERSION
# 2020.02.01 V3.2 @diazlaz - Added SWCA - Schaff Wave CrossOver Average
# 2020.01.29 V3.1 @diazlaz - Added ADXPaintBarsMode
#                            Integrated Derivative RSI Oscillator
# 2020.01.29 V3.0 @diazlaz - Modules Restructured 3.0 Framework
#                            Improved ADX formula for Trend Detection
#                            Integrated HeikinAshiCandles and HeikinAshiCandles MTF
#                            set aggregationPeriod to MTF aggregation (default 5 mins)
#                            Integrated PaintBars MACD Neutral Condition trigger
#                            set enableNeutralMACDPainter to yes (default is no) and
#                            neutral gray will be evaluated by MACD (Dark Red/Green)
#                            Integrated Super Trend w/CCI and ATR
#                            Integrated CCI + TTM squeeze + TTM trend Combo
#                            Set treshold defaults to 7 (7 out of 10), removed scanmode.
#                            Integrated Slim Ribbon.
#                            Integrated DMI and BOP, restructured INPUTS to each module.
#                            threshold increased to 4 by default.
#                            Integrated ADX Trending States and BAR Line
#                            Integrated TTM Trends, modules selection
#                            and added threshold for signals
#                            Integrated FREMA
#                            Labels, Additional Dashboard, Lower Study
#
#
#LINK
#https://usethinkscript.com/threads/a-trend-momentum-and-cycle-trading-system-v3-0-csa.1596/
#
#INSTRUCTION
#Here is how I envision the paintbar working:
#When the supertrend indicator is long that would equal +1, when its short -1.
#When the TMO is green that would equal +1. when its red its -1.
#When the TOP Cycle Trader is above zero that would equal +1, when its below zero -1.
#When all 3 indicators are equal to +1 making +3 total the price bars would paint #green.
#When all 3 indicators are equal to -1 making -3 total the price bars would paint red.
#If the combination of all 3 indicators equals anything else, the price bars would #paint gray meaning all 3 indicators were not in agreement.
#


declare upper;

#INPUTS
input showLabels = yes;
input PaintBars = yes;
input ADXPaintBarsMode = no;
input enableNeutralMACDPainter = no;
input aggregationPeriod = AggregationPeriod.FIVE_MIN;
input threshold = 7;

input displace = 0;

#CORE
DefineGlobalColor("CycleLineColor", Color.RED);
DefineGlobalColor("CyclehistColor", Color.BLUE);
DefineGlobalColor("ZeroLineColor", Color.YELLOW);
DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Bearish", Color.RED);
DefineGlobalColor("Neutral", Color.MAGENTA); #Color.YELLOW
DefineGlobalColor("Off", Color.DARK_GRAY);
DefineGlobalColor("On", Color.GREEN);
DefineGlobalColor("Sync1", Color.YELLOW);
DefineGlobalColor("Sync2", Color.CYAN);
DefineGlobalColor("Up", Color.GREEN);
DefineGlobalColor("Down", Color.RED);
DefineGlobalColor("NUp", Color.DARK_GREEN);
DefineGlobalColor("NDown", Color.DARK_RED);
DefineGlobalColor("Neutral", Color.BLUE);
DefineGlobalColor("Neutral2", Color.PLUM);

def h = high;
def l = low;
def o = open;
def c = close;
def hl = hl2;

def h1 = high(period = aggregationPeriod);
def l1 = low(period = aggregationPeriod);
def o1 = open(period = aggregationPeriod);
def c1 = close(period = aggregationPeriod);
def hl2 = hl2(period = aggregationPeriod);

def na = Double.NaN;

AddLabel(showLabels, "A trend, momentum and cycle Trading System v3.2", Color.CYAN);

####################################
##### START OF MODULES SECTION #####
####################################

#START OF HA1 - Heikin Ashi Candles Module V1.0
#
#CHANGELOG
# 2020.01.26 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableHA1 = yes; #Heikin Ashi Candles
def HA1_haopen;
def HA1_hahigh;
def HA1_halow;
def HA1_haclose;
def HA1_sState;
def HA1_sBullish;
def HA1_sBearish;
def HA1_sNeutral;

HA1_haopen = CompoundValue(1, (HA1_haopen[1] + HA1_haclose[1]) / 2, (o[1] + c[1]) / 2);
HA1_hahigh = Max(Max(h, HA1_haopen), HA1_haclose[1]);
HA1_halow = Min(Min(l, HA1_haopen), HA1_haclose[1]);
HA1_haclose = (o + h + l + c) / 4;
HA1_sState = if HA1_haclose > HA1_haopen then 100 else -100;
HA1_sBullish = if(enableHA1 and HA1_sState == 100,1,0);
HA1_sBearish = if(enableHA1 and HA1_sState == -100,1,0);
HA1_sNeutral = 0;

AddLabel(showlabels and enableHA1, "HA",
if IsNan(HA1_sState) then COLOR.DARK_GRAY else
if HA1_sState[-displace] > 0 then COLOR.GREEN else
if HA1_sState[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF HA1 - Heikin Ashi Candles Module V1.0

#START OF HAMTF - Heikin Ashi Candles MTF Module V1.0
#
#CHANGELOG
# 2020.01.26 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
def HAMTF_haopen;
def HAMTF_hahigh;
def HAMTF_halow;
def HAMTF_haclose;
def HAMTF_sState;
def HAMTF_sBullish;
def HAMTF_sBearish;
def HAMTF_sNeutral;

input enableHAMTF = yes; #Heikin Ashi Candles MTF

HAMTF_haopen = CompoundValue(1, (HAMTF_haopen[1] + HAMTF_haclose[1]) / 2, (o1[1] + c1[1]) / 2);
HAMTF_hahigh = Max(Max(h1, HAMTF_haopen), HAMTF_haclose[1]);
HAMTF_halow = Min(Min(l1, HAMTF_haopen), HAMTF_haclose[1]);
HAMTF_haclose = (o1 + h1 + l1 + c1) / 4;
HAMTF_sState = if HAMTF_haclose > HAMTF_haopen then 100 else -100;
HAMTF_sBullish = if(enableHAMTF and HAMTF_sState == 100,1,0);
HAMTF_sBearish = if(enableHAMTF and HAMTF_sState == -100,1,0);
HAMTF_sNeutral = 0;
AddLabel(showlabels and enableHAMTF, "HA",
if IsNan(HAMTF_sState) then COLOR.DARK_GRAY else
if HAMTF_sState[-displace] > 0 then COLOR.GREEN else
if HAMTF_sState[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF HAMTF - Heikin Ashi Candles MTF Module V1.0

#START OF TTM - TTM Trend Module V1.0
#
#CHANGELOG
# 2020.01.26 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableTTM = yes; #TTM Trend
def TTM_trendup;
def TTM_trenddown;
def TTM_sState;
def TTM_sBullish;
def TTM_sBearish;
def TTM_sNeutral;

TTM_trendup = if TTM_Trend().TrendUp == 1 then 1 else 0;
TTM_trenddown = if TTM_Trend().TrendDown == 1 then 1 else 0;
TTM_sState = if TTM_trendup then 100 else if TTM_trenddown then -100 else 0;
TTM_sBullish = if(enableTTM and TTM_sState == 100,1,0);
TTM_sBearish = if(enableTTM and TTM_sState == -100,1,0);
TTM_sNeutral = 0;
AddLabel(showlabels and enableTTM, "TTM", if IsNan(TTM_sState) then COLOR.DARK_GRAY else
if TTM_sState[-displace] > 0 then COLOR.GREEN else
if TTM_sState[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF TTM - TTM Trend Module V1.0


#START OF ST1 - SuperTrend (Mobius) Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSuperTrend = yes; #ST1 - SuperTrend (Mobius)
input ST1_atrmult = 1.0; #SuperTrend
input ST1_natr = 4; #SuperTrend
input ST1_avgtype = AverageType.HULL; #SuperTrend

def ST1_atr = MovingAverage(ST1_avgtype, TrueRange(h, c, l), ST1_natr);
def ST1_up = hl + (ST1_atrmult * ST1_atr);
def ST1_dn = hl + (-ST1_atrmult * ST1_atr);
def ST1_st = if c < ST1_st[1] then ST1_up else ST1_dn;
def ST1_ssupertrend = if c < ST1_st then -100 else 100;
def ST1_sbullish = enableSuperTrend and ST1_ssupertrend == 100;
def ST1_sbearish = enableSuperTrend and ST1_ssupertrend == -100;
def ST1_sneutral = 0;
AddLabel(showlabels and enableSuperTrend, "SuperTrend", if IsNan(ST1_ssupertrend) then COLOR.DARK_GRAY else
if ST1_ssupertrend[-displace] > 0 then COLOR.GREEN else
if ST1_ssupertrend[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF ST1 - SuperTrend (Mobius) Module V1.0


#START OF STHA - SuperTrend HA (Mobius) Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSuperTrendHA = yes; #ST1 - SuperTrend HA (Mobius)
input STHA_atrmult = 1.0; #SuperTrend
input STHA_natr = 4; #SuperTrend
input STHA_avgtype = AverageType.HULL; #SuperTrend

def STHA_atr = MovingAverage(STHA_avgtype, TrueRange(ha1_hahigh, ha1_HAclose, ha1_HAlow), STHA_natr);
def STHA_up = HL + (STHA_atrmult * STHA_atr);
def STHA_dn = HL + (-STHA_atrmult * STHA_atr);
def STHA_st = if c < STHA_st[1] then STHA_up else STHA_dn;
def STHA_ssupertrend = if c < STHA_st then -100 else 100;
def STHA_sbullish = enableSuperTrendHA and STHA_ssupertrend == 100;
def STHA_sbearish = enableSuperTrendHA and STHA_ssupertrend == -100;
def STHA_sneutral = 0;
AddLabel(showlabels and enableSuperTrend, "SuperTrendHA", if IsNan(STHA_ssupertrend) then COLOR.DARK_GRAY else
if STHA_ssupertrend[-displace] > 0 then COLOR.GREEN else
if STHA_ssupertrend[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF STHA - SuperTrend HA (Mobius) Module V1.0


#START OF FRE - Frema Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableFrema = yes; #FREMA
input FRE_aa = .1; #FREMA

def FRE_cc;
def FRE_zeroline = 0;
def FRE_re1;
def FRE_re2;
def FRE_re3;
def FRE_re4;
def FRE_re5;
def FRE_re6;
def FRE_re7;
def FRE_re8;
def FRE_ema;
FRE_cc = if FRE_cc[1] == 0 then .9 else 1 – FRE_aa;
FRE_ema = FRE_aa * c + FRE_cc * FRE_ema[1];
FRE_re1 = FRE_cc * FRE_ema + FRE_ema[1];
FRE_re2 = Power(FRE_cc, 2) * FRE_re1 + FRE_re1[1];
FRE_re3 = Power(FRE_cc, 4) * FRE_re2 + FRE_re2[1];
FRE_re4 = Power(FRE_cc, 8) * FRE_re3 + FRE_re3[1];
FRE_re5 = Power(FRE_cc, 16) * FRE_re4 + FRE_re4[1];
FRE_re6 = Power(FRE_cc, 32) * FRE_re5 + FRE_re5[1];
FRE_re7 = Power(FRE_cc, 64) * FRE_re6 + FRE_re6[1];
FRE_re8 = Power(FRE_cc, 128) * FRE_re7 + FRE_re7[1];
def FRE_ema_signal = FRE_ema – FRE_aa * FRE_re8;
def FRE_sfrema = if (FRE_ema_signal > FRE_zeroline) then 100 else -100;
def FRE_sbullish = enableFrema and FRE_sfrema == 100;
def FRE_sbearish = enableFrema and FRE_sfrema == -100;
def FRE_sneutral = 0;

AddLabel(showlabels and enableFrema, "Frema", if IsNan(FRE_sfrema) then COLOR.DARK_GRAY else
if FRE_sfrema[-displace] > 0 then COLOR.GREEN else
if FRE_sfrema[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF FRE - Frema Module V1.0


#START OF TMO - TMO Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableTMO = yes; #TMO
input TMO_length = 14; #TMO
input TMO_calclength = 5; #TMO
input TMO_smoothlength = 3; #TMO

def TMO_data = fold i = 0 to TMO_length
        with s
        do s + (if c > getValue(o, i)
                then 1
                else if c < getValue(o, i)
                     then - 1
                     else 0);
def TMO_ema5 = ExpAverage(TMO_data, TMO_calclength);
def TMO_main = ExpAverage(TMO_ema5, TMO_smoothlength);
def TMO_signal = ExpAverage(TMO_main, TMO_smoothlength);
def TMO_smain = if TMO_main > TMO_signal then 100 else -100;
def TMO_ssignal = if TMO_main > TMO_signal then 100 else - 100;
def TMO_ob = if isNaN(c) then double.nan else round(TMO_length * .7);
def TMO_os = if isNaN(c) then double.nan else -round(TMO_length * .7);
def TMO_stmo = if TMO_main > TMO_signal then 100 else -100;
def TMO_sbullish = enableTMO and TMO_stmo == 100;
def TMO_sbearish = enableTMO and TMO_stmo == -100;
def TMO_sneutral = 0;

AddLabel(showlabels and enableTMO, "TMO", if IsNan(TMO_stmo) then COLOR.DARK_GRAY else
if TMO_stmo[-displace] > 0 then COLOR.GREEN else
if TMO_stmo[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF TMO - TMO Module V1.0


#START OF SL1 - SLIM Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSLIM = yes;
input SL1_ssuperfast = 8;
input SL1_sfast = 13;
input SL1_sslow = 21;

def SL1_superfast = ExpAverage(c[-displace], SL1_ssuperfast);
def SL1_fast = ExpAverage(c[-displace], SL1_sfast);
def SL1_slow = ExpAverage(c[-displace], SL1_sslow);
def SL1_buy = SL1_superfast > SL1_fast and SL1_fast > SL1_slow and l > SL1_superfast;
def SL1_stopbuy = SL1_superfast <= SL1_fast;
def SL1_buynow = !SL1_buy[1] and SL1_buy;
def SL1_buysignal = CompoundValue(1, if SL1_buynow and !SL1_stopbuy then 1
else if SL1_buysignal[1] == 1 and SL1_stopbuy then 0 else SL1_buysignal[1], 0);
def SL1_buy_signal = SL1_buysignal[1] == 0 and SL1_buysignal == 1;
def SL1_momentum_down = SL1_buysignal[1] == 1 and SL1_buysignal == 0;
def SL1_sell = SL1_superfast < SL1_fast and SL1_fast < SL1_slow and h < SL1_superfast;
def SL1_stopsell = SL1_superfast >= SL1_fast;
def SL1_sellnow = !SL1_sell[1] and SL1_sell;
def SL1_sellsignal = CompoundValue(1, if SL1_sellnow and !SL1_stopsell then 1
else if SL1_sellsignal[1] == 1 and SL1_stopsell then 0 else SL1_sellsignal[1], 0);
def SL1_sell_signal = SL1_sellsignal[1] == 0 and SL1_sellsignal;
def SL1_momentum_up = SL1_sellsignal[1] == 1 and SL1_sellsignal == 0;
def SL1_sstate = if SL1_buy_signal then 100 else if SL1_momentum_up then 10 else if SL1_sell_signal then -100 else if SL1_momentum_down then -10 else SL1_sstate[1];
def SL1_sbullish = enableSLIM and SL1_sstate == 100;
def SL1_sbearish = enableSLIM and SL1_sstate == -100;
def SL1_sneutral = 0;
AddLabel(showlabels and enableSLIM, "SLIM", if IsNan(SL1_sstate) then COLOR.DARK_GRAY else
if SL1_sstate[-displace] >= 100 then COLOR.GREEN else
if SL1_sstate[-displace] <= -100 then COLOR.RED
else COLOR.YELLOW);
#END OF SL1 - SLIM Module V1.0


#START OF TOP - TOP Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableTOP = yes; #TOP
input TOP_fastcyclelength = 5; #TOP
input TOP_slowcyclelength = 8; #TOP

def TOP_fastvar = ExpAverage((H + L) / 2, TOP_fastcyclelength);
def TOP_slowvar = Average((H + L) / 2, TOP_slowcyclelength);
def TOP_diffvar = TOP_fastvar - TOP_slowvar;
def TOP_pdiffvar = TOP_diffvar;
def TOP_pdiffvar2 = TOP_diffvar;
def TOP_stop = if TOP_diffvar > 0 then 100 else -100;
def TOP_sbullish = enableTOP and TOP_stop == 100;
def TOP_sbearish = enableTOP and TOP_stop == -100;
def TOP_sneutral = 0;
AddLabel(showlabels and enableTOP, "TOP", if IsNan(TOP_stop) then COLOR.DARK_GRAY else
if TOP_stop[-displace] > 0 then COLOR.GREEN else
if TOP_stop[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF TOP - TOP Module V1.0


#START OF DMI - DMI Trend Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableDMI = yes; #DMI Trend
input DMI_dmilength = 13;

def DMI_hidiff = h - h[1];
def DMI_lodiff = l[1] - l;
def DMI_plusdm = if DMI_hidiff > DMI_lodiff and DMI_hidiff > 0 then DMI_hidiff else 0;
def DMI_minusdm =  if DMI_lodiff > DMI_hidiff and DMI_lodiff > 0 then DMI_lodiff else 0;
def DMI_atrdmi = WildersAverage(TrueRange(h, c, l), DMI_dmilength);
Def DMI_diplus = 100 * WildersAverage(DMI_plusdm, DMI_dmilength) / DMI_atrdmi;
Def DMI_diminus = 100 * WildersAverage(DMI_minusdm, DMI_dmilength) / DMI_atrdmi;
def DMI_sdmi = if DMI_diplus > DMI_diminus then 100 else if DMI_diminus > DMI_diplus then -100 else 0;
def DMI_sbullish = enableDMI and DMI_sdmi == 100;
def DMI_sbearish = enableDMI and DMI_sdmi == -100;
def DMI_sneutral = 0;
AddLabel(showlabels and enableDMI, "DMI", if IsNan(DMI_sdmi) then COLOR.DARK_GRAY else
if DMI_sdmi[-displace] > 0 then COLOR.GREEN else
if DMI_sdmi[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF DMI - DMI Trend Module V1.0


#START OF BOP - BOP Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableBOP = yes; #BOP
input BOP_averagetype = {Simple, Exponential, default Weighted, Wilders, Hull,  Disabled};
input BOP_boplength = 16;

def BOP_rawbmp = if h != l then (c - o) / (h - l) else 1;
def BMP;
switch (BOP_averagetype) {
case Simple:
BMP = Average(BOP_rawbmp, BOP_boplength);
case Exponential:
BMP = ExpAverage(BOP_rawbmp, BOP_boplength);
case Weighted:
BMP = wma(BOP_rawbmp, BOP_boplength);
case Wilders:
BMP = WildersAverage(BOP_rawbmp, BOP_boplength);
case Hull:
BMP = HullMovingAvg(BOP_rawbmp, BOP_boplength);
case Disabled:
BMP = BOP_rawbmp;
}
def BOP_sbop = if BMP > 0 then 100 else -100;
def BOP_sbullish = enableBOP and BOP_sbop == 100;
def BOP_sbearish = enableBOP and BOP_sbop == -100;
def BOP_sneutral = 0;
AddLabel(showlabels and enableBOP, "BOP", if IsNan(BOP_sbop) then COLOR.DARK_GRAY else
if BOP_sbop[-displace] > 0 then COLOR.GREEN else
if BOP_sbop[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF BOP - BOP Module V1.0


#START OF STCCI - Super Trend w/CCI Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSuperTrendCCI = yes; #Super Trend w/CCI
input STCCI_lengthcci = 50;
input STCCI_lengthatr = 21;
input STCCI_atrfactor = 1.0;
def STCCI_pricedata = hl;
def STCCI_atrcci = Average(TrueRange(h, c, l), STCCI_lengthatr) * STCCI_atrfactor;
def STCCI_price = c + l + h;
def STCCI_lindev = lindev(STCCI_price, STCCI_lengthcci);
def STCCI_cci = if STCCI_lindev == 0
       then 0
       else (STCCI_price - Average(STCCI_price, STCCI_lengthcci)) / STCCI_lindev / 0.015;
def STCCI_mt1 = if STCCI_cci > 0
       then Max(STCCI_mt1[1], STCCI_pricedata - STCCI_atrcci)
       else Min(STCCI_mt1[1], STCCI_pricedata + STCCI_atrcci);
def STCCI_ssupertrendcci = if c < STCCI_mt1 and c < ST1_st then -100 else if C > STCCI_mt1 and c > ST1_st then 100 else 0;
def STCCI_sbullish = enableSuperTrendCCI and STCCI_ssupertrendcci == 100;
def STCCI_sbearish = enableSuperTrendCCI and STCCI_ssupertrendcci == -100;
def STCCI_sneutral = 0;
AddLabel(showlabels and enableSuperTrendCCI, "SuperTrendCCI", if IsNan(STCCI_ssupertrendcci) then COLOR.DARK_GRAY else
if STCCI_ssupertrendcci[-displace] > 0 then COLOR.GREEN else
if STCCI_ssupertrendcci[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF STCCI - Super Trend w/CCI Module V1.0


#START OF CTT - CCI + TTM squeeze + TTM trend Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableCCITTM = yes; #CCI + TTM squeeze + TTM trend
def CTT_ccibuy = CCI(length = 14).CCI > 0 and CCI(length = 50).CCI > 0;
def CTT_ccisell = CCI(length = 14).CCI < 0 and CCI(length = 50).CCI < 0;
def CTT_strendup = TTM_Trend().TrendUp;
def CTT_strenddn = TTM_Trend().TrendDown;
def CTT_squeezeup = TTM_Squeeze().Histogram >= 0;
def CTT_squeezedn = TTM_Squeeze().Histogram <= 0;
def CTT_buy = CTT_ccibuy and CTT_strendup and CTT_squeezeup;
def CTT_sell = CTT_ccisell and CTT_strenddn and CTT_squeezedn;
def CTT_sccittm = if CTT_buy then 100 else if CTT_sell then -100 else 0;
def CTT_sbullish = enableCCITTM and CTT_sccittm == 100;
def CTT_sbearish = enableCCITTM and CTT_sccittm == -100;
def CTT_sneutral = 0;
AddLabel(showlabels and enableCCITTM, "CCI+TTM", if IsNan(CTT_sccittm) then COLOR.DARK_GRAY else
if CTT_sccittm[-displace] > 0 then COLOR.GREEN else
if CTT_sccittm[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF CTT - CCI + TTM squeeze + TTM trend Module V1.0


#START OF DRSI - Derivative RSI Oscillator Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
# Derivative RSI Oscillator
# Can be used alone as a smoother RSI or as part of the Carting Wealth strategy when
# combined with the MACD and MAs Charting Wealth study.
# By Horserider 12/25/2019

#USAGE
#
#Derivative RSI Oscillator
input enableDRSI = yes; #Derivative RSI Oscillator
input DRSI_length = 14;
input DRSI_length1 = 5;
input DRSI_length2 = 3;
input DRSI_length3 = 9;
input DRSI_price = close;
input DRSI_averagetype = averagetype.EXPONENTIAL;
input DRSI_averagetype2 = averagetype.SIMPLE;

def DRSI_netchgavg = WildersAverage(DRSI_price - DRSI_price[1], DRSI_length);
def DRSI_totchgavg = WildersAverage(AbsValue(DRSI_price - DRSI_price[1]), DRSI_length);
def DRSI_chgratio = if DRSI_totchgavg != 0 then DRSI_netchgavg / DRSI_totchgavg else 0;

def DRSI_rsi = (50 * (DRSI_chgratio + 1) - 50);

def DRSI_x = MovingAverage(DRSI_averagetype,DRSI_rsi, DRSI_length1);
def DRSI_x2 = MovingAverage(DRSI_averagetype, DRSI_x, DRSI_length2);
def DRSI_xs = MovingAverage(DRSI_averagetype2, DRSI_x2 , DRSI_length3);
def DRSI_rsi__linediff = DRSI_x2 - DRSI_xs;
def DRSI_rsi_line = DRSI_rsi__linediff;
def DRSI_sstate = if DRSI_rsi_line >= 0 then if DRSI_rsi_line > DRSI_rsi_line[1] then 100 else 10 else if DRSI_rsi_line < DRSI_rsi_line[1] then -100 else -10;
def DRSI_sbullish = enableDRSI and DRSI_sstate > 0;
def DRSI_sbearish = enableDRSI and DRSI_sstate < 0;
def DRSI_sneutral = 0;
AddLabel(showlabels and enableDRSI, "DRSI", if IsNan(DRSI_sstate) then COLOR.DARK_GRAY else
if DRSI_sstate[-displace] > 0 then COLOR.GREEN else
if DRSI_sstate[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF DRSI - Derivative RSI Oscillator Module V1.0


#START OF SWCA - Schaff Wave CrossOver Average Module V1.0
#
#CHANGELOG
# 2020.02.01 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSWCA = yes;

input SWCA_fastlengthtrend = 48;
input SWCA_slowlengthtrend = 104;
input SWCA_kperiodtrend = 36;
input SWCA_dperiodtrend = 8;
input SWCA_averagetypetrend = AverageType.EXPONENTIAL;
input SWCA_fastlengthwave = 12;
input SWCA_slowlengthwave = 26;
input SWCA_kperiodwave = 9;
input SWCA_dperiodwave = 2;
input SWCA_averagetypewave = AverageType.EXPONENTIAL;

def SWCA_macdtrend = MovingAverage(SWCA_averagetypetrend, c, SWCA_fastlengthtrend) - MovingAverage(SWCA_averagetypetrend, c, SWCA_slowlengthtrend);
def SWCA_macdwave = MovingAverage(SWCA_averagetypewave, c, SWCA_fastlengthwave) - MovingAverage(SWCA_averagetypewave, c, SWCA_slowlengthwave);
def SWCA_fastk1trend = FastKCustom(SWCA_macdtrend, SWCA_kperiodtrend);
def SWCA_fastk1wave = FastKCustom(SWCA_macdwave, SWCA_kperiodwave);
def SWCA_fastd1trend = MovingAverage(SWCA_averagetypetrend, SWCA_fastk1trend, SWCA_dperiodtrend);
def SWCA_fastd1wave = MovingAverage(SWCA_averagetypewave, SWCA_fastk1wave, SWCA_dperiodwave);
def SWCA_fastk2trend = FastKCustom(SWCA_fastd1trend, SWCA_kperiodtrend);
def SWCA_fastk2wave = FastKCustom(SWCA_fastd1wave, SWCA_kperiodwave);
def SWCA_stctrend = MovingAverage(SWCA_averagetypetrend, SWCA_fastk2trend, SWCA_dperiodtrend);
def SWCA_stcwave = MovingAverage(SWCA_averagetypewave, SWCA_fastk2wave, SWCA_dperiodwave);

def SWCA_sstate = if SWCA_stctrend > SWCA_stcwave then 100 else -100;
def SWCA_sbullish = enableSWCA and SWCA_sstate == 100;
def SWCA_sbearish = enableSWCA and SWCA_sstate == -100;
def SWCA_sneutral = 0;
AddLabel(showlabels and enableSWCA, "SWCA", if IsNan(SWCA_sstate) then COLOR.DARK_GRAY else
if SWCA_sstate[-displace] > 0 then COLOR.GREEN else
if SWCA_sstate[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF SWCA - Schaff Wave CrossOver Average Module V1.0



###################################
#####  END OF MODULES SECTION #####
###################################

#ADX TRENDING
input ADXlength = 8; #ADX
input ADXTrending = 25; #ADX
input showADXBar = yes;
input DI_length = 8;

def DX = if (diplus(di_length) + diminus(di_length) > 0) then 100 * AbsValue(diplus(di_length) - diminus(di_length)) / (diplus(di_length) + diminus(di_length)) else 0;
def ADX = WildersAverage(DX, adxlength);

plot ADXCross = LowestAll(low);
ADXCross.SetPaintingStrategy(PaintingStrategy.LINE);
ADXCross.AssignValueColor(
    if ADX > ADXTrending then Color.LIME
    else Color.DARK_GRAY);
ADXCross.SetLineWeight(5);
ADXCross.SetHiding(!showADXBar);

#MACD Neutal Eval
input fastLength_1 = 12;
input slowLength_1 = 26;
input MACDLength_1 = 9;
input AverageType_1 = {SMA, default EMA};
def macd_Val_1 = MACD(fastLength_1, slowLength_1, MACDLength_1, AverageType_1).Value;
def macd_Avg1 = MACD(fastLength_1, slowLength_1, MACDLength_1, AverageType_1).Avg;
def MACD_trig = if MACD().value > MACD().avg then 1 else 0;

AddLabel(showlabels,
if ADX > ADXTrending then "Trending" else
"Non Trending",
if IsNan(c) then COLOR.DARK_GRAY else
if ADX > ADXTrending then COLOR.GREEN else
COLOR.DARK_GRAY);

AddLabel(showlabels,
"MACD",
if IsNan(c) then COLOR.DARK_GRAY else
if macd_Val_1 > macd_Avg1 then COLOR.DARK_GREEN else
COLOR.DARK_RED);


#STATE
def sResults = HA1_sState + HAMTF_sState + TTM_sState + ST1_ssupertrend + STHA_ssupertrend + FRE_sfrema + TMO_stmo + SL1_sstate + TOP_stop + DMI_sdmi + BOP_sbop
+ STCCI_ssupertrendcci + CTT_sccittm + DRSI_sstate + SWCA_sstate;

def sBullish = HA1_sBullish + HAMTF_sBullish + TTM_sBullish + ST1_sbullish + STHA_sbullish + FRE_sbullish + TMO_sbullish + SL1_sbullish + TOP_sbullish + DMI_sbullish + BOP_sbullish + STCCI_sbullish + CTT_sbullish + DRSI_sbullish + SWCA_sbullish;

def sBearish = HA1_sBearish + HAMTF_sBearish + TTM_sBearish + ST1_sbearish + STHA_sbearish + FRE_sbearish + TMO_sbearish + SL1_sbearish + TOP_sbearish + DMI_sbearish + BOP_sbearish + STCCI_sbearish + CTT_sbearish + DRSI_sbearish + SWCA_sbearish;

def bullish = if(sBullish >= threshold,1,0);
def bearish = if(sBearish >= threshold,1,0);
def sState = if bullish then 100 else if bearish then -100 else 0;
def sState2 = if sState != 0 then sState else sState2[1];

#COLORBARS
AssignPriceColor(
if PaintBars then
    if ADXPaintBarsMode then if ADX > ADXTrending then COLOR.ORANGE else COLOR.DARK_GRAY
    else
    if bullish then COLOR.GREEN else if bearish then COLOR.RED
     else
     if enableNeutralMACDPainter then
      if macd_Val_1 > macd_Avg1 then COLOR.DARK_GREEN else COLOR.DARK_RED
     else
    COLOR.GRAY
else COLOR.CURRENT);

AddLabel(showlabels,
if bullish[-displace] then "LONG" else
if bearish[-displace] then "SHORT"
else "HOLD",
if IsNan(c) then COLOR.DARK_GRAY else
if bullish[-displace] then COLOR.GREEN else
if bearish[-displace] then COLOR.RED
else COLOR.GRAY);

#END OF CSA - trend, a momentum and a cycle based indicator for ThinkorSwim V3.2
Lastest Release: V3.2 - Last Updated: February 1, 2020

Release Modules

  1. Module HA1 - Heikin Ashi Candles
  2. Module HAMTF - Heikin Ashi Candles MTF (Multiple Time Frame)
  3. Module TTM - TTM Trend Module
  4. Module ST1 - SuperTrend Module
  5. Module STHA - SuperTrend HA
  6. Module SL1 - SLIM Module
  7. Module TOP - TOP Module
  8. Module DMI - DMI Trend
  9. Module BOP - BOP Module
  10. Module STCCI - Super Trend w/CCI Module
  11. Module CTT - CCI + TTM squeeze + TTM trend Module
  12. Module DRSI - Derivative RSI Oscillator Module
  13. Module SWCA - Schaff Wave CrossOver Average

Ruby:
#CSA - trend, a momentum and a cycle based indicator for ThinkorSwim V3.2
#
#VERSION
# 2020.02.01 V3.2 @diazlaz - Added SWCA - Schaff Wave CrossOver Average
# 2020.01.29 V3.1 @diazlaz - Added ADXPaintBarsMode
#                            Integrated Derivative RSI Oscillator
# 2020.01.29 V3.0 @diazlaz - Modules Restructured 3.0 Framework
#                            Improved ADX formula for Trend Detection
#                            Integrated HeikinAshiCandles and HeikinAshiCandles MTF
#                            set aggregationPeriod to MTF aggregation (default 5 mins)
#                            Integrated PaintBars MACD Neutral Condition trigger
#                            set enableNeutralMACDPainter to yes (default is no) and
#                            neutral gray will be evaluated by MACD (Dark Red/Green)
#                            Integrated Super Trend w/CCI and ATR
#                            Integrated CCI + TTM squeeze + TTM trend Combo
#                            Set treshold defaults to 7 (7 out of 10), removed scanmode.
#                            Integrated Slim Ribbon.
#                            Integrated DMI and BOP, restructured INPUTS to each module.
#                            threshold increased to 4 by default.
#                            Integrated ADX Trending States and BAR Line
#                            Integrated TTM Trends, modules selection
#                            and added threshold for signals
#                            Integrated FREMA
#                            Labels, Additional Dashboard, Lower Study
#
#
#LINK
#https://usethinkscript.com/threads/a-trend-momentum-and-cycle-trading-system-v3-0-csa.1596/
#
#INSTRUCTION
#Here is how I envision the paintbar working:
#When the supertrend indicator is long that would equal +1, when its short -1.
#When the TMO is green that would equal +1. when its red its -1.
#When the TOP Cycle Trader is above zero that would equal +1, when its below zero -1.
#When all 3 indicators are equal to +1 making +3 total the price bars would paint #green.
#When all 3 indicators are equal to -1 making -3 total the price bars would paint red.
#If the combination of all 3 indicators equals anything else, the price bars would #paint gray meaning all 3 indicators were not in agreement.
#


declare upper;

#INPUTS
input showLabels = yes;
input PaintBars = yes;
input ADXPaintBarsMode = no;
input enableNeutralMACDPainter = no;
input aggregationPeriod = AggregationPeriod.FIVE_MIN;
input threshold = 7;

input displace = 0;

#CORE
DefineGlobalColor("CycleLineColor", Color.RED);
DefineGlobalColor("CyclehistColor", Color.BLUE);
DefineGlobalColor("ZeroLineColor", Color.YELLOW);
DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Bearish", Color.RED);
DefineGlobalColor("Neutral", Color.MAGENTA); #Color.YELLOW
DefineGlobalColor("Off", Color.DARK_GRAY);
DefineGlobalColor("On", Color.GREEN);
DefineGlobalColor("Sync1", Color.YELLOW);
DefineGlobalColor("Sync2", Color.CYAN);
DefineGlobalColor("Up", Color.GREEN);
DefineGlobalColor("Down", Color.RED);
DefineGlobalColor("NUp", Color.DARK_GREEN);
DefineGlobalColor("NDown", Color.DARK_RED);
DefineGlobalColor("Neutral", Color.BLUE);
DefineGlobalColor("Neutral2", Color.PLUM);

def h = high;
def l = low;
def o = open;
def c = close;
def hl = hl2;

def h1 = high(period = aggregationPeriod);
def l1 = low(period = aggregationPeriod);
def o1 = open(period = aggregationPeriod);
def c1 = close(period = aggregationPeriod);
def hl2 = hl2(period = aggregationPeriod);

def na = Double.NaN;

AddLabel(showLabels, "A trend, momentum and cycle Trading System v3.2", Color.CYAN);

####################################
##### START OF MODULES SECTION #####
####################################

#START OF HA1 - Heikin Ashi Candles Module V1.0
#
#CHANGELOG
# 2020.01.26 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableHA1 = yes; #Heikin Ashi Candles
def HA1_haopen;
def HA1_hahigh;
def HA1_halow;
def HA1_haclose;
def HA1_sState;
def HA1_sBullish;
def HA1_sBearish;
def HA1_sNeutral;

HA1_haopen = CompoundValue(1, (HA1_haopen[1] + HA1_haclose[1]) / 2, (o[1] + c[1]) / 2);
HA1_hahigh = Max(Max(h, HA1_haopen), HA1_haclose[1]);
HA1_halow = Min(Min(l, HA1_haopen), HA1_haclose[1]);
HA1_haclose = (o + h + l + c) / 4;
HA1_sState = if HA1_haclose > HA1_haopen then 100 else -100;
HA1_sBullish = if(enableHA1 and HA1_sState == 100,1,0);
HA1_sBearish = if(enableHA1 and HA1_sState == -100,1,0);
HA1_sNeutral = 0;

AddLabel(showlabels and enableHA1, "HA",
if IsNan(HA1_sState) then COLOR.DARK_GRAY else
if HA1_sState[-displace] > 0 then COLOR.GREEN else
if HA1_sState[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF HA1 - Heikin Ashi Candles Module V1.0

#START OF HAMTF - Heikin Ashi Candles MTF Module V1.0
#
#CHANGELOG
# 2020.01.26 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
def HAMTF_haopen;
def HAMTF_hahigh;
def HAMTF_halow;
def HAMTF_haclose;
def HAMTF_sState;
def HAMTF_sBullish;
def HAMTF_sBearish;
def HAMTF_sNeutral;

input enableHAMTF = yes; #Heikin Ashi Candles MTF

HAMTF_haopen = CompoundValue(1, (HAMTF_haopen[1] + HAMTF_haclose[1]) / 2, (o1[1] + c1[1]) / 2);
HAMTF_hahigh = Max(Max(h1, HAMTF_haopen), HAMTF_haclose[1]);
HAMTF_halow = Min(Min(l1, HAMTF_haopen), HAMTF_haclose[1]);
HAMTF_haclose = (o1 + h1 + l1 + c1) / 4;
HAMTF_sState = if HAMTF_haclose > HAMTF_haopen then 100 else -100;
HAMTF_sBullish = if(enableHAMTF and HAMTF_sState == 100,1,0);
HAMTF_sBearish = if(enableHAMTF and HAMTF_sState == -100,1,0);
HAMTF_sNeutral = 0;
AddLabel(showlabels and enableHAMTF, "HA",
if IsNan(HAMTF_sState) then COLOR.DARK_GRAY else
if HAMTF_sState[-displace] > 0 then COLOR.GREEN else
if HAMTF_sState[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF HAMTF - Heikin Ashi Candles MTF Module V1.0

#START OF TTM - TTM Trend Module V1.0
#
#CHANGELOG
# 2020.01.26 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableTTM = yes; #TTM Trend
def TTM_trendup;
def TTM_trenddown;
def TTM_sState;
def TTM_sBullish;
def TTM_sBearish;
def TTM_sNeutral;

TTM_trendup = if TTM_Trend().TrendUp == 1 then 1 else 0;
TTM_trenddown = if TTM_Trend().TrendDown == 1 then 1 else 0;
TTM_sState = if TTM_trendup then 100 else if TTM_trenddown then -100 else 0;
TTM_sBullish = if(enableTTM and TTM_sState == 100,1,0);
TTM_sBearish = if(enableTTM and TTM_sState == -100,1,0);
TTM_sNeutral = 0;
AddLabel(showlabels and enableTTM, "TTM", if IsNan(TTM_sState) then COLOR.DARK_GRAY else
if TTM_sState[-displace] > 0 then COLOR.GREEN else
if TTM_sState[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF TTM - TTM Trend Module V1.0


#START OF ST1 - SuperTrend (Mobius) Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSuperTrend = yes; #ST1 - SuperTrend (Mobius)
input ST1_atrmult = 1.0; #SuperTrend
input ST1_natr = 4; #SuperTrend
input ST1_avgtype = AverageType.HULL; #SuperTrend

def ST1_atr = MovingAverage(ST1_avgtype, TrueRange(h, c, l), ST1_natr);
def ST1_up = hl + (ST1_atrmult * ST1_atr);
def ST1_dn = hl + (-ST1_atrmult * ST1_atr);
def ST1_st = if c < ST1_st[1] then ST1_up else ST1_dn;
def ST1_ssupertrend = if c < ST1_st then -100 else 100;
def ST1_sbullish = enableSuperTrend and ST1_ssupertrend == 100;
def ST1_sbearish = enableSuperTrend and ST1_ssupertrend == -100;
def ST1_sneutral = 0;
AddLabel(showlabels and enableSuperTrend, "SuperTrend", if IsNan(ST1_ssupertrend) then COLOR.DARK_GRAY else
if ST1_ssupertrend[-displace] > 0 then COLOR.GREEN else
if ST1_ssupertrend[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF ST1 - SuperTrend (Mobius) Module V1.0


#START OF STHA - SuperTrend HA (Mobius) Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSuperTrendHA = yes; #ST1 - SuperTrend HA (Mobius)
input STHA_atrmult = 1.0; #SuperTrend
input STHA_natr = 4; #SuperTrend
input STHA_avgtype = AverageType.HULL; #SuperTrend

def STHA_atr = MovingAverage(STHA_avgtype, TrueRange(ha1_hahigh, ha1_HAclose, ha1_HAlow), STHA_natr);
def STHA_up = HL + (STHA_atrmult * STHA_atr);
def STHA_dn = HL + (-STHA_atrmult * STHA_atr);
def STHA_st = if c < STHA_st[1] then STHA_up else STHA_dn;
def STHA_ssupertrend = if c < STHA_st then -100 else 100;
def STHA_sbullish = enableSuperTrendHA and STHA_ssupertrend == 100;
def STHA_sbearish = enableSuperTrendHA and STHA_ssupertrend == -100;
def STHA_sneutral = 0;
AddLabel(showlabels and enableSuperTrend, "SuperTrendHA", if IsNan(STHA_ssupertrend) then COLOR.DARK_GRAY else
if STHA_ssupertrend[-displace] > 0 then COLOR.GREEN else
if STHA_ssupertrend[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF STHA - SuperTrend HA (Mobius) Module V1.0


#START OF FRE - Frema Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableFrema = yes; #FREMA
input FRE_aa = .1; #FREMA

def FRE_cc;
def FRE_zeroline = 0;
def FRE_re1;
def FRE_re2;
def FRE_re3;
def FRE_re4;
def FRE_re5;
def FRE_re6;
def FRE_re7;
def FRE_re8;
def FRE_ema;
FRE_cc = if FRE_cc[1] == 0 then .9 else 1 – FRE_aa;
FRE_ema = FRE_aa * c + FRE_cc * FRE_ema[1];
FRE_re1 = FRE_cc * FRE_ema + FRE_ema[1];
FRE_re2 = Power(FRE_cc, 2) * FRE_re1 + FRE_re1[1];
FRE_re3 = Power(FRE_cc, 4) * FRE_re2 + FRE_re2[1];
FRE_re4 = Power(FRE_cc, 8) * FRE_re3 + FRE_re3[1];
FRE_re5 = Power(FRE_cc, 16) * FRE_re4 + FRE_re4[1];
FRE_re6 = Power(FRE_cc, 32) * FRE_re5 + FRE_re5[1];
FRE_re7 = Power(FRE_cc, 64) * FRE_re6 + FRE_re6[1];
FRE_re8 = Power(FRE_cc, 128) * FRE_re7 + FRE_re7[1];
def FRE_ema_signal = FRE_ema – FRE_aa * FRE_re8;
def FRE_sfrema = if (FRE_ema_signal > FRE_zeroline) then 100 else -100;
def FRE_sbullish = enableFrema and FRE_sfrema == 100;
def FRE_sbearish = enableFrema and FRE_sfrema == -100;
def FRE_sneutral = 0;

AddLabel(showlabels and enableFrema, "Frema", if IsNan(FRE_sfrema) then COLOR.DARK_GRAY else
if FRE_sfrema[-displace] > 0 then COLOR.GREEN else
if FRE_sfrema[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF FRE - Frema Module V1.0


#START OF TMO - TMO Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableTMO = yes; #TMO
input TMO_length = 14; #TMO
input TMO_calclength = 5; #TMO
input TMO_smoothlength = 3; #TMO

def TMO_data = fold i = 0 to TMO_length
        with s
        do s + (if c > getValue(o, i)
                then 1
                else if c < getValue(o, i)
                     then - 1
                     else 0);
def TMO_ema5 = ExpAverage(TMO_data, TMO_calclength);
def TMO_main = ExpAverage(TMO_ema5, TMO_smoothlength);
def TMO_signal = ExpAverage(TMO_main, TMO_smoothlength);
def TMO_smain = if TMO_main > TMO_signal then 100 else -100;
def TMO_ssignal = if TMO_main > TMO_signal then 100 else - 100;
def TMO_ob = if isNaN(c) then double.nan else round(TMO_length * .7);
def TMO_os = if isNaN(c) then double.nan else -round(TMO_length * .7);
def TMO_stmo = if TMO_main > TMO_signal then 100 else -100;
def TMO_sbullish = enableTMO and TMO_stmo == 100;
def TMO_sbearish = enableTMO and TMO_stmo == -100;
def TMO_sneutral = 0;

AddLabel(showlabels and enableTMO, "TMO", if IsNan(TMO_stmo) then COLOR.DARK_GRAY else
if TMO_stmo[-displace] > 0 then COLOR.GREEN else
if TMO_stmo[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF TMO - TMO Module V1.0


#START OF SL1 - SLIM Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSLIM = yes;
input SL1_ssuperfast = 8;
input SL1_sfast = 13;
input SL1_sslow = 21;

def SL1_superfast = ExpAverage(c[-displace], SL1_ssuperfast);
def SL1_fast = ExpAverage(c[-displace], SL1_sfast);
def SL1_slow = ExpAverage(c[-displace], SL1_sslow);
def SL1_buy = SL1_superfast > SL1_fast and SL1_fast > SL1_slow and l > SL1_superfast;
def SL1_stopbuy = SL1_superfast <= SL1_fast;
def SL1_buynow = !SL1_buy[1] and SL1_buy;
def SL1_buysignal = CompoundValue(1, if SL1_buynow and !SL1_stopbuy then 1
else if SL1_buysignal[1] == 1 and SL1_stopbuy then 0 else SL1_buysignal[1], 0);
def SL1_buy_signal = SL1_buysignal[1] == 0 and SL1_buysignal == 1;
def SL1_momentum_down = SL1_buysignal[1] == 1 and SL1_buysignal == 0;
def SL1_sell = SL1_superfast < SL1_fast and SL1_fast < SL1_slow and h < SL1_superfast;
def SL1_stopsell = SL1_superfast >= SL1_fast;
def SL1_sellnow = !SL1_sell[1] and SL1_sell;
def SL1_sellsignal = CompoundValue(1, if SL1_sellnow and !SL1_stopsell then 1
else if SL1_sellsignal[1] == 1 and SL1_stopsell then 0 else SL1_sellsignal[1], 0);
def SL1_sell_signal = SL1_sellsignal[1] == 0 and SL1_sellsignal;
def SL1_momentum_up = SL1_sellsignal[1] == 1 and SL1_sellsignal == 0;
def SL1_sstate = if SL1_buy_signal then 100 else if SL1_momentum_up then 10 else if SL1_sell_signal then -100 else if SL1_momentum_down then -10 else SL1_sstate[1];
def SL1_sbullish = enableSLIM and SL1_sstate == 100;
def SL1_sbearish = enableSLIM and SL1_sstate == -100;
def SL1_sneutral = 0;
AddLabel(showlabels and enableSLIM, "SLIM", if IsNan(SL1_sstate) then COLOR.DARK_GRAY else
if SL1_sstate[-displace] >= 100 then COLOR.GREEN else
if SL1_sstate[-displace] <= -100 then COLOR.RED
else COLOR.YELLOW);
#END OF SL1 - SLIM Module V1.0


#START OF TOP - TOP Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableTOP = yes; #TOP
input TOP_fastcyclelength = 5; #TOP
input TOP_slowcyclelength = 8; #TOP

def TOP_fastvar = ExpAverage((H + L) / 2, TOP_fastcyclelength);
def TOP_slowvar = Average((H + L) / 2, TOP_slowcyclelength);
def TOP_diffvar = TOP_fastvar - TOP_slowvar;
def TOP_pdiffvar = TOP_diffvar;
def TOP_pdiffvar2 = TOP_diffvar;
def TOP_stop = if TOP_diffvar > 0 then 100 else -100;
def TOP_sbullish = enableTOP and TOP_stop == 100;
def TOP_sbearish = enableTOP and TOP_stop == -100;
def TOP_sneutral = 0;
AddLabel(showlabels and enableTOP, "TOP", if IsNan(TOP_stop) then COLOR.DARK_GRAY else
if TOP_stop[-displace] > 0 then COLOR.GREEN else
if TOP_stop[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF TOP - TOP Module V1.0


#START OF DMI - DMI Trend Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableDMI = yes; #DMI Trend
input DMI_dmilength = 13;

def DMI_hidiff = h - h[1];
def DMI_lodiff = l[1] - l;
def DMI_plusdm = if DMI_hidiff > DMI_lodiff and DMI_hidiff > 0 then DMI_hidiff else 0;
def DMI_minusdm =  if DMI_lodiff > DMI_hidiff and DMI_lodiff > 0 then DMI_lodiff else 0;
def DMI_atrdmi = WildersAverage(TrueRange(h, c, l), DMI_dmilength);
Def DMI_diplus = 100 * WildersAverage(DMI_plusdm, DMI_dmilength) / DMI_atrdmi;
Def DMI_diminus = 100 * WildersAverage(DMI_minusdm, DMI_dmilength) / DMI_atrdmi;
def DMI_sdmi = if DMI_diplus > DMI_diminus then 100 else if DMI_diminus > DMI_diplus then -100 else 0;
def DMI_sbullish = enableDMI and DMI_sdmi == 100;
def DMI_sbearish = enableDMI and DMI_sdmi == -100;
def DMI_sneutral = 0;
AddLabel(showlabels and enableDMI, "DMI", if IsNan(DMI_sdmi) then COLOR.DARK_GRAY else
if DMI_sdmi[-displace] > 0 then COLOR.GREEN else
if DMI_sdmi[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF DMI - DMI Trend Module V1.0


#START OF BOP - BOP Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableBOP = yes; #BOP
input BOP_averagetype = {Simple, Exponential, default Weighted, Wilders, Hull,  Disabled};
input BOP_boplength = 16;

def BOP_rawbmp = if h != l then (c - o) / (h - l) else 1;
def BMP;
switch (BOP_averagetype) {
case Simple:
BMP = Average(BOP_rawbmp, BOP_boplength);
case Exponential:
BMP = ExpAverage(BOP_rawbmp, BOP_boplength);
case Weighted:
BMP = wma(BOP_rawbmp, BOP_boplength);
case Wilders:
BMP = WildersAverage(BOP_rawbmp, BOP_boplength);
case Hull:
BMP = HullMovingAvg(BOP_rawbmp, BOP_boplength);
case Disabled:
BMP = BOP_rawbmp;
}
def BOP_sbop = if BMP > 0 then 100 else -100;
def BOP_sbullish = enableBOP and BOP_sbop == 100;
def BOP_sbearish = enableBOP and BOP_sbop == -100;
def BOP_sneutral = 0;
AddLabel(showlabels and enableBOP, "BOP", if IsNan(BOP_sbop) then COLOR.DARK_GRAY else
if BOP_sbop[-displace] > 0 then COLOR.GREEN else
if BOP_sbop[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF BOP - BOP Module V1.0


#START OF STCCI - Super Trend w/CCI Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSuperTrendCCI = yes; #Super Trend w/CCI
input STCCI_lengthcci = 50;
input STCCI_lengthatr = 21;
input STCCI_atrfactor = 1.0;
def STCCI_pricedata = hl;
def STCCI_atrcci = Average(TrueRange(h, c, l), STCCI_lengthatr) * STCCI_atrfactor;
def STCCI_price = c + l + h;
def STCCI_lindev = lindev(STCCI_price, STCCI_lengthcci);
def STCCI_cci = if STCCI_lindev == 0
       then 0
       else (STCCI_price - Average(STCCI_price, STCCI_lengthcci)) / STCCI_lindev / 0.015;
def STCCI_mt1 = if STCCI_cci > 0
       then Max(STCCI_mt1[1], STCCI_pricedata - STCCI_atrcci)
       else Min(STCCI_mt1[1], STCCI_pricedata + STCCI_atrcci);
def STCCI_ssupertrendcci = if c < STCCI_mt1 and c < ST1_st then -100 else if C > STCCI_mt1 and c > ST1_st then 100 else 0;
def STCCI_sbullish = enableSuperTrendCCI and STCCI_ssupertrendcci == 100;
def STCCI_sbearish = enableSuperTrendCCI and STCCI_ssupertrendcci == -100;
def STCCI_sneutral = 0;
AddLabel(showlabels and enableSuperTrendCCI, "SuperTrendCCI", if IsNan(STCCI_ssupertrendcci) then COLOR.DARK_GRAY else
if STCCI_ssupertrendcci[-displace] > 0 then COLOR.GREEN else
if STCCI_ssupertrendcci[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF STCCI - Super Trend w/CCI Module V1.0


#START OF CTT - CCI + TTM squeeze + TTM trend Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableCCITTM = yes; #CCI + TTM squeeze + TTM trend
def CTT_ccibuy = CCI(length = 14).CCI > 0 and CCI(length = 50).CCI > 0;
def CTT_ccisell = CCI(length = 14).CCI < 0 and CCI(length = 50).CCI < 0;
def CTT_strendup = TTM_Trend().TrendUp;
def CTT_strenddn = TTM_Trend().TrendDown;
def CTT_squeezeup = TTM_Squeeze().Histogram >= 0;
def CTT_squeezedn = TTM_Squeeze().Histogram <= 0;
def CTT_buy = CTT_ccibuy and CTT_strendup and CTT_squeezeup;
def CTT_sell = CTT_ccisell and CTT_strenddn and CTT_squeezedn;
def CTT_sccittm = if CTT_buy then 100 else if CTT_sell then -100 else 0;
def CTT_sbullish = enableCCITTM and CTT_sccittm == 100;
def CTT_sbearish = enableCCITTM and CTT_sccittm == -100;
def CTT_sneutral = 0;
AddLabel(showlabels and enableCCITTM, "CCI+TTM", if IsNan(CTT_sccittm) then COLOR.DARK_GRAY else
if CTT_sccittm[-displace] > 0 then COLOR.GREEN else
if CTT_sccittm[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF CTT - CCI + TTM squeeze + TTM trend Module V1.0


#START OF DRSI - Derivative RSI Oscillator Module V1.0
#
#CHANGELOG
# 2020.01.29 V1.0 @diazlaz Initial Module Release
#
#CREDITS
# Derivative RSI Oscillator
# Can be used alone as a smoother RSI or as part of the Carting Wealth strategy when
# combined with the MACD and MAs Charting Wealth study.
# By Horserider 12/25/2019

#USAGE
#
#Derivative RSI Oscillator
input enableDRSI = yes; #Derivative RSI Oscillator
input DRSI_length = 14;
input DRSI_length1 = 5;
input DRSI_length2 = 3;
input DRSI_length3 = 9;
input DRSI_price = close;
input DRSI_averagetype = averagetype.EXPONENTIAL;
input DRSI_averagetype2 = averagetype.SIMPLE;

def DRSI_netchgavg = WildersAverage(DRSI_price - DRSI_price[1], DRSI_length);
def DRSI_totchgavg = WildersAverage(AbsValue(DRSI_price - DRSI_price[1]), DRSI_length);
def DRSI_chgratio = if DRSI_totchgavg != 0 then DRSI_netchgavg / DRSI_totchgavg else 0;

def DRSI_rsi = (50 * (DRSI_chgratio + 1) - 50);

def DRSI_x = MovingAverage(DRSI_averagetype,DRSI_rsi, DRSI_length1);
def DRSI_x2 = MovingAverage(DRSI_averagetype, DRSI_x, DRSI_length2);
def DRSI_xs = MovingAverage(DRSI_averagetype2, DRSI_x2 , DRSI_length3);
def DRSI_rsi__linediff = DRSI_x2 - DRSI_xs;
def DRSI_rsi_line = DRSI_rsi__linediff;
def DRSI_sstate = if DRSI_rsi_line >= 0 then if DRSI_rsi_line > DRSI_rsi_line[1] then 100 else 10 else if DRSI_rsi_line < DRSI_rsi_line[1] then -100 else -10;
def DRSI_sbullish = enableDRSI and DRSI_sstate > 0;
def DRSI_sbearish = enableDRSI and DRSI_sstate < 0;
def DRSI_sneutral = 0;
AddLabel(showlabels and enableDRSI, "DRSI", if IsNan(DRSI_sstate) then COLOR.DARK_GRAY else
if DRSI_sstate[-displace] > 0 then COLOR.GREEN else
if DRSI_sstate[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF DRSI - Derivative RSI Oscillator Module V1.0


#START OF SWCA - Schaff Wave CrossOver Average Module V1.0
#
#CHANGELOG
# 2020.02.01 V1.0 @diazlaz Initial Module Release
#
#CREDITS
#
#USAGE
#
input enableSWCA = yes;

input SWCA_fastlengthtrend = 48;
input SWCA_slowlengthtrend = 104;
input SWCA_kperiodtrend = 36;
input SWCA_dperiodtrend = 8;
input SWCA_averagetypetrend = AverageType.EXPONENTIAL;
input SWCA_fastlengthwave = 12;
input SWCA_slowlengthwave = 26;
input SWCA_kperiodwave = 9;
input SWCA_dperiodwave = 2;
input SWCA_averagetypewave = AverageType.EXPONENTIAL;

def SWCA_macdtrend = MovingAverage(SWCA_averagetypetrend, c, SWCA_fastlengthtrend) - MovingAverage(SWCA_averagetypetrend, c, SWCA_slowlengthtrend);
def SWCA_macdwave = MovingAverage(SWCA_averagetypewave, c, SWCA_fastlengthwave) - MovingAverage(SWCA_averagetypewave, c, SWCA_slowlengthwave);
def SWCA_fastk1trend = FastKCustom(SWCA_macdtrend, SWCA_kperiodtrend);
def SWCA_fastk1wave = FastKCustom(SWCA_macdwave, SWCA_kperiodwave);
def SWCA_fastd1trend = MovingAverage(SWCA_averagetypetrend, SWCA_fastk1trend, SWCA_dperiodtrend);
def SWCA_fastd1wave = MovingAverage(SWCA_averagetypewave, SWCA_fastk1wave, SWCA_dperiodwave);
def SWCA_fastk2trend = FastKCustom(SWCA_fastd1trend, SWCA_kperiodtrend);
def SWCA_fastk2wave = FastKCustom(SWCA_fastd1wave, SWCA_kperiodwave);
def SWCA_stctrend = MovingAverage(SWCA_averagetypetrend, SWCA_fastk2trend, SWCA_dperiodtrend);
def SWCA_stcwave = MovingAverage(SWCA_averagetypewave, SWCA_fastk2wave, SWCA_dperiodwave);

def SWCA_sstate = if SWCA_stctrend > SWCA_stcwave then 100 else -100;
def SWCA_sbullish = enableSWCA and SWCA_sstate == 100;
def SWCA_sbearish = enableSWCA and SWCA_sstate == -100;
def SWCA_sneutral = 0;
AddLabel(showlabels and enableSWCA, "SWCA", if IsNan(SWCA_sstate) then COLOR.DARK_GRAY else
if SWCA_sstate[-displace] > 0 then COLOR.GREEN else
if SWCA_sstate[-displace] < 0 then COLOR.RED
else COLOR.DARK_GRAY);
#END OF SWCA - Schaff Wave CrossOver Average Module V1.0



###################################
#####  END OF MODULES SECTION #####
###################################

#ADX TRENDING
input ADXlength = 8; #ADX
input ADXTrending = 25; #ADX
input showADXBar = yes;
input DI_length = 8;

def DX = if (diplus(di_length) + diminus(di_length) > 0) then 100 * AbsValue(diplus(di_length) - diminus(di_length)) / (diplus(di_length) + diminus(di_length)) else 0;
def ADX = WildersAverage(DX, adxlength);

plot ADXCross = LowestAll(low);
ADXCross.SetPaintingStrategy(PaintingStrategy.LINE);
ADXCross.AssignValueColor(
    if ADX > ADXTrending then Color.LIME
    else Color.DARK_GRAY);
ADXCross.SetLineWeight(5);
ADXCross.SetHiding(!showADXBar);

#MACD Neutal Eval
input fastLength_1 = 12;
input slowLength_1 = 26;
input MACDLength_1 = 9;
input AverageType_1 = {SMA, default EMA};
def macd_Val_1 = MACD(fastLength_1, slowLength_1, MACDLength_1, AverageType_1).Value;
def macd_Avg1 = MACD(fastLength_1, slowLength_1, MACDLength_1, AverageType_1).Avg;
def MACD_trig = if MACD().value > MACD().avg then 1 else 0;

AddLabel(showlabels,
if ADX > ADXTrending then "Trending" else
"Non Trending",
if IsNan(c) then COLOR.DARK_GRAY else
if ADX > ADXTrending then COLOR.GREEN else
COLOR.DARK_GRAY);

AddLabel(showlabels,
"MACD",
if IsNan(c) then COLOR.DARK_GRAY else
if macd_Val_1 > macd_Avg1 then COLOR.DARK_GREEN else
COLOR.DARK_RED);


#STATE
def sResults = HA1_sState + HAMTF_sState + TTM_sState + ST1_ssupertrend + STHA_ssupertrend + FRE_sfrema + TMO_stmo + SL1_sstate + TOP_stop + DMI_sdmi + BOP_sbop
+ STCCI_ssupertrendcci + CTT_sccittm + DRSI_sstate + SWCA_sstate;

def sBullish = HA1_sBullish + HAMTF_sBullish + TTM_sBullish + ST1_sbullish + STHA_sbullish + FRE_sbullish + TMO_sbullish + SL1_sbullish + TOP_sbullish + DMI_sbullish + BOP_sbullish + STCCI_sbullish + CTT_sbullish + DRSI_sbullish + SWCA_sbullish;

def sBearish = HA1_sBearish + HAMTF_sBearish + TTM_sBearish + ST1_sbearish + STHA_sbearish + FRE_sbearish + TMO_sbearish + SL1_sbearish + TOP_sbearish + DMI_sbearish + BOP_sbearish + STCCI_sbearish + CTT_sbearish + DRSI_sbearish + SWCA_sbearish;

def bullish = if(sBullish >= threshold,1,0);
def bearish = if(sBearish >= threshold,1,0);
def sState = if bullish then 100 else if bearish then -100 else 0;
def sState2 = if sState != 0 then sState else sState2[1];

#COLORBARS
AssignPriceColor(
if PaintBars then
    if ADXPaintBarsMode then if ADX > ADXTrending then COLOR.ORANGE else COLOR.DARK_GRAY
    else
    if bullish then COLOR.GREEN else if bearish then COLOR.RED
     else
     if enableNeutralMACDPainter then
      if macd_Val_1 > macd_Avg1 then COLOR.DARK_GREEN else COLOR.DARK_RED
     else
    COLOR.GRAY
else COLOR.CURRENT);

AddLabel(showlabels,
if bullish[-displace] then "LONG" else
if bearish[-displace] then "SHORT"
else "HOLD",
if IsNan(c) then COLOR.DARK_GRAY else
if bullish[-displace] then COLOR.GREEN else
if bearish[-displace] then COLOR.RED
else COLOR.GRAY);

#END OF CSA - trend, a momentum and a cycle based indicator for ThinkorSwim V3.2
@diazlaz This is great! Thank you!! Looks like it does not work on tick charts? Thank you again !!
 

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