Support and Resistance (High Volume Boxes) [ChartPrime] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
pbXIHzS.png


Author Message:
The "Support and Resistance" indicator identifies key support and resistance levels using pivot points and volume analysis. It visually represents these levels with dynamically colored boxes, indicating the strength of the volume. This helps traders recognize potential price reversals and key zones for buy and sell opportunities.

CODE:

CSS:
#// Indicator for TOS
#// © ChartPrime
#indicator("Support and Resistance (High Volume Boxes) [ChartPrime]", shorttitle = "SR Breaks and Retests [ChartPrime]"
# Converted by Sam4Cok@Samer800    - 09/2024

input timframe = AggregationPeriod.MIN;
input showBreakoutLabels = yes;
input showBreakoutVolLabel = yes;
input showRetests = yes;
input SwingType = {default "Bullish AND Bearish", "Bearish", "Bullish"}; #  'Display'
input source = FundamentalType.CLOSE;
input maxBoxLength   = 500;
input lookbackPeriod = 20; #, "Lookback Period"
input DeltaVolumeFilterLength = 2; #Hint DeltaVolumeFilterLength: Higher input, will filter low volume boxes.
input AdjustBoxWidth = 1.0; #, "Adjust Box Width"


def na = Double.NaN;
def last = IsNaN(close);
def current = GetAggregationPeriod();
def tf = Max(current, timframe);
def src = Fundamental(FundamentalType = source, Period = tf);
def bANDb = SwingType == SwingType."Bullish AND Bearish";
def bear = SwingType == SwingType."Bearish" or bANDb;
def bull = SwingType == SwingType."Bullish" or bANDb;

