Anchored VWAP Indicator for ThinkorSwim

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

Is there a way to auto anchor to premarket high and not update the anchor point every time there's a new pivot point?
 
Last edited:
Is there a way to auto anchor to premarket high and not update the anchor point every time there's a new pivot point?
@Jonas J
I am not sure which version of the anchored vwap you are referring to, so I modified the version that I had posted above to include Premarkethigh. Due to gaps in the premarket, sometimes an anchored vwap may not print at some aggregations and not others.

Ruby:
## START STUDY
## Anchored_VWAP3
## linus, 2014-06-28, v0.3
## 20211117 Sleepyz modified to use a cond to anchor vwap

input ticks = 2;
input nhighlow = 5;

def h=if isnan(high) then h[1] else high;
def l=if isnan(low) then l[1] else low;

#VWAP Anchored to Date/Time

input startdateselection = {default Daily, Custom};
input starttimeselection = {default HOD, LOD, RTH, PRE, PRE_HIGH, Custom};
input showtodayonly      = yes;
input colorvwap          = yes;

input startdate = 20211130;
input starttime = 0930;
def premarkethigh = if getyyyYMMDD()!=GetYYYYMMDD()[1] then h else if GetTime() < RegularTradingStart(GetYYYYMMDD()) and h > premarkethigh[1] then h else premarkethigh[1];
def premarkethighbar = if h == premarkethigh then BarNumber() else Double.NaN;
def hday = if GetYYYYMMDD() != GetYYYYMMDD()[1] then h else if h > hday[1] then h else hday[1];
def hbar = if h == hday then BarNumber() else Double.NaN;
def lday = if GetYYYYMMDD() != GetYYYYMMDD()[1] then l else if l < lday[1] then l else lday[1];
def lbar = if l == lday then BarNumber() else Double.NaN;
def time = if starttimeselection == starttimeselection.RTH
           then 0930
           else if starttimeselection == starttimeselection.PRE or starttimeselection == starttimeselection.PRE_HIGH
           then 0400
           else starttime;

def ymd         = GetYYYYMMDD();
def bn          = BarNumber();
def c           = close;
def v           = volume;
def vw          = vwap;

def volumesum;
def volumevwapsum;
def volumevwap2sum;
def price;
def deviation;


def anchor        =  if startdateselection == startdateselection.Daily and ymd != ymd[1] or                      startdateselection == startdateselection.Custom and GetYYYYMMDD() < startdate
                     then 0                     
                     else if starttimeselection == starttimeselection.PRE_HIGH
                     and BarNumber() == HighestAll(premarkethighbar)
                     then 1                   
                     else if starttimeselection == starttimeselection.HOD
                     and BarNumber() == HighestAll(hbar)
                     then 1
                     else if starttimeselection == starttimeselection.LOD
                     and BarNumber() == HighestAll(lbar)
                     then 1
                     else if starttimeselection == starttimeselection.RTH
                     and GetTime() crosses above RegularTradingStart(GetYYYYMMDD())     
                     or starttimeselection == starttimeselection.PRE
                     and GetTime() crosses below RegularTradingEnd(GetYYYYMMDD()[1])
                     or starttimeselection == starttimeselection.Custom
                     and SecondsFromTime(time)[1] <= 0 and
                         SecondsFromTime(time) >= 0
                     then 1
                     else anchor[1];


volumesum      = if  (anchor)  then volumesum[1] + volume else 0;
volumevwapsum  = if  (anchor)  then volumevwapsum[1] + volume * vwap else 0;
volumevwap2sum = if  (anchor)  then volumevwap2sum[1] + volume * Sqr(vwap) else 0;
price          = volumevwapsum / volumesum;
deviation      = Sqrt(Max(volumevwap2sum / volumesum - Sqr(price), 0));

;

plot VWAP      = if showtodayonly and startdateselection == startdateselection.Daily and !IsNaN(close(period = AggregationPeriod.DAY)[-1]) then Double.NaN else price;
VWAP.AssignValueColor(if colorvwap and close[1] < VWAP then Color.RED else Color.CYAN);

input showbands = yes;
input numDev1  = 1.0;
input numDev2  = 2.0;
input numDev3  = 3.0;

