Support and Resistance Fractal Pivot Points For ThinkOrSwim

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Support and Resistance Fractal Pivots For ThinkOrSwim
This was a member request; found on JQ's OneNote
Fractal Pivots: use multiple instances of the study set with different lengths to find fractal pivots inside wider fractal ranges.
FDTlCLo.png

Ruby:
### OneNote Archive Name: Support Resistance Fractal Pivots _MobiusNube
## Archive Section: Support Resistance
## Suggested Tos Name: SupportResistanceFractalPivots_MobiusNube
## Archive Date: 6.18.2018
 
# Support and Resistance Fractal Pivots
# Mobius
# V01.01.2011
#hint: Fractal Pivots: use multiple instances of the study set with different lengths to find fractal pivots inside wider fractal ranges.
 
# Original S3 and R3 removed, scan plots and labels added - Nube
# Scan for [pattern] 'is equal to' 'R1'.
# 11.19.17 added Quasimodo pattern labels after seeing Q about it in the Lounge. Requisite comment about having a *hunch* it's not better than any other pattern - Nube
# 12.12.17 added broken uptrend/downtrend and turned off tightening label when price is above/below R2/S2
 
# User Inputs
input n = 5; #hint n: Length for calculations.
input labels = yes; #hint labels: pattern labels
input Bubbles = no; #hint Bubbles: at most recent S and R pivot
 
def h   = high;
def l   = low;
def c   = close;
def na  = Double.NaN;
def bar = BarNumber();
 
# Internal Script Reference
script LinePlot {
    input LineLimit = 0;
    input OnExpansion = yes;
    input data = close;
    input bar = 0;
    def ThisBar = HighestAll(bar);
    def cLine = if bar == ThisBar
                then data
                else Double.NaN;
    plot P = if ThisBar - LineLimit <= bar
             then HighestAll(cLine)
             else Double.NaN;
}
# Variables
 
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
def PH = if (bar > n and
             h == Highest(h, n) and
             hh)
          then h
           else na;
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PL = if (bar > n and
             l == Lowest(l, n) and
             ll)
          then l
           else na;
def PHBar = if     !IsNaN(PH)
             then  bar
              else PHBar[1];
def PLBar = if     !IsNaN(PL)
             then  bar
              else PLBar[1];
def PHL = if     !IsNaN(PH)
           then  PH
            else PHL[1];
def priorPHBar = if     PHL != PHL[1]
                  then  PHBar[1]
                   else priorPHBar[1];
def PLL = if     !IsNaN(PL)
           then  PL
            else PLL[1];
def priorPLBar = if     PLL != PLL[1]
                  then  PLBar[1]
                   else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots  = bar >= HighestAll(priorPLBar);
def R1pivot = if     PHL > 0 and
                     HighPivots
               then  PHL
                else na;
def S1pivot = if     LowPivots
               then  PLL
                else na;
def R1value = R1pivot;
def R1bar   = PHbar;
def R1limit = bar - R1bar;
def R2bar   = priorPHbar;
def R2limit = bar - R2bar;
def R2value = if     bar == HighestAll(R2bar)
               then  h
                else R2value[1];
def S1value = S1pivot;
def S1bar   = PLbar;
def S1limit = bar - S1bar;
def S2bAR   = PriorPLBar;
def S2limit = bar - S2bar;
def S2value = if     bar == HighestAll(S2bar)
               then  l
                else S2value[1];
# Plots
plot R1 = LinePlot(Linelimit = R1Limit, data = R1value, bar = R1bar);
plot R2 = LinePlot(LineLimit = R2limit, data = R2value, bar = R2bar).P;
plot S1 = LinePlot(LineLimit = S1limit, data = S1value, bar = S1bar).P;
plot S2 = LinePlot(LineLimit = S2limit, data = S2value, bar = S2bar).P;
 
def minR12 = Min(R1, R2);
def maxR12 = Max(R1, R2);
def minS12 = Min(S1, S2);
def maxS12 = Max(S1, S2);
 
    R1.HideBubble();
    R1.HideTitle();
    R1.SetStyle(Curve.Long_Dash);
    R1.AssignValueColor(if     R1 == minR12
                         then  CreateColor(105,55,55)
                          else CreateColor(140,55,55));
    R2.HideBubble();
    R2.HideTitle();
    R2.SetStyle(Curve.Long_Dash);
    R2.AssignValueColor(if     R2 == minR12
                         then  CreateColor(105,55,55)
                          else CreateColor(140,55,55));
