Order Block Band[Sam4Cok] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
OanUERz.png

Using the multiple band options (BB/KC/DC) to mark areas of Support and Resistance

The scrip finds the highest and lowest levels of the bands to mark up futures areas of interest.
If the High/Lows are being broken on the selected band , or if the look back range has expired without finding new levels, the script will stop plotting them until new levels are found.
Moreover, you can select to plot the OB as lines or band with option to show minimum OB line length to plot.
* I added multi timeframe if you want to show the OB on the different aggregation. Enjoy :)

CODE:

CSS:
#https://www.tradingview.com/script/387pRGG2-BB-Order-Blocks/
# Created by Sam4Cok@Samer800 - 10/2022 - inspired by MensaTrader code "BB Order Blocks"

#//Inputs
input useChartTime = yes;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;
#//OB Setting
input MinOrderBlockLength = 3;
input lookbackLength = 100;    # "Lookback Length"
input ShowObThresholdLine = yes;         # "Plot OB Levels"
input OrderBlockStyle = {Default Line, Band};   # "Plot OB Style"
#//Band Setting
input bandType = {default BB, KC, DC};
input BandMovAvg = {default SMA, EMA, HMA, RMA, WMA, VWMA, SWMA, Linreg, Median};
input threshold = 0.1;
input BandLength = 20;
input BandMultiplier = 1.7;
input KCTrueRange = yes;      # "Use True Range (KC)"
input DCAlternateSource = no; # "Use Alternate Source (DC)"
input ShowBands = no;         # "Plot Bands"

def na = Double.NaN;
def plotCon = if OrderBlockStyle ==OrderBlockStyle.Line then 1 else 0;

def h;
def l;
def c;
#def BandSource;
if useChartTime {
    h = high;
    l = low;
    c = close;
#    BandSource = hlc3;
    } else {
    h = high(period=Aggregation);
    l = low(period=Aggregation);
    c = close(period=Aggregation);
#    BandSource = hlc3(period=Aggregation);
}

#swma(source)
script swma {
    input x = close;
    def swma = x[3] * 1 / 6 + x[2] * 2 / 6 + x[1] * 2 / 6 + x[0] * 1 / 6;
    plot return = swma;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
    plot result = VWMA;
}
#ma(type, source, length) =>
script ma {
    input type   = "SMA";
    input source = close;
    input length = 100;
    def ma;
    ma = if type == "SMA" then SimpleMovingAvg(source, length) else
     if type == "EMA" then ExpAverage(source, length) else
     if type == "HMA" then HullMovingAvg(source, length) else
     if type == "RMA" then WildersAverage(TrueRange(high, close, low), length) else
     if type == "WMA" then WMA(source, length) else
     if type == "VWMA" then VWMA(source, length) else
     if type == "SWMA" then SWMA(source) else
     if type == "Linreg" then Inertia(source, length) else
     if type == "Median" then Median(source, length) else Double.NaN;
    plot result = ma;
}

#### BB
def sDev = stdev(c,BandLength);
def BBDev = (BandMultiplier + threshold);
def BBMid = ma(BandMovAvg, c, BandLength);
def BBup = BBMid + BandMultiplier * sDev;
def BBlo = BBMid - BandMultiplier * sDev;
def BBup2  = BBMid + BBDev * sDev;
def BBlo2  = BBMid - BBDev * sDev;
def BBupper  = BBup;
def BBlower  = BBlo;
def BBupper2 = BBup2;
def BBlower2 = BBlo2;
def BBmiddle = BBMid;

##### KC
def KCDev = BandMultiplier + threshold;
def tr    = TrueRange(h,c,l);
def span  = ma(BandMovAvg, if KCTrueRange then tr else (h - l), BandLength);
def KCavg = ma(BandMovAvg, c, BandLength);
def KCUp  = KCavg + BandMultiplier * span;
def KClo  = KCavg - BandMultiplier * span;
def KCUp2 = KCavg + KCDev * span;
def KClo2 = KCavg - KCDev * span;
def KCupper  = KCUp;
def KClower  = KClo;
def KCupper2 = KCUp2;
def KClower2 = KClo2;
def KCmiddle = KCavg;

