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.
num_days = 1000 data1 = pd.DataFrame((np.random.randn(num_days) + np.random.uniform(low=0.0, high=0.2, size=num_days)),index=index,columns=['Data1']) data2 = pd.DataFrame((np.random.randn(num_days) + np.random.uniform(low=0.0, high=0.2, size=num_days)),index=index,columns=['Data2']) data3 = pd.DataFrame((np.random.randn(num_days) + np.random.uniform(low=0.0, high=0.2, size=num_days)),index=index,columns=['Data3']) data4 = pd.DataFrame((np.random.randn(num_days) + np.random.uniform(low=0.0, high=0.2, size=num_days)),index=index,columns=['Data4']) data = pd.concat([data1,data2,data3,data4],axis=1) data = data.cumsum() + 100 data.iloc = 100 data.head()
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.
perf = data.calc_stats()
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.
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:
import pandas_datareader.data as web stocks = ['AAPL','AMZN','MSFT','NFLX'] data = web.DataReader(stocks,data_source='yahoo',start='01/01/2010')['Adj Close'] data.sort_index(ascending=True,inplace=True) perf = data.calc_stats()
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.
returns = data.to_log_returns().dropna() print(returns.head())
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.
ax = returns.hist(figsize=(20, 10),bins=30)
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…