Auto Pivot Points Support & Resistance Indicator for ThinkorSwim

BenTen

BenTen

Administrative
Staff
VIP
Warehouse
If you're having trouble with drawing trend lines, support and resistance levels then this indicator can help. It's called Projection Pivots, developed by Mobius for ThinkorSwim.

Upon adding the indicator, it will plot several trend lines, support, and resistance channels based on critical pivot points of the stock.



What I normally do is load this up at the end of each trading day and plot new S/R areas onto my chart based on the suggestion of the Projection Pivots indicator. I would also do this at the end of the week as well. It's important that you have these levels up to date on your chart.

thinkScript Code

Rich (BB code):
#ProjectionPivots_v03_JQ
#03.04.2019
#Original Code and Concept by Mobius:
# V01.08.2012 Projection Pivots
# mobius

# Notes:
# 03.04.2019 added linits on extensions
# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits

#declare Once_Per_Bar;
#Inputs
input n = 21;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;
input DisplayLabel = yes;    #JQ 7.8.2018 added
    addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE);    #JQ 7.8.2018 added

 
# Universal Header _v030429019 _JQ
#     code from various sources including Mobius, NoLongerNube and others
# Comment out unnecessary portions to preserve tos memory and enhance speed

# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019
# iData Definitions
    def vHigh = high;  # creates the variable vHigh.  Use of the variable reduce data calls to tos iData server
#    def initHigh =  CompoundValue(1, high, high);  # creates and initialized variable for High
    def vLow = low;
#    def initLow = CompoundValue(1, low, low);
    def vOpen = open;
#    def initOpen = CompoundValue(1, open, open);
    def vClose = close;
#    def initClose = CompoundValue(1, close, close);
    def vVolume = volume;
#    def initVolume = CompoundValue(1, volume, volume);
    def nan = Double.NaN;
# Bar Time & Date
    def bn = BarNumber();
    def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
#    def Today = GetDay() ==GetLastDay();
#    def time = GetTime();
#    def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
    # def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX[1];
#    def RTS  = RegularTradingStart(GetYYYYMMDD());
#    def RTE  = RegularTradingEnd(GetYYYYMMDD());
#    def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
#    def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH[1];

# bars that start and end the sessions  #(borrowed from nube)
#    def rthStartBar    = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingStart(GetYYYYMMDD())
#                         then bn
#                         else rthStartBar[1], 0);
#    def rthEndBar      = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else rthEndBar[1], 1);
#    def globexStartBar = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses below RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else globexStartBar[1], 1);
#    def rthSession = if   bn crosses above rthStartBar #+ barsExtendedBeyondSession
#                     then 1
#                     else if   bn crosses above rthEndBar #+ barsExtendedBeyondSession
#                          then 0
#                     else rthSession[1];

# Bubble Locations
    def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());  #corrected 11.12.2018 (JQ) 
        # syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)

def PH;
   def PL;
   def hh = fold i = 1 to n + 1
            with p = 1
            while p
            do vHigh > getValue(vHigh, -i);
       PH = if (bn > n and
                vHigh == highest(vHigh, n) and
                hh)
            then vHigh
            else double.NaN;
   def ll = fold j = 1 to n + 1
            with q = 1
            while q
            do vLow < getValue(low, -j);
       PL = if (bn > n and
                vLow == lowest(vLow, n) and
                ll)
            then vLow
            else double.NaN;
   def PHBar = if !isNaN(PH)
               then bn
               else PHBar[1];
   def PLBar = if !isNaN(PL)
               then bn
               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 = bn >= highestAll(priorPHBar);
   def LowPivots = bn >= highestAll(priorPLBar);
   def FirstRpoint = if HighPivots
                     then bn - PHBar
                     else 0;
   def PriorRpoint = if HighPivots
                     then bn - PriorPHBar
                     else 0;
   def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
                       / (PHBar - PriorPHBar);
   def FirstSpoint = if LowPivots
                     then bn - PLBar
                     else 0;
   def PriorSpoint = if LowPivots
                     then bn - PriorPLBar
                     else 0;
   def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
                   / (PLBar - PriorPLBar);
   def RExtend = if bn == highestall(PHBar)
                 then 1
                 else RExtend[1];
   def SExtend = if bn == highestall(PLBar)
                 then 1
                 else SExtend[1];

  plot pivotHigh = if HighPivots
                   then PH
                   else double.NaN;
       pivotHigh.SetDefaultColor(GetColor(1));
       pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
       pivotHigh.setHiding(!showValues);

  plot pivotHighLine = if PHL > 0 and
                          HighPivots
                       then PHL
                       else double.NaN;
       pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES);  # Mobius original was DASHES
       pivotHighLine.setDefaultColor(color.uptick);    #JQ 7.8.2018 added
       pivotHighLine.setHiding(!showLines);

  plot RLine = pivotHigh;
       RLine.enableApproximation();
       RLine.SetDefaultColor(Color.LIGHT_GRAY);
       RLine.SetStyle(Curve.Short_DASH);

# Added code to limit resistance estension line (JQ 03.04.2019)
  def calc_ResistanceExtension = if RExtend
                    then (bn - PHBar) * RSlope + PHL
                    else double.NaN;
  plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)
                                   and calc_ResistanceExtension[1] >=  (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
                                   and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
                               then calc_ResistanceExtension else double.nan;
       line_ResistanceExtension.SetStyle(Curve.Short_DASH);
       line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
       line_ResistanceExtension.setLineWeight(1);

# Low Plots
  plot pivotLow = if LowPivots
                  then PL
                  else double.NaN;
       pivotLow.setDefaultColor(GetColor(4));
       pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
       pivotLow.setHiding(!showValues);

  plot pivotLowLine = if PLL > 0 and
                         LowPivots
                      then PLL
                      else double.NaN;
       pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES);  # Mobius original was DASHES
       pivotLowLine.setDefaultColor(color.DOWNTICK);#  #  JQ 7.8.2018 added
       pivotLowLine.setHiding(!showLines);

  plot SupportLine = pivotLow;
       SupportLine.enableApproximation();
       SupportLine.SetDefaultColor(color.LIGHT_GRAY);
       SUpportLine.SetStyle(Curve.Short_DASH);

