Event Anchored VWAP For ThinkOrSwim

Yello

New member
I have the anchored VWAP study. But I need it to be anchored to an event, new low/high, gap up/down etc.

I can set the date in the vwap study. It works really well. However, start date is then same for every ticker, means I need to adjust the date in the ticker every single time.

Is there a way in thinkscript to have the date stay with the ticker?
 
Last edited by a moderator:
This is a popular anchored vwap study on this site that I adapted to start on a boolean condition (true/false).

In the code, I have identified some condition that you could test how it works. The default one is (he) hasearnings(), and displayed in the image below, using Ford's and Apple's earnings dates automatically.

To test the other conditions, comment (#) he and uncomment one of the others, etc., etc.

Screenshot 2023-07-24 103608.jpg
Code:
## 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;
input hidegaps = yes;

#Cond defined-------------------------------------------------------------------
def prevdayhigh = high(period = AggregationPeriod.DAY)[1];
def prevdaylow  = low(period = AggregationPeriod.DAY)[1];
def ema1 = expaverage(close, 8);
def ema2 = expaverage(close, 21);
def he = hasearnings();
def poc = reference VolumeProfile("time per profile" = "MONTH", "on expansion" = No, profiles = 1);
def cond =
he;
#ema1 crosses ema2;
#close crosses prevdayhigh;
#close crosses prevdaylow;
#close crosses poc;
#-------------------------------------------------------------------------------

def dir = cond;
def up = dir crosses above 0;
def dn = dir crosses below 0;
def h = if dir crosses 0 then Highest(high, nhighlow) else high;
def l = if dir crosses 0 then Lowest(low, nhighlow) else low;

def LocH = (h + (TickSize() * ticks)) * volume;
def LocL = (l - (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;
plot VwapC2;
plot VwapH;
plot VwapL;
plot VwapH2;
plot VwapL2;

if hidegaps {
    VwapC = if dn or up then Double.NaN else PC / VC;
    VwapC2 = if dn or up then Double.NaN else PC2 / VC2;
    VwapH = if dn then Double.NaN else PH / VH;
    VwapL = if up then Double.NaN else PL / VL;
    VwapH2 = if dn then Double.NaN else PH2 / VH2;
    VwapL2 = if up then Double.NaN else PL2 / VL2;
} else {
    VwapC = PC / VC;
    VwapC2 = PC2 / VC2;
    VwapH = PH / VH;
    VwapL = PL / VL;
    VwapH2 = PH2 / VH2;
    VwapL2 = 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();
 
Last edited by a moderator:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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