# I request combining 3 indicators into 1 paintbar study

H

#### hockeycoachdoug

##### New member
VIP
I watched a webinar awhile back that made the premise that combining a trend based indicator, a momentum based indicator and a cycle based indicator gives a good indication of directional bias. I would like to combine these 3 components into one paintbar type indicator and am hoping one of you coding pros might find value in this and be willing to code it here for the community.
For the trend indicator I want to use supertrend available here by Mobius. Here is the link SuperTrend by Mobius
For the momentum indicator I want to use True Momentum Oscillator (TMO). Here is the link TMO for TOS
For the cycle indicator I want to use TOP Cycle Trader. I am posting the code for this indicator here-

Code:
``````declare lower;
#plot Data = close;
input   FastCycleLength = 5;
input   SlowCycleLength = 8;
#def   CycleLineColor=DefineGlobalColor(Color.RED);
#input   CycleHistColor=Color.BLUE;
#input   ZeroLineColor1 = GetColor(1);
DefineGlobalColor("CycleLineColor", Color.RED);
DefineGlobalColor("CyclehistColor", Color.BLUE);
DefineGlobalColor("ZeroLineColor", Color.YELLOW);
def H = high;
def L = low;
#def FastVar(0),SlowVar(0),DiffVar(0);

def FastVar = ExpAverage((H + L) / 2, FastCycleLength);
def SlowVar = Average((H + L) / 2, SlowCycleLength);
def DiffVar = FastVar - SlowVar;

plot pDiffVar = DiffVar;
pDiffVar.SetDefaultColor(GlobalColor("CycleLineColor"));
plot pDiffVar2 = DiffVar;
pDiffVar2.SetDefaultColor(GlobalColor("CycleHistColor"));
plot pZeroLine = 0;
pZeroLine.SetDefaultColor(GlobalColor("ZeroLineColor"));
#Plot1(DiffVar,"Cycle",CycleLineColor);
#Plot2(DiffVar,"CycleHist",CycleHistColor);
#Plot3(0    ,"Zero",ZeroLineColor);``````
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.

I am open to any and all suggestions. I considered MACD for the momentum indicator but thought TMO was better. Considered Doncian channel for trend but thought supertrend was better. I am hoping one of you coders out there find this idea worthy of putting in the effort to code and share with us here.

