Support/Resistance Zones for ThinkorSwim

Support/Resistance or Supply/Demand whichever you prefer. Based on pivots and ATR. Change the n to get zones for the time you wish.

Update: ATR is now straight lines and user can input ATR multiple.
New share: https://tos.mx/Lv0FsZi

DfssJTt.png


https://tos.mx/VeGhwv
Code:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019

input length = 252;
input averageType = AverageType.WILDERS;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);

# User Inputs
input n = 21; #hint n: periods used for pivot calculations.
def Num_Dev_Dn = ATR;
def Num_Dev_up = -ATR;


# 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 LowerBandr1 = R1 + Num_Dev_Dn ;
plot UpperBandr1 = R1 + Num_Dev_up ;
AddCloud(UpperBandr1, R1, Color.GREEN, Color.RED );
AddCloud(LowerBandr1, R1, Color.GREEN, Color.RED );
lowerbandr1.hide();
upperbandr1.hide();


plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.GREEN, 1);

plot LowerBandr2 = R2 + Num_Dev_Dn ;
plot UpperBandr2 = R2 + Num_Dev_up ;
AddCloud(UpperBandr2, R2, Color.GREEN, Color.RED);
AddCloud(LowerBandr2, R2, Color.GREEN, Color.RED);
lowerbandr2.hide();
upperbandr2.hide();

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.GREEN, 1);

plot LowerBandr3 = R3 + Num_Dev_Dn ;
plot UpperBandr3 = R3 + Num_Dev_up ;
AddCloud(UpperBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbandr3.hide();
upperbandr3.hide();

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
R4.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R4PHBarOrigin), PHValue, "R4", Color.GREEN, 1);

plot LowerBandr4 = R4 + Num_Dev_Dn ;
plot UpperBandr4 = R4 + Num_Dev_up ;
AddCloud(UpperBandr4, R4, Color.LIME, Color.PINK);
AddCloud(LowerBandr4, R4, Color.LIME, Color.PINK);
lowerbandr4.hide();
upperbandr4.hide();


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 LowerBands1 = S1 + Num_Dev_Dn ;
plot UpperBands1 = S1 + Num_Dev_up ;
AddCloud(UpperBands1, S1, Color.GREEN, Color.RED);
AddCloud(LowerBands1, S1, Color.GREEN, Color.RED);
lowerbands1.hide();
upperbands1.hide();

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.RED, 0);

plot LowerBands2 = S2 + Num_Dev_Dn ;
plot UpperBands2 = S2 + Num_Dev_up ;
AddCloud(UpperBands2, S2, Color.GREEN, Color.RED);
AddCloud(LowerBands2, S2, Color.GREEN, Color.RED);
lowerbands2.hide();
upperbands2.hide();

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.RED, 0);

plot LowerBands3 = S3 + Num_Dev_Dn ;
plot UpperBands3 = S3 + Num_Dev_up ;
AddCloud(UpperBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbands3.hide();
upperbands3.hide();


plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
S4.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S4PLBarOrigin), PLValue, "S4", Color.RED, 0);

plot LowerBands4 = S4 + Num_Dev_Dn ;
plot UpperBands4 = S4 + Num_Dev_up ;
AddCloud(UpperBands4, S4, Color.LIME, Color.PINK);
AddCloud(LowerBands4, S4, Color.LIME, Color.PINK);
lowerbands4.hide();
upperbands4.hide();

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
where can i find only the red/yellow/green candlestick chart indicator
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Greetings. I'm new to this forum but not to trading and TOS. I just wanted to say "thanks" for automating the Support and Resistance lines. I do have some questions, naturally.
1) Is there a way to remove the red and green boxes? I would be very happy with just the bubbles showing S1 and R1.
2) If I were to set my charts to Daily 1 year and Daily 10 years, what should the "n" factor be? And the same for 5 minutes charts?
Thank you for this supply/demand zone indicator!

What is the purpose of those red/green dots at the close of each candle? Is there a way to turn them OFF? Or, which code to # out? I have read all posts in this thread, but have not seen any reply in how to turn the dots OFF.
 
@Shaco I don't see any red/green dots at the close of each candle. For someone to be able to troubleshoot further:
  1. Is it possible that you have an additional indicator loaded on your chart that is plotting the dots?
  2. Could you cut and paste the script that contains the dots?
  3. Could you post a screenshot that displays the dots. Please do not provide a "zoomed" in image. A shot of the whole chart is best when asking questions.
Unsure of how to upload screenshots to the forum, Here are directions.
 
@Shaco I don't see any red/green dots at the close of each candle. For someone to be able to troubleshoot further:
  1. Is it possible that you have an additional indicator loaded on your chart that is plotting the dots?
  2. Could you cut and paste the script that contains the dots?
  3. Could you post a screenshot that displays the dots. Please do not provide a "zoomed" in image. A shot of the whole chart is best when asking questions.
Unsure of how to upload screenshots to the forum, Here are directions.
Hi @MerryDay, I just copy and paste the code from post #1 and it is the only study on my chart, which looks like this:

yrBhQ42.png


There are green dots at each candle close. My Lenovo laptop is slow and the CPU throttles, so I'm trying to remove paintings that are not needed. If there are other tips to improve chart speed when zooming or changing ticker, please share.
 
Last edited by a moderator:
@Shaco Sorry, as I said, I am not seeing any dots in post#1, that is why we ask for posts to include the script that you are using. Maybe something got changed. Or it is a different study. No one will be able to assist further without seeing the code.
 
@Shaco Sorry, as I said, I am not seeing any dots in post#1, that is why we ask for posts to include the script that you are using. Maybe something got changed. Or it is a different study. No one will be able to assist further without seeing the code.
Sorry for being a pain. I just re-copied and pasted the codes from post #1 again into my study just to be sure, and it is still showing the dots. Here are the codes currrently in my study (which is the exact same codes as post #1):

Code:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019

input length = 252;
input averageType = AverageType.WILDERS;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);

# User Inputs
input n = 21; #hint n: periods used for pivot calculations.
def Num_Dev_Dn = ATR;
def Num_Dev_up = -ATR;


# 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 LowerBandr1 = R1 + Num_Dev_Dn ;
plot UpperBandr1 = R1 + Num_Dev_up ;
AddCloud(UpperBandr1, R1, Color.GREEN, Color.RED );
AddCloud(LowerBandr1, R1, Color.GREEN, Color.RED );
lowerbandr1.hide();
upperbandr1.hide();


plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.GREEN, 1);

plot LowerBandr2 = R2 + Num_Dev_Dn ;
plot UpperBandr2 = R2 + Num_Dev_up ;
AddCloud(UpperBandr2, R2, Color.GREEN, Color.RED);
AddCloud(LowerBandr2, R2, Color.GREEN, Color.RED);
lowerbandr2.hide();
upperbandr2.hide();

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.GREEN, 1);

plot LowerBandr3 = R3 + Num_Dev_Dn ;
plot UpperBandr3 = R3 + Num_Dev_up ;
AddCloud(UpperBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbandr3.hide();
upperbandr3.hide();

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
R4.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R4PHBarOrigin), PHValue, "R4", Color.GREEN, 1);

