The Salty Economist

Things I Should Have Learned in High School
posts - 56, comments - 0, trackbacks - 0

June 2009 Blog Posts

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...

posted @ Monday, June 29, 2009 6:31 AM | Feedback (108) |

Getting Stock Prices: F# & NHibernate (1)

Ok. so now we a program that can retrieve stock prices and print them out.  But, what I really want to do is save them to a database.  So, let's see how to make F# work with NHibernate.  Let's start with something super simple. First, I create a simple class (in VB 'cause I know it better) -------------------------------------------------------------------------  Option Explicit On Namespace StockPrices     Public Class COMPANY         Private _ID As Integer         Private _CompanyName As String         Private _CompanyTicker As String         Public Overridable Property Id() As Integer             Get                 Id = _ID             End Get             Set(ByVal value As Integer)                 _ID = value             End Set         End Property         Public Overridable Property...

posted @ Sunday, June 28, 2009 10:28 AM | Feedback (1091) |

Getting Stock Prices with F#: Results

Here is the final program: ------------------------------------------------------------------------------ #light open System open System.Collections.Generic open System.IO open System.Data.SqlClient open System.Text.RegularExpressions open System.Net [<Measure>] type dollars [<Measure>] type volume let money (f:float) = f * 1.<dollars> let vol (f:float) = f * 1.<volume> let toFloat (someBucks : float<dollars>) =      someBucks / 1.0<dollars>         type Span = { Start: DateTime; End: DateTime } type Price = { Open: float<dollars>; High: float<dollars>; Low:float<dollars>; Close:float<dollars>; Volume: float<volume>} type Event =     | StkPrice of Price     | Split of float     | Dividend of float<dollars> type Observation = { Date: DateTime; Event: Event} let span sy sm sd ey em ed = {Start = new DateTime(sy, sm, sd); End = new DateTime(ey, em, ed)} let commonUrl ticker span =    ...

posted @ Saturday, June 27, 2009 9:10 AM | Feedback (682) |

Getting Stock Prices with F# (3)

We now have to parse our stock price data downloaded from Yahoo. We first create a function parsePrice that takes a row from the Yahoo download and parses it into the fields we case about. let parsePrice (line: string) =     let tokens = line.Split([|','|])     { Date = DateTime.Parse(tokens.[0]);       Event = StkPrice ({Open = money (Double.Parse(tokens.[1]));       High = money (Double.Parse(tokens.[2]));       Low = money (Double.Parse(tokens.[3]));       Close = money (Double.Parse(tokens.[4]));            Volume = vol (Double.Parse(tokens.[5]))})} The function line.Split() takes an array of characters to use as delimiters to split a string.  What you need to know here is that an array in F# is...

posted @ Friday, June 26, 2009 9:26 AM | Feedback (99) |

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...

posted @ Thursday, June 25, 2009 4:14 PM | Feedback (0) |

Getting Stock Prices with F# (1)

So how we get the stock prices for our F# program? Well Yahoo of course. Luca Bolognese's WebLog has a great writeup of how to download stock prices from Yahoo's finance site.  I have borrowed liberally from his work and made some tweaks of my own. The first part of my F# code is: #light open System open System.Collections.Generic open System.IO open System.Data.SqlClient open System.Text.RegularExpressions open System.Net [<Measure>] type dollars [<Measure>] type volume We immediately run into two statements for setting up units of measure.  The statement [<Measure>] type dollars identifies a unit of measure named dollars in our program. Now what are units of measure? In F#, you can set up floating point numbers as...

posted @ Thursday, June 25, 2009 11:20 AM | Feedback (598) |

Jacques Lake

June 21, 2009.  First hike of the season to Jacques Lake. 15 miles in and out, but well worth it.  Got caught in a downpour halfway home, but we had our ponchos. Here are a couple more: These are Venus Slippers which are not all that common up here.  Very Cool. Here is Mt Edith Cavell.  This is the view from our front porch at the Pine Bungalows. And  here's my girl  

posted @ Tuesday, June 23, 2009 9:06 PM | Feedback (614) |

The Raison d'être for F#

Well everything I've done up to now, I could just as easily done in VB or C#.  So what's the point? The thing that really caught my eye about F# was the chance to convert my single-threaded code into multi-threaded code with minimal effort.  I have a number of projects where I end up writing batch processes.  These processes often involve independent operations that could take advantage of multi-core or processor machines. The problem at hand fits the bill.  I have 100 industries that each have to have an index calculated; each index is separate from any other industries index.  Thus, I could...

posted @ Tuesday, June 23, 2009 8:38 AM | Feedback (101) |

Getting Started with F#(4)

OK, so now we have a function that will return a vector of prices for a given stock ticker, all indexed to the first price in the list. My strategy here will be: (1) to loop over all the companies in any specific industry and retrieve each company's indexed price data (2) load each company's data into a row of a two-dimensional array, where the columns are dates (3) once the array is loaded up, calculate the value of any given day as the average value across all the companies that have non-zero entries for the day. So here is my code: let BuildIndustryIndex id group_type_id industry_order...

posted @ Monday, June 22, 2009 5:05 PM | Feedback (657) |

The Trouble with Tuples

Tuples are bizarre - what can I say. So here's the scoop.  A Tuple is a variable that can have multiple values, but that does not have to be explitly defined. For example, I have a data point (stock price) that has both a date and a closing price.  I can use the following to instantiate an instance and assign it values: let aprice = Convert.ToDateTime("01/01/1990"), 25.50 Note, the values are separated by commas - commas are required.  The above statement says create an instance of the Tuple aprice that has a date of 01/01/1990 and a price of 25.50. If I wanted to add an opening...

posted @ Saturday, June 20, 2009 11:08 AM | Feedback (242) |

Getting Started with F# (3)

OK, so now we have database access.  What to do next? The main flow of the program is to loop over all 100 industries, and then for each industry create an index that is made up of all the stock prices for the companies in the industry.  For each company, I need to retrieve its historical stock prices (from 1990 forward).  Because I want each company to be weighted equally, I need to take the vector of prices and index them to the starting point.  In other words, I divide the vector of prices by the initial price, thereby creating an...

posted @ Friday, June 19, 2009 8:12 AM | Feedback (913) |

Getting Started with F# (2)

So I finally got my project created and saved in F# (see my prior post) I now need to do something. I figured the first thing I would bite off would be my database access.  This always seems to trip me up when I learn a new language. I know I will need ADO.Net, so the first thing I do is add a reference to my project for System.Data.Sqlclient.  (I am using MS SQL Server as my data store.) Here is my first F# Code! /---------------------------------------------------------------------------- #light open System open System.Collections.Generic open System.IO open System.Data.SqlClient let servername = "BIG_ROCK\LOGGERSEDGE" let dbname = "SMDATA" let connstr = "Data Source=" + servername + "; Initial...

posted @ Thursday, June 18, 2009 4:34 PM | Feedback (240) |

Getting Started with F#

So I had this session at DevTeach on F# and it seemed kind of cool.  What I really liked about was the possibility to build multi-threaded apps (without a lot of extra effort) to make full use of my PC's horsepower.  So the first thing I needed was a manageable project that was heavy on calculations.  I finally settled on a stock market application. This is the problem:  I have stock market data for 1,300 companies that goes back to 1990 -- basically 7,000 price observations.  The companies are grouped according to industries.  I have 100 industries with an average of 13 companies per industry. ...

posted @ Thursday, June 18, 2009 3:49 PM | Feedback (2912) |

Powered by:
Powered By Subtext Powered By ASP.NET