addChartBubble(if     Bubbles
               &&     Bar == HighestAll(PHBar)
                then  PHBar
                 else na, R1,
               if     R1 == minR12
                then  "R1"
                 else "R2",
               GetColor(5));
 
    S1.HideBubble();
    S1.HideTitle();
    S1.SetStyle(Curve.Long_Dash);
    S1.AssignValueColor(if     S1 == minS12
                         then  CreateColor(55,115,55)
                          else CreateColor(55,85,55));
    S2.HideBubble();
    S2.HideTitle();
    S2.SetStyle(Curve.Long_Dash);
    S2.AssignValueColor(if     S2 == minS12
                         then  CreateColor(55,115,55)
                          else CreateColor(55,85,55));
addChartBubble(if     Bubbles
               &&     Bar == HighestAll(PLBar)
                then  PLBar
                 else na, S1,
               if     S1 == MaxS12
                then  "S1"
                 else "S2",
               CreateColor(50,175,105),0);
 
# scan plots for up/down trend and broadening, tightening and Quasimodo
# Remember: Scan for pattern being equal to 1
# Quasimodo pattern here is being defined as a broadening pattern that pivots opposite the primary trend.
 
def Trend   = InertiaAll(c);
def TrendUp = trend > trend[1];
def TrendDn = trend < trend[1];
def maxR12bar = Max(R1bar, R2bar);
def maxS12bar = Max(S1bar, S2bar);
def upt = sum(h >= h[1] ,2) == 2;
def dnt = sum(h <  h[1] ,2) == 2;
def upb = sum(l >  l[1] ,2) == 2;
def dnb = sum(l <= l[1] ,2) == 2;
 
def quasiDNpvt = if     upt && dnt[-2]
                  then  1
                   else na;
def quasiDNbar = if     !IsNaN(quasiDNpvt)
                  then  bar
                   else quasidnbar[1];
def quasiDNval = if     bar == quasiDNbar
                  then  h
                   else quasiDNval[1];
def quasiUPpvt = if     dnb && upb[-2]
                  then  1
                   else na;
def quasiUPbar = if     !IsNaN(quasiUPpvt)
                  then  bar
                   else quasiUPbar[1];
def quasiUPval = if     bar == quasiUPbar
                  then  l
                   else quasiUpval[1];
 
def Broaden =  if(S1 == minS12
               && R2 == minR12,1,0);
def QuasiUp =  if(Broaden == 1 && TrendDn
               && quasiUPbar > Max(maxS12bar, maxR12bar)
               && quasiUPbar > quasiDNbar
               && between(quasiUPval, S1Value, S2Value),
                  1,0);
def QuasiDn =  if(Broaden == 1 && TrendUp
               && quasiDNbar > Max(maxS12bar, maxR12bar)
               && quasiDNbar > quasiUPbar
               && between(quasiDNval, R1Value, R2Value), 1,0);
 
    plot DownTrend  = if(S1 == minS12 && R2 == maxR12
                      && c < R2value,1,0);
    plot UpTrend    = if(S1 == maxS12 && R2 == minR12
                      && c > S2value,1,0);
    plot BrokenDownTrend  = if(S1 == minS12 && R2 == maxR12
                            && c > R2value,1,0);                  
    plot BrokenUpTrend    = if(S1 == maxS12 && R2 == minR12
                            && c < S2value,1,0);
    plot Tightening = if(S1 == maxS12 && R2 == maxR12
                      && c < R2value && c > S2value,1,0);
    plot Broadening = if(Broaden == 1 &&
                         QuasiUp == 0 &&
                         QuasiDn == 0,1,0);
    plot Quasimodo  = if(QuasiUp == 1 or
                         QuasiDn == 1,1,0);
 
         DownTrend.Hide();
         UpTrend.Hide();
         Broadening.Hide();
         Tightening.Hide();
         Quasimodo.Hide();
         BrokenDownTrend.Hide();
         BrokenUpTrend.Hide();
 
addlabel(if(labels && DownTrend == 1,1,0), "Down Trend", Color.Light_Red);
addlabel(if(labels && Uptrend == 1,1,0), "Up Trend", Color.Green);
addlabel(if(labels && Broadening == 1,1,0), "Broadening", GetColor(8));
addlabel(if(labels && Tightening == 1,1,0), "Tightening", GetColor(3));
addlabel(if(labels && Quasimodo == 1,1,0), "Quasimodo", Color.Orange);
addlabel(if(labels && BrokenDownTrend == 1,1,0), "Broken Down Trend", CreateColor(185,40,40));
addlabel(if(labels && BrokenUptrend == 1,1,0), "Broken Up Trend", CreateColor(50,185,80));
 