I also want to create a scan/ watchlist using this where I can add a number of stock symbols to a watchlist and have the following columns showing symbol, color of bar on a daily basis (or long term) (red, green, or gray), color of bar on a 15 or 30 minute basis (short term)(red, green, or gray, and a column showing average volatility for some period of bars to use as a sort column to find the symbols that are moving.

I want to use this in 3 ways. First as a way to confirm directional bias of trade I am in ( I should be long if bars are green). second with the scanner portion, when both a long term (daily) and a short term (15 or 30 min) are both same color then enter a trade in that colors direction. Lastly as a portfolio management feature. Take a portion of money and break into 5-10 slots each represented by a sector ETF such as 50K broken into 5 trading slots of 10K committed to each ETF. When daily bars are green for a specific ETF, that slot is long all 10K, when its red that slot is in cash.

Last edited by a moderator:
• Y

##### Member
I hope somebody can help because I'm trying to do a similar thing.

D

#### diazlaz

##### Well-known member
VIP
Warehouse

Happy New Year! - stuck at the airport and delayed, and had some time to start catching up, and saw this request, had some time at the airport, here is a initial start, we can continue to improve this (adding alerts, scans, arrows, labels, etc). Ruby:
``````#trend, a momentum and a cycle based indicator for ThinkorSwim V1.0
#@hockeycoachdoug Community Request
#
#VERSION
# 2019.12.30 V1.0 @diazlaz - Community Ask/Release
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP
input PaintBars = yes;

#CORE
DefineGlobalColor("CycleLineColor", Color.RED);
DefineGlobalColor("CyclehistColor", Color.BLUE);
DefineGlobalColor("ZeroLineColor", Color.YELLOW);

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

# SuperTrend (Mobius Chat Room Request)
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST then UP else DN;
def sSuperTrend = if close < ST then -100 else 100;

#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 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;

#STATE
def sResults = sSuperTrend + sTMO + sTOP;
def sState = if sResults == 300 then 100 else if sResults == -300 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;

#COLORBARS
AssignPriceColor(
if PaintBars then
if sState == 100 then COLOR.GREEN else if sState == -100 then COLOR.RED
else COLOR.GRAY
else COLOR.CURRENT);

#END OF STUDY``````

H

#### hockeycoachdoug

##### New member
VIP
Right on diazlaz, and many thanks. Any thoughts on the scan portion? How to get columns in the watch list of symbols showing bar color for various time lengths?

• D

#### diazlaz

##### Well-known member
VIP
Warehouse
hi @hockeycoachdoug,

Here is a minimal scan/watchlist version (1.0):

Example of a scan, looking for bullish, SP500, in the 5 minute timeframe: 51 in the bullish 5 minute timeframe.

Ruby:
``````#trend, a momentum and a cycle based indicator for ThinkorSwim V1.0
#SCANNER and WATCHLIST V1.0
#
#INPUTS
input AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP

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

# SuperTrend (Mobius Chat Room Request)
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST then UP else DN;
def sSuperTrend = if close < ST then -100 else 100;

#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 sTMO = if Main > Signal then 100 else -100;

def FastVar = ExpAverage((H + L) / 2, FastCycleLength);
def SlowVar = Average((H + L) / 2, SlowCycleLength);
def DiffVar = FastVar - SlowVar;
def sTOP = if DiffVar > 0 then 100 else -100;

#STATE
def sResults = sSuperTrend + sTMO + sTOP;
def sState = if sResults == 300 then 100 else if sResults == -300 then -100 else 0;

#SCANS and WATCHLIST (UNCOMMENT)
plot Bullish = sState == 100; #scan bullish
#plot Bearish = sState == -100; #scan bearish
#plot sWatchlist = sState; #watchlist``````
you can use the code for scans and watchlist development.

• hockeycoachdoug and BenTen
H

#### hockeycoachdoug

##### New member
VIP
diazlaz- all I can say is WOW! You are a TOS wizard. Many thanks. What a great community.

• Y

##### Member
@diazlaz I was attempting my rendition of what was posted by you using some other indicators and had some trouble. I know the issue is with the TTM portion of the code, it doesn't seem to recognize that part; maybe it is because it is one of the copyrighted indicators? Anyways hopefully you can help, I've been looking for a way to combine strategys for a while and really appreciate it.
Code:
``````#@hockeycoachdoug Community Request
#
#VERSION
# 2019.12.30 V1.0 @diazlaz - Community Ask/Release
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input PaintBars = yes;
input AA = .1; # Frema
input compBarsTTM = 6;#TTM

#CORE
DefineGlobalColor("CycleLineColor", Color.RED);
DefineGlobalColor("CyclehistColor", Color.BLUE);
DefineGlobalColor("ZeroLineColor", Color.YELLOW);

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

# SuperTrend (Mobius Chat Room Request)
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST then UP else DN;
def sSuperTrend = if close < ST then -100 else 100;

#FREMA Component
# Vars:
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 == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA;
RE1 = CC * EMA + EMA;
RE2 = Power(CC, 2) * RE1 + RE1;
RE3 = Power(CC, 4) * RE2 + RE2;
RE4 = Power(CC, 8) * RE3 + RE3;
RE5 = Power(CC, 16) * RE4 + RE4;
RE6 = Power(CC, 32) * RE5 + RE5;
RE7 = Power(CC, 64) * RE6 + RE6;
RE8 = Power(CC, 128) * RE7 + RE7;

def EMA_Signal = EMA – AA * RE8;
def sFrema = if (EMA_Signal > zeroLine) then 100 else -100;

def TrendUp = Double.NaN;
def TrendDown = Double.NaN;
def sTTM = if Trendup then 100 else -100;

#STATE
def sResults = sSuperTrend + sFrema + sTTM;
def sState = if sResults == 300 then 100 else if sResults == -300 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;

#COLORBARS
AssignPriceColor(
if PaintBars then
if sState == 100 then COLOR.GREEN else if sState == -100 then COLOR.RED
else COLOR.GRAY
else COLOR.CURRENT);

#END OF STUDY``````

D

#### diazlaz

##### Well-known member
VIP
Warehouse

Integrated FREMA in this V1.2 version, also exposed the states and signals in labels.

Ruby:
``````#trend, a momentum and a cycle based indicator for ThinkorSwim V1.0
#@hockeycoachdoug Community Request
#
#VERSION
# 2019.12.31 V1.2 @diazlaz - Integrated FREMA
# 2019.12.30 V1.1 @diazlaz - Labels
# 2019.12.30 V1.0 @diazlaz - Community Ask/Release
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP
input AA = .1; #FREMA
input showLabels = yes;
input paintBars = yes;
input displace = 0;

#CORE
DefineGlobalColor("CycleLineColor", Color.RED);
DefineGlobalColor("CyclehistColor", Color.BLUE);
DefineGlobalColor("ZeroLineColor", Color.YELLOW);
DefineGlobalColor("Off", Color.DARK_GRAY);

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

# SuperTrend (Mobius Chat Room Request)
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST then UP else DN;
def sSuperTrend = if close < ST then -100 else 100;

#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 == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA;
RE1 = CC * EMA + EMA;
RE2 = Power(CC, 2) * RE1 + RE1;
RE3 = Power(CC, 4) * RE2 + RE2;
RE4 = Power(CC, 8) * RE3 + RE3;
RE5 = Power(CC, 16) * RE4 + RE4;
RE6 = Power(CC, 32) * RE5 + RE5;
RE7 = Power(CC, 64) * RE6 + RE6;
RE8 = Power(CC, 128) * RE7 + RE7;

def EMA_Signal = EMA – AA * RE8;
def sFrema = if (EMA_Signal > zeroLine) then 100 else -100;

#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 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;

#STATE
def sResults = sSuperTrend + sTMO + sTOP + sFrema;
def sState = if sResults == 400 then 100 else if sResults == -400 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;

#COLORBARS
AssignPriceColor(
if PaintBars then
if sState == 100 then COLOR.GREEN else if sState == -100 then COLOR.RED
else COLOR.GRAY
else COLOR.CURRENT);

#LABELS

AddLabel(showlabels, "SuperTrend", if IsNan(c) then COLOR.DARK_GRAY else
if sSuperTrend[-displace] > 0 then COLOR.GREEN else
if sSuperTrend[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));

AddLabel(showlabels, "TMO", if IsNan(c) then COLOR.DARK_GRAY else
if sTMO[-displace] > 0 then COLOR.GREEN else
if sTMO[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));

AddLabel(showlabels, "TOP", if IsNan(c) then COLOR.DARK_GRAY else
if sTOP[-displace] > 0 then COLOR.GREEN else
if sTOP[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));

AddLabel(showlabels, "FREMA", if IsNan(c) then COLOR.DARK_GRAY else
if sFrema[-displace] > 0 then COLOR.GREEN else
if sFrema[-displace] < 0 then COLOR.RED
else GlobalColor("Off"));

if sState[-displace] > 0 then "LONG" else
if sState[-displace] < 0 then "SHORT"
else "HOLD",
if IsNan(c) then COLOR.DARK_GRAY else
if sState[-displace] > 0 then COLOR.GREEN else
if sState[-displace] < 0 then COLOR.RED
else COLOR.GRAY);

