# FractalPivots_MTF_VWAP Using:
# Support and Resistance Fractal Pivots
# Mobius
# V01.01.2011
# Sleepyz - Usethinkscript request
# Modified to add an aggregation input and an agg based VWAP anchored at the last agg based pivot
# User Inputs
input n = 2; #hint n: Length for calculations.
# Internal Script Reference
script LinePlot {
input LineLimit = 0;
input OnExpansion = yes;
input data = close;
input bar = 0;
def ThisBar = HighestAll(bar);
def cLine = if bar == ThisBar
then data
else Double.NaN;
plot P = if ThisBar - LineLimit <= bar
then HighestAll(cLine)
else Double.NaN;
}
# Variables
input agg = AggregationPeriod.DAY;
def o = open(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def c = close(period = agg);
def bar = BarNumber();
def hh = fold i = 1 to n + 1
with p = 1
while p
do h > GetValue(h, -i);
def PH = if (bar > n and
h == Highest(h, n) and
hh)
then h
else Double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do l < GetValue(l, -j);
def PL = if (bar > n and
l == Lowest(l, n) and
ll)
then l
else Double.NaN;
def PHBar = if !IsNaN(PH)
then bar
else PHBar[1];
def PLBar = if !IsNaN(PL)
then bar
else PLBar[1];
def PHL = if !IsNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !IsNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots = bar >= HighestAll(priorPLBar);
def R1pivot = if PHL > 0 and
HighPivots
then PHL
else Double.NaN;
def S1pivot = if LowPivots
then PL
else Double.NaN;
def R1value = R1pivot;
def R1bar = PHBar;
def R1limit = bar - R1bar;
def R2bar = priorPHBar;
def R2limit = bar - R2bar;
def R2value = if BarNumber() == HighestAll(R2bar)
then h
else R2value[1];
def R3bar = if priorPHBar != priorPHBar[1]
then priorPHBar[1]
else R3bar[1];
def R3limit = bar - R3bar;
def R3value = if BarNumber() == HighestAll(R3bar)
then h
else R3value[1];
def S1value = S1pivot;
def S1bar = PLBar;
def S1limit = bar - S1bar;
def S2bAR = priorPLBar;
def S2limit = bar - S2bAR;
def S2value = if bar == HighestAll(S2bAR)
then l
else S2value[1];
def S3bar = if priorPLBar != priorPLBar[1]
then priorPLBar[1]
else S3bar[1];
def S3limit = bar - S3bar;
def S3value = if BarNumber() == HighestAll(S3bar)
then l
else S3value[1];
# Plots
input x = 1;
def x1 = x + 1;
input showbubbles = yes;
plot R1 = LinePlot(Linelimit = R1limit, data = R1value, bar = R1bar);
R1.SetDefaultColor(Color.GREEN);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R1, "R1", Color.GREEN);
plot R2 = LinePlot(LineLimit = R2limit, data = R2value, bar = R2bar).P;
R2.SetDefaultColor(Color.YELLOW);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R2value, "R2", Color.YELLOW);
plot R3 = LinePlot(LineLimit = R3limit, data = R3value, bar = R3bar).P;
R3.SetDefaultColor(Color.BLUE);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R3value, "R3", Color.BLUE);
plot S1 = LinePlot(LineLimit = S1limit, data = S1value, bar = S1bar).P;
S1.SetDefaultColor(Color.RED);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S1, "S1", Color.RED);
plot S2 = LinePlot(LineLimit = S2limit, data = S2value, bar = S2bAR).P;
S2.SetDefaultColor(Color.PLUM);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S2, "S2", Color.PLUM);
plot S3 = LinePlot(LineLimit = S3limit, data = S3value, bar = S3bar).P;
S3.SetDefaultColor(Color.MAGENTA);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S3, "S3", Color.MAGENTA);
# End Code Support and Resistance Fractal Pivots
#VWAP Anchored from Date
def v = volume(period = agg);
def wap = vwap(period = agg);
def xx = Max(R1bar, Max(R2bar, Max(R3bar, Max(S1bar, Max(S2bAR, S3bar)))));
def startdate = if BarNumber() == xx then low else startdate[1];
input numDevUp = 2.0;
def anchor = BarNumber() >= HighestAll(xx) and startdate;
def volumeSum = if anchor then CompoundValue(1, volumeSum[1] + v, v) else 0;
def volumeVwapSum = if anchor then CompoundValue(1, volumeVwapSum[1] + v * wap, v * wap) else 0;
def volumeVwap2Sum = if anchor then CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(wap), v * Sqr(wap)) else 0;
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
plot VWAP = price;
input showbands = yes;
plot UpperBand = if !showbands then Double.NaN else price + numDevUp * deviation;
plot LowerBand = if !showbands then Double.NaN else price - numDevUp * deviation;
VWAP.SetDefaultColor(GetColor(0));
UpperBand.SetDefaultColor(GetColor(2));
LowerBand.SetDefaultColor(GetColor(4));
## END STUDY