plot UpperBand1  = if !showbands then Double.NaN else VWAP + numDev1 * deviation;
plot LowerBand1  = if !showbands then Double.NaN else VWAP - numDev1 * deviation;
plot UpperBand2  = if !showbands then Double.NaN else VWAP + numDev2 * deviation;
plot LowerBand2  = if !showbands then Double.NaN else VWAP - numDev2 * deviation;
plot UpperBand3  = if !showbands then Double.NaN else VWAP + numDev3 * deviation;
plot LowerBand3  = if !showbands then Double.NaN else VWAP - numDev3 * deviation;

VWAP.SetDefaultColor(Color.CYAN);
UpperBand1.SetDefaultColor(Color.GREEN);
LowerBand1.SetDefaultColor(Color.RED);
UpperBand2.SetDefaultColor(Color.GREEN);
LowerBand2.SetDefaultColor(Color.RED);
UpperBand3.SetDefaultColor(Color.GREEN);
LowerBand3.SetDefaultColor(Color.RED);
VWAP.HideBubble();
UpperBand1.HideBubble();
LowerBand1.HideBubble();
UpperBand2.HideBubble();
LowerBand2.HideBubble();
UpperBand3.HideBubble();
LowerBand3.HideBubble();

input showclouds = yes;
AddCloud(if showclouds then UpperBand3 else Double.NaN, UpperBand2, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showclouds then LowerBand3 else Double.NaN, LowerBand2, Color.LIGHT_RED, Color.LIGHT_RED);


input showbubblesline = yes;
input bubblemoverVWAP_Labels = 5;#Hint bubblemoverVWAP_Labels: Number of Spaces bubble offset in expansion area
def n = bubblemoverVWAP_Labels;
def n1 = n + 1;

AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] ,
              "V:\n" + Round(price[n1] , 2),
               Color.ORANGE);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev1 * deviation[n1] ,
              "V1:\n" + Round((price[n1] + numDev1 * deviation[n1]), 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev2 * deviation[n1] ,
              "V2:\n" + Round(price[n1] + numDev2 * deviation[n1], 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev3 * deviation[n1] ,
              "V3:\n" + Round(price[n1] + numDev3 * deviation[n1], 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev1 * deviation[n1] ,
              "V1:\n" + Round(price[n1] - numDev1 * deviation[n1], 2),
               Color.RED, no);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev2 * deviation[n1] ,
              "V2:\n" + Round(price[n1] - numDev2 * deviation[n1] , 2),
               Color.RED, no);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev3 * deviation[n1] ,
              "V3:\n" + Round(price[n1] - numDev3 * deviation[n1] , 2),
               Color.RED, no);


input showbubblescurrSTD = no;
input bubblemoverVSTD = 1;#Hint bubblemoverVSTD: Number of Spaces VSTD bubble offset in expansion
def p = bubblemoverVSTD;
def p1 = p + 1;

AddChartBubble(showbubblescurrSTD and IsNaN(c[p]) and !IsNaN(c[p1]) ,
               c[p1],
               Round(((c[p1] - price[p1]) / deviation[p1]), 1) +
              "\n" + Round(c[p1], 2) ,
               if c[p1] > VWAP[p1]
               then Color.GREEN
               else Color.RED,
               if c[p1] > VWAP[p1]
               then yes else no );
 
Last edited:
I am not sure which version of the anchored vwap you are referring to, so I modified the version that I had posted above to include Premarkethigh. Due to gaps in the premarket, sometimes an anchored vwap may not print at some aggregations and not others.
Thanks, this is amazing. But throwing it on a the charts now, it only sometimes works, and even with "showtodayonly" off i can see its not showing previous days. (if i through it on a high volume multi day move, its not showing previous days, its only connected to todays premarket high, and only sometimes) This is good though thank you, i will see if i can correct my problem
 
Thanks, this is amazing. But throwing it on a the charts now, it only sometimes works, and even with "showtodayonly" off i can see its not showing previous days. (if i through it on a high volume multi day move, its not showing previous days, its only connected to todays premarket high, and only sometimes) This is good though thank you, i will see if i can correct my problem

I have modified the definitions of high/low to capture instances when there is missing high/low data by using the last instance of that data. This seems to help with the PRE_HIGH option not working constitently as was mentioned with the original post and your observations.