# Added code to limit support estension line (JQ 03.04.2019)
  def calc_SupportExtension = if SExtend
                          then (bn - PLBar) * SSlope + PLL
                          else double.NaN;
  plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)
                                   and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
                                   and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
                               then calc_supportExtension else double.nan;
       line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
       line_SupportExtension.SetStyle(Curve.Short_DASH);
       line_SupportExtension.setLineWeight(1);

  plot BarNumbersBelow = bn;
       BarNumbersBelow.SetDefaultColor(GetColor(0));
       BarNumbersBelow.setHiding(!showBarNumbers);
       BarNumbersBelow.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

Shareable Link

http://tos.mx/WhZxG6

Video Tutorial

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@TraderPaul81 I just updated the code. Should work now.
 
S

soary

New member
This looks great but I am looking for an indicator that will plot the traditional pivot lines on the chart automatically even if I change symbols and time frames.
The Formulas for classic Pivot Points: Pivot Point Formulas. Investopedia
PIVOT POINTS (P) = (HIGH + LOW + CLOSE) = ALL RESULTS / 3
RESISTENCE 1 (R1) = (P * 2) - LOW
RESISTENCE 2 (R2) = P + (HIGH – LOW)
SUPPORT 1 (S1) = (P * 2) - HIGH
SUPPORT 2 (S2)= P – (HIGH – LOW)
Resistance (r3) = pp + 2 * (h – l);
Support (s3) = pp – 2 * (h – l);
The one in TOS does not work well on a daily chart no matter what setting it does not place clean lines on the chart but it works with in-traday charts. I want it to work after the close on a daily chart.
Thanks
 
H

horserider

Active member
Warehouse
@soary I did not look at the calculations but try Theotradepivots and see if it fits what you want.
 
S

soary

New member
@soary I did not look at the calculations but try Theotradepivots and see if it fits what you want.
uuuuuggghhh don't ever send anyone to theotrade again. I looked in TOS and there was no Theotradepivots so I Googled and found the website Theotrade. It claims free instant access when you join. Not so, I join and it begins with a hour long marketing audio on a page with nothing but a bunch of marketing sales and when you click on instant access to get away from it, then it takes you to a paypal subscription page. I never got to the blogs to look up pivot points. What a poor first impression for a website. I can't wait for them to send me their stuff so that I can unsubscribe and block them. Thanks for trying.
 
H

horserider

Active member
Warehouse
@soary Sorry. Must have been in my studies so long I thought it was standard to ToS. Here it is. Adjust the n value to your needs.

Code:
# Pivot_Array (***Please copy and paste this name to study name above***)
# Mobius
# V01.09.13.2017
#hint: Plots the most recent 4 resistance pivots and support pivots. \n For scanning: Find the study in list of studies then select either BearScan or BullScan as the left side entry in the Scan Wizard and equal to as the center definition and close price as the right side entry.

# User Inputs
input n = 3; #hint n: periods used for pivot calculations.

# 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;
}
# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def BBar = bar == HighestAll(bar);
# Parent High
def ParentHigh = HighestAll(h);
def ParentHBarOrigin = if h == ParentHigh
                       then bar
                       else ParentHBarOrigin[1];
def ParentHBarID = bar - HighestAll(ParentHBarOrigin);
# R1
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
def PivotH = if (bar > n and
                 h == Highest(h, n) and
                 hh)
            then h
            else Double.NaN;
def PHValue = if !IsNaN(PivotH)
              then PivotH
              else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
                  then bar
                  else PHBarOrigin[1];
def PHBarID = bar - PHBarOrigin;
# R2
def R2PHValue = if PHBarOrigin != PHBarOrigin[1]
              then PHValue[1]
              else R2PHValue[1];
def R2PHBarOrigin = if PHBarOrigin != PHBarOrigin[1]
                  then PHBarOrigin[1]
                  else R2PHBarOrigin[1];
def R2PHBarID = bar - R2PHBarOrigin;
# R3
def R3PHValue = if R2PHBarOrigin != R2PHBarOrigin[1]
              then R2PHValue[1]
              else R3PHValue[1];
def R3PHBarOrigin = if R2PHBarOrigin != R2PHBarOrigin[1]
                  then R2PHBarOrigin[1]
                  else R3PHBarOrigin[1];
def R3PHBarID = bar - R3PHBarOrigin;
# R4
def R4PHValue = if R3PHBarOrigin != R3PHBarOrigin[1]
              then R3PHValue[1]
              else R4PHValue[1];
def R4PHBarOrigin = if R3PHBarOrigin != R3PHBarOrigin[1]
                  then R3PHBarOrigin[1]
                  else R4PHBarOrigin[1];
def R4PHBarID = bar - R4PHBarOrigin;

# Parent Low
def ParentLow = LowestAll(l);
def ParentLBarOrigin = if l == ParentLow
                       then bar
                       else ParentLBarOrigin[1];
def ParentLBarID = bar - HighestAll(ParentLBarOrigin);
# S1
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else Double.NaN;
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
                  then bar
                  else PLBarOrigin[1];
def PLBarID = bar - PLBarOrigin;
# S2
def S2PLValue = if PLBarOrigin != PLBarOrigin[1]
              then PLValue[1]
              else S2PLValue[1];
def S2PLBarOrigin = if PLBarOrigin != PLBarOrigin[1]
                  then PLBarOrigin[1]
                  else S2PLBarOrigin[1];
def S2PLBarID = bar - S2PLBarOrigin;
# S3
def S3PLValue = if S2PLBarOrigin != S2PLBarOrigin[1]
              then S2PLValue[1]
              else S3PLValue[1];
def S3PLBarOrigin = if S2PLBarOrigin != S2PLBarOrigin[1]
                  then S2PLBarOrigin[1]
                  else S3PLBarOrigin[1];
def S3PLBarID = bar - S3PLBarOrigin;
# S4
def S4PLValue = if S3PLBarOrigin != S3PLBarOrigin[1]
              then S3PLValue[1]
              else S4PLValue[1];
def S4PLBarOrigin = if S3PLBarOrigin != S3PLBarOrigin[1]
                  then S3PLBarOrigin[1]
                  else S4PLBarOrigin[1];
def S4PLBarID = bar - S4PLBarOrigin;

# Plots
plot PR1 = LinePlot(BarID = ParentHBarID,
                    Value = ParentHigh,
                    BarOrigin = HighestAll(ParentHBarOrigin));
     PR1.SetDefaultColor(Color.GREEN);
