Code: # # TD Ameritrade IP Company, Inc. (c) 2011-2019 # input numDevDn = -2.0; input numDevUp = 2.0; 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 = volumeVwapSum / volumeSum; def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0)); plot VWAP = price; plot UpperBand = price + numDevUp * deviation; plot LowerBand = price + numDevDn * deviation; VWAP.setDefaultColor(getColor(0)); UpperBand.setDefaultColor(getColor(2)); LowerBand.setDefaultColor(getColor(4));
If I'm reading thinkorswim's code correctly (and I'm not convinced I am), the formula basically boils down to: Code: sqrt(max(((total_volume * (vwap**2)) / total_volume) - (vwap**2), 0)) Using this I'm still not able to get the same figures as thinkorswim.
To be honest - I'm not convenient with this formula and calculation from TT site. I don't use this, so I just wanted to explain what I see there. But digging more into this - from the mathematical point I see it more like this: VWAP = sum(Pi x Voli) / sum(Voli) where: P - price (but here you need to be aware that it can be last price, but it cal also be (high+low+close)/3 or any other combination) Vol - volume i - iteration for first bar VWAP = P1 (so close or (hi+lo+cl)/3 or other combination) for next bars it's like in above equation deviation = sqrt(SumVar / N) where: SumVar = sum( (Pi - VWAP)^2 ) (so e.g. for three units, let say 3 bars from session start you have (P1 - VWAP)^2 + (P2 - VWAP)^2 + (P3 - VWAP)^2) P - as above i - as above VWAP - as above N - number of iterations Bands: VWAP + dev VWAP - dev VWAP + 2*dev VWAP - 2*dev VWAP + 3*dev VWAP -3*dev But it still will give you other results than those from TWS. So maybe it will be better if someone who use that kind of stuff and understand it well say something about it.
I ended up getting pretty close using: Code: vwap_sum += (((high + low + close) / 3) - vwap)^2 deviation = sqrt(vwap_sum / total_bars) I didn't get very far with the thinkorswim code, but MichalTr's comment helped a lot.
And here is Sierra Charts formula and explanation for Standard Deviation bands/lines: https://www.sierrachart.com/index.p..._Price_-_VWAP_-_with_Standard_Deviation_Lines https://www.sierrachart.com/index.php?page=doc/StudiesReference.php&ID=249&Name=Standard_Deviation
It is very, very complicated. I have studied futures trading for a long time. I also have a math degree and know probability, statistics, and coding. I use the futures premium to figure out what is happening in the market. Here is an example of it. I have lots of threads about it. You can read all of them in my threads and posts. Here are some. This thread shows you what I made with thinkScript and IBKR EXCEL API https://www.elitetrader.com/et/threads/pro-trading-tools.336836/ This thread I discuss the technique with Halil https://www.elitetrader.com/et/threads/relative-value-spreads-vs-auction-theory-market-profile-and-all-that.336011 This thread I talk about the edge I found https://www.elitetrader.com/et/threads/trading-risk-on-risk-off.336324/ It's a lot of work. But if you read the stuff you will learn a lot.
If you want to get into the details, this link shows how to build VWAP in ADL. https://community.tradingtechnologies.com/articles/3311/vwap-technical-indicator-for-adl-algos.html This screenshot shows a portion of the code. The rest may be found in TT Community (the link above).