roll_vwap | R Documentation |
Performs the same operation as function VWAP()
from package
TTR,
but using vectorized functions, so it's a little faster.
roll_vwap(ohlc, close = ohlc[, 4, drop = FALSE], look_back)
ohlc |
An OHLC time series of prices in xts format. |
close |
A time series of close prices. |
look_back |
The size of the look-back interval, equal to the number of rows of data used for calculating the average price. |
The function roll_vwap()
calculates the volume-weighted
average closing price, defined as the sum of the prices multiplied by
trading volumes in the look-back interval, divided by the sum of trading
volumes in the interval. If the argument close
is passed in explicitly,
then its volume-weighted average value over time is calculated.
An xts time series with a single column and the same number of
rows as the argument ohlc
.
# Calculate and plot rolling volume-weighted average closing prices (VWAP)
prices_rolling <- roll_vwap(ohlc=HighFreq::SPY["2013-11"], look_back=11)
chart_Series(HighFreq::SPY["2013-11-12"], name="SPY prices")
add_TA(prices_rolling["2013-11-12"], on=1, col="red", lwd=2)
legend("top", legend=c("SPY prices", "VWAP prices"),
bg="white", lty=c(1, 1), lwd=c(2, 2),
col=c("black", "red"), bty="n")
# Calculate running returns
returns_running <- ohlc_returns(xtsv=HighFreq::SPY)
# Calculate the rolling volume-weighted average returns
roll_vwap(ohlc=HighFreq::SPY, close=returns_running, look_back=11)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.