#END OF STUDY``````

• Y

##### Member
@diazlaz Is there any way to use ttm? Im trying statements like def sTTM = if trendup then 100 else -100

D

#### diazlaz

##### Well-known member
VIP
Warehouse
@diazlaz Is there any way to use ttm? Im trying statements like def sTTM = if trendup then 100 else -100
can you post the code ur trying to integrate? are you talking about the TTM_Squeeze? What are the rules you want to apply to it?

Y

##### Member
can you post the code ur trying to integrate? are you talking about the TTM_Squeeze? What are the rules you want to apply to it?
I'm trying to integrate the TTM Trend
Code:
``````def TrendUp = Double.NaN;
def TrendDown = Double.NaN;
def sTTM = if trendup then 100 else -100;``````
One more thing I was attempting to do myself was to add more parameters to the +3 -3 system.
For the indicators I'm using (TTM Trend, Supertrend, Frema) it is most effective if instead of switching green to grey to red based on the +3 -3 system to do as follows. (The ultimate plan is to make a strategy of it not use it as a paintbar study)
+3 Would trigger a buy signal, after this buy signal takes place a sell signal will not be given until 2/3 of the indicators are bearish or the equivalent of a -1 in the system.
This would differentiate from the current system by giving a sell signal before a -3 is recorded in the system while also giving a hold even if the total is 2. The opposite is true for shorting. -3 would trigger a short signal, a sum of +1 would signal a sell of that short.
Essentially entries are +3 or -3, sells are +1 or -1 accordingly.
But all of this depends on getting the ttm to work so If you could figure that out how to integrate it that would be great

D

#### diazlaz

##### Well-known member
VIP
Warehouse
@YungTraderFromMontana Try this piece of code to integrate TTM:

Ruby:
``````#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;``````

• hockeycoachdoug
Y

Thanks so much

Y

##### Member
I tried to modify it to paint green if 2/3 of the indicators are bullish and I can't get it to work. You seem to know the ins and outs of Thinkscript so I bet you could help. Despite the code I added to the State and Colorbars sections it only paintsbars based on all 3 being bullish.
Code:
``````#@hockeycoachdoug Community Request
#
#VERSION
# 2019.12.30 V1.0 @diazlaz - Community Ask/Release
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input PaintBars = yes;
input AA = .1; # Frema
input compBarsTTM = 6;#TTM

#CORE
DefineGlobalColor("CycleLineColor", Color.RED);
DefineGlobalColor("CyclehistColor", Color.BLUE);
DefineGlobalColor("ZeroLineColor", Color.YELLOW);

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

# SuperTrend (Mobius Chat Room Request)
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST then UP else DN;
def sSuperTrend = if close < ST then -100 else 100;

#FREMA Component
# Vars:
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 == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA;
RE1 = CC * EMA + EMA;
RE2 = Power(CC, 2) * RE1 + RE1;
RE3 = Power(CC, 4) * RE2 + RE2;
RE4 = Power(CC, 8) * RE3 + RE3;
RE5 = Power(CC, 16) * RE4 + RE4;
RE6 = Power(CC, 32) * RE5 + RE5;
RE7 = Power(CC, 64) * RE6 + RE6;
RE8 = Power(CC, 128) * RE7 + RE7;

def EMA_Signal = EMA – AA * RE8;
def sFrema = if (EMA_Signal > zeroLine) then 100 else -100;

#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;

#STATE
def sResults = sSuperTrend + sFrema + sTTM;
def sState = if sResults == 300 then 300 else if sResults == -300 then -300 else if sResults == 100 then 100 else if sResults == -100 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;

#COLORBARS
AssignPriceColor(
if PaintBars then
if sResults == 300 or 100 then Color.GREEN else if sResults == -300 or -100 then Color.RED
else Color.GRAY
else Color.CURRENT);``````

D

#### diazlaz

##### Well-known member
VIP
Warehouse
ok - made a few changes, integrated TTM Trends, created modules and added a threshold. set the input to the number of conditions to trigger a signal (long or short), default to 3, but can easily be changed, all modules and signals are activated by default, can disable it via the input.

Version 1.4 Threshold and Signal Selection: Code:

Ruby:
``````#trend, a momentum and a cycle based indicator for ThinkorSwim V1.4
#@hockeycoachdoug Community Request
#
#VERSION
# 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
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP
input AA = .1; #FREMA
input showLabels = yes;
input PaintBars = yes;
input threshold = 3;
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 na = Double.NaN;

#TTM Trend
input enableTTM = yes;
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 enableSuperTrend = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST 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"));

#FREMA
input enableFrema = yes;
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 == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA;
RE1 = CC * EMA + EMA;
RE2 = Power(CC, 2) * RE1 + RE1;
RE3 = Power(CC, 4) * RE2 + RE2;
RE4 = Power(CC, 8) * RE3 + RE3;
RE5 = Power(CC, 16) * RE4 + RE4;
RE6 = Power(CC, 32) * RE5 + RE5;
RE7 = Power(CC, 64) * RE6 + RE6;
RE8 = Power(CC, 128) * RE7 + RE7;
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 enableTMO = yes;
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"));

input enableTOP = yes;
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"));

#STATE
def sResults = sSuperTrend + sTMO + sTOP + sFrema + sTTM;
def sState = if sResults == 400 then 100 else if sResults == -400 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;
def sBullish = sBullish_TTM + sBullish_SuperTrend + sBullish_Frema + sBullish_TMO + sBullish_TOP;
def sBearish = sBearish_TTM + sBearish_SuperTrend + sBearish_Frema + sBearish_TMO + sBearish_TOP;
def bullish = if(sBullish >= threshold,1,0);
def bearish = if(sBearish >= threshold,1,0);

#COLORBARS
AssignPriceColor(
if PaintBars then
if bullish then COLOR.GREEN else if bearish then COLOR.RED
else COLOR.GRAY
else COLOR.CURRENT);

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 STUDY``````

D

#### diazlaz

##### Well-known member
VIP
Warehouse
the bullish and bearish are Boolean that has the signal, you can use these in your scans, watchlist, alerts, arrows, or any other conditional checks to extend the script and trading plan.

