Quickstart

Here is a quick rundown of ffn’s capabilities. For a more complete guide, read the source, or check out the API docsarrow-up-right.

import ffn
#%pylab inline

Data Retrieval

The main method for data retrieval is the get function. The get function uses a data provider to download data from an external service and packs that data into a pandas DataFrame for further manipulation.

data = ffn.get('agg,hyg,spy,eem,efa', start='2010-01-01', end='2014-01-01')
print data.head()
              agg    hyg     spy    eem    efa
Date
2010-01-04  90.45  64.87  103.44  39.32  49.08
2010-01-05  90.86  65.18  103.71  39.61  49.13
2010-01-06  90.81  65.35  103.79  39.69  49.33
2010-01-07  90.70  65.61  104.23  39.46  49.14
2010-01-08  90.75  65.72  104.57  39.77  49.53

[5 rows x 5 columns]

By default, the data is downloaded from Yahoo! Finance and the Adjusted Close is used as the security’s price. Other data sources are also available and you may select other fields as well. Fields are specified by using the following format: {ticker}:{field}. So, if we want to get the Open, High, Low, Close for aapl, we would do the following:

print ffn.get('aapl:Open,aapl:High,aapl:Low,aapl:Close', start='2010-01-01', end='2014-01-01').head()
            aaplopen  aaplhigh  aapllow  aaplclose
Date
2010-01-04    213.43    214.50   212.38     214.01
2010-01-05    214.60    215.59   213.25     214.38
2010-01-06    214.38    215.23   210.75     210.97
2010-01-07    211.75    212.00   209.05     210.58
2010-01-08    210.30    212.00   209.06     211.98

[5 rows x 4 columns]

The default data provider is ffn.data.web()arrow-up-right. This is basically just a thin wrapper around pandas’ pandas.io.data provider. Please refer to the appropriate docs for more info (data sources, etc.). The ffn.data.csv()arrow-up-right provider is also available when we want to load data from a local file. In this case, we can tell ffn.data.get()arrow-up-right to use the csv provider. In this case, we also want to merge this new data with the existing data we downloaded earlier. Therefore, we will provide the data object as the existing argument, and the new data will be merged into the existing DataFrame.

As we can see above, the dbc column was added to the DataFrame. Internally, get is using the function ffn.merge, which is useful when you want to merge TimeSeries and DataFrames together. We plan on adding many more data sources over time. If you know your way with Python and would like to contribute a data provider, please feel free to submit a pull request - contributions are always welcome!

Data Manipulation

Now that we have some data, let’s start manipulating it. In quantitative finance, we are often interested in the returns of a given time series. Let’s calculate the returns by simply calling the to_returnsarrow-up-right or to_log_returnsarrow-up-right extension methods.

Let’s look at the different distributions to see how they look.

_images/quickstart_10_0.png

We can also use the numerous functions packed into numpy, pandas and the like to further analyze the returns. For example, we can use the corr function to get the pairwise correlations between assets.

agg

hyg

spy

eem

efa

dbc

agg

1.00

-0.11

-0.33

-0.23

-0.29

-0.18

hyg

-0.11

1.00

0.77

0.75

0.76

0.49

spy

-0.33

0.77

1.00

0.88

0.92

0.59

eem

-0.23

0.75

0.88

1.00

0.90

0.62

efa

-0.29

0.76

0.92

0.90

1.00

0.61

dbc

-0.18

0.49

0.59

0.62

0.61

1.00

6 rows × 6 columns

Here we used the convenience method as_format to have a prettier output. We could also plot a heatmap to better visualize the results.

_images/quickstart_14_0.png

We used the ffn.core.plot_corr_heatmap()arrow-up-right, which is a convenience method that simply calls ffn’s ffn.core.plot_heatmap()arrow-up-right with sane arguments.

Let’s start looking at how all these securities performed over the period. To achieve this, we will plot rebased time series so that we can see how they each performed relative to eachother.

_images/quickstart_16_0.png

Performance Measurement

For a more complete view of each asset’s performance over the period, we can use the ffn.core.calc_stats()arrow-up-right method which will create a ffn.core.GroupStatsarrow-up-right object. A GroupStats object wraps a bunch of ffn.core.PerformanceStatsarrow-up-right objects in a dict with some added convenience methods.

Now that we have our GroupStats object, we can analyze the performance in greater detail. For example, the plot method yields a graph similar to the one above.

_images/quickstart_20_0.png

We can also display a wide array of statistics that are all contained in the PerformanceStats object. This will probably look crappy in the docs, but do try it out in a Notebook. We are also actively trying to improve the way we display this wide array of stats.

Lots to look at here. We can also access the underlying PerformanceStats for each series, either by index or name.

_images/quickstart_25_0.png

Most of the stats are also available as pandas objects - see the stats, return_table, lookback_returns attributes.

Numerical Routines and Financial Functions

ffn also provides commonly used numerical routines and plans to add many more in the future. One can easily determine the proper weights using a mean-variance approach using the ffn.core.calc_mean_var_weights()arrow-up-right function.

Some other interesting functions are the clustering routines, such as a Python implementation of David Varadi’s Fast Threshold Clustering Algorithm (FTCA)

Last updated