scrtlabs / catalyst

An Algorithmic Trading Library for Crypto-Assets in Python
http://enigma.co
Apache License 2.0
2.48k stars 724 forks source link

Pyfolio error - module 'empyrical.utils' has no attribute 'default_returns_func' #428

Open eranhirs opened 6 years ago

eranhirs commented 6 years ago

Dear Catalyst Maintainers,

Description of Issue

Trying to use pyfolio with catalyst. Specifically, the Full tear sheet report like in this example: https://quantopian.github.io/pyfolio/notebooks/zipline_algo_example/

As documented here: https://github.com/enigmampc/catalyst/issues/73 You have to use pip install empyrical==0.2.2 otherwise I get the error ImportError: cannot import name information_ratio

So I did and it solved that specific error.

What happened instead?

Now I get the error {AttributeError}module 'empyrical.utils' has no attribute 'default_returns_func'

Reproduction Steps

  1. run pip install pyfolio
  2. run pip install empyrical==0.2.2
  3. Taking perf from catalyst def analyze, run import pyfolio and then pyfolio.create_full_tear_sheet(perf.returns, positions=perf.positions, transactions=perf.transactions)

What steps have you taken to resolve this already?

In this issue https://github.com/quantopian/zipline/issues/1855 It seems they solved this problem, which doesn't require downgrading empyrical.

Here is another user having the same problem as I do: https://github.com/quantopian/empyrical/issues/91

Anything else?

Catalyst should have documentation on adding pyfolio as it really seems as a great tool for visualizing the results.

eranhirs commented 6 years ago

Work in progress using pyfolio.create_full_tear_sheet.

Problems run into & fixed:

  1. pyfolio 0.7.0 is the latest working version.
  2. transactions requires reformatting, fixed using extract_transactions.
  3. transactions requires a symbol column, fixed with code below.
  4. benchmark_rets is required, otherwise it tries fetching from Yahoo Finance the data, fixed.
  5. create_full_tear_sheet only works with daily returns, fixed with code below.

Todo:

  1. positions requires a different format, for now I removed positions from the call.
  2. APPROX_BDAYS_PER_MONTH = 21 is incorrect for crypto.
  3. Check why results are different from catalyst results.

Here is the code so far:

transaction_df = extract_transactions(perf)
transaction_df['symbol'] = transaction_df['sid'].apply(lambda x: x.base_currency)
returns_daily = perf.returns.resample("D", how="ohlc")['close']
benchmark_returns_daily = perf.benchmark_period_return.resample("D", how="ohlc")['close']
pyfolio.create_full_tear_sheet(returns_daily, transactions=transaction_df, benchmark_rets=benchmark_returns_daily)
sam31415 commented 6 years ago

Does this help? https://forum.catalystcrypto.io/t/a-shot-at-pyfolio/89

eranhirs commented 6 years ago

Thanks @sam31415 this helps! Managed to use gen_round_trip_stats which is great 👍

In any case, it is still using pyfolio 0.7.0, ideally would like to upgrade to pyfolio 0.9.0 like described in the issue.