plot LowerBandr4 = R4 + Num_Dev_Dn ;
plot UpperBandr4 = R4 + Num_Dev_up ;
AddCloud(UpperBandr4, R4, Color.LIME, Color.PINK);
AddCloud(LowerBandr4, R4, Color.LIME, Color.PINK);
lowerbandr4.hide();
upperbandr4.hide();


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 LowerBands1 = S1 + Num_Dev_Dn ;
plot UpperBands1 = S1 + Num_Dev_up ;
AddCloud(UpperBands1, S1, Color.GREEN, Color.RED);
AddCloud(LowerBands1, S1, Color.GREEN, Color.RED);
lowerbands1.hide();
upperbands1.hide();

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.RED, 0);

plot LowerBands2 = S2 + Num_Dev_Dn ;
plot UpperBands2 = S2 + Num_Dev_up ;
AddCloud(UpperBands2, S2, Color.GREEN, Color.RED);
AddCloud(LowerBands2, S2, Color.GREEN, Color.RED);
lowerbands2.hide();
upperbands2.hide();

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.RED, 0);

plot LowerBands3 = S3 + Num_Dev_Dn ;
plot UpperBands3 = S3 + Num_Dev_up ;
AddCloud(UpperBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbands3.hide();
upperbands3.hide();


plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
S4.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S4PLBarOrigin), PLValue, "S4", Color.RED, 0);

plot LowerBands4 = S4 + Num_Dev_Dn ;
plot UpperBands4 = S4 + Num_Dev_up ;
AddCloud(UpperBands4, S4, Color.LIME, Color.PINK);
AddCloud(LowerBands4, S4, Color.LIME, Color.PINK);
lowerbands4.hide();
upperbands4.hide();

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
 
@Shaco I loaded the script that you posted still no dots.
Screenshot (149).png

:unsure: I am determined to fix this so hang with me.

I want you to share link your chart. Click on the button that looks like:
Screenshot (85).png

Create the link and post the code it gives you.
 
@Shaco
Got it! 🥳
The DOTS are now hidden.
Ruby:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019

input length = 252;
input averageType = AverageType.WILDERS;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);

# User Inputs
input n = 21; #hint n: periods used for pivot calculations.
def Num_Dev_Dn = ATR;
def Num_Dev_up = -ATR;


# 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 LowerBandr1 = R1 + Num_Dev_Dn ;
plot UpperBandr1 = R1 + Num_Dev_up ;
AddCloud(UpperBandr1, R1, Color.GREEN, Color.RED );
AddCloud(LowerBandr1, R1, Color.GREEN, Color.RED );
lowerbandr1.hide();
upperbandr1.hide();


plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.GREEN, 1);

plot LowerBandr2 = R2 + Num_Dev_Dn ;
plot UpperBandr2 = R2 + Num_Dev_up ;
AddCloud(UpperBandr2, R2, Color.GREEN, Color.RED);
AddCloud(LowerBandr2, R2, Color.GREEN, Color.RED);
lowerbandr2.hide();
upperbandr2.hide();

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.GREEN);
R3.hide();
#AddChartBubble(bar == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.GREEN, 1);

plot LowerBandr3 = R3 + Num_Dev_Dn ;
plot UpperBandr3 = R3 + Num_Dev_up ;
AddCloud(UpperBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbandr3.hide();
upperbandr3.hide();

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
R4.SetDefaultColor(Color.GREEN);
R4.hide();
#AddChartBubble(bar == HighestAll(R4PHBarOrigin), PHValue, "R4", Color.GREEN, 1);

plot LowerBandr4 = R4 + Num_Dev_Dn ;
plot UpperBandr4 = R4 + Num_Dev_up ;
AddCloud(UpperBandr4, R4, Color.LIME, Color.PINK);
AddCloud(LowerBandr4, R4, Color.LIME, Color.PINK);
lowerbandr4.hide();
upperbandr4.hide();


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 LowerBands1 = S1 + Num_Dev_Dn ;
plot UpperBands1 = S1 + Num_Dev_up ;
AddCloud(UpperBands1, S1, Color.GREEN, Color.RED);
AddCloud(LowerBands1, S1, Color.GREEN, Color.RED);
lowerbands1.hide();
upperbands1.hide();

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.RED, 0);

plot LowerBands2 = S2 + Num_Dev_Dn ;
plot UpperBands2 = S2 + Num_Dev_up ;
AddCloud(UpperBands2, S2, Color.GREEN, Color.RED);
AddCloud(LowerBands2, S2, Color.GREEN, Color.RED);
lowerbands2.hide();
upperbands2.hide();

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.RED);
S3.hide();
#AddChartBubble(bar == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.RED, 0);