Last edited:
Support and Resistance Fractal Pivots And Trendlines For ThinkOrSwim
7D61xFO.png

Ruby:
# Fractal Pivots with Trend Lines
# Mobius
# V01.08.2012

input n = 4;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;

# Internal Script Reference
script LinePlot {
    input BarID = 0;
    input Value = 0;
    input BarOrigin = 0;
    def ThisBar = HighestAll(BarOrigin);
    def ValueLine = if BarOrigin == ThisBar
                then Value
                else Double.NaN;
    plot P = if ThisBar - BarID <= BarOrigin
             then HighestAll(ValueLine)
             else Double.NaN;
}
   def h = high;
   def l = low;
   def bar = barNumber();
   def PH;
   def PL;
   def hh = fold i = 1 to n + 1
            with p = 1
            while p
            do h > getValue(h, -i);
       PH = if (bar > n and
                h == highest(h, n) and
                hh)
            then h
            else double.NaN;
   def ll = fold j = 1 to n + 1
            with q = 1
            while q
            do l < getValue(low, -j);
       PL = if (bar > n and
                l == lowest(l, n) and
                ll)
            then l
            else double.NaN;
   def PHBar = if !isNaN(PH)
               then bar
               else PHBar[1];
   def PLBar = if !isNaN(PL)
               then bar
               else PLBar[1];
   def PHL = if !isNaN(PH)
             then PH
             else PHL[1];
   def priorPHBar = if PHL != PHL[1]
                    then PHBar[1]
                    else priorPHBar[1];
   def PriorPH = if PHL != PHL[1]
                 then PHL[1]
                 else PriorPH[1];
   def PLL = if !isNaN(PL)
             then PL
             else PLL[1];
   def PriorPLBar = if PLL != PLL[1]
                    then PLBar[1]
                    else priorPLBar[1];
   def PriorPL = if PLL != PLL[1]
                 then PLL[1]
                 else PriorPL[1];
   def HighPivots = bar >= highestAll(priorPHBar);
   def LowPivots = bar >= highestAll(priorPLBar);
   def FirstRpoint = if HighPivots
                     then bar - PHBar
                     else 0;
   def PriorRpoint = if HighPivots
                     then bar - PriorPHBar
                     else 0;
   def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
                       / (PHBar - PriorPHBar);
   def FirstSpoint = if LowPivots
                     then bar - PLBar
                     else 0;
   def PriorSpoint = if LowPivots
                     then bar - PriorPLBar
                     else 0;
   def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
                   / (PLBar - PriorPLBar);
   def RExtend = if bar == highestall(PHBar)
                 then 1
                 else RExtend[1];
   def SExtend = if bar == highestall(PLBar)
                 then 1
                 else SExtend[1];
  plot PriorPHLine = LinePlot(PriorRPoint, PriorPH, PriorPHBar);
       PriorPHLine.SetStyle(Curve.Firm);
       PriorPHLine.SetDefaultColor(Color.Red);
  plot PivotHighLine = LinePlot(FirstRPoint, PH, PHBar);
       PivotHighLine.SetStyle(Curve.Firm);
       PivotHighLine.SetDefaultColor(Color.Red);
  plot pivotHigh = if HighPivots
                   then PH
                   else double.NaN;
       pivotHigh.SetDefaultColor(GetColor(1));
       pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
       pivotHigh.setHiding(!showValues);
  plot RLine = pivotHigh;
       RLine.enableApproximation();
       RLine.SetDefaultColor(GetColor(7));
       RLine.SetStyle(Curve.Short_DASH);
  plot RExtension = if RExtend
                    then (bar - PHBar) * RSlope + PHL
                    else double.NaN;
       RExtension.SetStyle(Curve.Short_DASH);
       RExtension.SetDefaultColor(GetColor(7));
  plot PriorPLLine = LinePlot(PriorSPoint, PriorPL, PriorPLBar);
       PriorPLLine.SetStyle(Curve.Firm);
       PriorPLLine.SetDefaultColor(Color.Green);
  plot PivotLowLine = LinePlot(FirstSPoint, PL, PLBar);
       PivotLowLine.SetStyle(Curve.Firm);
       PivotLowLine.SetDefaultColor(Color.Green);
  plot pivotLow = if LowPivots
                   then PL
                   else double.NaN;
       pivotLow.SetDefaultColor(GetColor(1));
       pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
       pivotLow.setHiding(!showValues);
  plot SupportLine = pivotLow;
       SupportLine.enableApproximation();
       SupportLine.SetDefaultColor(GetColor(7));
       SUpportLine.SetStyle(Curve.Short_DASH);
  plot SupportExtension = if SExtend
                          then (bar - PLBar) * SSlope + PLL
                          else double.NaN;
       SupportExtension.SetDefaultColor(GetColor(7));
       SupportExtension.SetStyle(Curve.Short_DASH);
  plot BN = bar;
       BN.SetDefaultColor(GetColor(0));
       BN.setHiding(!showBarNumbers);
       BN.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
  plot PivotDot = if !isNaN(pivotHigh)
                  then pivotHigh
                  else if !isNaN(pivotLow)
                  then pivotLow
                  else double.NaN;
       pivotDot.SetDefaultColor(GetColor(7));
       pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
       pivotDot.SetLineWeight(3);

