Showing posts with label Yahoo Financial API. Show all posts
Showing posts with label Yahoo Financial API. Show all posts

Friday, September 23, 2011

Yahoo Download Stock Data

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.

Implementation

First we define an Enum that holds different period types. The name of the class "PeriodType".
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


In order to test I created a console application that will print  the downloaded data to console.
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 !

Wednesday, September 21, 2011

See stock on your screen.

Preface

I think that if you intrested in stock trading , and you want to see the graph of you favorite stock,
you will like this blog for sure !


Please see the GUI of our next program.
We will build a program that will display a stock data. In the progran you will enter Ticker Name, From Date, End Date and the program display the data in a graph.We will use  Yahoo download component  that I posted in other blog.




Desing

As every good design we will start with UML


As we see in the picture we defined an Interface that called "ZedGraph" . Zedgraph expose common properties of our Graph object . Please see below several of them :
  • Description - A description of what this demo is showing.
  • Title - Graph Title
Than we will define base graph "BaseGraph" for all stock graphes. The class is abstract so no one can instance it. The class is implemented the interface that we created above so he must impement
properties that we defined in interface. The main public function of the class is "AddLine" . The function
know how to display the data in the graph.

In order to show the data we must created a class that will be inherited from the base.
So we will create class "SingleGraph". The graph will inherit from "BaseGraph" and the class will be responsible for charting.

In our window form class in order to show the graph we will implement following code

     YahooHistoryDownloader yahoo = new YahooHistoryDownloader();
            List<StockDownloader.Bar> list = yahoo.GetBarHistory(stockName, from, toDate);
            if (list.Count > 0)
            {
                singleGraph.ClearGraph();
                singleGraph.AddLine(singleGraph.GraphPane, list, Action.Add, Color.Red, stockName);
                singleGraph.ZedGraphControl.AxisChange();
                singleGraph.ZedGraphControl.Refresh();


Our task is complete I prepare for you code that you can play with it .


Code can be downloaded from this location.

Summary

We wanted to see the stock data and see how our stock do today.
So we created simple design how to do it and implement it as window form.
In my next blogs we will define stock with indicator, so it will be other graph type and
more intresting.