Getting Stock Prices: F# & NHibernate (2)

In my last post, I showed how to connect and read from my database with FluentNHibernate.  So now I want to iterate over all the companies in my database:

let companyList = aSession.CreateCriteria(typeof<StockPrices.COMPANY>).List()

for someObj in companyList do
    let xCo = someObj :?> StockPrices.COMPANY
    printfn "Company Name: %s,  Ticker: %s" xCo.COMPANY_NAME xCo.COMPANY_TICKER

The first statement returns a complete list of companies in my database into a list collection.  The objects in the list are not of type StockPrices.COMPANY, however, but are of the generic object type.  This forces me to cast them in my do loop:

let xCo = someObj :?> StockPrices.COMPANY

As before, I need to use the downcast operator (:?>) to cast my generic object (someObj) into something I can use.

I run the program and it works.  Life is good.

The next thing I want to do is go get the stock prices for each of the companies in my database.

    let prices = GetPrices.getStockPrices xCo.COMPANY_TICKER 2009 1 1 2009 5 31
   
    for aObs : GetPrices.Observation in prices do
        let aPrice (aObs : GetPrices.Observation) =
            match aObs.Event with
            | GetPrices.Event.StkPrice(p) -> GetPrices.toFloat(p.Close)
            | GetPrices.Event.Dividend(d) -> GetPrices.toFloat(d)
            | _ -> -999.0
        printfn "Here's %s: %s %10.5f" xCo.COMPANY_NAME (aObs.Date.ToShortDateString()) (aPrice(aObs))
        ()

In the above snippet, I first call a function "GetPrices.getStockPrices,"  passing it a ticker and a date range (01/01/2009 to 05/01/2009).  The function reference refers to an F# module named "GetPrices" and the function getStockPrices in that module.

So let's see how to use modules in F#.

 I think the easiest way to create a module is to

(1)  Create a new file in your project;
(2)  use the key word "module" at the head of the file  (even before #light) and give it a name; and
(3)  move it up in the file order so that it is compiled prior to the calling program.

Piece of cake.

So, what I have done is taken all my code to get stock prices from Yahoo and wrapped it in a module called GetPrices.

My code is in an earlier post: Getting Stock Prices with F#: Results.

At the bottom of the module GetPrices, I add a function  getStockPrices.

let getStockPrices ticker y0 m0 d0 y1 m1 d1 =
    let aSpan = span y0 m0 d0 y1 m1 d1
    let aURL = priceUrl ticker aSpan
    let results = Async.RunSynchronously(loadPricesAsync aURL parsePrice)
    results

This is the function that I call from my main program to retrieve stock prices for a ticker over a date range (span)

The cool thing about modules is once they are included, you get intellisense for all your objects.  The functions and types in the module are all available without a dot (.) reference.  For example, in the snippet:

let prices = GetPrices.getStockPrices xCo.COMPANY_TICKER 2009 1 1 2009 5 31

my main program knows (from the F# compiler) that the variable "prices" is of type GetPrices.Observation listThere, were, however, a few things the compiler did not know.  I had to specifically 'type' the variable aObs to be of type GetPrices.Observation; I also had to define a more specific match to the Event type.  I had to use the notation GetPrices.Event.StkPrice(p) instead of just StkPrice(p).

Modules are a must use...pretty sweet.

Print | posted @ Monday, June 29, 2009 6:31 AM

Comments on this entry:

No comments posted yet.

Your comment:

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