Potential Breakout (PBO) Indicator for ThinkorSwim

gravityflyer

New member
VIP
Hi all,

I just wanted to share my first thinkScript indicator and kindly seeking your assistance in refining the code.

Code:
# Potential Breakout (PBO) indicator
# gravityflyer
# v1.0 - 2021.04.05

declare lower;
declare real_size;

#Input Price Line
input priceType = PriceType.LAST;
plot LastPrice = close(priceType = priceType);
LastPrice.SetDefaultColor(GetColor(7));

#Input Bolinger Band Width
input averageType = AverageType.Simple;
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -1.0;
input Num_Dev_Up = 1.0;
input BulgeLength = 150;
input SqueezeLength = 150;

def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).UpperBand;
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).LowerBand;
def midLine = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).MidLine;

plot Bandwidth = (upperBand - lowerBand) / midLine * 100;
Bandwidth.SetDefaultColor(Color.WHITE);

#Painting Strategy
LastPrice.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
LastPrice.DefineColor("Normal", Color.GRAY);
LastPrice.DefineColor("BuySignal", Color.GREEN);
LastPrice.DefineColor("SellSignal", Color.RED);
LastPrice.AssignValueColor(if LastPrice > upperBand then LastPrice.Color("BuySignal") else if LastPrice < lowerBand then LastPrice.Color("SellSignal") else LastPrice.Color("Normal"));

#Input Reference Line
plot ZeroLine = 0;
ZeroLine.SetDefaultColor(GetColor(8));
ZeroLine.setStyle(Curve.SHORT_DASH);

In short, the indicator works as I intended. Yay for me! :giggle: That said, the presentation needs more refinement. Some issues I'm having:
  • The scale is obviously not consistent with the price/BB superimposed. I thought "declare real_size" would fix the issue?
  • How do I fill the upper and lower bands with a color, ideally with a lower opacity? Would that be with the "Add.Cloud" code? As indicated by the code, buy/sell signals would occur above/below such values.
  • In terms of painting strategy, you'll notice it's painting the LastPrice and thus the signal is slightly delayed (see pic below). Is there a way to repaint on the current price or should I not worry about it?
xKuJkIc.png

  • Finally, how can I add buy/sell signals at the bottom of the indicator below the 0 reference line?
Thank you in advance for your assistance!
 

gravityflyer

New member
VIP
interesting. this actually looks pretty good on the 2min chart

Glad you're enjoying it! Here's v2 of my script if interested...

Code:
# Potential Breakout (PBO) indicator
# gravityflyer
# v1.0 - 2021.04.05
# v2.0 - 2021.04.07: Converted indicator into single line.

declare lower;

#Input Bolinger Band Width
input averageType = AverageType.Simple;
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -1.0;
input Num_Dev_Up = 1.0;
input BulgeLength = 150;
input SqueezeLength = 150;


def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).UpperBand;
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).LowerBand;
def midLine = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).MidLine;

#plot Bandwidth = (upperBand - lowerBand) / midLine * 100;
#Bandwidth.SetDefaultColor(Color.WHITE);

#Input Price Line
#input priceType = PriceType.LAST;
#plot LastPrice = close(priceType = priceType);
#LastPrice.SetDefaultColor(GetColor(7));

#PBO Painting Strategy
plot PBO = If(IsNaN(close), Double.NaN, 0);
PBO.DefineColor("Normal", Color.GRAY);
PBO.DefineColor("BuySignal", Color.GREEN);
PBO.DefineColor("SellSignal", Color.RED);
PBO.AssignValueColor(if price > upperBand then PBO.Color("BuySignal") else if price < lowerBand then PBO.Color("SellSignal") else PBO.Color("Normal"));
PBO.SetPaintingStrategy(PaintingStrategy.POINTS);
PBO.SetLineWeight(3);

#Arrows#
plot BUY = 0;
plot TakeProfit = 0;
plot UpSignal = if price crosses above upperBand then BUY else Double.NaN;
plot DownSignal = if price crosses below lowerBand then TakeProfit else Double.NaN;

input showBreakoutSignals = yes;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

UpSignal.SetDefaultColor(Color.GREEN);
DownSignal.SetDefaultColor(Color.RED);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 

Similar threads

Top