#addChartBubble(Bar == HighestAll(ParentHBarOrigin), ParentHigh, "High", color.yellow, 1);
plot R1 = LinePlot(BarID = PHBarID,
                   Value = PHValue,
                   BarOrigin = PHBarOrigin);
     R1.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(PHBarOrigin), PHvalue, "R1", color.green, 1);
plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
     R2.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R2PHBarOrigin), PHvalue, "R2", color.green, 1);
plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
     R3.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R3PHBarOrigin), PHvalue, "R3", color.green, 1);
plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
     R4.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R4PHBarOrigin), PHvalue, "R4", color.green, 1);

plot PS1 = LinePlot(BarID = ParentLBarID,
                   Value = ParentLow,
                   BarOrigin = HighestAll(ParentLBarOrigin));
     PS1.SetDefaultColor(Color.RED);
AddChartBubble(Bar == HighestAll(ParentLBarOrigin), ParentLow, "Low", Color.Yellow, 0);
plot S1 = LinePlot(BarID = PLBarID,
                   Value = PLValue,
                   BarOrigin = PLBarOrigin);
     S1.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(PLBarOrigin), PLvalue, "S1", color.red, 0);
plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
     S2.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S2PLBarOrigin), PLvalue, "S2", color.red, 0);
plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
     S3.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S3PLBarOrigin), PLvalue, "S3", color.red, 0);
plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
     S4.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S4PLBarOrigin), PLvalue, "S4", color.red, 0);
# Signals
def UpSignal;
def DnSignal;
if close crosses above R4
{
    UpSignal = low;
}
else if close crosses above R3
{
    UpSignal = low;
}
else if close crosses above R2
{
    UpSignal = low;
}
else if close crosses above R1
{
    UpSignal = low;
}
else
{
    UpSignal = UpSignal[1];
}
plot UpArrows = if low == UpSignal
                then low
                else double.nan;
     UpArrows.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
     UpArrows.SetDefaultColor(Color.CYAN);
if close crosses below S1
{
    DnSignal = high;
}
else if close crosses below S2
{
    DnSignal = high;
}
else if close crosses below S3
{
    DnSignal = high;
}
else if close crosses below S4
{
    DnSignal = high;
}
else
{
    DnSignal = DnSignal[1];
}
plot DnArrow = if high == DnSignal
               then high
               else double.nan;
DnArrow.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
DnArrow.SetDefaultColor(Color.YELLOW);
plot BearScan = if (close crosses below S1) OR
                   (close crosses below S2)
                then close
                else double.nan;
plot BullScan = if (close crosses above R1) OR
                   (close crosses above R2)
                then close
                else double.nan;
# End Code Fractal Array
 
S

San

Member
VIP
@BenTen and @horserider , How to use these indicator for Monthly, Weekly,Hourly 4hours,30mts, 15 and 5 mts.
 
S

soary

New member
@BenTen and @horserider , How to use these indicator for Monthly, Weekly,Hourly 4hours,30mts, 15 and 5 mts.
San, The idea is for the script to use the H, L, C data from the monthly, weekly and daily charts and since the data changes I would draw them onto the chart so that when I go to a lower time frames the lines appear. The daily lines would be changed daily but the monthly and weekly would not be changed until the new week or month. These act as support and resistance levels so that when price hit any of them in any time frame you know you are at a key level, I still have questions on the script so I will respond to the post. Hope that helps.
 
S

soary

New member
@soary Sorry. Must have been in my studies so long I thought it was standard to ToS. Here it is. Adjust the n value to your needs.

Code:
# Pivot_Array (***Please copy and paste this name to study name above***)
# Mobius
# V01.09.13.2017
#hint: Plots the most recent 4 resistance pivots and support pivots. \n For scanning: Find the study in list of studies then select either BearScan or BullScan as the left side entry in the Scan Wizard and equal to as the center definition and close price as the right side entry.

# User Inputs
input n = 3; #hint n: periods used for pivot calculations.

# 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;
}
# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def BBar = bar == HighestAll(bar);
# Parent High
def ParentHigh = HighestAll(h);
def ParentHBarOrigin = if h == ParentHigh
                       then bar
                       else ParentHBarOrigin[1];
def ParentHBarID = bar - HighestAll(ParentHBarOrigin);
# R1
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
def PivotH = if (bar > n and
                 h == Highest(h, n) and
                 hh)
            then h
            else Double.NaN;
def PHValue = if !IsNaN(PivotH)
              then PivotH
              else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
                  then bar
                  else PHBarOrigin[1];
def PHBarID = bar - PHBarOrigin;
# R2
def R2PHValue = if PHBarOrigin != PHBarOrigin[1]
              then PHValue[1]
              else R2PHValue[1];
def R2PHBarOrigin = if PHBarOrigin != PHBarOrigin[1]
                  then PHBarOrigin[1]
                  else R2PHBarOrigin[1];
def R2PHBarID = bar - R2PHBarOrigin;
# R3
def R3PHValue = if R2PHBarOrigin != R2PHBarOrigin[1]
              then R2PHValue[1]
              else R3PHValue[1];
def R3PHBarOrigin = if R2PHBarOrigin != R2PHBarOrigin[1]
                  then R2PHBarOrigin[1]
                  else R3PHBarOrigin[1];
def R3PHBarID = bar - R3PHBarOrigin;
# R4
def R4PHValue = if R3PHBarOrigin != R3PHBarOrigin[1]
              then R3PHValue[1]
              else R4PHValue[1];
def R4PHBarOrigin = if R3PHBarOrigin != R3PHBarOrigin[1]
                  then R3PHBarOrigin[1]
                  else R4PHBarOrigin[1];
def R4PHBarID = bar - R4PHBarOrigin;

# Parent Low
def ParentLow = LowestAll(l);
def ParentLBarOrigin = if l == ParentLow
                       then bar
                       else ParentLBarOrigin[1];
def ParentLBarID = bar - HighestAll(ParentLBarOrigin);
# S1
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else Double.NaN;
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
                  then bar
                  else PLBarOrigin[1];
def PLBarID = bar - PLBarOrigin;
# S2
def S2PLValue = if PLBarOrigin != PLBarOrigin[1]
              then PLValue[1]
              else S2PLValue[1];
