BranDonCMK
New member
Greetings all,
I've been learning to trade for a couple years now and started to write very basic codes for scanning and plotting signals. I have a very specific setup that I try to follow involving the TTM Squeeze and its momentum histogram, a few EMAs, Keltner Channels, and the MACD for bullish or bearish trades. I'm having trouble figuring out how to add the MACD histogram into the code and the specific condition I'm looking for would be 3 bars in a row of increasing or declining momentum on the histogram. I was able to define and plot the same condition for the squeeze histogram, but when I try to incorporate the MACD histogram, I get errors because the input "averageType" is in both MACD and Keltner codes. Here is my script below.... forgive me if it's not written very cleanly.
#Squeeze Signal Conditions
input displace = 0;
input factor = 1.0;
input length = 20;
input price = close;
input averageType = AverageType.EXPONENTIAL;
input trueRangeAverageType = AverageType.EXPONENTIAL;
def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);
def Avg = average[-displace];
def Upper_Band = average[-displace] + shift[-displace];
def Lower_Band = average[-displace] - shift[-displace];
def triggerZone = close >= Lower_Band and close <= Upper_Band;
def squeezeHistogram = TTM_Squeeze(close,20,1.5,2.0,1.0).Histogram;
def normalSqueeze = if TTM_Squeeze(close, 20, 1.5, 2.0, 1.0).SqueezeAlert == 0 then 1 else 0;
def aggroSqueeze = if TTM_Squeeze(close, 20, 2.0, 2.0, 1.0).SqueezeAlert == 0 then 1 else 0;
def slowSqueeze = if TTM_Squeeze(close, 20, 1.0, 2.0, 1.0).SqueezeAlert == 0 then 1 else 0;
def squeezeSignal = if !IsNan(close) then 0 else Double.nan;
def squeezeFinalSignal = normalSqueeze or slowsqueeze;
def squeezeAggroSignal = aggrosqueeze and !normalSqueeze;
def histogramUp = squeezeHistogram > squeezehistogram[1] and squeezeHistogram[1] > squeezeHistogram[2];
def histogramDown = squeezeHistogram < squeezeHistogram[1] and squeezeHistogram[1] < squeezeHistogram[2];
def EMA5 = movAvgExponential(close,5);
def EMA8 = movAvgExponential(close, 8);
def EMA13 = movAvgExponential(close, 13);
def EMA21 = movAvgExponential(close, 21);
def EMA34 = movAvgExponential(close, 34);
def SMA50 = simpleMovingAvg(close, 50);
def bullishStacked = EMA8 > EMA13 and close > EMA21;
def bearishStacked = EMA8 < EMA13 and close < EMA21;
plot buySignal = triggerZone and squeezeFinalSignal and histogramUp and bullishStacked;
plot sellSignal = triggerZone and squeezeFinalSignal and histogramDown and bearishStacked;
plot aggrobuySignal = triggerZone and squeezeaggroSignal and histogramUp and bullishStacked;
plot aggrosellSignal = triggerZone and squeezeaggroSignal and histogramDown and bearishStacked;
BuySignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
SellSignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Down);
aggrobuySignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
aggrosellSignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Down);
BuySignal.setLineWeight(2);
SellSignal.SetLineWeight(2);
aggroBuySignal.setLineWeight(2);
aggroSellSignal.SetLineWeight(2);
BuySignal.setDefaultColor(color.MAGENTA);
SellSignal.SetDefaultColor(color.MAGENTA);
aggroBuySignal.setDefaultColor(color.plum);
aggroSellSignal.SetDefaultColor(color.plum);
I've been learning to trade for a couple years now and started to write very basic codes for scanning and plotting signals. I have a very specific setup that I try to follow involving the TTM Squeeze and its momentum histogram, a few EMAs, Keltner Channels, and the MACD for bullish or bearish trades. I'm having trouble figuring out how to add the MACD histogram into the code and the specific condition I'm looking for would be 3 bars in a row of increasing or declining momentum on the histogram. I was able to define and plot the same condition for the squeeze histogram, but when I try to incorporate the MACD histogram, I get errors because the input "averageType" is in both MACD and Keltner codes. Here is my script below.... forgive me if it's not written very cleanly.
#Squeeze Signal Conditions
input displace = 0;
input factor = 1.0;
input length = 20;
input price = close;
input averageType = AverageType.EXPONENTIAL;
input trueRangeAverageType = AverageType.EXPONENTIAL;
def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);
def Avg = average[-displace];
def Upper_Band = average[-displace] + shift[-displace];
def Lower_Band = average[-displace] - shift[-displace];
def triggerZone = close >= Lower_Band and close <= Upper_Band;
def squeezeHistogram = TTM_Squeeze(close,20,1.5,2.0,1.0).Histogram;
def normalSqueeze = if TTM_Squeeze(close, 20, 1.5, 2.0, 1.0).SqueezeAlert == 0 then 1 else 0;
def aggroSqueeze = if TTM_Squeeze(close, 20, 2.0, 2.0, 1.0).SqueezeAlert == 0 then 1 else 0;
def slowSqueeze = if TTM_Squeeze(close, 20, 1.0, 2.0, 1.0).SqueezeAlert == 0 then 1 else 0;
def squeezeSignal = if !IsNan(close) then 0 else Double.nan;
def squeezeFinalSignal = normalSqueeze or slowsqueeze;
def squeezeAggroSignal = aggrosqueeze and !normalSqueeze;
def histogramUp = squeezeHistogram > squeezehistogram[1] and squeezeHistogram[1] > squeezeHistogram[2];
def histogramDown = squeezeHistogram < squeezeHistogram[1] and squeezeHistogram[1] < squeezeHistogram[2];
def EMA5 = movAvgExponential(close,5);
def EMA8 = movAvgExponential(close, 8);
def EMA13 = movAvgExponential(close, 13);
def EMA21 = movAvgExponential(close, 21);
def EMA34 = movAvgExponential(close, 34);
def SMA50 = simpleMovingAvg(close, 50);
def bullishStacked = EMA8 > EMA13 and close > EMA21;
def bearishStacked = EMA8 < EMA13 and close < EMA21;
plot buySignal = triggerZone and squeezeFinalSignal and histogramUp and bullishStacked;
plot sellSignal = triggerZone and squeezeFinalSignal and histogramDown and bearishStacked;
plot aggrobuySignal = triggerZone and squeezeaggroSignal and histogramUp and bullishStacked;
plot aggrosellSignal = triggerZone and squeezeaggroSignal and histogramDown and bearishStacked;
BuySignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
SellSignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Down);
aggrobuySignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
aggrosellSignal.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Down);
BuySignal.setLineWeight(2);
SellSignal.SetLineWeight(2);
aggroBuySignal.setLineWeight(2);
aggroSellSignal.SetLineWeight(2);
BuySignal.setDefaultColor(color.MAGENTA);
SellSignal.SetDefaultColor(color.MAGENTA);
aggroBuySignal.setDefaultColor(color.plum);
aggroSellSignal.SetDefaultColor(color.plum);