Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is it possible to import external return data into pyfolio?

Hi all,

I have a Forex based strategy built outside of Quantopian. Is it possible to import the returns from this strategy into pyfolio and produce a report? A couple of the forum posts suggest it is possible but I can't find documentation on how to do it.

Regards,
Mark

10 responses

Hi Mark,

Yes, you can pass Pyfolio any returns series and generate a returns tear sheet. Load your returns into a pandas Series and pass that Series to pyfolio.tears.create_returns_tear_sheet.

The best documentation for this functionality is in the Pyfolio code itself: https://github.com/quantopian/pyfolio/blob/master/pyfolio/tears.py#L195

You may also find these tutorials to be helpful: http://quantopian.github.io/pyfolio/single_stock_example/

I'd be interested to hear any thoughts you have about making Pyfolio a useful tool beyond Quantopian. We've tried to design much of the functionality to be asset-class agnostic.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Andrew,

I will give it a try. My ultimate goal is to analyze the integrated returns of this strategy and a strategy I created in Quantopian, will that be possible? Is it possible to combine the returns of a Q strategy and an external strategy?

Regards,
Mark

Hi Andrew,

I was able to upload a returns series as you describe, however when I try to generate a tearsheet I get a key error, stating the following,

KeyError: "None of [DatetimeIndex(['2015-06-01', '2015-07-01', '2015-08-01', '2015-09-01',\n '2015-12-01', '2015-01-13', '2015-01-14', '2015-01-15',\n '2015-01-16', '2015-01-19', \n ...\n '2015-06-18', '2015-06-19', '2015-06-22', '2015-06-23',\n '2015-06-24', '2015-06-25', '2015-06-26', '2015-06-29',\n '2015-06-30', '2015-01-07'],\n dtype='datetime64[ns]', name=u'Date', length=127, freq=None, tz='UTC')] are in the [index]"

If I inspect the data series it looks correct, if I run,

returns_data.ix[:,'Daily Return']  

I get output of the form,

Date
2015-02-01 00:00:00+00:00 0.0154
2015-05-01 00:00:00+00:00 0.0039
2015-06-01 00:00:00+00:00 0.0016
2015-07-01 00:00:00+00:00 0.0000
2015-08-01 00:00:00+00:00 -0.0069
...

And if I look at the number of rows that is correct as well. Also some reports are generated correctly before I get the keyerror reported. Not sure what I am doing wrong. Can someone assist?

Here is the code I am running in my notebook cell,

import matplotlib.pyplot as pyplot  
import pyfolio as pf


#: My Returns data  
returns_data = local_csv('Returns-2015.csv',date_column='Date',use_date_column_as_index=True, timezone='UTC')

#len(returns_data)  
#returns_data.ix[:,'Daily Return']  
#returnSeries = returns_data.ix[:,'Daily Return']  
#type(returnSeries)  
pf.tears.create_returns_tear_sheet(returnSeries)                 

Regards,
Mark

Hi Mark,

It looks like you are running into problems when pyfolio tries to match your returns time series up with the SPY returns time series used to calculate beta.

Try normalizing the index of returnSeries using, returnSeries.index = returnSeries.index.normalize().

If that doesn't work, could you post the full KeyError traceback?

Andrew

Hi Andrew,

Thanks for taking a look at this. I am getting the same error after adding your code. Here is the code,

import matplotlib.pyplot as pyplot
import pyfolio as pf

: My Returns data

returns_data = local_csv('Returns-2015.csv',date_column='Date',use_date_column_as_index=True, timezone='UTC')

len(returns_data)

returns_data.ix[:,'Daily Return']

returnSeries = returns_data.ix[:,'Daily Return']

type(returnSeries)

returnSeries.index = returnSeries.index.normalize()
pf.tears.create_returns_tear_sheet(returnSeries)

Here is the full traceback,

KeyError Traceback (most recent call last)
in ()
11 #type(returnSeries)
12 returnSeries.index = returnSeries.index.normalize()
---> 13 pf.tears.create_returns_tear_sheet(returnSeries)

