rowTwavg
First introduced in version 2.00.19/3.00.6
Syntax
rowTwavg(X, Y)
Please see rowFunctions for the parameters and calculation rules.
Details
Calculates the time-weighted average of X by row.
The formula is the same as twavg.
Parameters
X is of numeric type. It can be an array vector, a columnar tuple, or a matrix.
Y is of integral or temporal type. It can be an array vector, a columnar tuple, or a matrix. The non-NULL values in Y must be sorted in ascending order.
Returns
Returns a vector whose length is the same as the number of rows in the input arguments. Returns NULL for rows where the number of valid elements is less than 2.
Examples
Example 1: The columnar tuples store the price series and corresponding timestamps
for multiple stocks, where each row represents one stock. rowTwavg
is used to calculate the TWAP (time-weighted average price) for each stock over the
trading interval row by row.
// Price series of 3 stocks in the same trading interval, with each row in the columnar tuple representing one stock
prices = (25.10 25.15 25.08 25.20, 80.50 80.60 80.55 80.70, 12.30 12.28 12.35 12.32).setColumnarTuple!()
times = (09:30:00.000 09:30:05.000 09:30:12.000 09:31:00.000, 09:30:00.000 09:30:08.000 09:30:20.000 09:31:00.000, 09:30:00.000 09:30:03.000 09:30:10.000 09:31:00.000).setColumnarTuple!()
// Calculate the TWAP for each stock row by row
rowTwavg(prices, times)
// output: [25.0898,80.5533,12.3393]
Example 2: This example uses array vectors to store temperature readings and sampling
times for multiple devices, where each row represents one device and may contain a
different number of samples. rowTwavg calculates the time-weighted
average temperature for each device row by row.
// Temperature readings of multiple devices, with each row representing one device.
temp = array(DOUBLE[], 0, 3)
temp.append!([36.5 36.8 37.2 36.9])
temp.append!([40.1 40.5 40.3])
temp.append!([22.0 22.5 23.0 22.8 22.6])
ts = array(SECOND[], 0, 3)
ts.append!([08:00:00 08:00:10 08:05:00 08:10:00])
ts.append!([08:00:00 08:03:00 08:10:00])
ts.append!([08:00:00 08:02:00 08:04:00 08:06:00 08:10:00])
rowTwavg(temp, ts)
// output: [36.995,40.38,22.62]
