Getting Stock Prices with F# (2)

All right now.  Let's go grab some data.  Again, I use a lot of what is in Luca Bolognese's WebLog.

First, I create a function span that turns simple entries for the year, month and day, for both the start and the end dates, to a type Span:

let span sy sm sd ey em ed = {Start = new DateTime(sy, sm, sd); End = new DateTime(ey, em, ed)}

So, let's say I want to get data for AT&T from the May 1, 2009 to May 15, 2009.  I create my Span and create a variable ticker equal to AT&T's stock ticker:

let aSpan = span 2009 5 1 2009 5 15
//Ticker for AT&T is T
let ticker = "T"

I then need to create a URL to send a request to Yahoo:

let commonUrl ticker span =
    @"http://ichart.finance.yahoo.com/table.csv?s=" + ticker + "&a="   
    + (span.Start.Month - 1).ToString()
    + "&b=" + span.Start.Day.ToString()
    + "&c=" + span.Start.Year.ToString()
    + "&d=" + (span.End.Month - 1).ToString()
    + "&e=" + span.End.Day.ToString()
    + "&f=" + span.End.Year.ToString()

let priceUrl ticker span = commonUrl ticker span + "&g=d&ignore=.csv"

These two functions create a URL based on the ticker and the span passed in.

So we build our URL:

let aURL = priceUrl ticker aSpan
=
http://ichart.finance.yahoo.com/table.csv?s=T&a=4&b=1&c=2009&d=4&e=15&f=2009&g=d&ignore=.csv

Now that we have the URL we need to send the request to Yahoo and get back our data.

So we need three functions to help us do this:

(1) let loadWebStringAsync url =
    async {
        let req = WebRequest.Create(url: string)
        use! response = req.AsyncGetResponse()
        use reader = new StreamReader(response.GetResponseStream())
        return! reader.AsyncReadToEnd()}   

(2) let loadFromUrlAsync url =
    async {
        let! text = loadWebStringAsync url
        printfn "Output: %s " text
        return text }       

(3) let loadPricesAsync aURL = loadFromUrlAsync aURL

Function (1) makes an asynchronous Web request.  The use! statement stalls execution until a response is received.  That's the point of the (!).  The data that come back are fed into a StreamReader named "reader."  The function returns the reader, once all data are received.

Function (2) just calls loadWebStringAsync and puts the requested variable 'text'.  I then write it to the console.

Function (3) just calls function loadFromUrlAsync .

Next, I run the function loadPricesAsync.  Recall that the async wrappers do not actually execute anything until they are run -- they are merely storage placeholders for a set of instructions.

let results = Async.Run(loadPricesAsync aURL)

This is what we get:

Date,Open,High,Low,Close,Volume,Adj Close\n
2009-05-15,25.09,25.21,24.62,24.88,26496700,24.88\n
2009-05-14,25.42,25.42,24.90,24.98,30646700,24.98\n
2009-05-13,25.40,25.74,25.12,25.24,22668500,25.24\n
2009-05-12,25.49,25.90,25.31,25.73,23279000,25.73\n
2009-05-11,25.22,25.63,25.07,25.36,25613100,25.36\n
2009-05-08,25.70,25.83,25.06,25.25,41608900,25.25\n
2009-05-07,26.20,26.42,25.33,25.45,53202900,25.45\n
2009-05-06,26.64,26.78,26.27,26.69,24744600,26.69\n
2009-05-05,26.51,26.75,26.35,26.50,19289800,26.50\n
2009-05-04,26.11,26.73,26.11,26.69,27262100,26.69\n
2009-05-01,25.88,26.01,25.46,26.01,22080500,26.01\n

Our next chore is to parse this mess!

Print | posted @ Thursday, June 25, 2009 4:14 PM

Comments on this entry:

No comments posted yet.

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 7 and 8 and type the answer here: