The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,551,611
since: 19 Jan 2005

CLINQ v1.1 - Extending the AggregateViewAdapter to Create Continuous VWAP

posted Thu 17 Apr 08

Volume Weighted Average Price is a financial figure that is often used as a gauge to determine a more realistic price for a given stock. It works by taking a history of ticks and for each stock, multiplying the quantity by the price (this is why it's called volume weighted). The final sum is then divided by the total quantity traded in the time series for that stock.

I was looking at the CLINQ implementation of continuous aggregates after I implemented a continuous standard deviation and realized that it was incredibly easy for individual application developers to build their own continuous aggregates that sit on top of the CLINQ framework. This way, you get to borrow all of the listening, monitoring, and listener-chaining functionality for free and just implement your own domain-specific aggregation logic.

Within CLINQ there is a new class called AggregateViewAdapter<T>. This class is responsible for listening to changes within a source collection and to changes within items in that collection. It also properly deals with subscribing to events from newly added objects in the collection and unsubscribing to removed objects. It also uses weak references to ease the burden on the garbage collector when using CLINQ. All of this functionality is done for you, leaving just the aggregation logic left to implement.

To show you how easy it is, here is the full source code for the ContinuousVwapMonitor class:

public class ContinuousVwapMonitor<T> : 
AggregateViewAdapter<T> where T:INotifyPropertyChanged
{
    private ContinuousValue<double> _output;
    private Func<T, double> _priceSelector;
    private Func<T, int> _qtySelector;
    public ContinuousVwapMonitor(ObservableCollection<T> input,
        Func<T, double> priceSelector,
        Func<T, int> quantitySelector,
        ContinuousValue<double> output)
        : base(input)
    {
        _output = output;
        _priceSelector = priceSelector;
        _qtySelector = quantitySelector;
	ReAggregate();

    }
    protected override void ReAggregate()
    {           
        int count = Input.Count;
        double weightedPrice = 0.0;
        int totalQuantity = 0;
        for (int x = 0; x < count; x++)
        {
            weightedPrice += _priceSelector(Input[x]) *
                _qtySelector(Input[x]);
            totalQuantity += _qtySelector(Input[x]);
        }
        double vwap = weightedPrice / totalQuantity;
        SetCurrentValue<double>(_output, vwap);
    }
}

As input to its constructor, this class takes a lambda function that selects a price value given a Tick model object, a lambda function that selects a quantity value given a Tick model object, the output value in terms of a ContinuousValue<double>, and the input collection. To get ContinuousVwap as an available option as an extension method on the ObservableCollection<T> class, you need to implement a quick language extension like this one:

public static class VwapExtender
{
    public static ContinuousValue<double> ContinuousVwap<T>(
        this ObservableCollection<T> input,
        Func<T, double> priceSelector,
        Func<T, int> quantitySelector) where T: INotifyPropertyChanged
    {
        ContinuousValue<double> output = new ContinuousValue<double>();
        ContinuousVwapMonitor<T> vwapMonitor =
            new ContinuousVwapMonitor<T>(input,
priceSelector, quantitySelector, output);
        return output;
    }
}

Now that you have extended ObservableCollection<T>, you can write Continuous Queries that also support Continuous Aggregates, giving you incredible power and flexibility in your UI, not to mention making your code easy to read, easy to build, easy to troubleshoot, and easy to maintain:

ContinuousCollection<Tick> appleTicks = 
from tick in TickFeed.AllTicks
    where tick.Symbol == "AAPL"
    select tick;




ContinuousValue<double> appleVWAP = appleTicks.ContinuousVwap<Tick>(
    tick => tick.Price,
    tick => tick.Quantity);

Now you can place the appleVWAP value as a bound object anywhere in your WPF GUI and it will automatically update as your ticker feed updates, which causes your AAPL subset feed to update. All of that taken care of for you automatically. You can even use the scalar appleVWAP value to store a history of VWAP-over-time values by listening to the property changed event on the appleVWAP object:

appleVWAP.PropertyChanged += new PropertyChangedEventHandler(vwap_Changed);
....
void vwap_Changed(object sender, PropertyChangedEventArgs e)
{
    _vwapHistory.AddItem((sender as ContinuousValue<double>).CurrentValue);
}

This VWAP history could be part of another ObservableCollection (which, to blow your mind, could also be queried using CLINQ!) which is then bound to a templated grid that converts the history into a line chart, plotting VWAP-over-time automatically.

Not only is CLINQ a powerful extension to LINQ, but extending CLINQ yourself with domain-specific logic is easy and can add tremendous value.

tags:            

links: digg this    del.icio.us    technorati    reddit




Tag Related Posts

CLINQ v1.1.0.0 Released!

Fri 02 May 08 5:38 P GMT-05
tags:          

CLINQ v1 Demo - Network Message Filtering

Wed 09 Jan 08 7:47 P GMT-05
tags:        

Continuous LINQ v1.0.0.0 Released

Tue 08 Jan 08 4:53 P GMT-05

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

Unexpected and Unsafe behavior in LINQ

Wed 14 Nov 07 8:09 P GMT-05
tags:    

Building a Ledger Style for WPF Grids

Tue 21 Aug 07 3:30 P GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Continuous LINQ

Mon 06 Aug 07 1:21 P GMT-05
tags:    

Dynamic, Observable LINQ Views

Tue 31 Jul 07 1:21 A GMT-05
tags:        

My first "Acropolis" Application

Mon 04 Jun 07 1:40 P GMT-05
tags:      

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05

Installing Orcas Beta 1 - VMware Style

Mon 23 Apr 07 12:16 P GMT-05

Will Silverlight be DOA?

Mon 16 Apr 07 8:02 P GMT-05

Exploring the MVC Pattern in WPF

Tue 10 Apr 07 12:51 P GMT-05
tags:                      

WPF Bindings == WTF Bindings?

Mon 12 Mar 07 6:31 P GMT-05

On MUDs

Thu 08 Mar 07 5:00 A GMT-05
tags:                    

Visual Studio "Orcas" - March CTP is Available

Wed 28 Feb 07 12:28 P GMT-05
tags:            

Objective-C Categories vs C# 3.5 Language Extensions

Mon 26 Feb 07 1:05 P GMT-05
tags:                

Cocoa Programming vs. WPF : NIB vs XAML

Tue 20 Feb 07 2:09 P GMT-05

Cocoa Bindings vs. WPF Binding

Thu 15 Feb 07 5:41 P GMT-05
tags:                

The Latte Effect

Thu 21 Dec 06 7:29 P GMT-05

Ulysses Agenda Makes Redmond Developer News

Wed 29 Nov 06 7:10 P GMT-05
tags:                

Ulysses Agenda - Network Engine Test 1

Mon 09 Oct 06 2:26 A GMT-05
tags:              

Ulysses Agenda : First Cut Networking Design

Thu 14 Sep 06 12:46 A GMT-05
tags:                  

First Impressions of Windows Vista RC1

Thu 07 Sep 06 1:30 P GMT-05
tags:                      

Localizing a WPF Application

Tue 22 Aug 06 11:39 A GMT-05
tags:            

WPF Slide Show and Photo Album

Fri 18 Aug 06 6:48 P GMT-05

.NET Framework 3.0 June CTP is out!

Fri 23 Jun 06 6:23 P GMT-05
tags:                

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

Tech-Ed 2006 - Session Reviews

Tue 13 Jun 06 6:22 P GMT-05
tags:                    

Tech-Ed 2006 Day 1 - Registration Day

Sun 11 Jun 06 7:17 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags:                  

Language Extensions in C# 3.0

Wed 31 May 06 2:17 P GMT-05

Lambda Lambda Lambda

Sun 21 May 06 1:01 A GMT-05

The Adventures of LINQ (Not Zelda)

Fri 19 May 06 11:21 P GMT-05
tags: