# 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