##### DC
def highSource = if DCAlternateSource then c else h;
def lowSource  = if DCAlternateSource then c else l;
def DChighDev = highSource + highSource * threshold/100;
def DClowDev  = lowSource - lowSource * threshold/100;
def DCupp  = Highest(highSource, BandLength);
def DClow  = Lowest(lowSource, BandLength);
def DCupp2 = Highest(DChighDev, BandLength);
def DClow2 = Lowest(DClowDev, BandLength);
def DCmid = (DCupp + DClow) / 2;
def DCupper  = DCupp;
def DClower  = DClow;
def DCupper2  = DCupp2;
def DClower2  = DClow2;
def DCmiddle = DCmid;

### Bands
def upper;
def lower;
def upper2;
def lower2;
def middle;
if bandType == bandType.BB
then {
    upper  = BBupper;
    lower  = BBlower;
    upper2 = BBupper2;
    lower2 = BBlower2;
    middle = BBmiddle;
} else
if bandType == bandType.KC
then {
    upper  = KCupper;
    lower  = KClower;
    upper2 = KCupper2;
    lower2 = KClower2;
    middle = KCmiddle;
} else
if bandType == bandType.DC
then {
    upper  = DCupper;
    lower  = DClower;
    upper2 = DCupper2;
    lower2 = DClower2;
    middle = DCmiddle;
} else
{
    upper  = na;
    lower  = na;
    upper2 = na;
    lower2 = na;
    middle = na;
}

#//Order Blocks BB
def obHigh  = Highest(upper, lookbackLength);
def obLow   = Lowest(lower, lookbackLength);

def obHigh2 = Highest(upper2, lookbackLength);
def obLow2  = Lowest(lower2, lookbackLength);

#//Plots
#//Upper Order Block
def Upcount = if(obHigh == obHigh[1], Upcount[1]+1,0);

def o1 = if plotCon then
         if Upcount>=MinOrderBlockLength then obHigh else na else obHigh; #"Order Block High"

def o2 = if ShowObThresholdLine then if plotCon then
         if Upcount>=MinOrderBlockLength then obHigh2 else na else obHigh2 else na;# "Order Block High2"

plot o11 = if ShowObThresholdLine then na else if plotCon then
           if Upcount>=MinOrderBlockLength then obHigh else na else obHigh;
o11.SetDefaultColor(Color.UPTICK);

AddCloud(o1, o2, Color.DARK_RED, Color.DARK_RED, yes); # "Sell Fill"

#//Lower Order Block
def Locount = if(obLow == obLow[1], Locount[1]+1,0);
def o3 = if plotCon then
         if Locount>=MinOrderBlockLength then obLow else na else obLow;

def o4 = if ShowObThresholdLine then if plotCon then
         if Locount>=MinOrderBlockLength then obLow2 else na else obLow2 else na;

plot o33 = if ShowObThresholdLine then na else if plotCon then
           if Locount>=MinOrderBlockLength then obLow else na else obLow;
o33.SetDefaultColor(Color.DOWNTICK);

AddCloud(o3, o4, Color.DARK_GREEN, Color.DARK_GREEN, yes); # "Buy Fill"

#//Plot bands
plot MidBand = if(ShowBands, middle,na);
MidBand.SetDefaultColor(Color.GRAY);

plot UpBand  = if(ShowBands, upper,na);
UpBand.SetDefaultColor(CreateColor(41,98,255));

plot LoBand  = if(ShowBands, lower,na);
LoBand.SetDefaultColor(CreateColor(41,98,255));

AddCloud(UpBand, LoBand, Color.DARK_GRAY);


# --- END Code
 
Last edited by a moderator:

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