plot LowerBands3 = S3 + Num_Dev_Dn ;
plot UpperBands3 = S3 + Num_Dev_up ;
AddCloud(UpperBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbands3.hide();
upperbands3.hide();


plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
S4.SetDefaultColor(Color.RED);
S4.hide();
#AddChartBubble(bar == HighestAll(S4PLBarOrigin), PLValue, "S4", Color.RED, 0);

plot LowerBands4 = S4 + Num_Dev_Dn ;
plot UpperBands4 = S4 + Num_Dev_up ;
AddCloud(UpperBands4, S4, Color.LIME, Color.PINK);
AddCloud(LowerBands4, S4, Color.LIME, Color.PINK);
lowerbands4.hide();
upperbands4.hide();

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
 
@Shaco No need for code changes. Just go in settings and change the display of the pivot points from boolean to numerical. Would be the r1, r2,r3,r4 and also s points , they should be numerical.
 
hi All,
I am trying to modify the script I found here. Idea it to stop painting the supply and demand zones after the zone is breached. The code seems big but it just doing repeated code for 10 zones.

Thanks in advance.

_J9eW92gQThRlSVfk-XzfG23dHjmyyo9s_Lo_hO9RmB0gaJ13kJg_q534eAOGLCl8XOLyLAAA-Q7MvQsy2xPo2rqG49_EGwgAbW0nfkrhjuRTItHPFW7hsK9E--DUEu2YBOfJrnpp19qAl62LDMgw4lkhxC2d_3HTLg0DvGZWmAs1aTb5ahlmi4yFMJEyNHhCCJsYKx71Sf4HMf1FfnarTunwz9E-cbb-c_iQiVQ9Qc2-ErYzoHX58uSr_kpan0Y8wTxi60yAdmywJxPZDSoqJ3wa-NhFAmQ2f3RM5AXuyd9eI08hgo3JW7rRXoIOgEiXW5muamVc72e_bjTYo8_csAV-hbcxDoVOt5iV5uOUehRg3whUFsawAnlSTsiS0NP3J3dO8jAGkRYXK6moyaYM8vFmbx_v1G6FMDUH9IaJncwxOLj8Q12kY7pz_raXZG0o_gdFDwJ5lYKqoIClViiMdlAv1569leJeYz9IyASU5tsfUb_YIYvMrNhnNu7i5V8Erts6GmwyAUjVcLexoR8wFQpOIc2XUXg7CbMhCdIUPdGoJUjEQZGTkvdk8bu5sBSJse18f9I7RBAa-CScu4mTI-IEMdL4HrCgLqN9riihPRVkDBUMYG0hKkCNyp-osGso6WnqF785h2496jhicSwMZGGI5s5yldC5ZeqVTeS767PQOLGLjk638NnIADgML6L80sDOn-wjS4-WpYzBQ3o6z4=w2296-h1228-no



# Support/Resistance Zones around pivot S/R points. #Added the zones using ATR to the Theotrade Pivots study. #Additions by Horserider 9/30/2019 input length = 52; input averageType = AverageType.WILDERS; def ATR = MovingAverage(averageType, TrueRange(high, close, low), length); # User Inputs input n = 4; #hint n: periods used for pivot calculations. def Num_Dev_Dn = ATR/3; def Num_Dev_up = -ATR/3; # 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 ChartAggMin = getAggregationPeriod() / 1000 / 60; #addlabel(1,ChartAggMin); # Variables def o = open; def bh = max(open,close); def bl = min(open,close); def h = high; def l = low; def c = close; def bar = BarNumber(); def BBar = bar == HighestAll(bar); DefineGlobalColor("ss1", Color.CYAN); DefineGlobalColor("dd1", Color.MAGENTA); DefineGlobalColor("ss2", Color.CYAN); DefineGlobalColor("dd2", Color.MAGENTA); DefineGlobalColor("ss3", Color.CYAN); DefineGlobalColor("dd3", Color.MAGENTA); DefineGlobalColor("ss4", Color.CYAN); DefineGlobalColor("dd4", Color.MAGENTA); DefineGlobalColor("ss5", Color.CYAN); DefineGlobalColor("dd5", Color.MAGENTA); DefineGlobalColor("ss6", Color.CYAN); DefineGlobalColor("dd6", Color.MAGENTA); DefineGlobalColor("ss7", Color.CYAN); DefineGlobalColor("dd7", Color.MAGENTA); DefineGlobalColor("ss8", Color.CYAN); DefineGlobalColor("dd8", Color.MAGENTA); DefineGlobalColor("ss9", Color.CYAN); DefineGlobalColor("dd9", Color.MAGENTA); DefineGlobalColor("ss0", Color.CYAN); DefineGlobalColor("dd0", Color.MAGENTA); # 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; # R5 def R5PHValue = if R4PHBarOrigin != R4PHBarOrigin[1] then R4PHValue[1] else R5PHValue[1]; def R5PHBarOrigin = if R4PHBarOrigin != R4PHBarOrigin[1] then R4PHBarOrigin[1] else R5PHBarOrigin[1]; def R5PHBarID = bar - R5PHBarOrigin; # R6 def R6PHValue = if R5PHBarOrigin != R5PHBarOrigin[1] then R5PHValue[1] else R6PHValue[1]; def R6PHBarOrigin = if R5PHBarOrigin != R5PHBarOrigin[1] then R5PHBarOrigin[1] else R6PHBarOrigin[1]; def R6PHBarID = bar - R6PHBarOrigin; # R7 def R7PHValue = if R6PHBarOrigin != R6PHBarOrigin[1] then R6PHValue[1] else R7PHValue[1]; def R7PHBarOrigin = if R6PHBarOrigin != R6PHBarOrigin[1] then R6PHBarOrigin[1] else R7PHBarOrigin[1]; def R7PHBarID = bar - R7PHBarOrigin; # R8 def R8PHValue = if R7PHBarOrigin != R7PHBarOrigin[1] then R7PHValue[1] else R8PHValue[1]; def R8PHBarOrigin = if R7PHBarOrigin != R7PHBarOrigin[1] then R7PHBarOrigin[1] else R8PHBarOrigin[1]; def R8PHBarID = bar - R8PHBarOrigin; # R9 def R9PHValue = if R8PHBarOrigin != R8PHBarOrigin[1] then R8PHValue[1] else R9PHValue[1]; def R9PHBarOrigin = if R8PHBarOrigin != R8PHBarOrigin[1] then R8PHBarOrigin[1] else R9PHBarOrigin[1]; def R9PHBarID = bar - R9PHBarOrigin; # R10 def R0PHValue = if R9PHBarOrigin != R9PHBarOrigin[1] then R9PHValue[1] else R0PHValue[1]; def R0PHBarOrigin = if R9PHBarOrigin != R9PHBarOrigin[1] then R9PHBarOrigin[1] else R0PHBarOrigin[1]; def R0PHBarID = bar - R0PHBarOrigin; # 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; # S5 def S5PLValue = if S4PLBarOrigin != S4PLBarOrigin[1] then S4PLValue[1] else S5PLValue[1]; def S5PLBarOrigin = if S4PLBarOrigin != S4PLBarOrigin[1] then S4PLBarOrigin[1] else S5PLBarOrigin[1]; def S5PLBarID = bar - S5PLBarOrigin; # S6 def S6PLValue = if S5PLBarOrigin != S5PLBarOrigin[1] then S5PLValue[1] else S6PLValue[1]; def S6PLBarOrigin = if S5PLBarOrigin != S5PLBarOrigin[1] then S5PLBarOrigin[1] else S6PLBarOrigin[1]; def S6PLBarID = bar - S6PLBarOrigin; # S7 def S7PLValue = if S6PLBarOrigin != S6PLBarOrigin[1] then S6PLValue[1] else S7PLValue[1]; def S7PLBarOrigin = if S6PLBarOrigin != S6PLBarOrigin[1] then S6PLBarOrigin[1] else S7PLBarOrigin[1]; def S7PLBarID = bar - S7PLBarOrigin; # S8 def S8PLValue = if S7PLBarOrigin != S7PLBarOrigin[1] then S7PLValue[1] else S8PLValue[1]; def S8PLBarOrigin = if S7PLBarOrigin != S7PLBarOrigin[1] then S7PLBarOrigin[1] else S8PLBarOrigin[1]; def S8PLBarID = bar - S8PLBarOrigin; # S9 def S9PLValue = if S8PLBarOrigin != S8PLBarOrigin[1] then S8PLValue[1] else S9PLValue[1]; def S9PLBarOrigin = if S8PLBarOrigin != S8PLBarOrigin[1] then S8PLBarOrigin[1] else S9PLBarOrigin[1]; def S9PLBarID = bar - S9PLBarOrigin; # S10 def S0PLValue = if S9PLBarOrigin != S9PLBarOrigin[1] then S9PLValue[1] else S0PLValue[1]; def S0PLBarOrigin = if S9PLBarOrigin != S9PLBarOrigin[1] then S9PLBarOrigin[1] else S0PLBarOrigin[1]; def S0PLBarID = bar - S0PLBarOrigin; # 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 LowerBandr1 = if C <= (R1 + Num_Dev_up) then (R1 ) else Double.NaN; plot UpperBandr1 = if C <= (R1 + Num_Dev_up) then (R1 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr1, R1, GlobalColor("ss1"), GlobalColor("dd1")); AddCloud(LowerBandr1, R1, GlobalColor("ss1"), GlobalColor("dd1")); lowerbandr1.hide(); upperbandr1.hide(); plot R2 = LinePlot(BarID = R2PHBarID, Value = R2PHValue, BarOrigin = R2PHBarOrigin); R2.SetDefaultColor(Color.GREEN); #AddChartBubble(bar == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.GREEN, 1); plot LowerBandr2 = if C <= (R2 + Num_Dev_up) then (R2 ) else Double.NaN; plot UpperBandr2 = if C <= (R2 + Num_Dev_up) then (R2 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr2, R2, GlobalColor("ss2"), GlobalColor("dd2")); AddCloud(LowerBandr2, R2, GlobalColor("ss2"), GlobalColor("dd2")); lowerbandr2.hide(); upperbandr2.hide(); plot R3 = LinePlot(BarID = R3PHBarID, Value = R3PHValue, BarOrigin = R3PHBarOrigin); R3.SetDefaultColor(Color.GREEN); R3.hide(); #AddChartBubble(bar == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.GREEN, 1); plot LowerBandr3 = if C <= (R3 + Num_Dev_up) then (R3 ) else Double.NaN; plot UpperBandr3 = if C <= (R3 + Num_Dev_up) then (R3 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr3, R3, GlobalColor("ss3"), GlobalColor("dd3")); AddCloud(LowerBandr3, R3, GlobalColor("ss3"), GlobalColor("dd3")); lowerbandr3.hide(); upperbandr3.hide(); plot R4 = LinePlot(BarID = R4PHBarID, Value = R4PHValue, BarOrigin = R4PHBarOrigin); R4.SetDefaultColor(Color.GREEN); R4.hide(); #AddChartBubble(bar == HighestAll(R4PHBarOrigin), PHValue, "R4", Color.GREEN, 1); plot LowerBandr4 = if C <= (R4 + Num_Dev_up) then (R4 ) else Double.NaN; plot UpperBandr4 = if C <= (R4 + Num_Dev_up) then (R4 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr4, R4, GlobalColor("ss4"), GlobalColor("dd4")); AddCloud(LowerBandr4, R4, GlobalColor("ss4"), GlobalColor("dd4")); lowerbandr4.hide(); upperbandr4.hide(); plot R5 = LinePlot(BarID = R5PHBarID, Value = R5PHValue, BarOrigin = R5PHBarOrigin); R5.SetDefaultColor(Color.GREEN); R5.hide(); #AddChartBubble(bar == HighestAll(R5PHBarOrigin), PHValue, "R5", Color.GREEN, 1); plot LowerBandr5 = if C <= (R5 + Num_Dev_up) then (R5 ) else Double.NaN; plot UpperBandr5 = if C <= (R5 + Num_Dev_up) then (R5 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr5, R5, GlobalColor("ss5"), GlobalColor("dd5")); AddCloud(LowerBandr5, R5, GlobalColor("ss5"), GlobalColor("dd5")); lowerbandr5.hide(); upperbandr5.hide(); plot R6 = LinePlot(BarID = R6PHBarID, Value = R6PHValue, BarOrigin = R6PHBarOrigin); R6.SetDefaultColor(Color.GREEN); R6.hide(); #AddChartBubble(bar == HighestAll(R5PHBarOrigin), PHValue, "R5", Color.GREEN, 1); plot LowerBandr6 = if C <= (R6 + Num_Dev_up) then (R6 ) else Double.NaN; plot UpperBandr6 = if C <= (R6 + Num_Dev_up) then (R6 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr6, R6, GlobalColor("ss6"), GlobalColor("dd6")); AddCloud(LowerBandr6, R6, GlobalColor("ss6"), GlobalColor("dd6")); lowerbandr6.hide(); upperbandr6.hide(); 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 LowerBands1 = if c >= (S1 + Num_Dev_Dn)then (S1 + Num_Dev_Dn) else Double.NaN; plot UpperBands1 = if c >= (S1 + Num_Dev_Dn)then (S1 ) else Double.NaN; AddCloud(UpperBands1, S1, GlobalColor("ss1"), GlobalColor("dd1")); AddCloud(LowerBands1, S1, GlobalColor("ss1"), GlobalColor("dd1")); lowerbands1.hide(); upperbands1.hide(); plot S2 = LinePlot(BarID = S2PLBarID, Value = S2PLValue, BarOrigin = S2PLBarOrigin); S2.SetDefaultColor(Color.RED); #AddChartBubble(bar == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.RED, 0); plot LowerBands2 = if c >= (S2 + Num_Dev_Dn)then (S2 + Num_Dev_Dn) else Double.NaN; plot UpperBands2 = if c >= (S2 + Num_Dev_Dn)then (S2 ) else Double.NaN; AddCloud(UpperBands2, S2, GlobalColor("ss2"), GlobalColor("dd2")); AddCloud(LowerBands2, S2, GlobalColor("ss2"), GlobalColor("dd2")); lowerbands2.hide(); upperbands2.hide(); plot S3 = LinePlot(BarID = S3PLBarID, Value = S3PLValue, BarOrigin = S3PLBarOrigin); S3.SetDefaultColor(Color.RED); S3.hide(); #AddChartBubble(bar == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.RED, 0); plot LowerBands3 = if c >= (S3 + Num_Dev_Dn)then (S3 + Num_Dev_Dn) else Double.NaN; plot UpperBands3 = if c >= (S3 + Num_Dev_Dn)then (S3 ) else Double.NaN; AddCloud(UpperBands3, S3, GlobalColor("ss3"), GlobalColor("dd3")); AddCloud(LowerBands3, S3, GlobalColor("ss3"), GlobalColor("dd3")); lowerbands3.hide(); upperbands3.hide(); plot S4 = LinePlot(BarID = S4PLBarID, Value = S4PLValue, BarOrigin = S4PLBarOrigin); S4.SetDefaultColor(Color.RED); S4.hide(); #AddChartBubble(bar == HighestAll(S4PLBarOrigin), PLValue, "S4", Color.RED, 0); plot LowerBands4 = if c >= (S4 + Num_Dev_Dn)then (S4 + Num_Dev_Dn) else Double.NaN; plot UpperBands4 = if c >= (S4 + Num_Dev_Dn)then (S4 ) else Double.NaN; AddCloud(UpperBands4, S4, GlobalColor("ss4"), GlobalColor("dd4")); AddCloud(LowerBands4, S4, GlobalColor("ss4"), GlobalColor("dd4"));; lowerbands4.hide(); upperbands4.hide(); plot S5 = LinePlot(BarID = S5PLBarID, Value = S5PLValue, BarOrigin = S5PLBarOrigin); S5.SetDefaultColor(Color.RED); S5.hide(); #AddChartBubble(bar == HighestAll(S5PLBarOrigin), PLValue, "S5", Color.RED, 0); plot LowerBands5 = if c >= (S5 + Num_Dev_Dn)then (S5 + Num_Dev_Dn) else Double.NaN; plot UpperBands5 = if c >= (S5 + Num_Dev_Dn)then (S5 ) else Double.NaN; AddCloud(UpperBands5, S5, GlobalColor("ss5"), GlobalColor("dd5")); AddCloud(LowerBands5, S5, GlobalColor("ss5"), GlobalColor("dd5")); lowerbands5.hide(); upperbands5.hide(); plot S6 = LinePlot(BarID = S6PLBarID, Value = S6PLValue, BarOrigin = S6PLBarOrigin); S6.SetDefaultColor(Color.RED); S6.hide(); plot LowerBands6 = if c >= (S6 + Num_Dev_Dn)then (S6 + Num_Dev_Dn) else Double.NaN; plot UpperBands6 = if c >= (S6 + Num_Dev_Dn)then (S6 ) else Double.NaN; AddCloud(UpperBands6, S6, GlobalColor("ss6"), GlobalColor("dd6")); AddCloud(LowerBands6, S6, GlobalColor("ss6"), GlobalColor("dd6")); lowerbands6.hide(); upperbands6.hide(); plot R7 = LinePlot(BarID = R7PHBarID, Value = R7PHValue, BarOrigin = R7PHBarOrigin); R7.SetDefaultColor(Color.GREEN); R7.hide(); #AddChartBubble(bar == HighestAll(R5PHBarOrigin), PHValue, "R5", Color.GREEN, 1); plot LowerBandr7 = if C <= (R7 + Num_Dev_up) then (R7 ) else Double.NaN; plot UpperBandr7 = if C <= (R7 + Num_Dev_up) then (R7 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr7, R7, GlobalColor("ss7"), GlobalColor("dd7")); AddCloud(LowerBandr7, R7, GlobalColor("ss7"), GlobalColor("dd7")); lowerbandr7.hide(); upperbandr7.hide(); plot S7 = LinePlot(BarID = S7PLBarID, Value = S7PLValue, BarOrigin = S7PLBarOrigin); S7.SetDefaultColor(Color.RED); S7.hide(); plot LowerBands7 = if c >= (S7 + Num_Dev_Dn)then (S7 + Num_Dev_Dn) else Double.NaN; plot UpperBands7 = if c >= (S7 + Num_Dev_Dn)then (S7 ) else Double.NaN; AddCloud(UpperBands7, S7, GlobalColor("ss7"), GlobalColor("dd7")); AddCloud(LowerBands7, S7, GlobalColor("ss7"), GlobalColor("dd7")); lowerbands7.hide(); upperbands7.hide(); plot R8 = LinePlot(BarID = R8PHBarID, Value = R8PHValue, BarOrigin = R8PHBarOrigin); R8.SetDefaultColor(Color.GREEN); R8.hide(); #AddChartBubble(bar == HighestAll(R5PHBarOrigin), PHValue, "R5", Color.GREEN, 1); plot LowerBandr8 = if C <= (R8 + Num_Dev_up) then (R8 ) else Double.NaN; plot UpperBandr8 = if C <= (R8 + Num_Dev_up) then (R8 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr8, R8, GlobalColor("ss8"), GlobalColor("dd8")); AddCloud(LowerBandr8, R8, GlobalColor("ss8"), GlobalColor("dd8")); lowerbandr8.hide(); upperbandr8.hide(); plot S8 = LinePlot(BarID = S8PLBarID, Value = S8PLValue, BarOrigin = S8PLBarOrigin); S8.SetDefaultColor(Color.RED); S8.hide(); plot LowerBands8 = if c >= (S8 + Num_Dev_Dn)then (S8 + Num_Dev_Dn) else Double.NaN; plot UpperBands8 = if c >= (S8 + Num_Dev_Dn)then (S8 ) else Double.NaN; AddCloud(UpperBands8, S8, GlobalColor("ss8"), GlobalColor("dd8")); AddCloud(LowerBands8, S8, GlobalColor("ss8"), GlobalColor("dd8")); lowerbands8.hide(); upperbands8.hide(); plot R9 = LinePlot(BarID = R9PHBarID, Value = R9PHValue, BarOrigin = R9PHBarOrigin); R9.SetDefaultColor(Color.GREEN); R9.hide(); #AddChartBubble(bar == HighestAll(R5PHBarOrigin), PHValue, "R5", Color.GREEN, 1); plot LowerBandr9 = if C <= (R9 + Num_Dev_up) then (R9 ) else Double.NaN; plot UpperBandr9 = if C <= (R9 + Num_Dev_up) then (R9 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr9, R9, GlobalColor("ss9"), GlobalColor("dd9")); AddCloud(LowerBandr9, R9, GlobalColor("ss9"), GlobalColor("dd9")); lowerbandr9.hide(); upperbandr9.hide(); plot S9 = LinePlot(BarID = S9PLBarID, Value = S9PLValue, BarOrigin = S9PLBarOrigin); S9.SetDefaultColor(Color.RED); S9.hide(); plot LowerBands9 = if c >= (S9 + Num_Dev_Dn)then (S9 + Num_Dev_Dn) else Double.NaN; plot UpperBands9 = if c >= (S9 + Num_Dev_Dn)then (S9 ) else Double.NaN; AddCloud(UpperBands9, S9, GlobalColor("ss9"), GlobalColor("dd9")); AddCloud(LowerBands9, S9, GlobalColor("ss9"), GlobalColor("dd9")); lowerbands9.hide(); upperbands9.hide(); plot R0 = LinePlot(BarID = R0PHBarID, Value = R0PHValue, BarOrigin = R0PHBarOrigin); R0.SetDefaultColor(Color.GREEN); R0.hide(); #AddChartBubble(bar == HighestAll(R5PHBarOrigin), PHValue, "R5", Color.GREEN, 1); plot LowerBandr0 = if C <= (R0 + Num_Dev_up) then (R0 ) else Double.NaN; plot UpperBandr0 = if C <= (R0 + Num_Dev_up) then (R0 + Num_Dev_up) else Double.NaN; AddCloud(UpperBandr0, R0, GlobalColor("ss0"), GlobalColor("dd0")); AddCloud(LowerBandr0, R0, GlobalColor("ss0"), GlobalColor("dd0")); lowerbandr0.hide(); upperbandr0.hide(); #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; #} script LinePlot2 { 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 if Close >= HighestAll(ValueLine) then HighestAll(ValueLine) else Double.Nan else Double.NaN; } addlabel(1,S9PLBarID); #188 addlabel(1,S0PLBarID); #199 addlabel(1,S0PLValue); #364.29 addlabel(1,S0PLBarOrigin); #4507 addlabel(1,HighestAll(S0PLBarOrigin)); #4507 def s0currentHigh = S0PLValue; def isHigherThanNextBars = fold s0i = 1 to 100 with s0p = 1 while s0p do s0currentHigh < getValue(close,-s0i); addlabel(1,isHigherThanNextBars); #4507 plot S0 = LinePlot(BarID = S0PLBarID, Value = S0PLValue, BarOrigin = S0PLBarOrigin); S0.SetDefaultColor(Color.magenta); #S0.hide(); plot LowerBands0 = if c >= (S0 + Num_Dev_Dn) then (S0 + Num_Dev_Dn) else Double.NaN; plot UpperBands0 = if c >= (S0 + Num_Dev_Dn) then (S0 ) else Double.NaN; AddCloud(UpperBands0, S0, GlobalColor("ss0"), GlobalColor("dd0")); AddCloud(LowerBands0, S0, GlobalColor("ss0"), GlobalColor("dd0")); lowerbands0.hide(); upperbands0.hide(); 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; Alert(condition = BearScan, text = "Sell", "alert type" = Alert.BAR, sound = Sound.Chimes); Alert(condition = BullScan, text = "Buy", "alert type" = Alert.BAR, sound = Sound.Chimes); # End Code Fractal Array
 
Well, without reading thorough all of that (whew!) I'll offer this: create another series when your initial condition is met. make it = 1 while the price is under the limit and 0 when the price is above. Then in your plot, use an AND to check whether the below is == 1 to plot and ==0 to ignore.

Code:
def condition = buy condition == 1 not buy == 0;
def limit = if condition == 1 then close + 5;
def is_below_limit = if condition then 1 
    else if close crosses above limit then 0 
    else is_below_limit[1];
plot limit_line = if is_below_limit == 1 then limit else double.nan;
THAT IS NOT REAL CODE... you'll have to adapt it to your use case above, but it should get you going in the right direction

-mashume
 
Well, without reading thorough all of that (whew!) I'll offer this: create another series when your initial condition is met. make it = 1 while the price is under the limit and 0 when the price is above. Then in your plot, use an AND to check whether the below is == 1 to plot and ==0 to ignore.

Code:
def condition = buy condition == 1 not buy == 0;
def limit = if condition == 1 then close + 5;
def is_below_limit = if condition then 1
    else if close crosses above limit then 0
    else is_below_limit[1];
plot limit_line = if is_below_limit == 1 then limit else double.nan;
THAT IS NOT REAL CODE... you'll have to adapt it to your use case above, but it should get you going in the right direction

-mashume
Thank you mashume for the quick response.

I actually did what you mentioned. Here is the section of the code. When the close price is less than zone, then I assign double.nan. This works and the zone disappears. But once the close price crosses up zone again, then it repaints (oval in the image). I am trying not to avoid repaint it once the zone is broken. I consider the zone broken on Jan 25 .


OeXij1PYvKv-4ipKV8f7uOxBcaQk3oT8oBOmz2ozs3xaPW42tXWFdXjcqGMi5HGwwx_ytjOVE8dDgqu4L7Q4iHYJJ3rlbgKhfdCGJSW8ihPDN-uzWvgxQ58s58yxc5hsgf1ANbQs_SVR-10JXTnkz5Gd84arhWEapp5Mkb217xNyEbew-Y29IeWBpROAnMfcoWeDkOxqNWxD07hjkDJ13jsFH8hlRQ7mS4Mt-d2YGDRsyRH5Qxwcr6VkbgfmbxMvNUbHv_m5Mt_60w32NXJP5FGrVa0gGo17cJbI47EtmtQajz9XJ1BcEzPib1H583gR15l0VmXZdxn8U9FfgMhQOj5tDYL6D_j-vZ4jeDQ0zM2bTqh4MWZ8c-u1yKv9AVMptr5jzecyOLzVVsHKRj-5mhAUVAJHLf6QBQHtd1sw0KXB0YfwcfPLW7aV7sCKuCYVVDqpew8p_GkVXqJn4FT7wBu9FlkvXR61Fhl8_7XH-qx2e_GMW9WLiwDBEOZ55mL2ygdll8nWFlIvXVr9Jf0F_svBZ3uwOhewC02M2AcTBKiTyxmtwuLtDvBktcM4cVgIEaqAQ1X1wJu2kLaITD7UC28RzE3iM4myg50r5C7W81gYoIZ9Mn1J3C7KQE4aYbxCVcRJmLB_UyWE6S3uKLN8VzTSHVZLfU1YC7WvyPPGCL5X3DsPew-pk5ruiylOoJ64FWz-QwDCw90Iu1zTJcQ4m20=w384-h255-no


Code:
plot S0 = LinePlot(BarID = S0PLBarID,
                   Value = S0PLValue,
                   BarOrigin = S0PLBarOrigin);
S0.SetDefaultColor(Color.magenta);
#S0.hide();

plot LowerBands0 = if c >= (S0 + Num_Dev_Dn) then (S0 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands0 = if c >= (S0 + Num_Dev_Dn) then (S0             ) else  Double.NaN;
AddCloud(UpperBands0, S0, GlobalColor("ss0"), GlobalColor("dd0"));
AddCloud(LowerBands0, S0, GlobalColor("ss0"), GlobalColor("dd0"));
lowerbands0.hide();
upperbands0.hide();


I tried to reduce the code set to only 4 zones. If you see when I run it on BA and daily timeframe, the middle cyan zone disappears on Jan 26 but returns back on Feb 1 - as price is above the zone now. I an trying to avoid the zone getting painted after Feb 1st.

Code:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019

input length = 52;
input averageType = AverageType.WILDERS;


def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);

# User Inputs
input n = 4; #hint n: periods used for pivot calculations.
def Num_Dev_Dn = ATR/3;
def Num_Dev_up = -ATR/3;



# 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 ChartAggMin = getAggregationPeriod() / 1000 / 60;
#addlabel(1,ChartAggMin);


# Variables
def o = open;
def bh = max(open,close);
def bl = min(open,close);
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def BBar = bar == HighestAll(bar);


DefineGlobalColor("ss1",      Color.CYAN);
DefineGlobalColor("dd1",      Color.MAGENTA);
DefineGlobalColor("ss2",      Color.CYAN);
DefineGlobalColor("dd2",      Color.MAGENTA);
DefineGlobalColor("ss3",      Color.CYAN);
DefineGlobalColor("dd3",      Color.MAGENTA);
DefineGlobalColor("ss4",      Color.CYAN);
DefineGlobalColor("dd4",      Color.MAGENTA);
DefineGlobalColor("ss5",      Color.CYAN);
DefineGlobalColor("dd5",      Color.MAGENTA);
DefineGlobalColor("ss6",      Color.CYAN);
DefineGlobalColor("dd6",      Color.MAGENTA);
DefineGlobalColor("ss7",      Color.CYAN);
DefineGlobalColor("dd7",      Color.MAGENTA);
DefineGlobalColor("ss8",      Color.CYAN);
DefineGlobalColor("dd8",      Color.MAGENTA);
DefineGlobalColor("ss9",      Color.CYAN);
DefineGlobalColor("dd9",      Color.MAGENTA);
DefineGlobalColor("ss0",      Color.CYAN);
DefineGlobalColor("dd0",      Color.MAGENTA);


# 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 LowerBandr1 = if C <= (R1 + Num_Dev_up) then (R1             ) else Double.NaN;
plot UpperBandr1 = if C <= (R1 + Num_Dev_up) then (R1 + Num_Dev_up) else Double.NaN;

AddCloud(UpperBandr1, R1, GlobalColor("ss1"), GlobalColor("dd1"));
AddCloud(LowerBandr1, R1, GlobalColor("ss1"), GlobalColor("dd1"));
lowerbandr1.hide();
upperbandr1.hide();


plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.GREEN, 1);