To isolate the HOD, LOD and PRE_HIGH, I used the highestall() function to identify those positions on the chart. This limits these to only display on the current day. Since these only print on the current day, the input showonlytoday set to NO will not work.

The Vwap also starts printing one of those 3 variables above once these are confirmed by the data after these is printed. For example, for a developing premarket high, if the current bar is the high of that period, the Vwap will only appear after the next bar that is lower than that high.

Ruby:
## START STUDY
## Anchored_VWAP3
## linus, 2014-06-28, v0.3
## 20211117 Sleepyz modified to use a cond to anchor vwap

input ticks = 2;
input nhighlow = 5;

def h=if isnan(high) then h[1] else high;
def l=if isnan(low) then l[1] else low;

#VWAP Anchored to Date/Time

input startdateselection = {default Daily, Custom};
input starttimeselection = {default HOD, LOD, RTH, PRE, PRE_HIGH, Custom};
input showtodayonly      = yes;
input colorvwap          = yes;

input startdate = 20211130;
input starttime = 0930;
def premarkethigh = if getyyyYMMDD()!=GetYYYYMMDD()[1] then h else if GetTime() < RegularTradingStart(GetYYYYMMDD()) and h > premarkethigh[1] then h else premarkethigh[1];
def premarkethighbar = if h == premarkethigh then BarNumber() else Double.NaN;
def hday = if GetYYYYMMDD() != GetYYYYMMDD()[1] then h else if h > hday[1] then h else hday[1];
def hbar = if h == hday then BarNumber() else Double.NaN;
def lday = if GetYYYYMMDD() != GetYYYYMMDD()[1] then l else if l < lday[1] then l else lday[1];
def lbar = if l == lday then BarNumber() else Double.NaN;
def time = if starttimeselection == starttimeselection.RTH
           then 0930
           else if starttimeselection == starttimeselection.PRE or starttimeselection == starttimeselection.PRE_HIGH
           then 0400
           else starttime;

def ymd         = GetYYYYMMDD();
def bn          = BarNumber();
def c           = close;
def v           = volume;
def vw          = vwap;

def volumesum;
def volumevwapsum;
def volumevwap2sum;
def price;
def deviation;


def anchor        =  if startdateselection == startdateselection.Daily and ymd != ymd[1] or                      startdateselection == startdateselection.Custom and GetYYYYMMDD() < startdate
                     then 0                     
                     else if starttimeselection == starttimeselection.PRE_HIGH
                     and BarNumber() == HighestAll(premarkethighbar)
                     then 1                   
                     else if starttimeselection == starttimeselection.HOD
                     and BarNumber() == HighestAll(hbar)
                     then 1
                     else if starttimeselection == starttimeselection.LOD
                     and BarNumber() == HighestAll(lbar)
                     then 1
                     else if starttimeselection == starttimeselection.RTH
                     and GetTime() crosses above RegularTradingStart(GetYYYYMMDD())     
                     or starttimeselection == starttimeselection.PRE
                     and GetTime() crosses below RegularTradingEnd(GetYYYYMMDD()[1])
                     or starttimeselection == starttimeselection.Custom
                     and SecondsFromTime(time)[1] <= 0 and
                         SecondsFromTime(time) >= 0
                     then 1
                     else anchor[1];


volumesum      = if  (anchor)  then volumesum[1] + volume else 0;
volumevwapsum  = if  (anchor)  then volumevwapsum[1] + volume * vwap else 0;
volumevwap2sum = if  (anchor)  then volumevwap2sum[1] + volume * Sqr(vwap) else 0;
price          = volumevwapsum / volumesum;
deviation      = Sqrt(Max(volumevwap2sum / volumesum - Sqr(price), 0));

;

plot VWAP      = if showtodayonly and startdateselection == startdateselection.Daily and !IsNaN(close(period = AggregationPeriod.DAY)[-1]) then Double.NaN else price;
VWAP.AssignValueColor(if colorvwap and close[1] < VWAP then Color.RED else Color.CYAN);

input showbands = yes;
input numDev1  = 1.0;
input numDev2  = 2.0;
input numDev3  = 3.0;

