mhallsmoore / qstrader

QuantStart.com - QSTrader backtesting simulation engine.
https://www.quantstart.com/qstrader/
MIT License
2.84k stars 851 forks source link

Getting ValueError: attempt to get argmax of an empty sequence` #94

Closed yadavsushil07 closed 8 years ago

yadavsushil07 commented 8 years ago

Getting below error while running the backtest, it started after I did pull all new changes after a month. I am passing a minute bar data.

Any clue what is happening. It used to give result on old code (an month old code.)

Traceback (most recent call last):
  File "lowest_low.py", line 116, in <module>
    main()
  File "/Users/mukeshyadav/.virtualenvs/py3-data/lib/python3.5/site-packages/click/core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "/Users/mukeshyadav/.virtualenvs/py3-data/lib/python3.5/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/Users/mukeshyadav/.virtualenvs/py3-data/lib/python3.5/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/mukeshyadav/.virtualenvs/py3-data/lib/python3.5/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "lowest_low.py", line 112, in main
    run(config, testing, tickers, filename, execute_date)
  File "lowest_low.py", line 83, in run
    results = backtest.simulate_trading(testing=testing)
  File "/Users/mukeshyadav/Projects/stocklysis/server/qstrader/qstrader/trading_session/backtest.py", line 69, in simulate_trading
    results = self.statistics.get_results()
  File "/Users/mukeshyadav/Projects/stocklysis/server/qstrader/qstrader/statistics/simple.py", line 69, in get_results
    statistics["max_drawdown_pct"] = self.calculate_max_drawdown_pct()
  File "/Users/mukeshyadav/Projects/stocklysis/server/qstrader/qstrader/statistics/simple.py", line 105, in calculate_max_drawdown_pct
    top_index = equity_series[:bottom_index].idxmax()
  File "/Users/mukeshyadav/.virtualenvs/py3-data/lib/python3.5/site-packages/pandas/core/series.py", line 1226, in idxmax
    i = nanops.nanargmax(_values_from_object(self), skipna=skipna)
  File "/Users/mukeshyadav/.virtualenvs/py3-data/lib/python3.5/site-packages/pandas/core/nanops.py", line 464, in nanargmax
    result = values.argmax(axis)
ValueError: attempt to get argmax of an empty sequence
mhallsmoore commented 8 years ago

Hi yadavsushil07,

Just to check - did you update the new requirements?

Mike.

ryankennedyio commented 8 years ago

@yadavsushil07 -- this error will happen when no ticks are passed through the backtesting loop. It's trying to find the highest drawdown point, but doesn't have any data to check against.

Are you running your own strategy? If so, suggest opening up one of the example strategies and copying it as closely as possible. A lot has changed in the last month or so.

femtotrader commented 8 years ago

Maybe we should return np.nan in case of a ValueError

def calculate_max_drawdown_pct(self):
    """
    Calculate the percentage drop related to the "worst"
    drawdown seen.
    """
    drawdown_series = pd.Series(self.drawdowns)
    equity_series = pd.Series(self.equity)
    bottom_index = drawdown_series.idxmax()
    try:
        top_index = equity_series[:bottom_index].idxmax()

        pct = (
            (equity_series.ix[top_index] - equity_series.ix[bottom_index]) /
            equity_series.ix[top_index] * 100
        )
        return round(pct, 4)
    except ValueError:
        return np.nan

because backtest shouldn't fail if we only apply DisplayStrategy (for example or any strategy that doesn't send any trade signal)

femtotrader commented 8 years ago

@yadavsushil07 please use triple backquotes (or indent) for code https://help.github.com/articles/creating-and-highlighting-code-blocks/

yadavsushil07 commented 8 years ago

@femtotrader Thanks will be doing it. The issue is resolved, I have modified Execution Handler. After changing the code as per the sample its working fine.

femtotrader commented 8 years ago

You shouldn't close this issue... because there is still an issue! when no trades occurs statistics/simple.py is raising same ValueError exception and it need to be solved on statistics side.

yadavsushil07 commented 8 years ago

Reopening the issue, as there shouldn't be ValueError when no data.

femtotrader commented 8 years ago

This PR https://github.com/mhallsmoore/qstrader/pull/97 should fix it... waiting to be merged by @mhallsmoore

mhallsmoore commented 8 years ago

Ok, I've merged #97.

femtotrader commented 8 years ago

Thanks