plot LowerBandr2 = if C <= (R2 + Num_Dev_up) then (R2             ) else Double.NaN;
plot UpperBandr2 = if C <= (R2 + Num_Dev_up) then (R2 + Num_Dev_up) else Double.NaN;
AddCloud(UpperBandr2, R2, GlobalColor("ss2"), GlobalColor("dd2"));
AddCloud(LowerBandr2, R2, GlobalColor("ss2"), GlobalColor("dd2"));
lowerbandr2.hide();
upperbandr2.hide();

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.GREEN);
R3.hide();
#AddChartBubble(bar == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.GREEN, 1);

plot LowerBandr3 = if C <= (R3 + Num_Dev_up) then (R3             ) else Double.NaN;
plot UpperBandr3 = if C <= (R3 + Num_Dev_up) then (R3 + Num_Dev_up) else Double.NaN;
AddCloud(UpperBandr3, R3, GlobalColor("ss3"), GlobalColor("dd3"));
AddCloud(LowerBandr3, R3, GlobalColor("ss3"), GlobalColor("dd3"));
lowerbandr3.hide();
upperbandr3.hide();

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
R4.SetDefaultColor(Color.GREEN);
R4.hide();
#AddChartBubble(bar == HighestAll(R4PHBarOrigin), PHValue, "R4", Color.GREEN, 1);

