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);
AddCloud(hma20, hma50, Color.GREEN, Color.RED);

#########
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)) ;
AddCloud(pASI, pSMA, GlobalColor("bull"), GlobalColor("bear"));
 
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);
AddCloud(hma20, hma50, Color.GREEN, Color.RED);

#########
#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);
AddCloud(hma20, hma50, Color.GREEN, Color.RED);

#########
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)) ;
AddCloud(pASI, pSMA, GlobalColor("bull"), GlobalColor("bear"));
This may help you get started.

There are two buy signals.
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.

Screenshot-2021-08-10-112434.jpg
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);
AddCloud(hma20, hma50, Color.GREEN, Color.RED);

#########
#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)) ;
#AddCloud(pASI, pSMA, GlobalColor("bull"), GlobalColor("bear"));

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;
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetDefaultColor(Color.WHITE);
buy.SetLineWeight(5);

plot buy1 = if hma20 crosses above hma50 and Zscore > 0 and ASI > sma then hma20 else Double.NaN;
buy1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy1.SetDefaultColor(Color.CYAN);
buy1.SetLineWeight(5);
 
Last edited:
This may help you get started.

There are two buy signals.
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);
AddCloud(hma20, hma50, Color.GREEN, Color.RED);

#########
#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)) ;
#AddCloud(pASI, pSMA, GlobalColor("bull"), GlobalColor("bear"));

plot buy = if Zscore crosses above 0 and ASI[1] < sma[1] and ASI > sma then hma20 else Double.NaN;
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetDefaultColor(Color.Blue);
buy.SetLineWeight(5);

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:
Solution

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