quantopian / pyfolio

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

Tear Sheet Issue - AttributeError: 'DataFrame' object has no attribute 'amount' #665

Open hakbrk opened 3 years ago

hakbrk commented 3 years ago

Problem Description

My zipline strategy closes positions at EOD, when I try to generate a tear sheet an error is raised because the positions dataframe is empty. I am using minute data, is it just not possible to use Pyfolio with minute data? I see that in previous updates support for intraday trading was addressed but I can't seem to get this working.

Please provide the full traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-f2b03b3db2d3> in <module>()
      5 perf = run_algorithm(start, end, initialize, base_capital, handle_data,analyze=analyze,
      6                      data_frequency = 'minute',
----> 7         bundle = 'test_minute_bundle')

C:\Users\xxxxx\anaconda3\envs\zip35\lib\site-packages\zipline\utils\run_algo.py in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, data, bundle, bundle_timestamp, trading_calendar, metrics_set, default_extension, extensions, strict_extensions, environ, blotter)
    439         local_namespace=False,
    440         environ=environ,
--> 441         blotter=blotter,
    442     )

C:\Users\xxxxx\anaconda3\envs\zip35\lib\site-packages\zipline\utils\run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, data, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter)
    238     ).run(
    239         data,
--> 240         overwrite_sim_params=False,
    241     )
    242 

C:\Users\xxxxxx\anaconda3\envs\zip35\lib\site-packages\zipline\algorithm.py in run(self, data, overwrite_sim_params)
    760             daily_stats = self._create_daily_stats(perfs)
    761 
--> 762             self.analyze(daily_stats)
    763         finally:
    764             self.data_portal = None

C:\Users\xxxxx\anaconda3\envs\zip35\lib\site-packages\zipline\algorithm.py in analyze(self, perf)
    474 
    475         with ZiplineAPI(self):
--> 476             self._analyze(self, perf)
    477 
    478     def __repr__(self):

<ipython-input-4-04e4700b8121> in analyze(context, perf)
    149 def analyze(context, perf):
    150     # Use PyFolio to generate a performance report
--> 151     returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(perf)
    152     pf.create_returns_tear_sheet(returns, benchmark_rets=None)
    153 

C:\Users\xxxxxx\anaconda3\envs\zip35\lib\site-packages\pyfolio\utils.py in extract_rets_pos_txn_from_zipline(backtest)
    160         raise ValueError("The backtest does not have any positions.")
    161     positions = pd.concat(raw_positions)
--> 162     positions = pos.extract_pos(positions, backtest.ending_cash)
    163     transactions = txn.make_transaction_frame(backtest.transactions)
    164     if transactions.index.tzinfo is None:

C:\Users\xxxxxxxx\anaconda3\envs\zip35\lib\site-packages\pyfolio\pos.py in extract_pos(positions, cash)
    136 
    137     positions = positions.copy()
--> 138     positions['values'] = positions.amount * positions.last_sale_price
    139 
    140     cash.name = 'cash'

C:\Users\xxxxxxxx\anaconda3\envs\zip35\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   3612             if name in self._info_axis:
   3613                 return self[name]
-> 3614             return object.__getattribute__(self, name)
   3615 
   3616     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'amount'

Please provide any additional information below:

Versions

george-adams1 commented 3 years ago

@hakbrk I got the same error. Have you been able to solve it?

hakbrk commented 3 years ago

I was not able to resolve this issue. What I have done since is to generate a trade by trade return, then use that data to generate three dataframes (returns, transactions, positions) and use Pyfolio as a stand-alone library, should be noted that the returns input needs to be converted to a panda series.