plot LowerBandr4 = if C <= (R4 + Num_Dev_up) then (R4             ) else Double.NaN;
plot UpperBandr4 = if C <= (R4 + Num_Dev_up) then (R4 + Num_Dev_up) else Double.NaN;
AddCloud(UpperBandr4, R4, GlobalColor("ss4"), GlobalColor("dd4"));
AddCloud(LowerBandr4, R4, GlobalColor("ss4"), GlobalColor("dd4"));
lowerbandr4.hide();
upperbandr4.hide();





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 LowerBands1 = if c >= (S1 + Num_Dev_Dn)then (S1 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands1 = if c >= (S1 + Num_Dev_Dn)then (S1             ) else  Double.NaN;
AddCloud(UpperBands1, S1, GlobalColor("ss1"), GlobalColor("dd1"));
AddCloud(LowerBands1, S1, GlobalColor("ss1"), GlobalColor("dd1"));
lowerbands1.hide();
upperbands1.hide();

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.RED, 0);

plot LowerBands2 = if c >= (S2 + Num_Dev_Dn)then (S2 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands2 = if c >= (S2 + Num_Dev_Dn)then (S2             ) else  Double.NaN;
AddCloud(UpperBands2, S2, GlobalColor("ss2"), GlobalColor("dd2"));
AddCloud(LowerBands2, S2, GlobalColor("ss2"), GlobalColor("dd2"));
lowerbands2.hide();
upperbands2.hide();

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.RED);
S3.hide();
#AddChartBubble(bar == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.RED, 0);

