Time Weighted Average Price (TWAP) Indicator for ThinkorSwim

Ok, upon further research I have learned that there are multiple uses of TWAP just as there are for VWAP... There is the standard TWAP and then some traders also use an anchored TWAP of one day and three days... This is all something I've been wanting to research for a while now and I'm glad I took the time to do so today... The bog standard TWAP is as simple as the following code...

Ruby:
input length = 21;
plot twap = Average(ohlc4, length);

That's pretty simple, eh...??? It's when you start adding in anchoring that more code is required... I'm going to monitor a trade strategy this coming week that is based on TWAP/VWAP crossover... Didn't you refer to that strategy @germanburrito...??? The strategy I saw uses standard TWAP/VWAP rather than anchored...
 
I have been having success with the 9ema crossing over TWAP followed by VWAP crossing TWAP. both the 9EMA and VWAP has to be trading above TWAP. Backtest it on the 5m timeframe
 
Can someone please build an intraday scanner for when VWAP crosses TWAP on MTF ..with alerts..in a watchlist column within 2 bars...

Code:
######################################################################
input showlabels = yes;
######################################################################
# VWAP
input timeFrame = {default DAY, WEEK, MONTH};

def cap = getAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
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_VWAP = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price_VWAP), 0));

plot VWAP = price_VWAP;
VWAP.setDefaultColor(getColor(0));
AddLabel(Showlabels, " VWAP = " + Round(VWAP, 2), if close >= VWAP then color.LIGHT_GREEN else color.LIGHT_RED);
######################################################################
#TWAP
input price = FundamentalType.HLC3;
script TimeWAP {

    input price = hlc3;
    input timeFrame = {default Day, Week, Month, Chart};
  

    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx;
    switch (timeFrame) {
    case Day:
        periodIndx = yyyyMmDd;
    case Week:
        periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    case Month:
        periodIndx = RoundDown(yyyyMmDd / 100, 0);
    case Chart:
        periodIndx = 0;
}

    def newday = CompoundValue(1, periodIndx != periodIndx[1], yes);
    rec cumeprice = if newday then price else price + cumeprice[1];
    rec cumebarnumber = if newday then 1 else 1 + cumebarnumber[1];

    plot TWAP = Round(cumeprice / cumebarnumber, 4);

}

plot TWAP = TimeWAP(Fundamental(price));
TWAP.AssignValueColor(if TWAP > TWAP[1] then Color.LIGHT_GREEN else if TWAP is equal to TWAP[1] then Color.GRAY else Color.RED);
TWAP.SetStyle(Curve.SHORT_DASH);
TWAP.SetLineWeight(2);
TWAP.HideBubble();
AddLabel(Showlabels, " TWAP = " + Round(TWAP, 2), if close >= TWAP then color.LIGHT_GREEN else color.LIGHT_RED);
######################################################################
Addlabel(showlabels, if VWAP >= TWAP then "Above" else "Below", if VWAP >= TWAP then color.WHITE else color.DARK_GRAY);
######################################################################
 
Last edited:
@iZaTrader How to you intend to use it multi time frame, it doesn't really work well multi time frame I notice it works best on the daily no?
What I have noticed, is the cross intraday tends to trend the 9ema as long as VWAP is above TWAP... 1m, 5m and 15m.. if its not lined up sequentially . I don't take the trade.. I am developing my discipline using it. I have not used it with the standard deviation as of yet, just the midline..
 
What I have noticed, is the cross intraday tends to trend the 9ema as long as VWAP is above TWAP... 1m, 5m and 15m.. if its not lined up sequentially . I don't take the trade.. I am developing my discipline using it. I have not used it with the standard deviation as of yet, just the midline..
try this this i added a cud between them that turns red or green, i use it everyday to trade.
 
Can someone please build an intraday scanner for when VWAP crosses TWAP ..with an alerts..in a watchlist column within 2 bars...
and create a label when the cross do occur.. here is the code below

sy5wKYl.png


Code:
######################################################################
input showlabels = yes;
######################################################################
# VWAP
input timeFrame = {default DAY, WEEK, MONTH};

def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
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_VWAP = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price_VWAP), 0));

plot VWAP = price_VWAP;
VWAP.setDefaultColor(getColor(0));
AddLabel(Showlabels, " VWAP = " + Round(VWAP, 2), if close >= VWAP then color.LIGHT_GREEN else color.LIGHT_RED);
######################################################################
#TWAP
input price = FundamentalType.HLC3;
script TimeWAP {

input price = hlc3;
input timeFrame = {default Day, Week, Month, Chart};


def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case Day:
periodIndx = yyyyMmDd;
case Week:
periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case Month:
periodIndx = RoundDown(yyyyMmDd / 100, 0);
case Chart:
periodIndx = 0;
}

def newday = CompoundValue(1, periodIndx != periodIndx[1], yes);
rec cumeprice = if newday then price else price + cumeprice[1];
rec cumebarnumber = if newday then 1 else 1 + cumebarnumber[1];

plot TWAP = Round(cumeprice / cumebarnumber, 4);

}

plot TWAP = TimeWAP(Fundamental(price));
TWAP.AssignValueColor(if TWAP > TWAP[1] then Color.LIGHT_GREEN else if TWAP is equal to TWAP[1] then Color.GRAY else Color.RED);
TWAP.SetStyle(Curve.SHORT_DASH);
TWAP.SetLineWeight(2);
TWAP.HideBubble();
AddLabel(Showlabels, " TWAP = " + Round(TWAP, 2), if close >= TWAP then color.LIGHT_GREEN else color.LIGHT_RED);
######################################################################
Addlabel(showlabels, if VWAP >= TWAP then "Above" else "Below", if VWAP >= TWAP then color.WHITE else color.DARK_GRAY);
######################################################################
 
finally figured out way to implement twap to expand like vwap, its awesome

Code:
#German Burrito twap

declare upper;
input price = ohlc4;
input timeFrame = { Day, Week, Month,default Chart};

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case Day:
    periodIndx = yyyyMmDd;
case Week:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case Month:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
case Chart:
    periodIndx = 0;
}

def newday = CompoundValue(1, periodIndx != periodIndx[1], yes);
def cumeprice = if newday then price else price + cumeprice[1];
def cumebarnumber = if newday then 1 else 1 + cumebarnumber[1];
#def cumebarnumber2 = if newday then

def TWAP_price = Round(cumeprice / cumebarnumber, 4);
plot TWAP = TWAP_price;
def sDev = stdevAll(data = TWAP_price);

#
# TD Ameritrade IP Company, Inc. (c) 2011-2020
#





def RTH = timeframe;
def n = if RTH and !RTH[1]
           then 1
           else if RTH
           then n[1] + 1
           else n[1];
def Avg = (fold i = 0 to n
           with s
           do s + getValue(close, i)) / n;
def VWAP_ = (fold ii = 0 to n
            with ss
            do ss + getValue(twap, ii)) / n;
def StDev = Sqrt((fold iii = 0 to n
                  with sss = 0
                  do sss + Sqr(Avg - getValue(close, iii))) / n);

plot "0" = twap+ stdev*2;
plot "1" = twap- stdev*2;


# End Code
 
Hi Everyone , Appreciate if Any one share the Anchored VWAP TWAP combined strategy indicator with a scan TOS links or code.
 

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
622 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