# End Code Pivots with Projections
 
Support and Resistance Fractal Pivots And Trendlines For ThinkOrSwim
7D61xFO.png

Ruby:
# Fractal Pivots with Trend Lines
# Mobius
# V01.08.2012

input n = 4;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;

# Internal Script Reference
script LinePlot {
    input BarID = 0;
    input Value = 0;
    input BarOrigin = 0;
    def ThisBar = HighestAll(BarOrigin);
    def ValueLine = if BarOrigin == ThisBar
                then Value
                else Double.NaN;
    plot P = if ThisBar - BarID <= BarOrigin
             then HighestAll(ValueLine)
             else Double.NaN;
}
   def h = high;
   def l = low;
   def bar = barNumber();
   def PH;
   def PL;
   def hh = fold i = 1 to n + 1
            with p = 1
            while p
            do h > getValue(h, -i);
       PH = if (bar > n and
                h == highest(h, n) and
                hh)
            then h
            else double.NaN;
   def ll = fold j = 1 to n + 1
            with q = 1
            while q
            do l < getValue(low, -j);
       PL = if (bar > n and
                l == lowest(l, n) and
                ll)
            then l
            else double.NaN;
   def PHBar = if !isNaN(PH)
               then bar
               else PHBar[1];
   def PLBar = if !isNaN(PL)
               then bar
               else PLBar[1];
   def PHL = if !isNaN(PH)
             then PH
             else PHL[1];
   def priorPHBar = if PHL != PHL[1]
                    then PHBar[1]
                    else priorPHBar[1];
   def PriorPH = if PHL != PHL[1]
                 then PHL[1]
                 else PriorPH[1];
   def PLL = if !isNaN(PL)
             then PL
             else PLL[1];
   def PriorPLBar = if PLL != PLL[1]
                    then PLBar[1]
                    else priorPLBar[1];
   def PriorPL = if PLL != PLL[1]
                 then PLL[1]
                 else PriorPL[1];
   def HighPivots = bar >= highestAll(priorPHBar);
   def LowPivots = bar >= highestAll(priorPLBar);
   def FirstRpoint = if HighPivots
                     then bar - PHBar
                     else 0;
   def PriorRpoint = if HighPivots
                     then bar - PriorPHBar
                     else 0;
   def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
                       / (PHBar - PriorPHBar);
   def FirstSpoint = if LowPivots
                     then bar - PLBar
                     else 0;
   def PriorSpoint = if LowPivots
                     then bar - PriorPLBar
                     else 0;
   def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
                   / (PLBar - PriorPLBar);
   def RExtend = if bar == highestall(PHBar)
                 then 1
                 else RExtend[1];
   def SExtend = if bar == highestall(PLBar)
                 then 1
                 else SExtend[1];
  plot PriorPHLine = LinePlot(PriorRPoint, PriorPH, PriorPHBar);
       PriorPHLine.SetStyle(Curve.Firm);
       PriorPHLine.SetDefaultColor(Color.Red);
  plot PivotHighLine = LinePlot(FirstRPoint, PH, PHBar);
       PivotHighLine.SetStyle(Curve.Firm);
       PivotHighLine.SetDefaultColor(Color.Red);
  plot pivotHigh = if HighPivots
                   then PH
                   else double.NaN;
       pivotHigh.SetDefaultColor(GetColor(1));
       pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
       pivotHigh.setHiding(!showValues);
  plot RLine = pivotHigh;
       RLine.enableApproximation();
       RLine.SetDefaultColor(GetColor(7));
       RLine.SetStyle(Curve.Short_DASH);
  plot RExtension = if RExtend
                    then (bar - PHBar) * RSlope + PHL
                    else double.NaN;
       RExtension.SetStyle(Curve.Short_DASH);
       RExtension.SetDefaultColor(GetColor(7));
  plot PriorPLLine = LinePlot(PriorSPoint, PriorPL, PriorPLBar);
       PriorPLLine.SetStyle(Curve.Firm);
       PriorPLLine.SetDefaultColor(Color.Green);
  plot PivotLowLine = LinePlot(FirstSPoint, PL, PLBar);
       PivotLowLine.SetStyle(Curve.Firm);
       PivotLowLine.SetDefaultColor(Color.Green);
  plot pivotLow = if LowPivots
                   then PL
                   else double.NaN;
       pivotLow.SetDefaultColor(GetColor(1));
       pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
       pivotLow.setHiding(!showValues);
  plot SupportLine = pivotLow;
       SupportLine.enableApproximation();
       SupportLine.SetDefaultColor(GetColor(7));
       SUpportLine.SetStyle(Curve.Short_DASH);
  plot SupportExtension = if SExtend
                          then (bar - PLBar) * SSlope + PLL
                          else double.NaN;
       SupportExtension.SetDefaultColor(GetColor(7));
       SupportExtension.SetStyle(Curve.Short_DASH);
  plot BN = bar;
       BN.SetDefaultColor(GetColor(0));
       BN.setHiding(!showBarNumbers);
       BN.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
  plot PivotDot = if !isNaN(pivotHigh)
                  then pivotHigh
                  else if !isNaN(pivotLow)
                  then pivotLow
                  else double.NaN;
       pivotDot.SetDefaultColor(GetColor(7));
       pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
       pivotDot.SetLineWeight(3);

