Line plots of a single variable

In this section, we'll focus on an introduction to EasyPlot. So, we're going to be installing the gnuplot command-line tool and the EasyPlot Haskell library. EasyPlot is just a Haskell wrapper for gnuplot. It allows us to interact with the gnuplotting utility from within Haskell and our IHaskell environment. We need data, and for that we're going to be pulling data from Yahoo Finance. I like Yahoo Finance because I can obtain the full history of any publicly traded company; and in this section, we're going to end with our first plot. Let's go to our Terminal, but first we need to install gnuplot. The command is as follows:

Now, let's do a cabal update and then cabal install EasyPlot, as shown in the following example:

If you didn't get any error messages, that means you're ready to go. Now we need data; and, as I said earlier, I would like to use financial data from publicly traded companies on the New York Stock Exchange. For that, we're going to go to Yahoo Finance, and the first company that we would like to look at is Apple Corporation. You have to know the symbol for all the corporations in order to use Yahoo Finance, and the symbol for Apple is AAPL. So, type aapl into the search box and the first option is Apple, as shown in the following screenshot:

Once you click on AAPL, a page will open and you will see lots of information about the Apple stock, but what we are interested in is the Historical Data, so let's scroll down until we find the link for Historical Data and click on it, as shown in the following screenshot:

Once the page reloads, you're going to see one-year historical data for the stock. We are going to change the start and end dates for this range to be date well before the creation of the Apple company. So, we will say January 1, 1960. For the end date, we are going to select July 7, 2016. This is shown in the following screenshot:

We need to hit Apply, and then we have to hit Apply again. Once the page reloads, you should see a link for Download Data. Let's click on it, and we can see that we have the ability to download a CSV file called table.csv. Let's go ahead and download that file, as shown in the following screenshot:

Let's change the filename that Yahoo provides to something that is more relevant. We will rename the file from table.csv to aapl.csv, by using the command shown in the following screenshot:

Let's open the aapl.csv file, as shown in the following example:

You can see that the CSV file has a header line, as well as several columns of information including the date, the volume, and the adjusted close. Now, the date column is a text field, the volume column is an integer field, and everything else is a floating-point field. You should also notice that this file is in reverse chronological order, which means the most recent date in the file is first, and the very last entry in the file is December 12, 1980, as can be seen in the following screenshot: 

The last record in this file is the first day of trading, and we see that the adjusted trade value on December 12, 1980 was 43 cents. The most recent adjusted close for Apple was 98 dollars and 83 cents. What I would like you to do now is to convert this file to a SQLite3 database. Also, I would like you to find two other companies on Yahoo Finance, and repeat this process so that you create a database containing three companies and their entire histories. I have already done this as a prework, and I have created a database containing Apple, Google, and Microsoft. You can choose any company from Yahoo Finance.

Let's go over to our Jupyter notebook. I have created a database called stocks.sqlite3, which is provided along with the files for this book, and it's already in my data folder, as demonstrated by the following screenshot:

Let's begin with an analysis of that dataset. So, we will need to go back into our analysis folder. We're going to create a new Haskell notebook, and we'll call this StockAnalysis. Let's import a few libraries, as shown in following screenshot:

So, we have imported Graphics.EasyPlot, Database.HDBC, and Database.HDBC.Sqlite3. Two of those imports we've seen from our discussion on the SQLite3 database. The first import, Graphics.EasyPlot, is the one that we are introducing in this section. Let's make a connection to our database, as shown in the following screenshot:

Now, what we would like to do is our first plot of the Apple adjusted close. So, let's pull out the raw adjusted close data for Apple, as shown in the following screenshot:

We are going to call this aaplRaw. We are going to ask our database for all of the adjusted close. We will name our column adjclose in our Apple table, and now we need to convert this data to the Double type, as shown in the following screenshot:

You can see that aaplRaw is a two-dimensional array of SQL values, and we need to convert this to a single-dimensional array of the Double type. So, let's do that, as shown in the following screenshot.

We have mapped fromSql . head. It's only going to be a two-dimensional array with one column, so we can just call head on that one column to get the value. We'll convert that into a Double type. Let's look at those values that we're going to plot. This is a new command, as follows:

X11 means that we're going to plot directly to the X11 Windows environment, and plot requires that we have two coordinates: x values and y values. plot also requires that we have a list of tuples, where each tuple consists of an x value and a y value. So, we have zipped together our x values, which are simply going to be 0, -1, on to infinity.

The reason why we're using negative values is because our dataset is in reverse chronological order. And then, our y values are going to be the adjusted close data. So, it's going to take a moment to show the following plotting diagram:

You'll notice a few things. This is not a true line plot. This is a scatter plot with lots of + signs for all of our data points. For most of Apple's history, going back 9,000 trading days, the adjusted close has been less than $20; and it's only been within the last, say, 3,000 trading days that we've seen an increase in the adjusted close share price.

In our next few sections, we're going to talk about how we can clean this image up and make it publication-ready. We're going to be talking about moving averages, and we're going to be talking about feature rescaling. Our next section is going to be all about the moving averages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset