Conditions not working on Bollinger band alert study

Aragorn

New member
Plus
Hello all, this is my 2nd post in the forums. I've created a study to alert me whenever price is outside the 3rd standard deviation bollinger band. It adds a chart bubble above the bar with an alert. This part of the study works perfectly - but I can't seem to get conditional plotting and hiding to work! Code below...

I am testing it on /NQ, which hit the 3rd std deviation band on 7/13

2 specific condition problems:

1) If the chart is in a squeeze (TTM_Squeeze study), then I do NOT want an alert and bubble to go off. This does not work. Either the alert shows up regardless of squeeze status or it never shows up.

Relevant variables in my code: input alertInSqueeze, conditiontest, showbubble (commented out at bottom of code)

2) I want to plot a dot at the price level of the upper/lower bollinger band for every bar that the alert is valid. I do not want the rest of the bands plotted to avoid chart clutter. The goal is to compare current bar price against the current boundary of the band.

This is specifically for lower time frames (e.g. daily band vs 2 hr chart)

I've tried various ways of coding things but I am obviously not good enough to figure it out...

Code:
declare upper;

input timeFrame = aggregationPeriod.DAY;
input label = "Daily";
input price = close;
input displace = 0;
input length = 20;
input devDn = -3.0;
input devUp = 3.0;
input avg = AverageType.EXPONENTIAL;
input alertInSqueeze = yes;   #if yes, show alert bubble regardless of squeeze, if no DON'T show alert on bars in active squeeze


def alert = ((price >= BollingerBands(close("period" = timeFrame), displace,length,devDn,devUp,avg).UpperBand OR price <= BollingerBands(price, displace,length,devDn,devUp,avg).LowerBand));

def conditiontest = if (TTM_Squeeze(close,20,1.5,2.0,1.0) == yes AND alertInSqueeze == yes AND alert==yes) OR (TTM_Squeeze(close,20,1.5,2.0,1.0) == no AND alert==yes) then 1 else 0;

###### conditiontest was to be used with the bottom line of code, but it did not work. Left in for troubleshooting

AddChartBubble(alert AND alert[1]==no, high, label +" 3 SD!",color.RED, yes);

###### This is the problem area for my plot
plot band = if alert==yes then BollingerBands(close("period" = timeFrame), displace,length,devDn,devUp,avg).UpperBand else Double.NaN;
band.setpaintingStrategy(paintingStrategy.POINTS);
band.setLineWeight(5);
band.setdefaultColor(color.WHITE);
band.setHiding(alert==no);


####### ways I tried to get conditional plotting to work are here in comments
#def showbubble = if (alert == yes AND conditiontest==1) then 1 else 0;

#AddChartBubble(conditiontest==1, high, label +" 3 SD!",color.RED, yes);
 
Last edited:
Clarification on problem #2 - currently the study seems to plot a dot correctly on the daily chart, but not on smaller time frames (which is a big part of what I wanted). This is strange because the alert bubble will still show up on smaller time frames as intended.
 

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