Using Pyfolio to display Futures Strategies
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
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.
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?
Thanks, Jonathan! I think pos.py would likely be the right place.
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.