Stock Buddy or When EzForecasting met Yahoo Finance

June 28, 2015

Contents

1 Introduction

!--l. 47--

As we already discussed elsewhere (http://bntech.info), EzForecasting is a general purpose, easy-to-use, easy-to-interpret, ”fire-and-...

June 28, 2015

Contents

1 Introduction

As we already discussed elsewhere (http://bntech.info), EzForecasting is a general purpose, easy-to-use, easy-to-interpret, ”fire-and-forget” forecasting tool. Since one of our design goals was EzForecasting to be robust and reliable we also needed a suitable testbed to debug and compare various algorithms. So we asked ourselves where one could find really ”difficult” time series? Well, this question is an easy one - the stock market is known to be an inexhaustible source of ”hard nuts to crack”. Of course we were well aware how ambitious (some authors call it naive) it is to use a relatively simple univariate model to the stock market data, but "the way we see it, if you’re going to test a time series tool, why not do it with some style"? So that’s how Stock Buddy was born - as a testbed for our forecasting development. Soon we realized, that Stock Buddy is a project that not only makes a lot of fun, but is also a platform that gives some insight into the dynamics of the stock market. Therefore we decided to make the code available on the web, with the hope that it might be useful to someone else.
What does Stock Buddy do? Well, as you can see from the code, it is a quite simple application that does the following:

  • download and visualize the history data from Yahoo finance

  • predict and plot the future stock values

  • let you play with the control settings of EzForecasting and see how stable the prediction is

In the next sections we will say a few words about each of these functionalities.

2 Downloading Yahoo finance data

There are plenty of C# examples awailable on the web, showing how to download history data from Yahoo finance. In our project we are going to use the classes:

  • YahooDataCollector - to download and parse the data as well as

  • YahooCodes - to hold all of the about 20 000 stock names and codes supported by Yahoo

2.1 YahooDataCollector

The core method here is

private static string GetResponse(string url)
{
...
using (var client = new WebClient())
{
...
return client.DownloadString(url);
}
...
}
The argument is a string of the following format:

http://ichart.finance.yahoo.com/table.csv?s={0}&a={1}&b={2}&c={3}&d={4}&e={5}&f={6}&g=d&ignore.csv.

The format parameters are:

  1. stock name, e.g. MSFT for Microsoft or KZX.V for KazaX Minerals Inc.

  2. from day

  3. from month

  4. from year

  5. to day

  6. to month

  7. to year

This function returns a string, which looks like this:

Date,Open,High,Low,Close,Volume,Adj Close
2015-06-22,1.10,1.13,1.05,1.07,510900,1.07
2015-06-19,1.02,1.10,0.983,1.10,558300,1.10
...
This string is not really convenient to work with and therefore we parse the columns in order to get the values of each individual column. This we will do with the help of the struct
public struct YahooFinance
{
public string Symbol;
public decimal Open;
public decimal Close;
public decimal High;
public decimal Low;
public decimal Volume;
public DateTime Date;
}

and the snippet below shows how we parse the data

var provider = new CultureInfo(”en-US”, true);
var response = GetResponse(url);
var list2 = new List YahooFinance ();
using (var reader = new StringReader(response))
{
reader.ReadLine();
while (reader.Peek() > -1)
{
string[] strArray = reader.ReadLine().Split(new char[] { ’,’});
DateTime time2 =
DateTime.Parse(strArray[index].Replace(””, ”, ”), provider);
var yahooItem = new YahooFinance
{
Date = time2,
Open = Convert.ToDecimal(strArray[1 + index]),
High = Convert.ToDecimal(strArray[2 + index]),
Low = Convert.ToDecimal(strArray[3 + index]),
Close = Convert.ToDecimal(strArray[4 + index]),
Volume = Convert.ToInt64(strArray[5 + index])
};
list2.Add(yahooItem);
}}

2.2 YahooCodes

This all is very nice, however we need to know the stock name in the first place (this is the first parameter to format the URL argument of GetResponse()). Actually we need to know all of the stock names and their descriptions. The class YahooCodes in its member string codes holds all of the stock names that as of June 2015 are supported by Yahoo finance. Each line of this string represents one stock e.g.

DPSM?3D Pioneer Systems, Inc.

stands for 3D Pioneer Systems, Inc. and the stock name is DPSM. The ”?” sign is used as a separator.
Later on we are going to initialize the entire list with a code similar to the snippet below:
YahooCodes yc = new YahooCodes();
using (StringReader sr = new StringReader(yc.codes))
{
while (sr.Peek() >= 0)
{
string[] strArray = sr.ReadLine().Split(new char[] ’?’ );
comboBox1.Items.Add(strArray[1]);
codes.Add(strArray[0]);
}
}
Here comboBox1 is the control, which diplays all available stocks with their human-readble names and List<string> codes is the holder for the stock names, which we are going to use when calling GetResponse(). The initialization takes place in the method FillTickerList() of the Form1 class. On the main GUI the list with the stocks is shown in the left part of Fig.1


PIC

Figure 1: The stocks list in the left, the history data (bottom right) and the predicted values (upper right).


3 Predicting the future values and playing with the controls

Thanks to the intentionally made simple interface of EzForecasting there is no much that is needed to be tuned or adjusted. Let us have a closer look at the constructor
(please refer to this document http://bntech.inf for details):
EzForecasting(double[] X, int StepsFrwd, int NumHarms = 1, int PolyFitOrd = 5, double CB = 0.95)
with the these arguments:

  • X - the raw data

  • StepsFrwd - specifies the prediction range, how many steps into the future.

  • NumHarms - the maximum number of seasonal components, which should be eliminated. The actual number of eliminated seasonal components might be equal or less than NumHarms.

  • PolyFitOrd - the maximum order of the polynomial used for trend elimination. The actual order of the subtracted polynomial might be equal or less than PolyFitOrd.

  • CB - the confidence bound used for the calculation of the upper- and the lower confidence bounds of the predicted future values.

The only available public method is:

int Forecst(int NumberPastVals2Consider, bool UsePEM = false)

  • NumberPastVals2Consider - this is the number of the historical data used for the prediction.

  • UsePEM - if set to TRUE, the actual number of the historical data used for the prediction will be estimated using prediction error minimization (PEM). In this case NumberPastVals2Consider specifies the upper limit.

Therefore we are going to use the controls as shown in Fig.2.


PIC

Figure 2: The configuration of the controls on the main panel.


In summary the configuration looks as follows:

  • parameter NumHarms - control hScrollBar3.

  • parameter PolyFitOrd - control hScrollBar4.

  • parameter CB - control hScrollBar2.

  • parameter NumberPastVals - control hScrollBar1.

  • parameter UsePEM - control checkBox1.

The forecasting is then just a piece of cake:

  1. create EzForecasting object

    EzForecasting ezf = new EzForecasting(ld.ToArray(), 50, Int32.Parse(NumHarmtxt.Text), Int32.Parse(TrendOrdTxt.Text), Double.Parse(CBtxt.Text) / 100.0);

  2. call the forecasting method:

    int n = checkBox1.Checked? hScrollBar1.Maximum:Int32.Parse(nTxt.Text);

    n = ezf.Forecst(n, checkBox1.Checked);

What still needs to be done is to plot the predicted values from ezf.FutureValues. The complete forecasting procedure from downloading the data to visualizing the predicted values is hooked with the SelectedIndexChanged event of the combobox, which calls the AnalyseWebData() method of the Form1 class.

4 Summary

We have discussed how to setup an application which, with the help of EzForecasting, can download stock history data from Yahoo finance and predict the future values. Have fun ...::-)

Go To Product

More
SEO by smuggler@abv.bg . . . . . . . . . . . . . . . . . .news sports rent a car . . . . . . .

...................................