#START STUDY
#Anchored_VWAPv3
#linus, 2014-03-10, v0.1
#10:24 linus: it carries over the previous pivot's lines for high, low and close. (it plots vwaps of the high, low and close that are reset each time a new pivot is found.)
#10:25 linus: i wrote it to experiment with vwap as stops. (the high and low vwaps that can be offset by the ticks input.)
#10:25 linus: but it should serve as an example of how to reset the vwaps based on a signal.
#10:35 linus: #hint: VWAP stops anchored off fractalTrader pivots.
#10:37 linus: the code calculates the pivots as PivH and PivL, and then restarts the high, low and close vwaps when it finds a new pivot. Otherwise it continues to calculate the high, low and close vwaps.
#10:37 linus: the dashed vwap plots are the saved from the previous pivot, and the solid vwap plots are since the last pivot.
#20220708 Sleepyz used missing logic from Mobius fractal pivots
#20230315 Sleepyz modified the VwapH and VwapL off the high/low respective of PivotH/PivotL
#20230317 Sleepyz added optional bubbles vs arrows at VwapH/VwapL
#hint: VWAP stops anchored off fractalTrader pivots.
#hint n: Lookback period for finding swing highs, lows.
input n = 20;
input showarrows = yes;
input showbubbles = yes;
#hint ticks: Offset High/Low VWAP lines by this number of ticks.
input ticks = 0.0;
def bn = BarNumber();
def na = Double.NaN;
def bnOK = bn > n;
def isHigher = fold i = 1 to n + 1 with p = 1
while p do high > GetValue(high, -i);
def HH = if bnOK and isHigher and
high == Highest(high, n)
then high else na;
def isLower = fold j = 1 to n + 1 with q = 1
while q do low < GetValue(low, -j);
def LL = if bnOK and isLower and
low == Lowest(low, n)
then low else na;
def PHBar = if !IsNaN(HH)
then bn
else PHBar[1];
def PLBar = if !IsNaN(LL)
then bn
else PLBar[1];
def PHL = if !IsNaN(HH)
then HH
else PHL[1];
def PLL = if !IsNaN(LL)
then LL
else PLL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bn >= HighestAll(priorPHBar);
def LowPivots = bn >= HighestAll(priorPLBar);
def PivH = if !IsNaN(HH) > 0 then HighPivots else na;
def PivL = if !IsNaN(LL) > 0 then LowPivots else na;
plot Up = bn == PLBar;
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);
Up.SetHiding(!showarrows);
AddChartBubble(showbubbles and bn == PLBar, low, AsText(low), Color.GREEN, no);
plot Dn = bn == PHBar;
Dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Dn.SetLineWeight(3);
Dn.SetDefaultColor(Color.ORANGE);
Dn.SetHiding(!showarrows);
AddChartBubble(showbubbles and bn == PHBar, high, AsText(high), Color.RED);
def LocH = (high + (TickSize() * ticks)) * volume;
def LocL = (low - (TickSize() * ticks)) * volume;
def LocC = close * volume;
rec PC;
rec VC;
rec PC2;
rec VC2;
rec PH;
rec VH;
rec PL;
rec VL;
rec PH2;
rec VH2;
rec PL2;
rec VL2;
if Dn or Up {
PC = LocC;
VC = volume;
PC2 = PC[1];
VC2 = VC[1];
} else {
PC = CompoundValue(1, LocC + PC[1], na);
VC = CompoundValue(1, volume + VC[1], na);
PC2 = CompoundValue(1, LocC + PC2[1], na);
VC2 = CompoundValue(1, volume + VC2[1], na);
}
if Dn {
PH = LocH;
VH = volume;
PH2 = PH[1];
VH2 = VH[1];
} else {
PH = CompoundValue(1, LocH + PH[1], na);
VH = CompoundValue(1, volume + VH[1], na);
PH2 = CompoundValue(1, LocH + PH2[1], na);
VH2 = CompoundValue(1, volume + VH2[1], na);
}
if Up {
PL = LocL;
VL = volume;
PL2 = PL[1];
VL2 = VL[1];
} else {
PL = CompoundValue(1, LocL + PL[1], na);
VL = CompoundValue(1, volume + VL[1], na);
PL2 = CompoundValue(1, LocL + PL2[1], na);
VL2 = CompoundValue(1, volume + VL2[1], na);
}
plot VwapC = if Dn[-1] or Up[-1] then na else PC / VC;
plot VwapC2 = if Dn[-1] or Up[-1] then na else PC2 / VC2;
plot VwapH = if Dn[-1] then na else PH / VH;
plot VwapL = if Up[-1] then na else PL / VL;
plot VwapH2 = if Dn[-1] then na else PH2 / VH2;
plot VwapL2 = if Up[-1] then na else PL2 / VL2;
VwapC.SetDefaultColor(Color.YELLOW);
VwapC.SetLineWeight(2);
VwapC.HideBubble();
VwapC2.SetDefaultColor(Color.YELLOW);
VwapC2.SetLineWeight(2);
VwapC2.SetStyle(Curve.SHORT_DASH);
VwapC2.HideBubble();
VwapH.SetDefaultColor(Color.DARK_RED);
VwapH.HideBubble();
VwapH.SetLineWeight(5);
VwapL.SetDefaultColor(Color.DARK_GREEN);
VwapL.HideBubble();
VwapL.SetLineWeight(5);
VwapH2.SetDefaultColor(Color.DARK_RED);
VwapH2.SetStyle(Curve.SHORT_DASH);
VwapH2.HideBubble();
VwapL2.SetDefaultColor(Color.DARK_GREEN);
VwapL2.SetStyle(Curve.SHORT_DASH);
VwapL2.HideBubble();
#END STUDY
;