def S2PLBarOrigin = if PLBarOrigin != PLBarOrigin[1]
                  then PLBarOrigin[1]
                  else S2PLBarOrigin[1];
def S2PLBarID = bar - S2PLBarOrigin;
# S3
def S3PLValue = if S2PLBarOrigin != S2PLBarOrigin[1]
              then S2PLValue[1]
              else S3PLValue[1];
def S3PLBarOrigin = if S2PLBarOrigin != S2PLBarOrigin[1]
                  then S2PLBarOrigin[1]
                  else S3PLBarOrigin[1];
def S3PLBarID = bar - S3PLBarOrigin;
# S4
def S4PLValue = if S3PLBarOrigin != S3PLBarOrigin[1]
              then S3PLValue[1]
              else S4PLValue[1];
def S4PLBarOrigin = if S3PLBarOrigin != S3PLBarOrigin[1]
                  then S3PLBarOrigin[1]
                  else S4PLBarOrigin[1];
def S4PLBarID = bar - S4PLBarOrigin;

# Plots
plot PR1 = LinePlot(BarID = ParentHBarID,
                    Value = ParentHigh,
                    BarOrigin = HighestAll(ParentHBarOrigin));
     PR1.SetDefaultColor(Color.GREEN);
#addChartBubble(Bar == HighestAll(ParentHBarOrigin), ParentHigh, "High", color.yellow, 1);
plot R1 = LinePlot(BarID = PHBarID,
                   Value = PHValue,
                   BarOrigin = PHBarOrigin);
     R1.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(PHBarOrigin), PHvalue, "R1", color.green, 1);
plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
     R2.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R2PHBarOrigin), PHvalue, "R2", color.green, 1);
plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
     R3.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R3PHBarOrigin), PHvalue, "R3", color.green, 1);
plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
     R4.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R4PHBarOrigin), PHvalue, "R4", color.green, 1);

plot PS1 = LinePlot(BarID = ParentLBarID,
                   Value = ParentLow,
                   BarOrigin = HighestAll(ParentLBarOrigin));
     PS1.SetDefaultColor(Color.RED);
AddChartBubble(Bar == HighestAll(ParentLBarOrigin), ParentLow, "Low", Color.Yellow, 0);
plot S1 = LinePlot(BarID = PLBarID,
                   Value = PLValue,
                   BarOrigin = PLBarOrigin);
     S1.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(PLBarOrigin), PLvalue, "S1", color.red, 0);
plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
     S2.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S2PLBarOrigin), PLvalue, "S2", color.red, 0);
plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
     S3.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S3PLBarOrigin), PLvalue, "S3", color.red, 0);
plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
     S4.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S4PLBarOrigin), PLvalue, "S4", color.red, 0);
# Signals
def UpSignal;
def DnSignal;
if close crosses above R4
{
    UpSignal = low;
}
else if close crosses above R3
{
    UpSignal = low;
}
else if close crosses above R2
{
    UpSignal = low;
}
else if close crosses above R1
{
    UpSignal = low;
}
else
{
    UpSignal = UpSignal[1];
}
plot UpArrows = if low == UpSignal
                then low
                else double.nan;
     UpArrows.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
     UpArrows.SetDefaultColor(Color.CYAN);
if close crosses below S1
{
    DnSignal = high;
}
else if close crosses below S2
{
    DnSignal = high;
}
else if close crosses below S3
{
    DnSignal = high;
}
else if close crosses below S4
{
    DnSignal = high;
}
else
{
    DnSignal = DnSignal[1];
}
plot DnArrow = if high == DnSignal
               then high
               else double.nan;
DnArrow.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
DnArrow.SetDefaultColor(Color.YELLOW);
plot BearScan = if (close crosses below S1) OR
                   (close crosses below S2)
                then close
                else double.nan;
plot BullScan = if (close crosses above R1) OR
                   (close crosses above R2)
                then close
                else double.nan;
# End Code Fractal Array
.............................................................................................................
horserider, Thanks I just added it and I have a few questions. I'm not a script writer so how to remove the color bubbles labels for the various levels? Do I only delete the lines that say,
addChartBubble(Bar == HighestAll(PHBarOrigin), PHvalue, "R1", color.green, 1);
.....................................................
To be explicit
on the daily chart after the close E/O/D would n = 1
on the weekly chart set up on a Saturday would n = 7 not counting the starting Saturday counting from or n = 8 counting the starting Saturday
on a monthly chart would n = 30 or 31 depending on the # days in that particular month if I look at it after the close on the last day of the month
......................................................
Also, I noticed some extra levels called PR1 and then it goes on to R1,R2....R4 and PS1 and then it goes on to S1,S2......S4, what are PR1 AND PS1? I'm guessing PR1 may be the Pivot followed by R1 to R4 but then what is PS1 since it already plots S1 TO S4?
........................................................
A line in the code says, Plots the most recent 4 resistance pivots and support pivots. Does this mean I should turn off extended hours market at close for example when I need to get the daily pivots?
 
Last edited:
S

San

Member
VIP
@soary, Thanks for your respond, I change n value as you advice above but it does not work.
@horserider , Please advice....
 
H

horserider

Active member
Warehouse
@soary I would just comment out the add bubble lines.
Sorry not sure how n works. It is in the fold calculations and I as yet have no idea how fold works. Math is not my best subject.
I believe PR1 and PS1 are the high and low of the chart being displayed.
I never use extended hours but my guess is it will choose points in the extended hours. Try it and see. Then you can decide which way gives you the lines you want.
 
H

horserider

Active member
Warehouse
@San Sorry no idea why not. If you change it from 3 to say 20 there should be a big difference.
 
Z

zeek

Member
VIP
I really like the trend line drawing in this script. I don't use the Pivots myself because i have another script for that but the automatic drawing of trendlines is super!
 
Last edited:
S

soary

New member
nice video but it is fast I looked multiple times at the ending but still didn't get the last part.. What was that at the end that seems to add the price of the levels towards the end? Can you describe that part? Thanks
 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@soary In the indicator's settings page, there is an option to turn on those values.
 
S

Saipan

New member
Hello!

I actually just registered so I could reach out to the group here with a question about enhancing the above script.

