Stock Performance Analysis using Financial Functions for Python
Putting it to use

In our framework, we’ll use ffn for all data retrieval and manipulation, as well as matplotlib for the graphical approach.

%matplotlib inline is a ‘magic function’ in iPython, called with a command line style syntax. With this back-end, the output of plotting commands is displayed inline within front-ends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document. Analogously, %pylab inline is used to control the default size of figures in the notebook.

We’ll start using ffn while importing data for analysis. By default, ffn.get function uses Yahoo Finance to gather historical data on securities and futures negotiated in Capital Markets from all over the world. We will restrain our analysis example on four “Big Tech” companies, commonly called “GAFA” — Google, Apple, Facebook and Amazon.

This should give us a data frame containing a ‘date’ indexed column, already in datetime format, along with four other columns, each providing the adjusted closing price of the chosen stocks.

As a start, a simple line chart can tell us how all these securities performed over the period.

Note: If you’re interested in a interactive chart approach, checkout my early post.

The .rebase() function allows us to plot the time series from a common starting point, so that we can see how each stock performed relative to each other.

Our next step should be focused on the returns of each security. Tackling frequency distribution, histograms are powerful tools for distinguishing shape, identifying central points, variation, amplitude and symmetry of the returns.

Since we’re looking at four companies from the technology sector, naturally, they follow a similar return pattern. This may be verified by finding the correlation between them, which can be observed by plotting a heat map, as follows:

Now, for ffn’s main performance measurement, we can calculate and display a wide array of statistics using the calc_stats() method.

Although the result interpretation requires a bit of statistical knowledge, this large set of metrics can provide us simple conclusions, such as: Amazon stocks had the best performance between the Big Techs analysed, since, in the given period of the time series (nearly 65 months), the company provided a 671.14% return to it’s share-holders. Apple presented a 182.03% return, followed by Google and Facebook’s almost identical return rate of 139.95% and 139.85%, respectively.

From there, we can also access the underlying performance stats for each series, including monthly returns, drawback charts and more.

Conclusion

Financial Functions for Python provides a huge set of useful information regarding stock performance in a very simple and straight-forward way. Combining it with matplotlib, we can intuitively detect stock trends or patterns, as well as technically interpret statistical insights to tell a enlightening story.
['Caio Milani']
['Programming', 'Technology', 'Finance', 'Business', 'Data Science']