Thursday, June 26, 2014

NOTE: This article demonstrates how F# in conjunction with Reactive Extensions can be used to connect to live data streaming. The full project with example client can be found at This Github link.

Disclaimer

I do not take any responsibility of the code and any damages caused by it. Code which is provided AS IS is only for demonstration purposes and in no way encourages anyone to use Google Finance API for business.

Introduction

It has been a week learning F# and I have finally converted my C# code that uses Google Finance API to F#

Here is what my code does. In simple words, it gives you back the IObservable<Tick> for one security (or symbol)

Here is the screenshot of how it looks like

Here is how you use it as a library in C#

DataMachine machine = new DataMachine();
IObservable<Tick> appleTicker = machine.GetStockQuote("AAPL"); // Get Apple's stock prices
appleTicker.Subscribe(tick => {
// Everytime price changes, this piece of code will be called and an
// An object of Tick will be supplied as tick
// You can either add them to your Graph series to plot live chart
// Or save them to CSV
});

Google Finance API

The simple finance API by Google is just a URL that takes symbol in 'q' GET parameter (or query string parameter). The following will give you the current price data for Yahoo in Json format.

How it works ?

It creates a thread using TaskFactory which queries Google Finance every 1 second, fetches the price and if there is a change in price produces the output into the Observable. Google Finance API gives back Json object. I am using Json.Net library by James-Newton King to parse that.