I really like this study within ToS, but I wanted to be able to extend it backwards so that I could get multiple pivots to cover an entire aggregate period (I've been looking at one day aggregate with minutely bars). I attempted to modify your script, but I admit that I don't have a 100% grasp on everything so I didn't quite manage it correctly. I want to be able to break it up into a specific time period "n" as you already have it for the original code, but I'm hoping to have that "n" dictate the time frame for multiple pivots and regression lines going back to the start of the chart. So if there are 100 bars and we have n=25 then we'd see four different groupings of this code across the chart.

Here's what I have so far:

Code:
#ProjectionPivots_v03_JQ

#03.04.2019

#Original Code and Concept by Mobius:

# V01.08.2012 Projection Pivots

# mobius



# Notes:

# 03.04.2019 added linits on extensions

# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits



#declare Once_Per_Bar;

#Inputs

input n = 21;

input n2 = 42;

input n3 = 63;

input n4 = 84;

input showLines = yes;

input showValues = no;

input showBarNumbers = no;

input ExtensionLengthBars = 20; # added to control length of Entension

input UpperExtensionPercentLimit = 5;

input LowerExtensionPercentLimit = 5;

input DisplayLabel = yes; #JQ 7.8.2018 added

addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE); #JQ 7.8.2018 added





# Universal Header _v030429019 _JQ

# code from various sources including Mobius, NoLongerNube and others

# Comment out unnecessary portions to preserve tos memory and enhance speed



# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019

# iData Definitions

def vHigh = high; # creates the variable vHigh. Use of the variable reduce data calls to tos iData server

# def initHigh = CompoundValue(1, high, high); # creates and initialized variable for High

def vLow = low;

# def initLow = CompoundValue(1, low, low);

def vOpen = open;

# def initOpen = CompoundValue(1, open, open);

def vClose = close;

# def initClose = CompoundValue(1, close, close);

def vVolume = volume;

# def initVolume = CompoundValue(1, volume, volume);

def nan = Double.NaN;

# Bar Time & Date

def bn = BarNumber();

def bn2 = bn[21];

def bn3 = bn[42];

def bn4 = bn[63];

def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);

def currentBar2 = HighestAll(if !IsNaN(vHigh[21]) then bn2 else nan)[21];

def currentBar3 = HighestAll(if !IsNaN(vHigh[42]) then bn3 else nan)[42];

def currentBar4 = HighestAll(if !IsNaN(vHigh[63]) then bn4 else nan)[63];

# def Today = GetDay() ==GetLastDay();

# def time = GetTime();

# def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

# def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX[1];

# def RTS = RegularTradingStart(GetYYYYMMDD());

# def RTE = RegularTradingEnd(GetYYYYMMDD());

# def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());

# def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH[1];



# bars that start and end the sessions #(borrowed from nube)

# def rthStartBar = CompoundValue(1,

# if !IsNaN(vClose)

# && time crosses above RegularTradingStart(GetYYYYMMDD())

# then bn

# else rthStartBar[1], 0);

# def rthEndBar = CompoundValue(1,

# if !IsNaN(vClose)

# && time crosses above RegularTradingEnd(GetYYYYMMDD())

# then bn

# else rthEndBar[1], 1);

# def globexStartBar = CompoundValue(1,

# if !IsNaN(vClose)

# && time crosses below RegularTradingEnd(GetYYYYMMDD())

# then bn

# else globexStartBar[1], 1);

# def rthSession = if bn crosses above rthStartBar #+ barsExtendedBeyondSession

# then 1

# else if bn crosses above rthEndBar #+ barsExtendedBeyondSession

# then 0

# else rthSession[1];



# Bubble Locations

def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber()); #corrected 11.12.2018 (JQ)

# syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)



def PH;

def PL;

def hh = fold i = 1 to n + 1

with p = 1

while p

do vHigh > getValue(vHigh, -i);

PH = if (bn > n and

vHigh == highest(vHigh, n) and

hh)

then vHigh

else double.NaN;

def ll = fold j = 1 to n + 1

with q = 1

while q

do vLow < getValue(low, -j);

PL = if (bn > n and

vLow == lowest(vLow, n) and

ll)

then vLow

else double.NaN;

def PHBar = if !isNaN(PH)

then bn

else PHBar[1];

def PLBar = if !isNaN(PL)

then bn

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 = bn >= highestAll(priorPHBar);

def LowPivots = bn >= highestAll(priorPLBar);

def FirstRpoint = if HighPivots

then bn - PHBar

else 0;

def PriorRpoint = if HighPivots

then bn - PriorPHBar

else 0;

def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))

/ (PHBar - PriorPHBar);

def FirstSpoint = if LowPivots

then bn - PLBar

else 0;

def PriorSpoint = if LowPivots

then bn - PriorPLBar

else 0;

def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))

/ (PLBar - PriorPLBar);

def RExtend = if bn == highestall(PHBar)

then 1

else RExtend[1];

def SExtend = if bn == highestall(PLBar)

then 1

else SExtend[1];



plot pivotHigh = if HighPivots

then PH

else double.NaN;

pivotHigh.SetDefaultColor(GetColor(1));

pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

pivotHigh.setHiding(!showValues);



plot pivotHighLine = if PHL > 0 and

HighPivots

then PHL

else double.NaN;

pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotHighLine.setDefaultColor(color.uptick); #JQ 7.8.2018 added

pivotHighLine.setHiding(!showLines);



plot RLine = pivotHigh;

RLine.enableApproximation();

RLine.SetDefaultColor(Color.LIGHT_GRAY);

RLine.SetStyle(Curve.Short_DASH);



# Added code to limit resistance estension line (JQ 03.04.2019)

def calc_ResistanceExtension = if RExtend

then (bn - PHBar) * RSlope + PHL

else double.NaN;

plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)

and calc_ResistanceExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))

and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))

then calc_ResistanceExtension else double.nan;

line_ResistanceExtension.SetStyle(Curve.Short_DASH);

line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_ResistanceExtension.setLineWeight(1);



# Low Plots

plot pivotLow = if LowPivots

then PL

else double.NaN;

pivotLow.setDefaultColor(GetColor(4));

pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

pivotLow.setHiding(!showValues);



plot pivotLowLine = if PLL > 0 and

LowPivots

then PLL

else double.NaN;

pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotLowLine.setDefaultColor(color.DOWNTICK);# # JQ 7.8.2018 added

pivotLowLine.setHiding(!showLines);



plot SupportLine = pivotLow;

SupportLine.enableApproximation();

