Saturday, 13 December 2008

Download share price history from Yahoo Finance

This problem took me a little while to figure out so I'm pretty happy that I've finally managed to get it working, and with a pretty simple solution. The crux of the problem was how to fetch from the internet a share price history for any given stock over a period of days, months or even years. As it turns out yahoo.finance offers a HTTP page which nicely returns this information for you. The format of the page address looks something like this:

http://ichart.yahoo.com/table.csv?s=MSFT&a=01&b=00&c=2007&d=31&e=11&f=2008&g=d&ignore=.csv

where:

s=the stock code and stock index if outside the US (i.e. MSFT or TEL.NZ)

a=the day of the month for the start date

b=the month of the year for the start date. Note that months seem to start from 0 so Jan is 0 and 11 is Dec

c=the year of the start date

d=the day of the end date

e=the month of the end date (also convert to go from 00 to 11)

f=the year of the end date

This web query can be used in a browser to return a list of share price data for the named stock from the start date to the end date. The list will show the opening value, closing value, and other data for each day in comma separated format. You simply need to parse this result set in a loop and use it as required.

In the sample code below I've made use of the NSURL and NSData objects to grab the data. Note, this code is reference only. It won't actually work, it's just to give you an idea of how this is done:


httpString = [NSString stringWithFormat:@"http://ichart.yahoo.com/table.csv?s=%@&a=%@&b=%@&c=%@&d=%@&e=%@&f=%@&g=d&ignore=.csv", stockCode, firstMonth, firstDay, firstYear, lastMonth, lastDay, lastYear];

NSURL *url = [NSURL URLWithString:httpString];

NSData *data = [NSData dataWithContentsOfURL:url];

NSString *s = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

NSArray *lines = [s componentsSeparatedByString:@"\n"];

NSArray *line;

for(i=0;i<[lines count];i++) {

 sCurLine = [lines objectAtIndex:i];

 line = [sCurLine componentsSeparatedByString:@","];


 /* the download should always contain 7 fields, but yahoo might change this at any time */

 if([line count]==7) {

  thisDate = [NSCalendarDate dateWithString:[line objectAtIndex:0] calendarFormat:@"%Y-%m-%d"];

  thisPrice = [[line objectAtIndex:4] floatValue];


  /* thisDate and thisPrice should now contain the date and price for this entry in the download (most recent first) */

  }

}

Tuesday, 2 December 2008

swishLounge

Having left my contract at Telecom I've had a little extra time to work on moneyLounge and I'm pretty happy with the results, it's starting to look and feel like a real application. The attached screenshot shows a few new features. I've added a ticker in the bottom left of the screen. This fades in and out details for various accounts/investments or categories that the user might be interested in. For example it can show price changes in an investment, how your net worth has changed in the month or how an account balance changed. The background of the ticker was created using a Quartz Composer Gradient interpolation and the effect is to give a subtle change of light intensity, as if a light is passing over the ticker. Most of the recent changes however have been made to the background code, mainly design tidy ups. I still need to implement a drag-drop facility to enable the user to grab an account or investment and drop it into the ticker to update it. Rather then using a Application Controller to manage all on screen objects I've divided the work up into a bunch of controllers for the different types of data objects, all of which are inherited from a generic moneyLounge controller. Some of the early objects I created have also had their interface tidied up and making changes or adding enhancements has become much easier with the cleaned up design. I've also begun methodical debugging of the code and am uncovering bugs underneath every stone that is unturned. Fortunately there's been no show-stoppers yet but next time I do something like this there will be a detailed design written before I start any coding.
Features requiring implementation include:
1. Complete the ticker
2. Various Universal graphs like Net Worth, Passive Income, Investment Performance.
3. File Export to QIF.
4. OCX load
5. Import share price history from CSV
6. Reinvestment dividends
7. Loan accounts
8. Color associations with Accounts and Investments.
9. (other stuff I haven't thought of just yet)
10. A design with the nine points I've mentioned above fleshed out properly