Preface
Almost everyone knows that Yahoo has API to retrieve financial data.We will write a client that will
consume Yahoo API to retrieve history stock data. Let's start to develop set of classes
that will handle required functionality.
consume Yahoo API to retrieve history stock data. Let's start to develop set of classes
that will handle required functionality.
Implementation
Yahoo stock data can be retrieved for various period types. It can be daily,monthly and etc..
Than we will define a class that holds information about single piece of data.
If we will request daily data , than class will holds single day, and if we request data by month
it will holds single month . The class name "Bar".
The main class that uses Yahoo API is class that named "YahooHistoryDownloader".
The class knows how to retrieve a stock data for different periods and different intervals.
The main function "GetBarHistory" .
Input parameters:
- string stockName - ticker name of the stock . "GOOG" for example.
- PeriodType periodType - enum that represent interval
- DateTime frDate - start date
- DateTime toDate - from date
The test function knows how to download data for the half year.
private static void GetDataFromStock(string stockName,PeriodType periodType)
{
YahooHistoryDownloader historyDown = new YahooHistoryDownloader();
List<Bar> listBar = historyDown.GetBarHistory(stockName,periodType, DateTime.Now.AddMonths(-6), DateTime.Now);
foreach (Bar bar in listBar)
Console.WriteLine(bar);
}
In order to use it
string stockName = "GOOG";
GetDataFromStock(stockName, PeriodType.Weekly );
Than you can collect the stock data and develop your trading strategy
Here is UML
All the code can be downloaded from here - Download
Please see how this component can be used to display stock graph in my next blog Click here !
Hello I'm looking to implement a way to have my performance tracked on my site at http://www.stocksandstocks.com . I'm looking for the stock portfolio to be as up to date as yahoo streaming quotes are. I want the P/L to be calculated automatically as the values change on yahoo. In a seperate piece I'm looking to stream a few pivot levels onto my site. Can you email me at chris@stocksandstocks.com if you can help?
ReplyDeleteThanks
Fantastic - I've never used the WebRequest stuff in .NET before.. I recently realised that I need to read current stock prices for a little program I'm writing. Your code has come to the rescue !
ReplyDeleteMANY THANKS !!
-Jason