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:

Gravatar # re: Getting Stock Prices with F# (2)
by deexu at 7/16/2010 1:32 AM

great range of Ed Hardy products. Ed Hardy Women's Ellerise Lowrise Sneaker · Ed Hardy Women's
ed hardy jeans, ed hardy hoody, ed hardy shirt, ed hardy clothing, ed hardy cap, ed glasses, ed belts,
women fashion shoes, men's clothes. helping .perhaps you will like
Ed Hardy
Ed Hardy shoes
Ed Hardy shirts
Ed Hardy clothes
Ed Hardy clothing
Ed Hardy shoes
Don Ed Hardy is an American tattoo collector raised in Southern California
Ed Hardy Clothing,Christian Audigier,Ed Hardy Shoes,Ed Hardy Swimwear,Ed Hardy Hat,
ED Hardy Caps
Ed Hardy Sunglasses
Ed Hardy Wallets
EdHardy
Gucci outlet store online, numerous cheap Gucci bags, handbags, wallets, purses, totes, shoes on sale,
cheap prices and authentic qualities
gucci handbags
gucci jewelryTBGB VF V
  
Gravatar # re: Getting Stock Prices with F# (2)
by lv handbags at 7/24/2010 7:41 AM

Louis Vuitton Cruise 2010 Collection budgets are tight. We're all trying to save money. But that doesn't mean you can't treat yourself to a new fashion accessory.Now you can enjoy a top-quality, handcrafted bag that's virtually identical to a Louis Vuitton design for a fraction of the retail price!

Our Louis Vuitton replica louis vuitton cruise collection are destined to be classics in your wardrobe. Their understated elegance will turn heads. And so will our prices.

Now anyone can enjoy the high-end style of a Louis Vuitton Damier Canvas designer handbag, without breaking the bank.

Satisfaction guaranteed

Shop with confidence. louis vuitton damier canvas bag Your designer replica handbag purchase is protected by our 100% satisfaction guarantee.


  
Gravatar # re: Getting Stock Prices with F# (2)
by ckangel3 at 7/31/2010 12:26 AM

Tiffany jewelry
mous Tiffany Jewelry Shop which sell directly Tiffany Rings, Earrings, Necklaces, Pendants, Bracelets,
Bangles, Accessories.
Tiffany co jewelry
Tiffany
Tiffany Bracelets
affordable tiffany jewelry, beautiful discount tiffany rings, tiffany necklaces,tiffany Pendants,
tiffany earrings and tiffany Bracelets.
Tiffany Charms
Tiffany Earrings
Tiffany Necklaces
Tiffany Rings
Tiffany Jewelry Online,Discount Tiffany & Co Jewelry On Sale,you can buy cheap Tiffany silver.
All jewelry come with Tiffany package
Tiffany co jewelry
links of london
links london
links of london jewellery

  
Gravatar # re: Getting Stock Prices with F# (2)
by lv handbags at 8/2/2010 3:39 AM

Damier Canvas,Louis Vuitton,Damier Canvas Handbags and other well known designer brands Cheap Louis Vuitton Damier Canvas, wallets purses and other accessories, At the cheapest prices. Fake Damier Cavas is considered one of the most famous,have a good fame among the customers,and easily recognizable fashion brands in the world. Of course,Damier Canvas handbags,wallets,bags and purses are your essentials. Louis Vuitton Damier Canvas collection Many fashion people only love the Damier Canvas collection, not for other reasons, Replica LV Damier Canvas but only for the Replica Damier Canvas is the symbol of Luxury.
  

Your comment:

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