plot LowerBands3 = if c >= (S3 + Num_Dev_Dn)then (S3 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands3 = if c >= (S3 + Num_Dev_Dn)then (S3             ) else  Double.NaN;
AddCloud(UpperBands3, S3, GlobalColor("ss3"), GlobalColor("dd3"));
AddCloud(LowerBands3, S3, GlobalColor("ss3"), GlobalColor("dd3"));
lowerbands3.hide();
upperbands3.hide();


plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
S4.SetDefaultColor(Color.RED);
S4.hide();
#AddChartBubble(bar == HighestAll(S4PLBarOrigin), PLValue, "S4", Color.RED, 0);

plot LowerBands4 = if c >= (S4 + Num_Dev_Dn)then (S4 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands4 = if c >= (S4 + Num_Dev_Dn)then (S4             ) else  Double.NaN;
AddCloud(UpperBands4, S4, GlobalColor("ss4"), GlobalColor("dd4"));
AddCloud(LowerBands4, S4, GlobalColor("ss4"), GlobalColor("dd4"));;
lowerbands4.hide();
upperbands4.hide();



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;

Alert(condition = BearScan, text = "Sell", "alert type" = Alert.BAR, sound = Sound.Chimes);
Alert(condition = BullScan, text = "Buy",  "alert type" = Alert.BAR, sound = Sound.Chimes);

# End Code Fractal Array
 
Last edited:
Well, without reading thorough all of that (whew!) I'll offer this: create another series when your initial condition is met. make it = 1 while the price is under the limit and 0 when the price is above. Then in your plot, use an AND to check whether the below is == 1 to plot and ==0 to ignore.

Code:
def condition = buy condition == 1 not buy == 0;
def limit = if condition == 1 then close + 5;
def is_below_limit = if condition then 1
    else if close crosses above limit then 0
    else is_below_limit[1];
plot limit_line = if is_below_limit == 1 then limit else double.nan;
THAT IS NOT REAL CODE... you'll have to adapt it to your use case above, but it should get you going in the right direction

-mashume

I tried implementing your suggestions. I think I did something wrong, as I still don't understand the tos scripts. It did not resolve the issue . here is the snippet.

Code:
def condition = if c >= (S3 + Num_Dev_Dn) then 1 else 0;

def limit = if condition == 1 then (S3 + Num_Dev_Dn) else 0;
def is_below_limit = if condition then 1
    else if close crosses below S3 then 0
    else is_below_limit[1];
plot limit_line = if is_below_limit == 1 then limit else double.nan;
limit_line.SetDefaultColor(Color.blue);




Also full code.



Code:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019

input length = 52;
input averageType = AverageType.WILDERS;


def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);

# User Inputs
input n = 4; #hint n: periods used for pivot calculations.
def Num_Dev_Dn = ATR/3;
def Num_Dev_up = -ATR/3;



# 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 ChartAggMin = getAggregationPeriod() / 1000 / 60;
#addlabel(1,ChartAggMin);


# Variables
def o = open;
def bh = max(open,close);
def bl = min(open,close);
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def BBar = bar == HighestAll(bar);


DefineGlobalColor("ss1",      Color.CYAN);
DefineGlobalColor("dd1",      Color.MAGENTA);
DefineGlobalColor("ss2",      Color.CYAN);
DefineGlobalColor("dd2",      Color.MAGENTA);
DefineGlobalColor("ss3",      Color.CYAN);
DefineGlobalColor("dd3",      Color.MAGENTA);
DefineGlobalColor("ss4",      Color.CYAN);
DefineGlobalColor("dd4",      Color.MAGENTA);
DefineGlobalColor("ss5",      Color.CYAN);
DefineGlobalColor("dd5",      Color.MAGENTA);
DefineGlobalColor("ss6",      Color.CYAN);
DefineGlobalColor("dd6",      Color.MAGENTA);
DefineGlobalColor("ss7",      Color.CYAN);
DefineGlobalColor("dd7",      Color.MAGENTA);
DefineGlobalColor("ss8",      Color.CYAN);
DefineGlobalColor("dd8",      Color.MAGENTA);
DefineGlobalColor("ss9",      Color.CYAN);
DefineGlobalColor("dd9",      Color.MAGENTA);
DefineGlobalColor("ss0",      Color.CYAN);
DefineGlobalColor("dd0",      Color.MAGENTA);


# 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 LowerBandr1 = if C <= (R1 + Num_Dev_up) then (R1             ) else Double.NaN;
plot UpperBandr1 = if C <= (R1 + Num_Dev_up) then (R1 + Num_Dev_up) else Double.NaN;

AddCloud(UpperBandr1, R1, GlobalColor("ss1"), GlobalColor("dd1"));
AddCloud(LowerBandr1, R1, GlobalColor("ss1"), GlobalColor("dd1"));
lowerbandr1.hide();
upperbandr1.hide();


plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.GREEN, 1);