# End Code Pivots with Projections
Thank you for posting this, the trend lines are spot on, is there a way to have it plot the previous 3 trend line extensions as well.
 
Thank you for posting this, the trend lines are spot on, is there a way to have it plot the previous 3 trend line extensions as well.
As a mean reversion trader, I agree with you, having the previous resistance point would be very helpful.
Unfortunately, currently the indicator can only be used to capture the current support and resistance.
 
Last edited:
Is there any difference between this and pivot pr

is there any major difference between that and this one https://usethinkscript.com/threads/...ort-resistance-indicator-for-thinkorswim.158/ ?
They aren't comparable given the huge difference in their calculations.

We have over 50 pivot point indicators on the forum:
https://usethinkscript.com/search/840996/?q=pivots&t=post&c[title_only]=1&o=replies&g=1

All of them will occasionally get you to the same place as that is their function.
But given the difference in manner of their various calculations, they will also plot differently.

Best practices guidelines dictate the manual drawing of support & resistance lines to pivot off of; as most trading platforms are not artificially intellegent enough to create pivots that work in all market conditions for all instruments.

The study linked in your post is the most popular, the most referenced S&R indicator on the forum. Use by scalpers and day traders of liquid stocks.
 
I'm amazed there isn't just 1 standard people are using instead of the 30 different scripts I'm stumbling upon.
 
Last edited by a moderator:
I'm amazed there isn't just 1 standard people are using instead of the 30 different scripts I'm stumbling upon.
There is only one standard:
https://cmtassociation.org/kb_categories/support-and-resistance/
Drilling down to review the examples shown in the above link, you can see that these levels can only be truly be plotted manually; by drawing the lines because the computer isn't AI enough to remove noise and outliers and because dynamic lengths are needed to account for volatility and contraction.

Every good strategy must have an S&R and while swing traders who are working from multiple timeframes and multiple charts will tend to drawn their own. Day Traders are less likely to. The many variations of Support & Resistance Indicators are their attempts of building a better mousetrap. This is not a criticism. We have dozens of S&Rs for the same reason we have dozens of oscillators (https://thepatternsite.com/Oscillators.html). Members choose the one that works best with their other indicators.

PS: Volume Support & Resistance is a new animal. It is not formally recognized as an S&R as Support & Resistance is currently only defined on price action.
 
Last edited:
As a mean reversion trader, I agree with you, having the previous resistance point would be very helpful.
Unfortunately, currently the indicator can only be used to capture the current support and resistance.
I have been able to use Welkin VSA upper chart to mark prev volsigma < 2 which helps
 

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