SupportLine.SetDefaultColor(color.LIGHT_GRAY);

SUpportLine.SetStyle(Curve.Short_DASH);



# Added code to limit support estension line (JQ 03.04.2019)

def calc_SupportExtension = if SExtend

then (bn - PLBar) * SSlope + PLL

else double.NaN;

plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)

and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))

and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))

then calc_supportExtension else double.nan;

line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_SupportExtension.SetStyle(Curve.Short_DASH);

line_SupportExtension.setLineWeight(1);



plot BarNumbersBelow = bn;

BarNumbersBelow.SetDefaultColor(GetColor(0));

BarNumbersBelow.setHiding(!showBarNumbers);

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

#---------------------------------------------------------------------------------



def PH2;

def PL2;

def hh2 = fold i2 = 1 to n + 1

with p2 = 21

while p2

do vHigh > getValue(vHigh, -i2);

PH2 = if (bn2 > n2 and

vHigh == highest(vHigh, n2) and

hh2)

then vHigh

else double.NaN;

def ll2 = fold j2 = 1 to n2 + 1

with q2 = 21

while q2

do vLow < getValue(low, -j2);

PL2 = if (bn2 > n and

vLow == lowest(vLow, n) and

ll2)

then vLow

else double.NaN;

def PHBar2 = if !isNaN(PH2)

then bn2

else PHBar2[1];

def PLBar2 = if !isNaN(PL2)

then bn2

else PLBar[1];

def PHL2 = if !isNaN(PH2)

then PH2

else PHL2[1];

def priorPHBar2 = if PHL2 != PHL2[1]

then PHBar2[1]

else priorPHBar2[1];

def PLL2 = if !isNaN(PL2)

then PL2

else PLL2[1];

def priorPLBar2 = if PLL2 != PLL2[1]

then PLBar2[1]

else priorPLBar2[1];

def HighPivots2 = bn2 >= highestAll(priorPHBar2)[21];

def LowPivots2 = bn2 >= highestAll(priorPLBar2)[21];

def FirstRpoint2 = if HighPivots2

then bn2 - PHBar2

else 0;

def PriorRpoint2 = if HighPivots2

then bn2 - PriorPHBar2

else 0;

def RSlope2 = (getvalue(PH2, FirstRpoint2) - getvalue(PH2, PriorRpoint2))

/ (PHBar2 - PriorPHBar2);

def FirstSpoint2 = if LowPivots2

then bn2 - PLBar2

else 0;

def PriorSpoint2 = if LowPivots2

then bn2 - PriorPLBar2

else 0;

def SSlope2 = (getvalue(PL2, FirstSpoint2) - getvalue(PL2, PriorSpoint2))

/ (PLBar2 - PriorPLBar2);

def RExtend2 = if bn2 == highestall(PHBar2)[21]

then 1

else RExtend2[1];

def SExtend2 = if bn2 == highestall(PLBar2)[21]

then 1

else SExtend2[1];



plot pivotHigh2 = if HighPivots2

then PH2

else double.NaN;

pivotHigh2.SetDefaultColor(GetColor(1));

pivotHigh2.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

pivotHigh2.setHiding(!showValues);



plot pivotHighLine2 = if PHL2 > 0 and

HighPivots2

then PHL2

else double.NaN;

pivotHighLine2.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotHighLine2.setDefaultColor(color.uptick); #JQ 7.8.2018 added

pivotHighLine2.setHiding(!showLines);



plot RLine2 = pivotHigh2;

RLine2.enableApproximation();

RLine2.SetDefaultColor(Color.LIGHT_GRAY);

RLine2.SetStyle(Curve.Short_DASH);



# Added code to limit resistance estension line (JQ 03.04.2019)

def calc_ResistanceExtension2 = if RExtend2

then (bn2 - PHBar2) * RSlope2 + PHL2

else double.NaN;

plot line_ResistanceExtension2 = if bn2 <= (Currentbar2 + ExtensionLengthBars)

and calc_ResistanceExtension2[1] >= (lowestall(vLow)[21] * (1-(lowerExtensionPercentLimit/100)))

and calc_ResistanceExtension2[1] <= (Highestall(vHigh)[21] * (1 + (upperExtensionPercentLimit/100)))

then calc_ResistanceExtension2 else double.nan;

line_ResistanceExtension2.SetStyle(Curve.Short_DASH);

line_ResistanceExtension2.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_ResistanceExtension2.setLineWeight(1);



# Low Plots

plot pivotLow2 = if LowPivots2

then PL2

else double.NaN;

pivotLow2.setDefaultColor(GetColor(4));

pivotLow2.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

pivotLow2.setHiding(!showValues);



plot pivotLowLine2 = if PLL2 > 0 and

LowPivots2

then PLL2

else double.NaN;

pivotLowLine2.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotLowLine2.setDefaultColor(color.DOWNTICK);# # JQ 7.8.2018 added

pivotLowLine2.setHiding(!showLines);



plot SupportLine2 = pivotLow2;

SupportLine2.enableApproximation();

SupportLine2.SetDefaultColor(color.LIGHT_GRAY);

SUpportLine2.SetStyle(Curve.Short_DASH);



# Added code to limit support estension line (JQ 03.04.2019)

def calc_SupportExtension2 = if SExtend2

then (bn2 - PLBar2) * SSlope2 + PLL2

else double.NaN;

plot line_SupportExtension2 = if bn2 <= (Currentbar2 + ExtensionLengthBars)

and calc_SupportExtension2[1] >= (lowestall(vLow)[21] * (1-(lowerExtensionPercentLimit/100)))

and calc_SupportExtension2[1] <= (Highestall(vHigh)[21] * (1 + (upperExtensionPercentLimit/100)))

then calc_supportExtension2 else double.nan;

line_SupportExtension2.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_SupportExtension2.SetStyle(Curve.Short_DASH);

line_SupportExtension2.setLineWeight(1);



plot BarNumbersBelow2 = bn2;

BarNumbersBelow2.SetDefaultColor(GetColor(0));

BarNumbersBelow2.setHiding(!showBarNumbers);

BarNumbersBelow2.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);



plot PivotDot2 = if !isNaN(pivotHigh2)

then pivotHigh2

else if !isNaN(pivotLow2)

then pivotLow2

else double.NaN;

pivotDot2.SetDefaultColor(GetColor(7));