plot LowerBandr2 = if C <= (R2 + Num_Dev_up) then (R2             ) else Double.NaN;
plot UpperBandr2 = if C <= (R2 + Num_Dev_up) then (R2 + Num_Dev_up) else Double.NaN;
AddCloud(UpperBandr2, R2, GlobalColor("ss2"), GlobalColor("dd2"));
AddCloud(LowerBandr2, R2, GlobalColor("ss2"), GlobalColor("dd2"));
lowerbandr2.hide();
upperbandr2.hide();

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.GREEN);
R3.hide();
#AddChartBubble(bar == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.GREEN, 1);

plot LowerBandr3 = if C <= (R3 + Num_Dev_up) then (R3             ) else Double.NaN;
plot UpperBandr3 = if C <= (R3 + Num_Dev_up) then (R3 + Num_Dev_up) else Double.NaN;
AddCloud(UpperBandr3, R3, GlobalColor("ss3"), GlobalColor("dd3"));
AddCloud(LowerBandr3, R3, GlobalColor("ss3"), GlobalColor("dd3"));
lowerbandr3.hide();
upperbandr3.hide();

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
R4.SetDefaultColor(Color.GREEN);
R4.hide();
#AddChartBubble(bar == HighestAll(R4PHBarOrigin), PHValue, "R4", Color.GREEN, 1);

plot LowerBandr4 = if C <= (R4 + Num_Dev_up) then (R4             ) else Double.NaN;
plot UpperBandr4 = if C <= (R4 + Num_Dev_up) then (R4 + Num_Dev_up) else Double.NaN;
AddCloud(UpperBandr4, R4, GlobalColor("ss4"), GlobalColor("dd4"));
AddCloud(LowerBandr4, R4, GlobalColor("ss4"), GlobalColor("dd4"));
lowerbandr4.hide();
upperbandr4.hide();





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 LowerBands1 = if c >= (S1 + Num_Dev_Dn)then (S1 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands1 = if c >= (S1 + Num_Dev_Dn)then (S1             ) else  Double.NaN;
AddCloud(UpperBands1, S1, GlobalColor("ss1"), GlobalColor("dd1"));
AddCloud(LowerBands1, S1, GlobalColor("ss1"), GlobalColor("dd1"));
lowerbands1.hide();
upperbands1.hide();

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.RED, 0);

