```
#START STUDY#Anchored_VWAP2
#linus, 2014-03-10, v0.1
#hint: VWAP stops anchored off fractalTrader pivots.
#hint n: Lookback period for finding swing highs, lows.
input n = 20;
#hint ticks: Offset High/Low VWAP lines by this number of ticks.
input ticks = 2.0;
def bnOK = barNumber() > 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 Double.NaN;
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 Double.NaN;
def PivH = if HH > 0 then HH else Double.NaN;
def PivL = if LL > 0 then LL else Double.NaN;
plot Up = !isNaN(PivL);
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);
plot Dn = !isNaN(PivH);
Dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Dn.SetLineWeight(3);
Dn.SetDefaultColor(Color.ORANGE);
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], Double.NaN);
VC = compoundValue(1, volume + VC[1], Double.NaN);
PC2 = compoundValue(1, LocC + PC2[1], Double.NaN);
VC2 = compoundValue(1, volume + VC2[1], Double.NaN);
}
if Dn {
PH = LocH;
VH = volume;
PH2 = PH[1];
VH2 = VH[1];
} else {
PH = compoundValue(1, LocH + PH[1], Double.NaN);
VH = compoundValue(1, volume + VH[1], Double.NaN);
PH2 = compoundValue(1, LocH + PH2[1], Double.NaN);
VH2 = compoundValue(1, volume + VH2[1], Double.NaN);
}
if Up {
PL = LocL;
VL = volume;
PL2 = PL[1];
VL2 = VL[1];
} else {
PL = compoundValue(1, LocL + PL[1], Double.NaN);
VL = compoundValue(1, volume + VL[1], Double.NaN);
PL2 = compoundValue(1, LocL + PL2[1], Double.NaN);
VL2 = compoundValue(1, volume + VL2[1], Double.NaN);
}
plot VwapC = if Dn or Up then Double.NaN else PC / VC;
plot VwapC2 = if Dn or Up then Double.NaN else PC2 / VC2;
plot VwapH = if Dn then Double.NaN else PH / VH;
plot VwapL = if Up then Double.NaN else PL / VL;
plot VwapH2 = if Dn then Double.NaN else PH2 / VH2;
plot VwapL2 = if Up then Double.NaN else PL2 / VL2;
# directional versus rotational logic tests
def C_1 = if (VwapC > VwapC[1],1,-1);
def C_2 = if (VwapC2 > VwapC2[1],1,-1);
def C_3 = if (VwapC > VwapC2,1,-1);
def H_1 = if (VwapH > VwapH[1],1,-1);
def H_2 = if (VwapH2 > VwapH2[1],1,-1);
def H_3 = if (VwapH > VwapH2,1,-1);
def L_1 = if (VwapL > VwapL[1],1,-1);
def L_2 = if (VwapL2 > VwapL2[1],1,-1);
def L_3 = if (VwapL > VwapL2,1,-1);
def context = C_1+C_2+C_3+H_1+H_2+H_3+L_1+L_2+L_3;
AddLabel(yes,if context < -8 then "Directional Down" else if context > 8 then "Directional Up" else "Rotational", (if context < -8 then Color.RED else if context > 8 then Color.GREEN else Color.YELLOW));
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();
VwapL.SetDefaultColor(Color.DARK_GREEN);
VwapL.HideBubble();
VwapH2.SetDefaultColor(Color.DARK_RED);
VwapH2.SetStyle(Curve.SHORT_DASH);
VwapH2.HideBubble();
VwapL2.SetDefaultColor(Color.DARK_GREEN);
VwapL2.SetStyle(Curve.SHORT_DASH);
VwapL2.HideBubble();
Alert((Up), "Buy", Alert.BAR, sound.Ring);
Alert((Dn), "Sell", Alert.BAR, sound.Ring);
#END STUDY
```