plot UpperBand1  = if !showbands then Double.NaN else VWAP + numDev1 * deviation;
plot LowerBand1  = if !showbands then Double.NaN else VWAP - numDev1 * deviation;
plot UpperBand2  = if !showbands then Double.NaN else VWAP + numDev2 * deviation;
plot LowerBand2  = if !showbands then Double.NaN else VWAP - numDev2 * deviation;
plot UpperBand3  = if !showbands then Double.NaN else VWAP + numDev3 * deviation;
plot LowerBand3  = if !showbands then Double.NaN else VWAP - numDev3 * deviation;

VWAP.SetDefaultColor(Color.CYAN);
UpperBand1.SetDefaultColor(Color.GREEN);
LowerBand1.SetDefaultColor(Color.RED);
UpperBand2.SetDefaultColor(Color.GREEN);
LowerBand2.SetDefaultColor(Color.RED);
UpperBand3.SetDefaultColor(Color.GREEN);
LowerBand3.SetDefaultColor(Color.RED);
VWAP.HideBubble();
UpperBand1.HideBubble();
LowerBand1.HideBubble();
UpperBand2.HideBubble();
LowerBand2.HideBubble();
UpperBand3.HideBubble();
LowerBand3.HideBubble();

input showclouds = yes;
AddCloud(if showclouds then UpperBand3 else Double.NaN, UpperBand2, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showclouds then LowerBand3 else Double.NaN, LowerBand2, Color.LIGHT_RED, Color.LIGHT_RED);


input showbubblesline = yes;
input bubblemoverVWAP_Labels = 5;#Hint bubblemoverVWAP_Labels: Number of Spaces bubble offset in expansion area
def n = bubblemoverVWAP_Labels;
def n1 = n + 1;

AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] ,
              "V:\n" + Round(price[n1] , 2),
               Color.ORANGE);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev1 * deviation[n1] ,
              "V1:\n" + Round((price[n1] + numDev1 * deviation[n1]), 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev2 * deviation[n1] ,
              "V2:\n" + Round(price[n1] + numDev2 * deviation[n1], 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev3 * deviation[n1] ,
              "V3:\n" + Round(price[n1] + numDev3 * deviation[n1], 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev1 * deviation[n1] ,
              "V1:\n" + Round(price[n1] - numDev1 * deviation[n1], 2),
               Color.RED, no);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev2 * deviation[n1] ,
              "V2:\n" + Round(price[n1] - numDev2 * deviation[n1] , 2),
               Color.RED, no);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev3 * deviation[n1] ,
              "V3:\n" + Round(price[n1] - numDev3 * deviation[n1] , 2),
               Color.RED, no);


input showbubblescurrSTD = no;
input bubblemoverVSTD = 1;#Hint bubblemoverVSTD: Number of Spaces VSTD bubble offset in expansion
def p = bubblemoverVSTD;
def p1 = p + 1;

AddChartBubble(showbubblescurrSTD and IsNaN(c[p]) and !IsNaN(c[p1]) ,
               c[p1],
               Round(((c[p1] - price[p1]) / deviation[p1]), 1) +
              "\n" + Round(c[p1], 2) ,
               if c[p1] > VWAP[p1]
               then Color.GREEN
               else Color.RED,
               if c[p1] > VWAP[p1]
               then yes else no );
 
Anchored VWAP indicator can useful for intraday trading. It can also be used on any timeframe including hourly, daily, and weekly. VWAP can be a great tool for analyzing the market, especially for day traders.

Here we have different Anchored VWAP indicators for ThinkorSwim. Feel free to test them out and use any that fits your trading style.
How do I remove the the one bar delay? I have messed with the script and can't figure it out, which line should I edit? The N value does not change the pivot setpoint as there still is a one bar delay from the the actual pivot high or low. Thanks!
 
Last edited by a moderator:
How do I remove the the one bar delay? I have messed with the script and can't figure it out, which line should I edit? The N value does not change the pivot setpoint as there still is a one bar delay from the the actual pivot high or low. Thanks!
In the 15 pages of this thread, the consensus amongst members is that it cannot be done.
 