pivotDot2.SetPaintingStrategy(PaintingStrategy.POINTS);

pivotDot2.SetLineWeight(3);



# End Code

#---------------------------------------------------------------------------------



def PH3;

def PL3;

def hh3 = fold i3 = 1 to n + 1

with p3 = 42

while p3

do vHigh > getValue(vHigh, -i3);

PH3 = if (bn3 > n3 and

vHigh == highest(vHigh, n3) and

hh3)

then vHigh

else double.NaN;

def ll3 = fold j3 = 1 to n3 + 1

with q3 = 42

while q3

do vLow < getValue(low, -j3);

PL3 = if (bn3 > n and

vLow == lowest(vLow, n) and

ll3)

then vLow

else double.NaN;

def PHBar3 = if !isNaN(PH3)

then bn3

else PHBar3[1];

def PLBar3 = if !isNaN(PL3)

then bn3

else PLBar[1];

def PHL3 = if !isNaN(PH3)

then PH3

else PHL3[1];

def priorPHBar3 = if PHL3 != PHL3[1]

then PHBar3[1]

else priorPHBar3[1];

def PLL3 = if !isNaN(PL3)

then PL3

else PLL3[1];

def priorPLBar3 = if PLL3 != PLL3[1]

then PLBar3[1]

else priorPLBar3[1];

def HighPivots3 = bn3 >= highestAll(priorPHBar3)[42];

def LowPivots3 = bn3 >= highestAll(priorPLBar3)[42];

def FirstRpoint3 = if HighPivots3

then bn3 - PHBar3

else 0;

def PriorRpoint3 = if HighPivots3

then bn3 - PriorPHBar3

else 0;

def RSlope3 = (getvalue(PH3, FirstRpoint3) - getvalue(PH3, PriorRpoint3))

/ (PHBar3 - PriorPHBar3);

def FirstSpoint3 = if LowPivots3

then bn3 - PLBar3

else 0;

def PriorSpoint3 = if LowPivots3

then bn3 - PriorPLBar3

else 0;

def SSlope3 = (getvalue(PL3, FirstSpoint3) - getvalue(PL3, PriorSpoint3))

/ (PLBar3 - PriorPLBar3);

def RExtend3 = if bn3 == highestall(PHBar3)[42]

then 1

else RExtend3[1];

def SExtend3 = if bn3 == highestall(PLBar3)[42]

then 1

else SExtend3[1];



plot pivotHigh3 = if HighPivots3

then PH3

else double.NaN;

pivotHigh3.SetDefaultColor(GetColor(1));

pivotHigh3.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

pivotHigh3.setHiding(!showValues);



plot pivotHighLine3 = if PHL3 > 0 and

HighPivots3

then PHL3

else double.NaN;

pivotHighLine3.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotHighLine3.setDefaultColor(color.uptick); #JQ 7.8.3018 added

pivotHighLine3.setHiding(!showLines);



plot RLine3 = pivotHigh3;

RLine3.enableApproximation();

RLine3.SetDefaultColor(Color.LIGHT_GRAY);

RLine3.SetStyle(Curve.Short_DASH);



# Added code to limit resistance estension line (JQ 03.04.3019)

def calc_ResistanceExtension3 = if RExtend3

then (bn3 - PHBar3) * RSlope3 + PHL3

else double.NaN;

plot line_ResistanceExtension3 = if bn3 <= (Currentbar3 + ExtensionLengthBars)

and calc_ResistanceExtension3[1] >= (lowestall(vLow)[42] * (1-(lowerExtensionPercentLimit/100)))

and calc_ResistanceExtension3[1] <= (Highestall(vHigh)[42] * (1 + (upperExtensionPercentLimit/100)))

then calc_ResistanceExtension3 else double.nan;

line_ResistanceExtension3.SetStyle(Curve.Short_DASH);

line_ResistanceExtension3.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_ResistanceExtension3.setLineWeight(1);



# Low Plots

plot pivotLow3 = if LowPivots3

then PL3

else double.NaN;

pivotLow3.setDefaultColor(GetColor(4));

pivotLow3.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

pivotLow3.setHiding(!showValues);



plot pivotLowLine3 = if PLL3 > 0 and

LowPivots3

then PLL3

else double.NaN;

pivotLowLine3.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotLowLine3.setDefaultColor(color.DOWNTICK);# # JQ 7.8.3018 added

pivotLowLine3.setHiding(!showLines);



plot SupportLine3 = pivotLow3;

SupportLine3.enableApproximation();

SupportLine3.SetDefaultColor(color.LIGHT_GRAY);

SUpportLine3.SetStyle(Curve.Short_DASH);



# Added code to limit support estension line (JQ 03.04.3019)

def calc_SupportExtension3 = if SExtend3

then (bn3 - PLBar3) * SSlope3 + PLL3

else double.NaN;

plot line_SupportExtension3 = if bn3 <= (Currentbar3 + ExtensionLengthBars)

and calc_SupportExtension3[1] >= (lowestall(vLow)[42] * (1-(lowerExtensionPercentLimit/100)))

and calc_SupportExtension3[1] <= (Highestall(vHigh)[42] * (1 + (upperExtensionPercentLimit/100)))

then calc_supportExtension3 else double.nan;

line_SupportExtension3.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_SupportExtension3.SetStyle(Curve.Short_DASH);

line_SupportExtension3.setLineWeight(1);



plot BarNumbersBelow3 = bn3;

BarNumbersBelow3.SetDefaultColor(GetColor(0));

BarNumbersBelow3.setHiding(!showBarNumbers);

BarNumbersBelow3.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);



plot PivotDot3 = if !isNaN(pivotHigh3)

then pivotHigh3

else if !isNaN(pivotLow3)

then pivotLow3

else double.NaN;

pivotDot3.SetDefaultColor(GetColor(7));

pivotDot3.SetPaintingStrategy(PaintingStrategy.POINTS);

pivotDot3.SetLineWeight(3);



# End Code

#---------------------------------------------------------------------------------



def PH4;

def PL4;

def hh4 = fold i4 = 1 to n + 1

with p4 = 63

while p4

do vHigh > getValue(vHigh, -i4);

PH4 = if (bn4 > n4 and

vHigh == highest(vHigh, n4) and

hh4)

then vHigh

else double.NaN;

def ll4 = fold j4 = 1 to n4 + 1

