quantopian / pyfolio

Portfolio and risk analytics in Python
https://quantopian.github.io/pyfolio
Apache License 2.0
5.71k stars 1.78k forks source link

Pyfolio round_trip_tear_sheet for futures - incorrect pnl calculations & stats #641

Open IrishMarine opened 4 years ago

IrishMarine commented 4 years ago

Hi Everyone,

Seeking help with correcting pnl calculations within pyfolio round_trips.py in order to correct the round_trip_tear_sheet for futures backtesting.

First, I ran a 50/100, 25 day breakout version of Andreas Clenow's core trend code from Trading Evolved with Norgate futures data and a $500k capital base, returning 11.1% annually and 635% cumulative.

image

When I run create_round_trip_tear sheet - the key pnl stats look like this - clearly a problem with how profit and loss is being calculated.

image

Digging under the hood to understand how pnl is being calculated, I ran the following code to view the extract_round_trips function:

import pyfolio as pf

returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(perf)

Correct dt error per https://github.com/quantopian/pyfolio/issues/509

transactions.drop(['dt'], axis=1, inplace=True)

rts = pf.round_trips.extract_round_trips(transactions)

rts.head()

image

We can see from this table that pnl is incorrect - and digging in to round_trips.py code and the transactions from perf, pnl is being calculated as follows:

pnl += -(price + prev_price)

and is not applying asset price_multipliers for futures.

The following github links address this, and outline a code fix in pos.py, but that fix doesn't apply in round_trips.py:

https://github.com/quantopian/pyfolio/issues/570

https://github.com/quantopian/pyfolio/pull/571

Take care, Ben