Back to Community
Pyfolio - a few basic questions

Hi there,

we are in the research phase of our new hedge-fund, and, rather than build everything from scratch, at this stage, I thought it would make sense to use 3rd party analytics software to generate some basic risk analysis, e.g. sharpe and sortino ratios, max dds etc. etc. I thought Pyfolio might fulfil our needs. But I have some questions:

(i) Our research backtester uses monthly return data and generates monthly portfolio returns. When I tried to run Pyfolio on our output, most of the charts looked pretty wrong. Can Pyfolio work with a monthly return stream? or do we need to use daily returns?

(ii) We are researching a global fund (primarily US + Europe), so the SPX is not a suitable index for us, and, indeed, we may use our own in-house fundamental indices as benchmarks. Is it possible to replace the S&P 500 as the benchmark index in Pyfolio?

(iii) We would like to run our systems offline. Is it possible to get Pyfolio to use local files/data frames of index returns, risk free rate, fama french factors etc. rather than downloading them every time?

Any help on these would be very much appreciated.

Thanks,

Carl

17 responses

Hi Carl,

Thanks for your questions. Responses inline below.

(i) Our research backtester uses monthly return data and generates monthly portfolio returns. When I tried to run Pyfolio on our output, most of the charts looked pretty wrong. Can Pyfolio work with a monthly return stream? or do we need to use daily returns?

We have made some attempts to allow specification of a duration parameter (for example, sharpe_ratio can take period='monthly' and it should work correctly). At this point, though, it's untested and likely to not work correctly, specifically for the tear sheets. It would probably require to use the period kwarg more consistently across the functions. We mostly use daily returns so it's unclear that we'll add that soon, but I'd definitely consider a pull request that adds it.

(ii) We are researching a global fund (primarily US + Europe), so the SPX is not a suitable index for us, and, indeed, we may use our own in-house fundamental indices as benchmarks. Is it possible to replace the S&P 500 as the benchmark index in Pyfolio?

Yes, you should be able to pass in any timeseries as benchmark_rets, e.g. create_full_tear_sheet(my_returns, benchmark_rets=my_bmark).

(iii) We would like to run our systems offline. Is it possible to get Pyfolio to use local files/data frames of index returns, risk free rate, fama french factors etc. rather than downloading them every time?

Yes, absolutely. As long as you have your returns in a (time) series, your positions as well as your transactions in a DataFrame, pyfolio should be able to handle it. This NB should give you an overview of how the input data must look like: https://github.com/quantopian/pyfolio/blob/master/pyfolio/examples/overview_slides.ipynb

Hope that helps, let me know if you have further questions.

Thomas

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.

Hi,

In relation to the ability for pyfolio to take return inputs other than daily, would the Bayesian Cone chart be corrected calculated based on monthly return say? Thanks so much.

Kind regards,
Simon

Hi Simon,

Yes, the Bayesian cone should not care about the unit of your measurements. The forward projections should then also be interpreted as monthly. Other things like Bayesian Sharpe Ratio or beta might be different though in that they have a fixed annualization for daily (SR) or expect daily factor returns (beta). I haven't tried either so it's possible I'm wrong, best way is to try.

Best,
Thomas

Hi Thomas,

Thanks for your prompt response. Yes, I can confirm rolling_sharpe and rolling_beta doesn't take periods other than daily very well, it tends to want to annualize everything assuming returns are daily. With Bayesian cone, the results looked reasonable, but I wasn't entirely sure, so it's good to have confirmation.

By the way, is there a good reference I can refer to understand how to create the Bayesian charts separately? I looked at the Bayesian.ipynb under GitHub and tears.py, but still couldn't quite figure it out.

Thank you for your time!

Kind regards,
Simon

Hi Simon,

Here's more info on the theoretical motivations:
http://blog.quantopian.com/bayesian-cone/

Unfortunately the documentation still isn't great, but the doc strings should be helpful. For example, here is what the Bayesian tear sheet calls to plot the Bayesian cone: https://github.com/quantopian/pyfolio/blob/master/pyfolio/bayesian.py#L602

Best,
Thomas

Thanks Thomas.

Hello!

I have an Anaconda installation on my Windows PC. I have loaded the PyFolio package.

I want to load a time series of data from an algorithm I am working on, and compare it to a benchmark, S&P, which I guess I also have to load in.

Currently my data is in the style of NAV data = Day 1 = 1000, Day 2 = 1002 etc etc.

  • Will I need to translate this into another format to be usable?

Once I am happy I have the right data,

  • do you then have to split it across two CSV files, one for returns of strategy, and one for returns of S&P. If so, does each CSV have two columns, one is the date/time and one is the return for the day?

Once I know the data is in the right format for Pandas.

I load the data into a time series (this bit I can do!)

  • Then, how do I get the series data into Pyfolio. How do I get the benchmark data into Pyfolio.

Once into Pyfolio, well lets see if I can get to that stage.

Thank you in advance, you will save me grey hairs! Any example code, other than the example stuff already provided, would be greatly appreciated.

I appear to have successfully got the date column into UTC format. Code here:

%matplotlib inline import pandas as pd
from pandas import Series, DataFrame, Panel
import datetime
import numpy as np
import matplotlib.pyplot as plt
import pyfolio as pf

ts = pd.read_csv('Data.csv', index_col='Date', parse_dates=True)
ts.head()

ts.shape

ts_utc = ts.tz_localize('UTC')

ts_utc

TypeError: Cannot compare tz-naive and tz-aware timestamps

Hi, I need to get the sharpe ratio of a portfolio containing different assets using each assets monthly return.
I am using create_returns_tear_sheet() for this purpose but i am not getting any result and get the following error everytime i run it :

ConnectionError: HTTPConnectionPool(host='www.google.com', port=80): Max retries exceeded with url: /finance/historical?q=SPY&startdate=Jan+01%2C+1970&enddate=Nov+16%2C+2017&output=csv (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 110] Connection timed out',))

as mentioned above the update for monthly return was going to be added. Is it added or no?

Also, I want to provide my own Benchmark series. How can i do that? Is there a example for this ?

Thanks in advance

tania: Yes, you should be able to pass in any timeseries as benchmark_rets, e.g. create_full_tear_sheet(my_returns, benchmark_rets=my_bmark).

Hi Tom,

This URL is not working:
https://github.com/quantopian/pyfolio/blob/master/pyfolio/examples/overview_slides.ipynb
Is there a better place to see an example for input data format for Pyfolio use?
thanks

Hi Thomas
i would also like to know how the data format has to look for pyfolio. Would be great if you could refresh the link or send some other resource.
Thanks in advance
Steffen

Pyfolio docs could do with an overhaul. In the abscence of that, the doc strings should be helpful:
https://github.com/quantopian/pyfolio/blob/master/pyfolio/tears.py#L105

Hi Thomas,

My problem is the same as Carl's. I usually use monthly return, so can Pyfolio work with a monthly return stream? Thank you.

TY P: Do you not have daily returns or do you just want to look at monthly returns for your analysis? There is a monthly returns plot in the tearsheet (among other daily analyses). Otherwise, I'm not sure it'd work, you can try but need to pass in the benchmark_returns with the same frequency.
Are you using pyfolio stand-alone?

Thank you, Thomas, for the response. I have only monthly returns for analysis. Yes, I use pyfolio stand-alone. I have a dataset in excel format. I want to read the excel file by pandas and then use pyfolio to analyze these monthly returns. In this case, do you have a suggestion? Thank you.