# Combining Indicators Help

#### aren1231

##### New member
I'm trying to combine a couple indicators to create a strategy that gives me a buy sell signal. Can anyone help?

Ruby:
``````Buy when HMA 20 crosses over 50, zscore (1hr) turns green, accumulation distribution index turns green
#########
HMA Cloud
#########
plot hma20 = hullMovingAvg(close, 20);
plot hma50 = hullMovingAvg(close, 50);

#########
Zscore (multiple time frame)
#########

#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the indicator, but please provide a link back to ThetaTrend.com
declare lower;

input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD = stdev(price,length);
def avgClose = simpleMovingAvg(price,length);
def ofoneSD = oneSD*price[1];
def Zscorevalue = ((price-avgClose)/oneSD);
def avgZv = average(Zscorevalue,20);

#Compute and plot Z-Score
plot Zscore = ((price-avgClose)/oneSD);
Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
Zscore.setLineWeight(2);
Zscore.assignValueColor(if Zscore > 0 then color.green else color.red);
plot avgZscore = average(Zscorevalue,ZavgLength);
avgZscore.setPaintingStrategy(paintingStrategy.LINE);

#This is an optional plot that will display the momentum of the Z-Score average
plot momZAvg = (avgZv-avgZv[5]);
#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
plot negtwo = -2;
zero.setDefaultColor(color.black);

########
Accumulation/Distribution (4hr)
########