plot LowerBands2 = if c >= (S2 + Num_Dev_Dn)then (S2 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands2 = if c >= (S2 + Num_Dev_Dn)then (S2             ) else  Double.NaN;
AddCloud(UpperBands2, S2, GlobalColor("ss2"), GlobalColor("dd2"));
AddCloud(LowerBands2, S2, GlobalColor("ss2"), GlobalColor("dd2"));
lowerbands2.hide();
upperbands2.hide();

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.RED);
S3.hide();
#AddChartBubble(bar == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.RED, 0);

plot LowerBands3 = if c >= (S3 + Num_Dev_Dn)then (S3 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands3 = if c >= (S3 + Num_Dev_Dn)then (S3             ) else  Double.NaN;
AddCloud(UpperBands3, S3, GlobalColor("ss3"), GlobalColor("dd3"));
AddCloud(LowerBands3, S3, GlobalColor("ss3"), GlobalColor("dd3"));
lowerbands3.hide();
upperbands3.hide();



def condition = if c >= (S3 + Num_Dev_Dn) then 1 else 0;

def limit = if condition == 1 then (S3 + Num_Dev_Dn) else 0;
def is_below_limit = if condition then 1
    else if close crosses below S3 then 0
    else is_below_limit[1];
plot limit_line = if is_below_limit == 1 then limit else double.nan;
limit_line.SetDefaultColor(Color.blue);


plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
S4.SetDefaultColor(Color.RED);
S4.hide();
#AddChartBubble(bar == HighestAll(S4PLBarOrigin), PLValue, "S4", Color.RED, 0);

plot LowerBands4 = if c >= (S4 + Num_Dev_Dn)then (S4 + Num_Dev_Dn) else  Double.NaN;
plot UpperBands4 = if c >= (S4 + Num_Dev_Dn)then (S4             ) else  Double.NaN;
AddCloud(UpperBands4, S4, GlobalColor("ss4"), GlobalColor("dd4"));
AddCloud(LowerBands4, S4, GlobalColor("ss4"), GlobalColor("dd4"));;
lowerbands4.hide();
upperbands4.hide();




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;

Alert(condition = BearScan, text = "Sell", "alert type" = Alert.BAR, sound = Sound.Chimes);
Alert(condition = BullScan, text = "Buy",  "alert type" = Alert.BAR, sound = Sound.Chimes);

# End Code Fractal Array
 
Hello all!
Would someone please help me grasp the following:
why different color combinations?
some s/r are Green/Red, some are Green/Gray,, some are Orange/Gray.....
 
Can someone please provide scan for
Support/Resistance or Supply/Demand whichever you prefer. Based on pivots and ATR. Change the n to get zones for the time you wish.

Update: ATR is now straight lines and user can input ATR multiple.
New share: https://tos.mx/Lv0FsZi

DfssJTt.png


https://tos.mx/VeGhwv
Code:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019

input length = 252;
input averageType = AverageType.WILDERS;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);

# User Inputs
input n = 21; #hint n: periods used for pivot calculations.
def Num_Dev_Dn = ATR;
def Num_Dev_up = -ATR;


# 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 LowerBandr1 = R1 + Num_Dev_Dn ;
plot UpperBandr1 = R1 + Num_Dev_up ;
AddCloud(UpperBandr1, R1, Color.GREEN, Color.RED );
AddCloud(LowerBandr1, R1, Color.GREEN, Color.RED );
lowerbandr1.hide();
upperbandr1.hide();


plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.GREEN, 1);

plot LowerBandr2 = R2 + Num_Dev_Dn ;
plot UpperBandr2 = R2 + Num_Dev_up ;
AddCloud(UpperBandr2, R2, Color.GREEN, Color.RED);
AddCloud(LowerBandr2, R2, Color.GREEN, Color.RED);
lowerbandr2.hide();
upperbandr2.hide();

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.GREEN, 1);

plot LowerBandr3 = R3 + Num_Dev_Dn ;
plot UpperBandr3 = R3 + Num_Dev_up ;
AddCloud(UpperBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBandr3, R3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbandr3.hide();
upperbandr3.hide();

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
R4.SetDefaultColor(Color.GREEN);
#AddChartBubble(bar == HighestAll(R4PHBarOrigin), PHValue, "R4", Color.GREEN, 1);

plot LowerBandr4 = R4 + Num_Dev_Dn ;
plot UpperBandr4 = R4 + Num_Dev_up ;
AddCloud(UpperBandr4, R4, Color.LIME, Color.PINK);
AddCloud(LowerBandr4, R4, Color.LIME, Color.PINK);
lowerbandr4.hide();
upperbandr4.hide();


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 LowerBands1 = S1 + Num_Dev_Dn ;
plot UpperBands1 = S1 + Num_Dev_up ;
AddCloud(UpperBands1, S1, Color.GREEN, Color.RED);
AddCloud(LowerBands1, S1, Color.GREEN, Color.RED);
lowerbands1.hide();
upperbands1.hide();

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.RED, 0);

plot LowerBands2 = S2 + Num_Dev_Dn ;
plot UpperBands2 = S2 + Num_Dev_up ;
AddCloud(UpperBands2, S2, Color.GREEN, Color.RED);
AddCloud(LowerBands2, S2, Color.GREEN, Color.RED);
lowerbands2.hide();
upperbands2.hide();

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.RED, 0);

plot LowerBands3 = S3 + Num_Dev_Dn ;
plot UpperBands3 = S3 + Num_Dev_up ;
AddCloud(UpperBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
AddCloud(LowerBands3, S3, Color.LIGHT_GRAY, Color.LIGHT_ORANGE);
lowerbands3.hide();
upperbands3.hide();


plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
S4.SetDefaultColor(Color.RED);
#AddChartBubble(bar == HighestAll(S4PLBarOrigin), PLValue, "S4", Color.RED, 0);

plot LowerBands4 = S4 + Num_Dev_Dn ;
plot UpperBands4 = S4 + Num_Dev_up ;
AddCloud(UpperBands4, S4, Color.LIME, Color.PINK);
AddCloud(LowerBands4, S4, Color.LIME, Color.PINK);
lowerbands4.hide();
upperbands4.hide();

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
Can you please provide scan for supports and resistances points?

Hey guys...i'm trying to create an addition to this script whereby I get an uparrow when the triggger line crosses the zero line, and vice-versa for a down arrow. Tried adding this to the bottom of the script but it didn't quite work.

plot upArrow1 = TriggerLine crosses above Zeroline;
upArrow1.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
upArrow1.SetDefaultColor(Color.green);

plot dnArrow1 = TriggerLine crosses below Zeroline;
dnArrow1.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
dnArrow1.SetDefaultColor(Color.red);

Thanks for taking the time to explain! appreciate it. still learning thinkscript. I was able to have success with the boolean, but would it be possible to have it plot instead like the above arrows in the pic attached? Below is the volume oscillator, above is the PMO oscillator which I just used the condition wizard to code it pretty easily. Guess I'm not getting how the zero line works in the VO or something...And obviously I'm using this as a lower study, if that matters.
M9fzg8a.png
[/IMG]
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
250 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