polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.
https://vectorbt.dev
Other
4.02k stars 590 forks source link

Cryptic Numba issues while trying to reproduce the documentation examples #72

Closed naquad closed 3 years ago

naquad commented 3 years ago

Hello.

I'm trying to start working with VectorBT, but I keep getting some very weird errors from Numba while invoking the code. Given that Numba is really bad at error descriptions it is hard to say if it is my mistake while copy-pasting or if it is a version problem.

Here's the proof of concept exhibiting the issue. It is mostly a copy-paste from the documentation and the article on the stop types comparison:

#!/usr/bin/env python3

import pandas as pd
import yfinance as yf
import vectorbt as vbt
from datetime import datetime

df = yf.Ticker('BTC-USD').history(interval='1h',
                                  start=datetime(2020, 1, 1),
                                  end=datetime(2020, 12, 1))

# sort of we have multiple data frames, that doesn't seem to influence the bug
# df.columns = pd.MultiIndex.from_tuples(
#     (c, 'BTC-USD')
#     for c in df.columns
# )

fast_ma = vbt.MA.run(df['Close'], 10, short_name='fast')
slow_ma = vbt.MA.run(df['Close'], 20, short_name='slow')

entries = fast_ma.ma_above(slow_ma, crossed=True)

exits = vbt.ADVSTEX.run(
    entries,
    df['Open'],
    df['High'],
    df['Low'],
    df['Close'],
    ts_stop=[0.1],
    stop_type=None, hit_price=None
).exits

And it's output is the following:

Traceback (most recent call last):
  File "/home/naquad/projects/tests/strat2/try1.py", line 39, in <module>
    tp_exits = df['BUY'].vbt.signals.generate_stop_exits(np.array([0.2]), 0, trailing=True)
  File "/home/naquad/.local/lib/python3.9/site-packages/vectorbt/signals/accessors.py", line 469, in generate_stop_exits
    exits = nb.generate_stop_ex_nb(
  File "/home/naquad/.local/lib/python3.9/site-packages/numba/core/dispatcher.py", line 415, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/naquad/.local/lib/python3.9/site-packages/numba/core/dispatcher.py", line 358, in error_rewrite
    reraise(type(e), e, None)
  File "/home/naquad/.local/lib/python3.9/site-packages/numba/core/utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.core.typeinfer.CallConstraint object at 0x7f4cb1d21fd0>.
Failed in nopython mode pipeline (step: analyzing bytecode)
Use of unsupported opcode (LIST_EXTEND) found

File "../../../.local/lib/python3.9/site-packages/vectorbt/signals/nb.py", line 79:
def generate_ex_nb(entries, wait, exit_choice_func_nb, *args):
    <source elided>
                # Run the UDF
                idxs = exit_choice_func_nb(col, from_i, to_i, *args)
                ^

During: resolving callee type: type(CPUDispatcher(<function generate_ex_nb at 0x7f4cb8545790>))
During: typing of call at /home/naquad/.local/lib/python3.9/site-packages/vectorbt/signals/nb.py (471)

Enable logging at debug level for details.

File "../../../.local/lib/python3.9/site-packages/vectorbt/signals/nb.py", line 471:
def generate_stop_ex_nb(entries, ts, stop, trailing, wait, first, flex_2d):
    <source elided>
    temp_idx_arr = np.empty((entries.shape[0],), dtype=np.int_)
    return generate_ex_nb(entries, wait, stop_choice_nb, ts, stop, trailing, wait, first, temp_idx_arr, flex_2d)
    ^

I've seen other errors like Use of unsupported opcode (...) found including CONTAINS_OP and similar ones. The versions I'm using are the following:

Versions: Python 3.9 numba 0.51.2 numpy 1.19.4 pandas 1.1.4 vectorbt 0.14.4 llvmlite 0.34.0 llvm 10.0.1-3

If there are recommended versions then please state them, so I could try to run it in the docker container.

P. S. Thank you for your work, as the person who wrote a much smaller and limited version of the backtesting engine than VectorBT I acknowledge the amount of work it took.

polakowo commented 3 years ago

Numba does not support Python 3.9 yet, use 3.6-3.8.

I appreciate your feedback, though it needs much more contributions to make it truly amazing.

naquad commented 3 years ago

Thank you for the quick answer, the issue is Python 3.9.