• markos and BenTen
M

#### mailbagman2000

##### New member
Just used this scan and found a lot of good buys. Also filtered for 3 and 6 months returns

Y

##### Member
ok - made a few changes, integrated TTM Trends, created modules and added a threshold. set the input to the number of conditions to trigger a signal (long or short), default to 3, but can easily be changed, all modules and signals are activated by default, can disable it via the input.

Version 1.4 Threshold and Signal Selection: Code:

Ruby:
``````#trend, a momentum and a cycle based indicator for ThinkorSwim V1.4
#@hockeycoachdoug Community Request
#
#VERSION
# 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
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP
input AA = .1; #FREMA
input showLabels = yes;
input PaintBars = yes;
input threshold = 3;
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 na = Double.NaN;

#TTM Trend
input enableTTM = yes;
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 enableSuperTrend = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST 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"));

#FREMA
input enableFrema = yes;
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 == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA;
RE1 = CC * EMA + EMA;
RE2 = Power(CC, 2) * RE1 + RE1;
RE3 = Power(CC, 4) * RE2 + RE2;
RE4 = Power(CC, 8) * RE3 + RE3;
RE5 = Power(CC, 16) * RE4 + RE4;
RE6 = Power(CC, 32) * RE5 + RE5;
RE7 = Power(CC, 64) * RE6 + RE6;
RE8 = Power(CC, 128) * RE7 + RE7;
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 enableTMO = yes;
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"));

input enableTOP = yes;
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"));

#STATE
def sResults = sSuperTrend + sTMO + sTOP + sFrema + sTTM;
def sState = if sResults == 400 then 100 else if sResults == -400 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;
def sBullish = sBullish_TTM + sBullish_SuperTrend + sBullish_Frema + sBullish_TMO + sBullish_TOP;
def sBearish = sBearish_TTM + sBearish_SuperTrend + sBearish_Frema + sBearish_TMO + sBearish_TOP;
def bullish = if(sBullish >= threshold,1,0);
def bearish = if(sBearish >= threshold,1,0);

#COLORBARS
AssignPriceColor(
if PaintBars then
if bullish then COLOR.GREEN else if bearish then COLOR.RED
else COLOR.GRAY
else COLOR.CURRENT);

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 STUDY``````
If you're still up for adding more to this study I got some more additions for you. Because you seamlessly mended the other studies I suggested I think adding ADX/DMI to avoid periods of consolidation would be a good idea.
I'm posting the code for a indicator of sorts that represents consolidation as white and trending periods as blue.
Code:
``````input ADXlength = 8;

plot ADXCross = if !IsNaN(close) then 0.75 else double.Nan;
if  ADX > 25 then Color.Blue
else Color.White);
I'll leave it up to you on how to represent it in the paintbar study, I guess greying those candles would work or possibly making it a lighter green or red in consolidation areas would work. It's impressive to see how far you've came with it.

Y

##### Member
ok - made a few changes, integrated TTM Trends, created modules and added a threshold. set the input to the number of conditions to trigger a signal (long or short), default to 3, but can easily be changed, all modules and signals are activated by default, can disable it via the input.

Version 1.4 Threshold and Signal Selection: Code:

Ruby:
``````#trend, a momentum and a cycle based indicator for ThinkorSwim V1.4
#@hockeycoachdoug Community Request
#
#VERSION
# 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
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP
input AA = .1; #FREMA
input showLabels = yes;
input PaintBars = yes;
input threshold = 3;
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 na = Double.NaN;

#TTM Trend
input enableTTM = yes;
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 enableSuperTrend = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST 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"));

#FREMA
input enableFrema = yes;
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 == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA;
RE1 = CC * EMA + EMA;
RE2 = Power(CC, 2) * RE1 + RE1;
RE3 = Power(CC, 4) * RE2 + RE2;
RE4 = Power(CC, 8) * RE3 + RE3;
RE5 = Power(CC, 16) * RE4 + RE4;
RE6 = Power(CC, 32) * RE5 + RE5;
RE7 = Power(CC, 64) * RE6 + RE6;
RE8 = Power(CC, 128) * RE7 + RE7;
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 enableTMO = yes;
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"));

input enableTOP = yes;
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"));

