pyfolio icon indicating copy to clipboard operation
pyfolio copied to clipboard

Using Pyfolio to display Futures Strategies

Open marketneutral opened this issue 7 years ago • 4 comments

I am hoping to use pyfolio to create the various tearsheets on a strategy that trades futures. My first attempt via

results = pd.read_pickle('../outfile.pickle')
returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)
pf.create_simple_tear_sheet(returns, positions, transactions)

demonstrates that pyfolio doesn't know anything about contract multipliers (also confirmed via bt = get_backtest(<hash>); bt.create_full_tear_sheet() on Q platform). Concretely, I have a short position established in a strategy like

    contract = data.current(continuous_future('US'), 'contract')
    order_target_percent(contract, -1.0)

i.e., a 100% of capital base short.

pyfolio doesn't know that there is a 1000 multiplier on this, so, for example, the "Exposure Plot" shows -0.0010 for that position.

Of course I wouldn't expect pyfolio to come with the data built-in, but there appears to be no way to tell pyfolio about these multipliers.

Is anyone using pyfolio to look at futures strategies? If so, how? Is anything in the works?

As a quick fix, I would propose passing in an optional multiplier dict.

cc: @AndreasClenow

marketneutral avatar Sep 27 '18 20:09 marketneutral

On my further investigations, it appears the change would be relatively simple as the positions DataFrame that you pass to the various create...tear... functions must contain $ notional positions. As such, if we add a multiplier dict only in utils.extract_rets_pos_txn_from_zipline(results, multipliers) that would solve this issue I think. I am happy to make the PR on this, but would like to get some guidance if that API alteration would be acceptable, or if there are better ideas.

marketneutral avatar Sep 28 '18 14:09 marketneutral

This would solve it....

from zipline.assets import Equity, Future

def apply_multipliers(positions):
    for asset in positions.columns:
        if type(asset) in [Equity, Future]:
            positions[asset] = positions[asset]*asset.price_multiplier
    return positions

results = pd.read_pickle('../outfile.pickle')
returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)
positions = apply_multipliers(positions)

Any guidance on where you'd like this in the pyfolio code base?

marketneutral avatar Sep 29 '18 17:09 marketneutral

Thanks, Jonathan! I think pos.py would likely be the right place.

twiecki avatar Sep 29 '18 18:09 twiecki

Thank you both for solving this issue - I see the same issue in trying to use the create_round_trip_tear_sheet function with futures. It appears that in round_trips.py , pnl is calculated based on 'price' , which is drawn from txn.py

Where should this be applied for correcting round_trips.py ?

Many thanks for the help.

IrishMarine avatar Apr 30 '20 19:04 IrishMarine