Aggregate. The VWAP function computes a volume-weighted average price for an instrument.
VWAP ( price_expression, volume_expression )
price_expression | a numeric expression containing a price to be incorporated into a volume-weighted average. |
volume_expression | a numeric expression containing a volume to be used in calculating a volume-weighted average. |
VWAP is a trading acronym for Volume-Weighted Average Price, the ratio of the value traded to total volume traded over a particular time horizon (usually one day). It is a measure of the average price a stock traded at over the trading horizon.
VWAP can be measured between any two points in time but is displayed as the one corresponding to elapsed time during the trading day by the information provider.
VWAP is often used in algorithmic trading. The VWAP is calculated using the following formula:
where:
PVWAP = Volume Weighted Average Price
Pj = price of trade j
Qj = quantity of trade
j = each individual trade that takes place over the defined period of time, excluding cross trades and basket cross trades.
Sybase extension.
The following example demonstrates how the VWAP function is used to calculate volume weighted averages for data in a CSV file,
where:
id = company trade symbol
x = stock price
-- create input stream schema CREATE SCHEMA InSchema ( id STRING, x FLOAT, y FLOAT ); -- create output stream schema CREATE SCHEMA OutSchema ( id STRING, x FLOAT, y FLOAT, vwap_result FLOAT ); -- create input stream CREATE INPUT STREAM StreamIn SCHEMA InSchema; -- create master window CREATE MASTER WINDOW ResultWindow SCHEMA OutSchema KEEP 5 ROWS ; -- create output stream CREATE OUTPUT STREAM StreamOut SCHEMA OutSchema; -- input stream read data from csv file by ReadFromCsvFileAdapterType adapter ATTACH INPUT ADAPTER ReadFromCSVFile TYPE ReadFromCsvFileAdapterType TO STREAM StreamIn PROPERTIES FILENAME = "$ProjectFolder\..\data\data.csv", TITLEROW = "false", TIMESTAMPCOLUMN = "false", RATE = "1", USECURRENTTIMESTAMP = "true" ; -- output stream write data to csv file by WriteToCsvFileAdapterType adapter ATTACH OUTPUT ADAPTER WriteToCSVFile TYPE WriteToCsvFileAdapterType TO STREAM StreamOut PROPERTIES FILENAME = "$ProjectFolder\..\data\result.csv" ; -- insert the calculated result to window INSERT INTO ResultWindow SELECT id,x,y, vwap(y,1) FROM StreamIn KEEP 5 ROWS; INSERT INTO StreamOut SELECT * FROM ResultWindow;Sample result:
Timestamp | id | x | y | vwap_result |
... | ... | ... | ... | ... |
1.25922E+15 | IBM | 75.15 | 9800 | 9800 |
1.25922E+15 | SWY | 21.7 | 400 | 5100 |
1.25922E+15 | MW | 37.08 | 200 | 3466.666667 |
... | ... | ... | ... | ... |