#STATE
def sResults = sSuperTrend + sTMO + sTOP + sFrema + sTTM;
def sState = if sResults == 400 then 100 else if sResults == -400 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;
def sBullish = sBullish_TTM + sBullish_SuperTrend + sBullish_Frema + sBullish_TMO + sBullish_TOP;
def sBearish = sBearish_TTM + sBearish_SuperTrend + sBearish_Frema + sBearish_TMO + sBearish_TOP;
def bullish = if(sBullish >= threshold,1,0);
def bearish = if(sBearish >= threshold,1,0);

#COLORBARS
AssignPriceColor(
if PaintBars then
if bullish then COLOR.GREEN else if bearish then COLOR.RED
else COLOR.GRAY
else COLOR.CURRENT);

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 STUDY``````
Also I have 2 trend studies to add that could possibly increase the accuracy of the study.
Code:
``````#DMITrend
input DMIlength = 13;

def hiDiff = high - high;
def loDiff = low - 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 ATR = WildersAverage(TrueRange(high, close, low), DMIlength);
Def DIPlus = 100 * WildersAverage(plusDM, DMIlength) / ATR;
Def DIMinus = 100 * WildersAverage(minusDM, DMIlength) / ATR;
Plot dmiSignal =  if !IsNaN(close) then 0.73 else double.Nan;
dmiSignal.SetPaintingStrategy(paintingStrategy.liNE_VS_SQUARES);

dmiSignal.AssignValueColor(
if DIPlus > DIMinus then Color.Green
else if DIMinus > DIPlus then Color.Red
else Color.White);

#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;
}
plot BoP =  if !IsNaN(close) then 0.6 else double.Nan;
AddChartBubble(IsNaN(MA1[-1]) && close , 0.6,  "BoP" , color.White, yes);
BoP.SetPaintingStrategy(paintingStrategy.liNE_VS_SQUARES);
BoP.AssignValueColor(
if BMP > 0 then Color.Green
else Color.Red);

BoP.HideBubble();``````

D

#### diazlaz

##### Well-known member
VIP
Warehouse
Version 1.5 - with ADX trending and non-trending states, labels and visuals, set ADXlength, ADXTrending and showADXBar to alter the behavior and enable or display of the trending bar.

trending bar - green is trending (change default in ADXTrending) and dark_gray is non-trending. Ruby:
``````#trend, a momentum and a cycle based indicator for ThinkorSwim V1.5
#@hockeycoachdoug Community Request
#
#VERSION
# 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
#
#
#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 AtrMult = 1.0; #SuperTrend
input nATR = 4; #SuperTrend
input AvgType = AverageType.HULL; #SuperTrend
input length = 14; #TMO
input calcLength = 5; #TMO
input smoothLength = 3; #TMO
input FastCycleLength = 5; #TOP
input SlowCycleLength = 8; #TOP
input AA = .1; #FREMA
input showLabels = yes;
input PaintBars = yes;
input threshold = 3;
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 na = Double.NaN;

#TTM Trend
input enableTTM = yes;
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 enableSuperTrend = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST 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"));

#FREMA
input enableFrema = yes;
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 == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA;
RE1 = CC * EMA + EMA;
RE2 = Power(CC, 2) * RE1 + RE1;
RE3 = Power(CC, 4) * RE2 + RE2;
RE4 = Power(CC, 8) * RE3 + RE3;
RE5 = Power(CC, 16) * RE4 + RE4;
RE6 = Power(CC, 32) * RE5 + RE5;
RE7 = Power(CC, 64) * RE6 + RE6;
RE8 = Power(CC, 128) * RE7 + RE7;
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 enableTMO = yes;
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"));

input enableTOP = yes;
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"));

else Color.DARK_GRAY);
"Non Trending",
if IsNan(c) then COLOR.DARK_GRAY else
COLOR.DARK_GRAY);

#STATE
def sResults = sSuperTrend + sTMO + sTOP + sFrema + sTTM;
def sState = if sResults == 400 then 100 else if sResults == -400 then -100 else 0;
def sState2 = if sState != 0 then sState else sState2;
def sBullish = sBullish_TTM + sBullish_SuperTrend + sBullish_Frema + sBullish_TMO + sBullish_TOP;
def sBearish = sBearish_TTM + sBearish_SuperTrend + sBearish_Frema + sBearish_TMO + sBearish_TOP;
def bullish = if(sBullish >= threshold,1,0);
def bearish = if(sBearish >= threshold,1,0);

#COLORBARS
AssignPriceColor(
if PaintBars then
if bullish then COLOR.GREEN else if bearish then COLOR.RED
else COLOR.GRAY
else COLOR.CURRENT);

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 STUDY``````

•  Questions 6