stefan-jansen / pyfolio-reloaded

Portfolio and risk analytics in Python
https://pyfolio.ml4trading.io/
Apache License 2.0
348 stars 109 forks source link

There is a bug in gen_drawdown_table #14

Closed ngwangshu closed 2 years ago

ngwangshu commented 2 years ago

Problem Description

Please provide a minimal, self-contained, and reproducible example:

    for i, (peak, valley, recovery) in enumerate(drawdown_periods):
        if pd.isnull(recovery):
            df_drawdowns.loc[i, "Duration"] = np.nan
        else:
            df_drawdowns.loc[i, "Duration"] = len(
                pd.date_range(peak, recovery, freq="B")
            )
        df_drawdowns.loc[i, "Peak date"] = peak.to_pydatetime().strftime(
            "%Y-%m-%d"
        )
        df_drawdowns.loc[i, "Valley date"] = valley.to_pydatetime().strftime(
            "%Y-%m-%d"
        )
        if isinstance(recovery, float):
            df_drawdowns.loc[i, "Recovery date"] = recovery
        else:
            df_drawdowns.loc[
                i, "Recovery date"
            ] = recovery.to_pydatetime().strftime("%Y-%m-%d")
        df_drawdowns.loc[i, "Net drawdown in %"] = (
            (df_cum.loc[peak] - df_cum.loc[valley]) / df_cum.loc[peak]
        ) * 100

Please provide the full traceback:

Traceback (most recent call last):
  File "/Users/apsarazhouyunfan/opt/miniconda3/bin/zipline", line 8, in <module>
    sys.exit(main())
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/zipline/__main__.py", line 106, in _
    return f(*args, **kwargs)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/zipline/__main__.py", line 300, in run
    return _run(
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/zipline/utils/run_algo.py", line 200, in _run
    perf = TradingAlgorithm(
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/zipline/algorithm.py", line 629, in run
    self.analyze(daily_stats)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/zipline/algorithm.py", line 453, in analyze
    self._analyze(self, perf)
  File "factor_375/MaxDailyReturnReservation.py", line 176, in analyze
    plot_drawdown_periods(returns=returns, ax=axes[0])
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/pyfolio/plotting.py", line 432, in plot_drawdown_periods
    df_drawdowns = timeseries.gen_drawdown_table(returns, top=top)
  File "/Users/apsarazhouyunfan/opt/miniconda3/lib/python3.9/site-packages/pyfolio/timeseries.py", line 1015, in gen_drawdown_table
    df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()
AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'

valley is a array subscript, it cannot be converted to date.

Versions

MBounouar commented 2 years ago

Hi, you need to provide a more meaningful example for us to understand what's going on here. I can only suspect, for the moment, that this is a data issue not related to pyfolio. Something is wrong with the returns data that is passed to the gen_drawdown_table .

stefan-jansen commented 2 years ago

Closing for now due to lack of activity, please feel free to reopen if not resolved.