@saak99
Here is a mod to the script I posted above to have an option to anchor at the HOD or LOD. It will only plot on the last day. Almost all of the optional plots of deviations, clouds, etc were set to no in the following image.
This is really close to what i am looking for, only problem is i want daily vwap but i would like for it to be anchored to the previous week, month, quarter and year high and low, is that possible @SleepyZ ?
 
Anchored VWAP indicator can useful for intraday trading. It can also be used on any timeframe including hourly, daily, and weekly. VWAP can be a great tool for analyzing the market, especially for day traders.

Here we have different Anchored VWAP indicators for ThinkorSwim. Feel free to test them out and use any that fits your trading style.

NfAwrZz.png

Hey BenTen, I'm wondering if there was a way I could make the DailyChart's Anchored VWAP, show up on the intraday chart?
 

VWAP Anchored_v02

Rich (BB code):
#START STUDY
#Anchored_VWAP2
#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.

#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;

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();
#END STUDY

Shareable Link: https://tos.mx/s17BmB

Video Tutorial

Hey Ben, Been using this indicator, thanks. I noticed that the VWAP Anchored_v02 lags in updating. I have to update chart (flip to another security and then back) to have the indicator update on chart. Has this been noticed? if so, is there anyway to resolve this? thanks.
 
Hey Ben, Been using this indicator, thanks. I noticed that the VWAP Anchored_v02 lags in updating. I have to update chart (flip to another security and then back) to have the indicator update on chart. Has this been noticed? if so, is there anyway to resolve this? thanks.

Modified the script to add missing logic from Mobius fractal pivots study. There does not appear to be any lag in plotting .

Ruby:
#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 used missing logic from Mobius fractal pivots

#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 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);

plot Dn        = Bn == PHBar;
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], 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 or Up then na else PC / VC;
plot VwapC2 = if Dn or Up then na else PC2 / VC2;
plot VwapH = if Dn then na else PH / VH;
plot VwapL = if Up then na else PL / VL;
plot VwapH2 = if Dn then na else PH2 / VH2;
plot VwapL2 = if Up 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();

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();
#END STUDY
;
 
Thanks. But looks like it's still not updating during session. Currently only using central/close vwap pivot.

The default setting is 20, so it does not update often. Try setting of 2 and you will be able to see dynamic update occurs..
 
The default setting is 20, so it does not update often. Try setting of 2 and you will be able to see dynamic update occurs..
right, i've tried various periods, 2 would not be ideal. it's not that it's not updating, but that it's not updating during session or dynamically- at all. i have to select another stock and return to see if there is a change which is not ideal.
 
right, i've tried various periods, 2 would not be ideal. it's not that it's not updating, but that it's not updating during session or dynamically- at all. i have to select another stock and return to see if there is a change which is not ideal.

Keep trying as what I posted is dynamically updating. Please understand that the pivots only plot after the number of bars before the pivot and after the pivot are true. So if the length is set to 20, then the pivot will plot the prior 20 bars with the arrow and vwap when those 20 future bars are true.
 
Without looking too closely, you are using gamma before it is defined. I just moved the vwap plot t o the bottom.

Code:
#yakBro intraday anchoredVWAP excluding extended hours volume 2019

declare upper;

def anchorTime = 0930;
def anchorEnd = 1600;

input ShowTodayOnly = yes;
def Today = if GetDay() == GetLastDay() then 1 else 0;
def postAnchorTime = if SecondsFromTime(anchorTime) >= 0 then 1 else 0;
def endAchorTime = if SecondsTillTime(anchorEnd) >= 0 then 1 else 0;

#plot anchorVWAP for intraday
def  volumeSum = CompoundValue(1, if postAnchorTime and endAchorTime then volumeSum[1] + volume else 0, volume);
def  volumeVwapSum = CompoundValue(1, if postAnchorTime and endAchorTime then volumeVwapSum[1] + volume * vwap else 0, volume * vwap);





#Inputs:
input nFE = 8;#hint nFE: length for Fractal Energy calculation.
input AlertOn = no;
input Glength  = 13;
input betaDev =  8;
input data = close;

def w = (2 * Double.Pi / Glength);
def beta = (1 - Cos(w)) / (Power(1.414, 2.0 / betaDev) - 1 );
def alpha = (-beta + Sqrt(beta * beta + 2 * beta));
def Go = Power(alpha, 4) * open +
             4 * (1 – alpha) * Go[1] – 6 * Power( 1 - alpha, 2 ) * Go[2] +
             4 * Power( 1 - alpha, 3 ) * Go[3] - Power( 1 - alpha, 4 ) * Go[4];