/usr/local/lib/python2.7/dist-packages/pyfolio/plotting.pyc in call_w_context(args, **kwargs) 44 if set_context:
45 with context():
---> 46 return func(
args, **kwargs)
47 else:
48 return func(*args, **kwargs)

/usr/local/lib/python2.7/dist-packages/pyfolio/tears.pyc in create_returns_tear_sheet(returns, live_start_date, cone_std, benchmark_rets, return_fig) 290
291 plotting.plot_rolling_beta(
--> 292 returns, benchmark_rets, ax=ax_rolling_beta)
293
294 plotting.plot_rolling_sharpe(

/usr/local/lib/python2.7/dist-packages/pyfolio/plotting.pyc in plot_rolling_beta(returns, factor_returns, legend_loc, ax, **kwargs) 680 ax.set_ylabel('Beta')
681 rb_1 = timeseries.rolling_beta(
--> 682 returns, factor_returns, rolling_window=APPROX_BDAYS_PER_MONTH * 6)
683 rb_1.plot(color='steelblue', lw=3, alpha=0.6, ax=ax, **kwargs)
684 rb_2 = timeseries.rolling_beta(

/usr/local/lib/python2.7/dist-packages/pyfolio/timeseries.pyc in rolling_beta(returns, factor_returns, rolling_window) 648 out.loc[end] = calc_alpha_beta(
649 returns.loc[beg:end],
--> 650 factor_returns.loc[beg:end])[1]
651
652 return out

/usr/local/lib/python2.7/dist-packages/pyfolio/timeseries.pyc in calc_alpha_beta(returns, factor_returns) 710
711 ret_index = returns.index
--> 712 beta, alpha = sp.stats.linregress(factor_returns.loc[ret_index].values,
713 returns.values)[:2]
714

/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in getitem(self, key) 1178 return self._getitem_tuple(key)
1179 else:
-> 1180 return self._getitem_axis(key, axis=0)
1181
1182 def _getitem_axis(self, key, axis=0):

/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in getitem_axis(self, key, axis) 1312 raise ValueError('Cannot index with multidimensional key')
1313
-> 1314 return self.
getitem_iterable(key, axis=axis)
1315
1316 # nested tuple slicing

/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in getitem_iterable(self, key, axis) 922 def getitem_iterable(self, key, axis=0):
923 if self.
should_validate_iterable(axis):
--> 924 self.
has_valid_type(key, axis)
925
926 labels = self.obj._get_axis(axis)

/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.pyc in has_valid_type(self, key, axis) 1260
1261 raise KeyError("None of [%s] are in the [%s]" %
-> 1262 (key, self.obj.
get_axis_name(axis)))
1263
1264 return True

KeyError: "None of [DatetimeIndex(['2015-06-01', '2015-07-01', '2015-08-01', '2015-09-01',\n '2015-12-01', '2015-01-13', '2015-01-14', '2015-01-15',\n '2015-01-16', '2015-01-19', \n ...\n '2015-06-18', '2015-06-19', '2015-06-22', '2015-06-23',\n '2015-06-24', '2015-06-25', '2015-06-26', '2015-06-29',\n '2015-06-30', '2015-01-07'],\n dtype='datetime64[ns]', name=u'Date', length=127, freq=None, tz='UTC')] are in the [index]"

Just an aside, I did not receive an email update to your response even though I am listening to this thread (there wasn't anything in my junk mail).

Regards,
Mark

Hi Andrew,

Just wanted to let you know I resolved my issues and I can now import results from a strategy developed outside of Q and combine it with Q based strategies, really like pyfolio!

Regards,
Mark

Hello Mark,

Would you be kind enough to share your code to load in the data?

Thanks,

Matthew

Hello Mark,

Would you be kind enough to share your code to load in the data?

"Just wanted to let you know I resolved my issues and I can now import results from a strategy developed outside of Q and combine it with Q based strategies, really like pyfolio!" Hi Mark,
Would you like to share your solution?

Thanks,
Xin