with q4 = 63

while q4

do vLow < getValue(low, -j4);

PL4 = if (bn4 > n and

vLow == lowest(vLow, n) and

ll4)

then vLow

else double.NaN;

def PHBar4 = if !isNaN(PH4)

then bn4

else PHBar4[1];

def PLBar4 = if !isNaN(PL4)

then bn4

else PLBar[1];

def PHL4 = if !isNaN(PH4)

then PH4

else PHL4[1];

def priorPHBar4 = if PHL4 != PHL4[1]

then PHBar4[1]

else priorPHBar4[1];

def PLL4 = if !isNaN(PL4)

then PL4

else PLL4[1];

def priorPLBar4 = if PLL4 != PLL4[1]

then PLBar4[1]

else priorPLBar4[1];

def HighPivots4 = bn4 >= highestAll(priorPHBar4)[63];

def LowPivots4 = bn4 >= highestAll(priorPLBar4)[63];

def FirstRpoint4 = if HighPivots4

then bn4 - PHBar4

else 0;

def PriorRpoint4 = if HighPivots4

then bn4 - PriorPHBar4

else 0;

def RSlope4 = (getvalue(PH4, FirstRpoint4) - getvalue(PH4, PriorRpoint4))

/ (PHBar4 - PriorPHBar4);

def FirstSpoint4 = if LowPivots4

then bn4 - PLBar4

else 0;

def PriorSpoint4 = if LowPivots4

then bn4 - PriorPLBar4

else 0;

def SSlope4 = (getvalue(PL4, FirstSpoint4) - getvalue(PL4, PriorSpoint4))

/ (PLBar4 - PriorPLBar4);

def RExtend4 = if bn4 == highestall(PHBar4)[63]

then 1

else RExtend4[1];

def SExtend4 = if bn4 == highestall(PLBar4)[63]

then 1

else SExtend4[1];



plot pivotHigh4 = if HighPivots4

then PH4

else double.NaN;

pivotHigh4.SetDefaultColor(GetColor(1));

pivotHigh4.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

pivotHigh4.setHiding(!showValues);



plot pivotHighLine4 = if PHL4 > 0 and

HighPivots4

then PHL4

else double.NaN;

pivotHighLine4.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotHighLine4.setDefaultColor(color.uptick); #JQ 7.8.4018 added

pivotHighLine4.setHiding(!showLines);



plot RLine4 = pivotHigh4;

RLine4.enableApproximation();

RLine4.SetDefaultColor(Color.LIGHT_GRAY);

RLine4.SetStyle(Curve.Short_DASH);



# Added code to limit resistance estension line (JQ 04.04.4019)

def calc_ResistanceExtension4 = if RExtend4

then (bn4 - PHBar4) * RSlope4 + PHL4

else double.NaN;

plot line_ResistanceExtension4 = if bn4 <= (Currentbar4 + ExtensionLengthBars)

and calc_ResistanceExtension4[1] >= (lowestall(vLow)[63] * (1-(lowerExtensionPercentLimit/100)))

and calc_ResistanceExtension4[1] <= (Highestall(vHigh)[63] * (1 + (upperExtensionPercentLimit/100)))

then calc_ResistanceExtension4 else double.nan;

line_ResistanceExtension4.SetStyle(Curve.Short_DASH);

line_ResistanceExtension4.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_ResistanceExtension4.setLineWeight(1);



# Low Plots

plot pivotLow4 = if LowPivots4

then PL4

else double.NaN;

pivotLow4.setDefaultColor(GetColor(4));

pivotLow4.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);

pivotLow4.setHiding(!showValues);



plot pivotLowLine4 = if PLL4 > 0 and

LowPivots4

then PLL4

else double.NaN;

pivotLowLine4.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES

pivotLowLine4.setDefaultColor(color.DOWNTICK);# # JQ 7.8.4018 added

pivotLowLine4.setHiding(!showLines);



plot SupportLine4 = pivotLow4;

SupportLine4.enableApproximation();

SupportLine4.SetDefaultColor(color.LIGHT_GRAY);

SUpportLine4.SetStyle(Curve.Short_DASH);



# Added code to limit support estension line (JQ 04.04.4019)

def calc_SupportExtension4 = if SExtend4

then (bn4 - PLBar4) * SSlope4 + PLL4

else double.NaN;

plot line_SupportExtension4 = if bn4 <= (Currentbar4 + ExtensionLengthBars)

and calc_SupportExtension4[1] >= (lowestall(vLow)[63] * (1-(lowerExtensionPercentLimit/100)))

and calc_SupportExtension4[1] <= (Highestall(vHigh)[63] * (1 + (upperExtensionPercentLimit/100)))

then calc_supportExtension4 else double.nan;

line_SupportExtension4.SetDefaultColor(color.LIGHT_GRAY); #was 7

line_SupportExtension4.SetStyle(Curve.Short_DASH);

line_SupportExtension4.setLineWeight(1);



plot BarNumbersBelow4 = bn4;

BarNumbersBelow4.SetDefaultColor(GetColor(0));

BarNumbersBelow4.setHiding(!showBarNumbers);

BarNumbersBelow4.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);



plot PivotDot4 = if !isNaN(pivotHigh4)

then pivotHigh4

else if !isNaN(pivotLow4)

then pivotLow4

else double.NaN;

pivotDot4.SetDefaultColor(GetColor(7));

pivotDot4.SetPaintingStrategy(PaintingStrategy.POINTS);

pivotDot4.SetLineWeight(4);

It's pretty much an extension of your code with additional displacement variables in place to attempt at gaining different segments of the pivots and associated lines. I'd love to have this working because then I could theoretically build a strategy around this to cover an entire day. I'd really appreciate it if you all could take a look and find out where I went wrong here. I also understand it's not dynamic yet either.. I was just trying to get it to work first.

Many thanks!
 
H

horserider

Active member
Warehouse
Seems you would have to define your segments by bar number. Looking back from bar number 100 to 75, 75 to 50 , 50 to 25 and 25 to 0 and then have the script run on just those specified bar numbers. Not something I would want to try. What is the purpose? If you draw enough lines some will surely work, hahaha.
 
S

Saipan

New member
Specifically, I wanted to be able to build a strategy in ToS and test it over the aggregate time frame. For example show when a strategy would have bought and sold over the course of a day by having the full chart within scope.
 

Top