Reactive Framework Labs
This article is one from the “Processing Financial Data with Microsoft Reactive Framework (Reactive Extensions for .NET)” series of articles that demonstrate some basic applications of Rx framework to financial data. This article shows how to calculate a running VWAP (volume weighted average price over a defined window of time). Other articles in the series are:
Visual Studio 2008 solution containing full source code for all examples can be downloaded here.
Calculating VWAP/Sliding Window Implementation
We will use this simple “Trade” type to represent a trade event. To simplify things we will only consider Price and Volume:
class Trade { public double Price { get; set; } public int Volume { get; set; } }
The following “TradeWithVWAP” type wraps underlying trade and attaches running VWAP value to it. It is used for calculation output:
class TradeWithVWAP { public Trade Trade { get; set; } public double VWAP { get; set; } }
Our main module starts with a simple market data generator that produces infinite stream of random prices ($30 to $33 range) at random volumes (1,000 to 100,000 shares) at a random frequency (0 to 3 seconds between trades). BuildVwapFeed functions takes generated trade stream and converts it to another stream with running VWAP attached to every trade. And finally we simply subscribe to VWAP-wrapped feed and output the results.