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