def Gh = Power(alpha, 4) * high +
             4 * (1 – alpha) * Gh[1] – 6 * Power( 1 - alpha, 2 ) * Gh[2] +
             4 * Power( 1 - alpha, 3 ) * Gh[3] - Power( 1 - alpha, 4 ) * Gh[4];
def Gl = Power(alpha, 4) * low +
             4 * (1 – alpha) * Gl[1] – 6 * Power( 1 - alpha, 2 ) * Gl[2] +
             4 * Power( 1 - alpha, 3 ) * Gl[3] - Power( 1 - alpha, 4 ) * Gl[4];
def Gc = Power(alpha, 4) * data +
             4 * (1 – alpha) * Gc[1] – 6 * Power( 1 - alpha, 2 ) * Gc[2] +
             4 * Power( 1 - alpha, 3 ) * Gc[3] - Power( 1 - alpha, 4 ) * Gc[4];
# Variables:
def o;
def h;
def l;
def c;
def CU1;
def CU2;
def CU;
def CD1;
def CD2;
def CD;
def L0;
def L1;
def L2;
def L3;



# Calculations
o = (Go + Gc[1]) / 2;
h = Max(Gh, Gc[1]);
l = Min(Gl, Gc[1]);
c = (o + h + l + Gc) / 4;
def gamma = Log(Sum((Max(Gh, Gc[1]) - Min(Gl, Gc[1])), nFE) /
        (Highest(Gh, nFE) - Lowest(Gl, nFE)))
            / Log(nFE);

L0 = (1 – gamma) * Gc + gamma * L0[1];
L1 = -gamma * L0 + L0[1] + gamma * L1[1];
L2 = -gamma * L1 + L1[1] + gamma * L2[1];
L3 = -gamma * L2 + L2[1] + gamma * L3[1];
if L0 >= L1
then {
    CU1 = L0 - L1;
    CD1 = 0;
} else {
    CD1 = L1 - L0;
    CU1 = 0;
}
if L1 >= L2
then {
    CU2 = CU1 + L1 - L2;
    CD2 = CD1;
} else {
    CD2 = CD1 + L2 - L1;
    CU2 = CU1;
}
if L2 >= L3
then {
    CU = CU2 + L2 - L3;
    CD = CD2;
} else {
    CU = CU2;
    CD = CD2 + L3 - L2;
}

plot anchorVWAP = if ShowTodayOnly and !Today then Double.NaN else if anchorTime then volumeVwapSum / volumeSum else Double.NaN;
anchorVWAP.SetStyle(Curve.FIRM);
anchorVWAP.SetLineWeight(2);


anchorVWAP.DefineColor("Up", GetColor(1));
anchorVWAP.DefineColor("Down", GetColor(0));
anchorVWAP.AssignValueColor(if gamma < .5 then anchorVWAP.Color("Down") else anchorVWAP.Color("Up"));

This is phenomenal! Thank you! Im using it to identify periods of choppiness (when Gamma is above 0.618) - is there a way you can possible add different aggregation periods to it? I'd like to use this indicator to get a better understand of choppiness on a wider time frame without having to toggle back and forth.

Thanks!!
 
when those 20 future bars are true.
I think this is where i see the problem. it looks like another time based issue with TOS. if the line paints after the fact, the initial move is already gone. may be ok for higher timeframes, not great for lower ones. thanks though for the insights.
 
Here is a scan for stocks that crosses ABOVE the previous day's VWAP. Place this code in the scanner and run this on a daily aggregation. I just ran this on the S&P 100 (Daily) and obtained 46 results

Code:
# Scan for crosses above previous day's VWAP
# tomsk
# 1.21.2020

# Run on a daily aggregation

def cap = getAggregationPeriod();
def yyyyMmDd = getYyyyMmDd();
def periodIndx = yyyyMmDd;
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def VWAP = price;

plot scan = close crosses above VWAP[1];
# End Scan for crosses above previous day's VWAP
tomsk, to use this Scan first needs to be imported as a Study or I can just use it as Scan?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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