roll_vwap: Calculate the volume-weighted average price of an _OHLC_ time...

View source: R/HighFreq.R

roll_vwapR Documentation

Calculate the volume-weighted average price of an OHLC time series over a rolling look-back interval.

Description

Performs the same operation as function VWAP() from package TTR, but using vectorized functions, so it's a little faster.

Usage

roll_vwap(ohlc, close = ohlc[, 4, drop = FALSE], look_back)

Arguments

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.

Details

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.

Value

An xts time series with a single column and the same number of rows as the argument ohlc.

Examples

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


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.