# TOS MTF AccumulationSwingIndex
#global variables
input agg  = AggregationPeriod.TEN_MIN ;
def cclose = close(period = agg);
def oopen  = open(period = agg);
def hhigh  = high(period = agg);
def llow   = low(period = agg);
# ########################################################
input smaLength = 10;
def limit = 30;
def AbsHighClose = AbsValue(hhigh - cclose[1]);
def AbsLowClose = AbsValue(llow - cclose[1]);
def AbsCloseOpen = AbsValue(cclose[1] - oopen[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
If(AbsHighClose >= (hhigh - llow), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen),
If(AbsLowClose >= (hhigh - llow),  AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
(50 * (((cclose - cclose[1]) + 0.50 * (cclose - oopen) + 0.25 * (cclose[1] - oopen[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
0 + if !IsNaN(nRes[1]) then nRes[1] else 0);
def ASI = nRes;
def sma = SimpleMovingAvg(ASI,smaLength);
# ########################################################
# charting and formatting
declare lower;
plot pASI = ASI;
pASI.SetLineWeight(3);
pASI.AssignValueColor(if asi>sma then Color.Green else Color.RED);
plot pSMA = sma;
pSMA.SetPaintingStrategy(PaintingStrategy.DASHES);
pSMA.SetLineWeight(1);
pSMA.AssignValueColor(Color.VIOLET);
DefineGlobalColor("bear",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("bull",  CreateColor(0, 165, 0)) ;

Last edited by a moderator:
Solution
I've updated the code. At first glance it looks like it will be amazing, but now I am trying to build a scan to go with it. Fingers crossed
Ruby:
``````#Buy when HMA 20 crosses over 50, zscore (1hr) turns green, accumulation distribution index turns green
#########
#HMA Cloud
#########
plot hma20 = HullMovingAvg(close, 20);
plot hma50 = HullMovingAvg(close, 50);

#########
#Zscore (multiple time frame)
#########

#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the indicator, but please provide a link back to ThetaTrend.com

input agg1 = aggregationPeriod.HOUR;
input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD =...``````
I'm trying to combine a couple indicators to create a strategy that gives me a buy sell signal. Can anyone help?

Ruby:
``````Buy when HMA 20 crosses over 50, zscore (1hr) turns green, accumulation distribution index turns green
#########
HMA Cloud
#########
plot hma20 = hullMovingAvg(close, 20);
plot hma50 = hullMovingAvg(close, 50);

#########
Zscore (multiple time frame)
#########

#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the indicator, but please provide a link back to ThetaTrend.com
declare lower;

input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD = stdev(price,length);
def avgClose = simpleMovingAvg(price,length);
def ofoneSD = oneSD*price[1];
def Zscorevalue = ((price-avgClose)/oneSD);
def avgZv = average(Zscorevalue,20);

#Compute and plot Z-Score
plot Zscore = ((price-avgClose)/oneSD);
Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
Zscore.setLineWeight(2);
Zscore.assignValueColor(if Zscore > 0 then color.green else color.red);
plot avgZscore = average(Zscorevalue,ZavgLength);
avgZscore.setPaintingStrategy(paintingStrategy.LINE);

#This is an optional plot that will display the momentum of the Z-Score average
plot momZAvg = (avgZv-avgZv[5]);
#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
plot negtwo = -2;
zero.setDefaultColor(color.black);

########
Accumulation/Distribution (4hr)
########

# TOS MTF AccumulationSwingIndex
#global variables
input agg  = AggregationPeriod.TEN_MIN ;
def cclose = close(period = agg);
def oopen  = open(period = agg);
def hhigh  = high(period = agg);
def llow   = low(period = agg);
# ########################################################
input smaLength = 10;
def limit = 30;
def AbsHighClose = AbsValue(hhigh - cclose[1]);
def AbsLowClose = AbsValue(llow - cclose[1]);
def AbsCloseOpen = AbsValue(cclose[1] - oopen[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
If(AbsHighClose >= (hhigh - llow), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen),
If(AbsLowClose >= (hhigh - llow),  AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
(50 * (((cclose - cclose[1]) + 0.50 * (cclose - oopen) + 0.25 * (cclose[1] - oopen[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
0 + if !IsNaN(nRes[1]) then nRes[1] else 0);
def ASI = nRes;
def sma = SimpleMovingAvg(ASI,smaLength);
# ########################################################
# charting and formatting
declare lower;
plot pASI = ASI;
pASI.SetLineWeight(3);
pASI.AssignValueColor(if asi>sma then Color.Green else Color.RED);
plot pSMA = sma;
pSMA.SetPaintingStrategy(PaintingStrategy.DASHES);
pSMA.SetLineWeight(1);
pSMA.AssignValueColor(Color.VIOLET);
DefineGlobalColor("bear",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("bull",  CreateColor(0, 165, 0)) ;

1. The first 'buy' uses the crosses of 3 indicators that you are combining. This limits the number of signals as it is difficult to have multiple indicators all cross (or change colors) at the same time.
2.The second 'buy1' uses the HMA cross and the other 2 indicators being true (green) when the HMA cross occurred. This will create more signals. You could create other scenarios by choosing different combinations of crosses and true indicators.

The white arrow(s) in the chart below are from the limited 'buy' plot. The cyan arrow(s) are from the 'buy1 plot. I Have changed most of your orignal plot/clouds to 'def' statements to allow the buy arrows to appear along with the HMA plots, as the HMA plots are an 'upper' indicator and the Zscore and PASI are lower indicators and do not plot well on the upper price pane.

Ruby:
``````#Buy when HMA 20 crosses over 50, zscore (1hr) turns green, accumulation distribution index turns green
#########
#HMA Cloud
#########
plot hma20 = HullMovingAvg(close, 20);
plot hma50 = HullMovingAvg(close, 50);

#########
#Zscore (multiple time frame)
#########

#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the indicator, but please provide a link back to ThetaTrend.com

input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD = StDev(price, length);
def avgClose = SimpleMovingAvg(price, length);
def ofoneSD = oneSD * price[1];
def Zscorevalue = ((price - avgClose) / oneSD);
def avgZv = Average(Zscorevalue, 20);

#Compute and plot Z-Score
def Zscore = ((price - avgClose) / oneSD);
#Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
#Zscore.setLineWeight(2);
#Zscore.assignValueColor(if Zscore > 0 then color.green else color.red);
#plot avgZscore = average(Zscorevalue,ZavgLength);
#avgZscore.setPaintingStrategy(paintingStrategy.LINE);

#This is an optional plot that will display the momentum of the Z-Score average
#plot momZAvg = (avgZv-avgZv[5]);
#Plot zero line and extreme bands
#plot zero = 0;
#plot two = 2;
#plot negtwo = -2;
#zero.setDefaultColor(color.black);

########
#Accumulation/Distribution (4hr)
########

# TOS MTF AccumulationSwingIndex
#global variables
input agg  = AggregationPeriod.TEN_MIN ;
def cclose = close(period = agg);
def oopen  = open(period = agg);
def hhigh  = high(period = agg);
def llow   = low(period = agg);
# ########################################################
input smaLength = 10;
def limit = 30;
def AbsHighClose = AbsValue(hhigh - cclose[1]);
def AbsLowClose = AbsValue(llow - cclose[1]);
def AbsCloseOpen = AbsValue(cclose[1] - oopen[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
If(AbsHighClose >= (hhigh - llow), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen),
If(AbsLowClose >= (hhigh - llow),  AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
(50 * (((cclose - cclose[1]) + 0.50 * (cclose - oopen) + 0.25 * (cclose[1] - oopen[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
0 + if !IsNaN(nRes[1]) then nRes[1] else 0);
def ASI = nRes;
def sma = SimpleMovingAvg(ASI, smaLength);
# ########################################################
# charting and formatting
#declare lower;
def pASI = ASI;
#pASI.SetLineWeight(3);
#pASI.AssignValueColor(if asi>sma then Color.Green else Color.RED);
#plot pSMA = sma;
#pSMA.SetPaintingStrategy(PaintingStrategy.DASHES);
#pSMA.SetLineWeight(1);
#pSMA.AssignValueColor(Color.VIOLET);
DefineGlobalColor("bear",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("bull",  CreateColor(0, 165, 0)) ;

plot buy = if hma20 crosses above hma50 and Zscore crosses above 0 and ASI[1] < sma[1] and ASI > sma then hma20 else Double.NaN;

plot buy1 = if hma20 crosses above hma50 and Zscore > 0 and ASI > sma then hma20 else Double.NaN;

Last edited:

1. The first 'buy' uses the crosses of 3 indicators that you are combining. This limits the number of signals as it is difficult to have multiple indicators all cross (or change colors) at the same time.
2.The second 'buy1' uses the HMA cross and the other 2 indicators being true (green) when the HMA cross occurred. This will create more signals. You could create other scenarios by choosing different combinations of crosses and true indicators.

The white arrow(s) in the chart below are from the limited 'buy' plot. The cyan arrow(s) are from the 'buy1 plot. I Have changed most of your orignal plot/clouds to 'def' statements to allow the buy arrows to appear along with the HMA plots, as the HMA plots are an 'upper' indicator and the Zscore and PASI are lower indicators and do not plot well on the upper price pane.
Oh my goodness, thank you for this!!!

I've updated the code. At first glance it looks like it will be amazing, but now I am trying to build a scan to go with it. Fingers crossed
Ruby:
``````#Buy when HMA 20 crosses over 50, zscore (1hr) turns green, accumulation distribution index turns green
#########
#HMA Cloud
#########
plot hma20 = HullMovingAvg(close, 20);
plot hma50 = HullMovingAvg(close, 50);

#########
#Zscore (multiple time frame)
#########

#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the indicator, but please provide a link back to ThetaTrend.com

input agg1 = aggregationPeriod.HOUR;
input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD = StDev(price, length);
def avgClose = SimpleMovingAvg(price, length);
def ofoneSD = oneSD * price[1];
def Zscorevalue = ((price - avgClose) / oneSD);
def avgZv = Average(Zscorevalue, 20);

#Compute and plot Z-Score
def Zscore = ((price - avgClose) / oneSD);
#Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
#Zscore.setLineWeight(2);
#Zscore.assignValueColor(if Zscore > 0 then color.green else color.red);
#plot avgZscore = average(Zscorevalue,ZavgLength);
#avgZscore.setPaintingStrategy(paintingStrategy.LINE);

#This is an optional plot that will display the momentum of the Z-Score average
plot momZAvg = (avgZv-avgZv[5]);
#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
plot negtwo = -2;
zero.setDefaultColor(color.black);

########
#Accumulation/Distribution (4hr)
########

# TOS MTF AccumulationSwingIndex
#global variables
input agg = AggregationPeriod.FOUR_HOURS ;
def cclose = close(period = agg);
def oopen = open(period = agg);
def hhigh = high(period = agg);
def llow = low(period = agg);
# ########################################################
input smaLength = 10;
def limit = 30;
def AbsHighClose = AbsValue(hhigh - cclose[1]);
def AbsLowClose = AbsValue(llow - cclose[1]);
def AbsCloseOpen = AbsValue(cclose[1] - oopen[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
If(AbsHighClose >= (hhigh - llow), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen),
If(AbsLowClose >= (hhigh - llow), AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
(50 * (((cclose - cclose[1]) + 0.50 * (cclose - oopen) + 0.25 * (cclose[1] - oopen[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
0 + if !IsNaN(nRes[1]) then nRes[1] else 0);
def ASI = nRes;
def sma = SimpleMovingAvg(ASI, smaLength);
# ########################################################
# charting and formatting
#declare lower;
def pASI = ASI;
#pASI.SetLineWeight(3);
#pASI.AssignValueColor(if asi>sma then Color.Green else Color.RED);
#plot pSMA = sma;
#pSMA.SetPaintingStrategy(PaintingStrategy.DASHES);
#pSMA.SetLineWeight(1);
#pSMA.AssignValueColor(Color.VIOLET);
DefineGlobalColor("bear", CreateColor(225, 0, 0)) ;
DefineGlobalColor("bull", CreateColor(0, 165, 0)) ;

plot buy = if Zscore crosses above 0 and ASI[1] < sma[1] and ASI > sma then hma20 else Double.NaN;

plot sell = if Zscore crosses below 0 and ASI[1] > sma[1] and ASI < sma then hma20 else Double.NaN;
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetDefaultColor(Color.red);
sell.SetLineWeight(5);``````

Last edited by a moderator:

87k+ Posts
191 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?