#trend, a momentum and a cycle based indicator for ThinkorSwim V2.1
#@hockeycoachdoug Community Request
#
#VERSION
# 2020.01.18 V2.1 @diazlaz - Integrated SuperTrend with HeikinAshiCandles
# 2020.01.17 V2.0 @diazlaz - Integrated HeikinAshiCandles and HeikinAshiCandles MTF
# - set aggregationPeriod to MTF aggregation (default 5 mins)
# 2020.01.09 V1.9 @diazlaz - Integrated PaintBars MACD Neutral Condition trigger
# - set enableNeutralMACDPainter to yes (default is no) and
# - neutral gray will be evaluated by MACD (Dark Red/Green)
# 2020.01.09 V1.8 @diazlaz - 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.
# 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
#
#LINK
#https://usethinkscript.com/threads/i-request-help-combining-3-indicators-into-1-paintbar-study.1390/
#
#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.
#
#Now includes: TTM Trend, Super Trend, Frema, TMO, TOP, DMI , BOP, Slim Ribbon and
#ADX as Trending/Non-Trending, Super Trend w/CCI and ATR combo and
#a CCI + TTM squeeze + TTM trend Combo with LONG/SHORT/HOLD signal selection
#based on threshold specified and indicators enabled.
#
declare upper;
#INPUTS
input showLabels = yes;
input PaintBars = yes;
input enableNeutralMACDPainter = no;
input aggregationPeriod = AggregationPeriod.FIVE_MIN;
input threshold = 7;
input enableHA = yes;
input enableHAMTF = yes;
input enableTTM = yes;
input enableSuperTrend = yes;
input enableSuperTrendHA = yes;
input enableFrema = yes;
input enableTMO = yes;
input enableSlim = yes;
input enableTOP = yes;
input enableDMI = yes;
input enableBOP = yes;
input enableSuperTrendCCIATR = yes;
input enableCCITTM = yes;
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 h1 = high(period = aggregationPeriod);
def l1 = low(period = aggregationPeriod);
def o1 = open(period = aggregationPeriod);
def c1 = close(period = aggregationPeriod);
def na = Double.NaN;
AddLabel(showLabels, "A trend, momentum and cycle Trading System v2.1", Color.CYAN);
#HeikinAshiCandles
def HA1open;
def HA1high;
def HA1low;
def HA1close;
HA1open = CompoundValue(1, (HA1open[1] + HA1close[1]) / 2, (o[1] + c[1]) / 2);
HA1high = Max(Max(h, HA1open), HA1close[1]);
HA1low = Min(Min(l, HA1open), HA1close[1]);
HA1close = (o + h + l + c) / 4;
def sHA1 = if HA1close > HA1open then 100 else -100;
def sBullish_HA1 = if(enableHA and sHA1 == 100,1,0);
def sBearish_HA1 = enableHA and sHA1 == -100;
AddLabel(showlabels and enableHA, "HA", if IsNan(sHA1) then COLOR.DARK_GRAY else
if sHA1[-displace] > 0 then COLOR.GREEN else
if sHA1[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#HeikinAshiCandles MTF
def HA2open;
def HA2high;
def HA2low;
def HA2close;
HA2open = CompoundValue(1, (HA2open[1] + HA2close[1]) / 2, (o1[1] + c1[1]) / 2);
HA2high = Max(Max(h1, HA2open), HA2close[1]);
HA2low = Min(Min(l1, HA2open), HA2close[1]);
HA2close = (o1 + h1 + l1 + c1) / 4;
def sHA2 = if HA2close > HA2open then 100 else -100;
def sBullish_HA2 = if(enableHAMTF and sHA2 == 100,1,0);
def sBearish_HA2 = enableHAMTF and sHA2 == -100;
AddLabel(showlabels and enableHAMTF, "HAMTF", if IsNan(sHA2) then COLOR.DARK_GRAY else
if sHA2[-displace] > 0 then COLOR.GREEN else
if sHA2[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#TTM Trend
def TrendUp = if TTM_Trend().TrendUp == 1 then 1 else 0;
def TrendDown = if TTM_Trend().TrendDown == 1 then 1 else 0;
def sTTM = if TrendUp then 100 else if TrendDown then -100 else 0;
def sBullish_TTM = if(enableTTM and sTTM == 100,1,0);
def sBearish_TTM = enableTTM and sTTM == -100;
AddLabel(showlabels and enableTTM, "TTM", if IsNan(sTTM) then COLOR.DARK_GRAY else
if sTTM[-displace] > 0 then COLOR.GREEN else
if sTTM[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#SuperTrend (Mobius)
input AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
def sSuperTrend = if close < ST then -100 else 100;
def sBullish_SuperTrend = enableSuperTrend and sSuperTrend == 100;
def sBearish_SuperTrend = enableSuperTrend and sSuperTrend == -100;
AddLabel(showlabels and enableSuperTrend, "SuperTrend", if IsNan(sSuperTrend) then COLOR.DARK_GRAY else
if sSuperTrend[-displace] > 0 then COLOR.GREEN else
if sSuperTrend[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#SuperTrend with HeikinAshiCandles (Mobius)
def ATR2 = MovingAverage(AvgType, TrueRange(ha1high, HA1close, HA1low), nATR);
def UP2 = HL2 + (AtrMult * ATR2);
def DN2 = HL2 + (-AtrMult * ATR2);
def ST2 = if HA1close < ST2[1] then UP2 else DN2;
def sSuperTrend2 = if HA1close < ST2 then -100 else 100;
def sBullish_SuperTrend2 = enableSuperTrendHA and sSuperTrend2 == 100;
def sBearish_SuperTrend2 = enableSuperTrendHA and sSuperTrend2 == -100;
AddLabel(showlabels and enableSuperTrendHA, "SuperTrendHA", if IsNan(sSuperTrend2) then COLOR.DARK_GRAY else
if sSuperTrend2[-displace] > 0 then COLOR.GREEN else
if sSuperTrend2[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#FREMA
input AA = .1; #FREMA
def CC;
def zeroLine = 0;
def RE1;
def RE2;
def RE3;
def RE4;
def RE5;
def RE6;
def RE7;
def RE8;
def EMA;
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];
def EMA_Signal = EMA – AA * RE8;
def sFrema = if (EMA_Signal > zeroLine) then 100 else -100;
def sBullish_Frema = enableFrema and sFrema == 100;
def sBearish_Frema = enableFrema and sFrema == -100;
AddLabel(showlabels and enableFrema, "Frema", if IsNan(sFrema) then COLOR.DARK_GRAY else
if sFrema[-displace] > 0 then COLOR.GREEN else
if sFrema[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#TMO
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
def 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);
def EMA5 = ExpAverage(data, calcLength);
def Main = ExpAverage(EMA5, smoothLength);
def Signal = ExpAverage(Main, smoothLength);
def sMain = if Main > Signal then 100 else -100;
def sSignal = if Main > Signal then 100 else - 100;
def ob = if isNaN(c) then double.nan else round(length * .7);
def os = if isNaN(c) then double.nan else -round(length * .7);
def sTMO = if Main > Signal then 100 else -100;
def sBullish_TMO = enableTMO and sTMO == 100;
def sBearish_TMO = enableTMO and sTMO == -100;
AddLabel(showlabels and enableTMO, "TMO", if IsNan(sTMO) then COLOR.DARK_GRAY else
if sTMO[-displace] > 0 then COLOR.GREEN else
if sTMO[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#SLIM
input sSuperFast = 8;
input sFast = 13;
input sSlow = 21;
def SuperFastM1 = ExpAverage(close[-displace], sSuperFast);
def FastM1 = ExpAverage(close[-displace], sFast);
def SlowM1 = ExpAverage(close[-displace], sSlow);
def buyM1 = SuperFastM1 > FastM1 and FastM1 > SlowM1 and low > SuperFastM1;
def stopbuyM1 = SuperFastM1 <= FastM1;
def buynowM1 = !buyM1[1] and buyM1;
def buysignalM1 = CompoundValue(1, if buynowM1 and !stopbuyM1 then 1
else if buysignalM1[1] == 1 and stopbuyM1 then 0 else buysignalM1[1], 0);
def Buy_SignalM1 = buysignalM1[1] == 0 and buysignalM1 == 1;
def Momentum_DownM1 = buysignalM1[1] == 1 and buysignalM1 == 0;
def sellM1 = SuperFastM1 < FastM1 and FastM1 < SlowM1 and high < SuperFastM1;
def stopsellM1 = SuperFastM1 >= FastM1;
def sellnowM1 = !sellM1[1] and sellM1;
def sellsignalM1 = CompoundValue(1, if sellnowM1 and !stopsellM1 then 1
else if sellsignalM1[1] == 1 and stopsellM1 then 0 else sellsignalM1[1], 0);
def Sell_SignalM1 = sellsignalM1[1] == 0 and sellsignalM1;
def Momentum_UpM1 = sellsignalM1[1] == 1 and sellsignalM1 == 0;
def sStateM1 = if Buy_SignalM1 then 100 else if Momentum_UpM1 then 10 else if Sell_SignalM1 then -100 else if Momentum_DownM1 then -10 else sStateM1[1];
def sBullish_SLIM = enableSLIM and sStateM1 == 100;
def sBearish_SLIM = enableSLIM and sStateM1 == -100;
AddLabel(showlabels and enableSLIM, "SLIM", if IsNan(sStateM1) then COLOR.DARK_GRAY else
if sStateM1[-displace] >= 100 then COLOR.GREEN else
if sStateM1[-displace] <= -100 then COLOR.RED
else COLOR.YELLOW);
#TOP Cycle Trader
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP
def FastVar = ExpAverage((H + L) / 2, FastCycleLength);
def SlowVar = Average((H + L) / 2, SlowCycleLength);
def DiffVar = FastVar - SlowVar;
def pDiffVar = DiffVar;
def pDiffVar2 = DiffVar;
def sTOP = if DiffVar > 0 then 100 else -100;
def sBullish_TOP = enableTOP and sTOP == 100;
def sBearish_TOP = enableTOP and sTOP == -100;
AddLabel(showlabels and enableTOP, "TOP", if IsNan(sTOP) then COLOR.DARK_GRAY else
if sTOP[-displace] > 0 then COLOR.GREEN else
if sTOP[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#DMITrend
input DMIlength = 13;
def hiDiff = high - high[1];
def loDiff = low[1] - low;
def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM = if loDiff > hiDiff and loDiff > 0 then loDiff else 0;
def ATRDMI = WildersAverage(TrueRange(high, close, low), DMIlength);
Def DIPlus = 100 * WildersAverage(plusDM, DMIlength) / ATRDMI;
Def DIMinus = 100 * WildersAverage(minusDM, DMIlength) / ATRDMI;
def sDMI = if DIPlus > DIMinus then 100 else if DIMinus > DIPlus then -100 else 0;
def sBullish_DMI = enableDMI and sDMI == 100;
def sBearish_DMI = enableDMI and sDMI == -100;
AddLabel(showlabels and enableDMI, "DMI", if IsNan(sDMI) then COLOR.DARK_GRAY else
if sDMI[-displace] > 0 then COLOR.GREEN else
if sDMI[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#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;
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;
}
def sBOP = if BMP > 0 then 100 else -100;
def sBullish_BOP = enableBOP and sBOP == 100;
def sBearish_BOP = enableBOP and sBOP == -100;
AddLabel(showlabels and enableBOP, "BOP", if IsNan(sBOP) then COLOR.DARK_GRAY else
if sBOP[-displace] > 0 then COLOR.GREEN else
if sBOP[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
# SUPERTREND BY MOBIUS AND CCI ATR TREND COMBINED
input lengthCCI = 50;
input lengthATR = 21;
input AtrFactor = 1.0;
def pricedata = hl2;
def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
then 0
else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
then Max(MT1[1], pricedata - ATRcci)
else Min(MT1[1], pricedata + ATRcci);
def sSuperTrendCCIATR = if c < MT1 and c < ST then -100 else if C > MT1 and c >ST then 100 else 0;
def sBullish_SuperTrendCCIATR = enableSuperTrendCCIATR and sSuperTrendCCIATR == 100;
def sBearish_SuperTrendCCIATR = enableSuperTrendCCIATR and sSuperTrendCCIATR == -100;
AddLabel(showlabels and enableSuperTrendCCIATR, "SuperTrendCCI", if IsNan(sSuperTrendCCIATR) then COLOR.DARK_GRAY else
if sSuperTrendCCIATR[-displace] > 0 then COLOR.GREEN else
if sSuperTrendCCIATR[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
# CCI + TTM squeeze + TTM trend
def CCIBuy = CCI(length = 14).CCI > 0 and CCI(length = 50).CCI > 0;
def CCISell = CCI(length = 14).CCI < 0 and CCI(length = 50).CCI < 0;
def sTrendUp = TTM_Trend().TrendUp;
def sTrendDn = TTM_Trend().TrendDown;
def SqueezeUp = TTM_Squeeze().Histogram >= 0;
def SqueezeDn = TTM_Squeeze().Histogram <= 0;
def Buy = CCIBuy and sTrendUp and SqueezeUp;
def Sell = CCISell and sTrendDn and SqueezeDn;
def sCCITTM = if buy then 100 else if sell then -100 else 0;
def sBullish_CCITTM = enableCCITTM and sCCITTM == 100;
def sBearish_CCITTM = enableCCITTM and sCCITTM == -100;
AddLabel(showlabels and enableCCITTM, "TTM CCI", if IsNan(sCCITTM) then COLOR.DARK_GRAY else
if sCCITTM[-displace] > 0 then COLOR.GREEN else
if sCCITTM[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));
#ADX TRENDING
input ADXlength = 8; #ADX
input ADXTrending = 25; #ADX
input showADXBar = yes;
def ADX = DMI(ADXlength).ADX;
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 = sHA1 + sHA2 + sSuperTrend + sTMO + sTOP + sFrema + sTTM + sDMI + sBOP + sStateM1 + sSuperTrend2;
def sBullish = sBullish_HA1 + sBullish_HA2 + sBullish_TTM + sBullish_SuperTrend + sBullish_Frema + sBullish_TMO + sBullish_TOP + sBullish_DMI + sBullish_BOP + sBullish_SLIM + sBullish_SuperTrendCCIATR + sBullish_CCITTM +
sBullish_SuperTrend2;
def sBearish = sBearish_HA1 + sBearish_HA2 + sBearish_TTM + sBearish_SuperTrend + sBearish_Frema + sBearish_TMO + sBearish_TOP + sBearish_DMI + sBearish_BOP + sBearish_SLIM + sBearish_SuperTrendCCIATR + sBearish_CCITTM +
sBearish_SuperTrend2;
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 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 trend, a momentum and a cycle based indicator for ThinkorSwim V2.1