#// 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
Script Pivots {
    input series    = close;
    input leftBars  = 20;
    input rightBars = 20;
    input isHigh = yes;
    def na = Double.NaN;
    def bn = BarNumber();
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] then series[pivotRange] else na;
    def pvtCond = !isNaN(series) and leftBars > 0 and rightBars > 0 and !isNaN(leftEdgeValue) and bn>0;
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
if isHigh {
    PivotPoint = if HH and barIndexH then series else na;
    } else {
    PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

def pivotHigh = Pivots(src, lookbackPeriod, lookbackPeriod, yes);
def pivotLow  = Pivots(src, lookbackPeriod, lookbackPeriod, no);
#// Volume Function
def sumVolUp = sum(if close(Period = tf) > open(Period = tf) then volume(Period = tf) else 0, DeltaVolumeFilterLength);
def sumVolDn = sum(if close(Period = tf) < open(Period = tf) then -volume(Period = tf) else 0, DeltaVolumeFilterLength);
def VolSrc =  sumVolUp + sumVolDn;
def Vol    = VolSrc[1];
def vol_hi = highest(Vol / 2.5, DeltaVolumeFilterLength);
def vol_lo = lowest(Vol / 2.5,  DeltaVolumeFilterLength);

def vol2 = Round(Vol/1000, 2);
#// Function to identify support and resistance boxes
#// Box width
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def atr   = WildersAverage(tr, 200);
def withd = atr * AdjustBoxWidth;
#// Find pivot points

def supLvlTop;
def supLvlBot;
def supLvlTop1;
def supLvlBot1;
def supCnt;
def bullState = {Default "ini", "1"};
def bullCond = !isNaN(pivotLow) and vol > vol_hi and bull;

#// Find support levels with Positive Volume
Switch (bullState[1]) {
Case "1" :
    bullState = bullState[1];
if bullCond {
    supCnt    = 0;
    supLvlTop1 = supLvlTop[1];
    supLvlBot1 = supLvlBot[1];
    supLvlTop = pivotLow;
    supLvlBot = pivotLow - withd;
    } else {
    supCnt    = supCnt[1] + 1;
    supLvlTop = if supCnt >= maxBoxLength then na else supLvlTop[1];
    supLvlBot = if supCnt >= maxBoxLength then na else supLvlBot[1];
    supLvlTop1 = if supCnt >= lookbackPeriod then na else supLvlTop1[1];
    supLvlBot1 = if supCnt >= lookbackPeriod then na else supLvlBot1[1];
    }
Default :
    bullState = bullState."1";
    supCnt    = 0;
    supLvlTop = na;
    supLvlBot = na;
    supLvlTop1 = na;
    supLvlBot1 = na;
#    bullLab    = no;
}
#// Find resistance levels with Negative Volume
def resLvlTop;
def resLvlBot;
def resLvlTop1;
def resLvlBot1;
def resCnt;
def bearState = {Default "ini", "1"};
def bearCond = !isNaN(pivotHigh) and vol < vol_lo and bear;

Switch (bearState[1]) {
Case "1" :
    bearState = bearState[1];
if bearCond {
    resCnt    = 0;
    resLvlTop1 = resLvlTop[1];
    resLvlBot1 = resLvlBot[1];
    resLvlTop = pivotHigh + withd;
    resLvlBot = pivotHigh;
    } else {
    resCnt    = resCnt[1] + 1;
    resLvlTop = if resCnt >= maxBoxLength then na else resLvlTop[1];
    resLvlBot = if resCnt >= maxBoxLength then na else resLvlBot[1];
    resLvlTop1 = if resCnt >= lookbackPeriod then na else resLvlTop1[1];
    resLvlBot1 = if resCnt >= lookbackPeriod then na else resLvlBot1[1];
    }

Default :
    bearState = bearState."1";
    resCnt    = 0;
    resLvlTop = na;
    resLvlBot = na;
    resLvlTop1 = na;
    resLvlBot1 = na;
}
#// Break of support or resistance conditions
def supCond = supLvlTop and supCnt > 2;
def resCond = resLvlBot and resCnt > 2;
def supCond1 = supLvlTop1 and supCnt > 0;
def resCond1 = resLvlBot1 and resCnt > 0;

def brekoutRes = resCond and (low  > resLvlTop) and (low[1]  <= resLvlTop[1]);
def resHolds   = resCond and (high < resLvlBot) and (high[1] >= resLvlBot[1]);
def brekoutSup = supCond and (high < supLvlBot) and (high[1] >= supLvlBot[1]);
def supHolds   = supCond and (low  > supLvlTop) and (low[1]  <= supLvlTop[1]);
def brekout_res = if isNaN(brekoutRes) then 0 else brekoutRes;
def res_holds   = if isNaN(resHolds)   then 0 else resHolds;
def sup_holds   = if isNaN(supHolds)   then 0 else supHolds;
def brekout_sup = if isNaN(brekoutSup) then 0 else brekoutSup;
def brekoutRes1 = resCond1 and (low  > resLvlTop1) and (low[1]  <= resLvlTop1[1]) and !brekout_res;
def resHolds1   = resCond1 and (high < resLvlBot1) and (high[1] >= resLvlBot1[1]) and !res_holds;
def supHolds1   = supCond1 and (low  > supLvlTop1) and (low[1]  <= supLvlTop1[1]) and !sup_holds;
def brekoutSup1 = supCond1 and (high < supLvlBot1) and (high[1] >= supLvlBot1[1]) and !brekout_sup;
def brekout_res1 = if isNaN(brekoutRes1) then 0 else brekoutRes1;
def res_holds1   = if isNaN(resHolds1)   then 0 else resHolds1;
def sup_holds1   = if isNaN(supHolds1)   then 0 else supHolds1;
def brekout_sup1 = if isNaN(brekoutSup1) then 0 else brekoutSup1;
#/ Check if Resistance become Support or Support Become Resistance
def res_is_sup = if brekout_res then yes else if res_holds then no else res_is_sup[1];
def sup_is_res = if brekout_sup then yes else if sup_holds then no else sup_is_res[1];
def res_is_sup1 = if brekout_res1 then yes else if res_holds1 then no else res_is_sup1[1];
def sup_is_res1 = if brekout_sup1 then yes else if sup_holds1 then no else sup_is_res1[1];

#// Plot Res and Sup breakouts and holds
def resAsSupHold = brekout_res and res_is_sup[1];
def supAsResHold = brekout_sup and sup_is_res[1];
def resAsSupHold1 = brekout_res1 and res_is_sup1[1];
def supAsResHold1 = brekout_sup1 and sup_is_res1[1];
def avgRes = if resLvlTop and resLvlBot then (resLvlTop + resLvlBot)/2 else avgRes[1];
def avgSup = if supLvlBot and supLvlTop then (supLvlBot + supLvlTop)/2 else avgSup[1];
def avgRes1 = if resLvlTop1 and resLvlBot1 then (resLvlTop1 + resLvlBot1)/2 else avgRes1[1];
def avgSup1 = if supLvlBot1 and supLvlTop1 then (supLvlBot1 + supLvlTop1)/2 else avgSup1[1];

plot ResHold   = if res_holds[-1]     then avgRes else na;      # "Resistance Holds"
plot SupHold   = if sup_holds[-1]     then avgSup else na;      # "Support Holds"
plot ResAsSup  = if resAsSupHold[-1]  then low else na;         # "Resistance as Support Holds"
plot SupAsRes  = if supAsResHold[-1]  then high else na;        # "Support as Resistance Holds"
plot ResHold1  = if res_holds1[-1]    then avgRes1 else na;     # "Resistance Holds"
plot SupHold1  = if sup_holds1[-1]    then avgSup1 else na;     # "Support Holds"
plot ResAsSup1 = if resAsSupHold1[-1] then low else na;         # "Resistance as Support Holds"
plot SupAsRes1 = if supAsResHold1[-1] then high else na;        # "Support as Resistance Holds"
ResHold.SetPaintingStrategy(PaintingStrategy.SQUARES);
SupHold.SetPaintingStrategy(PaintingStrategy.SQUARES);
ResAsSup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
SupAsRes.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
ResHold.SetDefaultColor(Color.DOWNTICK);
SupHold.SetDefaultColor(Color.LIGHT_GREEN);
ResAsSup.SetDefaultColor(Color.GREEN);
SupAsRes.SetDefaultColor(Color.RED);
ResHold1.SetPaintingStrategy(PaintingStrategy.SQUARES);
SupHold1.SetPaintingStrategy(PaintingStrategy.SQUARES);
ResAsSup1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
SupAsRes1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
ResHold1.SetDefaultColor(Color.DOWNTICK);
SupHold1.SetDefaultColor(Color.LIGHT_GREEN);
ResAsSup1.SetDefaultColor(Color.GREEN);
SupAsRes1.SetDefaultColor(Color.RED);

#--- plots
def supCol = if isNaN(supCol[1]) then 0 else
             if supLvlTop!=supLvlTop[1] then 0 else if brekout_sup then 1 else
             if supHolds then 0 else supCol[1];
def supCol1 = if isNaN(GetValue(supCol, supCnt+1)) then 0 else GetValue(supCol, supCnt+1);

def bullChange = supCnt == 1;
plot supVol = if showBreakoutVolLabel and bullChange[-1] then vol2 else na;
plot supTop = if !last and supLvlTop then supLvlTop else na;
plot supBot = if !last and supLvlBot then supLvlBot else na;
plot supTop1 = if !last and supLvlTop1 then supLvlTop1 else na;
plot supBot1 = if !last and supLvlBot1 then supLvlBot1 else na;

supVol.SetDefaultColor(Color.LIGHT_GREEN);
supTop.AssignValueColor(if !supCol then Color.DARK_GREEN else Color.DARK_RED);
supBot.AssignValueColor(if !supCol then Color.DARK_GREEN else Color.DARK_RED);
supTop1.AssignValueColor(if !supCol1 then Color.DARK_GREEN else Color.DARK_RED);
supBot1.AssignValueColor(if !supCol1 then Color.DARK_GREEN else Color.DARK_RED);
supVol.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
supTop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
supBot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
supTop1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
supBot1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


def resCol = if isNaN(resCol[1]) then 0 else
             if resLvlBot!=resLvlBot[1] then 0 else if brekout_res then 1 else
             if res_holds then 0 else resCol[1];
def resCol1 = if isNaN(GetValue(resCol, resCnt+1)) then 0 else GetValue(resCol, resCnt+1);

def bearChange = resCnt == 1;
plot resVol = if showBreakoutVolLabel and bearChange[-1] then vol2 else na;
plot resTop = if !last and resLvlTop then resLvlTop else na;
plot resBot = if !last and resLvlBot then resLvlBot else na;
plot resTop1 = if !last and resLvlTop1 then resLvlTop1 else na;
plot resBot1 = if !last and resLvlBot1 then resLvlBot1 else na;
resVol.SetDefaultColor(Color.LIGHT_RED);
resTop.AssignValueColor(if !resCol then Color.DARK_RED else Color.DARK_GREEN);
resBot.AssignValueColor(if !resCol then Color.DARK_RED else Color.DARK_GREEN);
resTop1.AssignValueColor(if !resCol1 then Color.DARK_RED else Color.DARK_GREEN);
resBot1.AssignValueColor(if !resCol1 then Color.DARK_RED else Color.DARK_GREEN);
resVol.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
resTop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
resBot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
resTop1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
resBot1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#-- Cloud
def sCol  = supCol;
def sCol1 = supCol1[-1];
def rCol  = resCol;
def rCol1 = resCol1[-1];

AddCloud(if (!sCol1) then supTop1[-1] else na, supBot1[-1], Color.DARK_GREEN);
AddCloud(if sCol1  then supTop1[-1] else na, supBot1[-1], Color.DARK_RED);
AddCloud(if (!sCol) and supTop==supTop[1] then supTop else na, supBot, Color.DARK_GREEN);
AddCloud(if (sCol)  and supTop==supTop[1] then supTop else na, supBot, Color.DARK_RED);

AddCloud(if !rCol1 then resTop1[-1] else na, resBot1[-1], Color.DARK_RED);
AddCloud(if rCol1 then resTop1[-1] else na, resBot1[-1], Color.DARK_GREEN);
AddCloud(if !rCol and resTop==resTop[1] then resTop else na, resBot, Color.DARK_RED);
AddCloud(if rCol and resTop==resTop[1] then resTop else na, resBot, Color.DARK_GREEN);


#-- Signals
#/ Break Out Labels
def BreakSup = brekout_sup and !sup_is_res[1];
def BreakRes = brekout_res and !res_is_sup[1];
def BreakSup1 = brekout_sup1 and !sup_is_res1[1];
def BreakRes1 = brekout_res1 and !res_is_sup1[1];

AddChartBubble(showBreakoutLabels and BreakSup, supLvlTop[1], "B", Color.RED);
AddChartBubble(showBreakoutLabels and BreakRes, resLvlBot[1], "B", Color.GREEN, no);

AddChartBubble(showBreakoutLabels and BreakSup1, supLvlTop1[1], "B", Color.RED);
AddChartBubble(showBreakoutLabels and BreakRes1, resLvlBot1[1], "B", Color.GREEN, no);

# retest Labels

def cntRetSup = if supCnt > 0 then
                if (sup_holds or sup_holds1) then cntRetSup[1] + 1 else cntRetSup[1] else 0;
def cntRetRes = if resCnt > 0 then
                if (res_holds or res_holds1) then cntRetRes[1] + 1 else cntRetRes[1] else 0;

AddLabel(showBreakoutLabels and supTop, "Sup Retest(" + cntRetSup + ")", Color.GREEN);
AddLabel(showBreakoutLabels and resTop, "Res Retest(" + cntRetRes + ")", Color.RED);

#-- end of code
 

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