Help wanted with custom buy/sell signals script

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);
 
Solution
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...
this is macd with lables see this is what your looking for. the for increesing histogram lines and above 0 use what you need
H = histogram
L = line
0 = abobe or below line

Code:
declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
addLabel(yes,"   H    ",if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
addLabel(yes,"  L     ", if Value > Avg then color.green else if Value < Avg then color.red else color.gray);
addLabel(yes,"  0     ", if avg > ZeroLine then color.green else if avg < ZeroLine then color.red else color.gray);
 
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);

if you are going to combine studies, start with 1 study and change some variable names. start with the common names, like length and averagetype. add letters to the variable that is after def to make it unique. then find and fix the other occurances of that former name that turned red.
then copy/paster the next study and repeat, changing some more variable names.
 
Solution
if you are going to combine studies, start with 1 study and change some variable names. start with the common names, like length and averagetype. add letters to the variable that is after def to make it unique. then find and fix the other occurances of that former name that turned red.
then copy/paster the next study and repeat, changing some more variable names.
Thanks, I would not have thought of that.
 

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