Trading Strategy Analysis using Python and the FFN Package – Part 2

Hi all, this is the second part to the “Trading Strategy Analysis using Python and the FFN Package” post (the first part can be found here).

Last time we went over the use of the “PerformanceStats” object in ffn, whereas this time I want to concentrate on the “GroupStats” object. The former is for use with single series of data, whereas the latter can accept a full DataFrame with multiple columns representing multiple price/equity series simultaneously.

Let’s now create some new data – this time lets set up a DataFrame containing 4 columns of data, each representing a trading strategy (or asset price) ‘equity curve’n and display the first 5 rows of that data.

The first step is identical to the first step for a single series of returns that we showed in part one of this tutorial – we change it into an ffn object, this time a “GroupStats” objectand assign that to a variable.

If we print the type of the variable we have just created – we can indeed see it is an “ffn.core.GroupStats” object – when dealing with a single series of returns we created an “ffn.core.PerformanceStats” object, which is worth noting.

<class ‘ffn.core.GroupStats’>

Plotting the multiple returns series is similar also:

We can also run the same commands on a pandas DataFrame of stock price series we have downloaded from Yahoo Finance:

If we were to plot the data now as follows – you will notice that the prices are automatically rebased to 100 at the start, in order to make them more comparable:

Let’s stick with using opur downloaded real stock price data from now on – we can display stats very quickly for them all at the same time using the same call as for a single series:

This table of information can be accessed as a normal Pandas DataFrame using the folkowing syntax (it is also index-able as normal):

And can be indexed, for example, as follows:

Up until now the GroupStats object has generated output very similar to the PerformanceStats object – so you may be wondering why have them both if they both just do the same things! Well let’s move onto some “GroupStats” specific functionality.

Let’s begin by creating a DataFrame containing a the series of log returns for each of the stock price curves in our “data” DataFrame.

We can quickly create a multiplot of each of the series’ log returns histograms along with screting a DataFrame containing the correlation matrix for them also.

If you want something more visually appealing than the above DataFrame, then it’s just as easy to create a heatmap of the correlation matrix instead:

There we go – that heatmap shows an output that is a little more relevant than our previous randomly generated data. Let’s stick with using this data downloaded from Yahoo Finance for the moment rather than the random data. We can very quickly output the “optimal portolio” based on classic Markowitz Mean/Variance Optimisation methods:

The lookback returns info can be accessed and indexed as follows:

A nice looking scatterplot matrix can easily be produced as follows:

We can also plot drawdown series for multiple assets simultaneously as follows:

I’ll leave it here for this post, I hope some of you found the above useful…there is further functionality available from the FFN package, but I’ll leave that up to you all to explore the more obscure parts of the module.

Until next time…

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePin on PinterestShare on Reddit
Written by s666