polakowo / vectorbt

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

Vectorbt documentation code examples throws unsupported error. #706

Open timqaa opened 1 month ago

timqaa commented 1 month ago

Hello everyone, Im really sorry for kinda clickbait title, but I really cant find similar issues and dont know what to do. Hope on your kindness guys Im using vectorbt like this (the simplest example of usage)

import vectorbt as vbt

price = vbt.YFData.download('BTC-USD').get('Close')

pf = vbt.Portfolio.from_holding(price, init_cash=100)
pf.total_profit()

and getting the "unsupported error" when its going to nb.simulate_from_signal_func_nb. I tried already everything but resuls are the same. Nothing works to me. Please explain what Im doing wrong

P.S ofc I checked versions of packages and tried to reinstall them again. Still nothing... Current versions Python 3.11.0b4, vectorbt 0.26.1, numba 0.59.1, numpy 1.26.4 . Oh, and Im using Windows 10 x64


THE FULL ERROR MESSAGE

UnsupportedError Traceback (most recent call last)
Cell In[55], line 5
1 import vectorbt as vbt
3 price = vbt.YFData.download('BTC-USD').get('Close')
----> 5 pf = vbt.Portfolio.from_holding(price, init_cash=100)
6 pf.total_profit()

File ~.pyenv\pyenv-win\versions\3.11.0b4\Lib\site-packages\vectorbt\portfolio\base.py:3035, in Portfolio.from_holding(cls, close, **kwargs)
3023 https://github.com/classmethod
3024 def from_holding(cls: tp.Type[PortfolioT], close: tp.ArrayLike, **kwargs) -> PortfolioT:
3025 """Simulate portfolio from holding.
3026
3027 Based on Portfolio.from_signals.
(...)
3033 500.0
3034 ```"""
-> 3035 return cls.from_signals(close, entries=True, exits=False, **kwargs)

File ~.pyenv\pyenv-win\versions\3.11.0b4\Lib\site-packages\vectorbt\portfolio\base.py:2960, in Portfolio.from_signals(cls, close, entries, exits, short_entries, short_exits, signal_func_nb, signal_args, size, size_type, price, fees, fixed_fees, slippage, min_size, max_size, size_granularity, reject_prob, lock_cash, allow_partial, raise_reject, log, accumulate, upon_long_conflict, upon_short_conflict, upon_dir_conflict, upon_opposite_entry, direction, val_price, open, high, low, sl_stop, sl_trail, tp_stop, stop_entry_price, stop_exit_price, upon_stop_exit, upon_stop_update, adjust_sl_func_nb, adjust_sl_args, adjust_tp_func_nb, adjust_tp_args, use_stops, init_cash, cash_sharing, call_seq, ffill_val_price, update_value, max_orders, max_logs, seed, group_by, broadcast_named_args, broadcast_kwargs, template_mapping, wrapper_kwargs, freq, attach_call_seq, **kwargs)
2957 checks.assert_numba_func(adjust_tp_func_nb)
2959 # Perform the simulation
-> 2960 order_records, log_records = nb.simulate_from_signal_func_nb(
2961 target_shape_2d,
2962 cs_group_lens, # group only if cash sharing is enabled to speed up
2963 init_cash,
2964 call_seq,
2965 signal_func_nb=signal_func_nb,
2966 signal_args=signal_args,
2967 size=broadcasted_args['size'],
2968 price=broadcasted_args['price'],
2969 size_type=broadcasted_args['size_type'],
2970 fees=broadcasted_args['fees'],
2971 fixed_fees=broadcasted_args['fixed_fees'],
2972 slippage=broadcasted_args['slippage'],
2973 min_size=broadcasted_args['min_size'],
2974 max_size=broadcasted_args['max_size'],
2975 size_granularity=broadcasted_args['size_granularity'],
2976 reject_prob=broadcasted_args['reject_prob'],
2977 lock_cash=broadcasted_args['lock_cash'],
2978 allow_partial=broadcasted_args['allow_partial'],
2979 raise_reject=broadcasted_args['raise_reject'],
2980 log=broadcasted_args['log'],
2981 accumulate=broadcasted_args['accumulate'],
2982 upon_long_conflict=broadcasted_args['upon_long_conflict'],
2983 upon_short_conflict=broadcasted_args['upon_short_conflict'],
2984 upon_dir_conflict=broadcasted_args['upon_dir_conflict'],
2985 upon_opposite_entry=broadcasted_args['upon_opposite_entry'],
2986 val_price=broadcasted_args['val_price'],
2987 open=broadcasted_args['open'],
2988 high=broadcasted_args['high'],
2989 low=broadcasted_args['low'],
2990 close=broadcasted_args['close'],
2991 sl_stop=broadcasted_args['sl_stop'],
2992 sl_trail=broadcasted_args['sl_trail'],
2993 tp_stop=broadcasted_args['tp_stop'],
2994 stop_entry_price=broadcasted_args['stop_entry_price'],
2995 stop_exit_price=broadcasted_args['stop_exit_price'],
2996 upon_stop_exit=broadcasted_args['upon_stop_exit'],
2997 upon_stop_update=broadcasted_args['upon_stop_update'],
2998 adjust_sl_func_nb=adjust_sl_func_nb,
2999 adjust_sl_args=adjust_sl_args,
3000 adjust_tp_func_nb=adjust_tp_func_nb,
3001 adjust_tp_args=adjust_tp_args,
3002 use_stops=use_stops,
3003 auto_call_seq=auto_call_seq,
3004 ffill_val_price=ffill_val_price,
3005 update_value=update_value,
3006 max_orders=max_orders,
3007 max_logs=max_logs,
3008 flex_2d=close.ndim == 2
3009 )
3011 # Create an instance
3012 return cls(
3013 wrapper,
3014 close,
(...)
3020 **kwargs
3021 )

File ~.pyenv\pyenv-win\versions\3.11.0b4\Lib\site-packages\numba\core\dispatcher.py:471, in _DispatcherBase._compile_for_args(self, *args, **kws)
468 error_rewrite(e, 'typing')
469 except errors.UnsupportedError as e:
470 # Something unsupported is present in the user code, add help info
--> 471 error_rewrite(e, 'unsupported_error')
472 except (errors.NotDefinedError, errors.RedefinedError,
473 errors.VerificationError) as e:
474 # These errors are probably from an issue with either the code
475 # supplied being syntactically or otherwise invalid
476 error_rewrite(e, 'interpreter')

File ~.pyenv\pyenv-win\versions\3.11.0b4\Lib\site-packages\numba\core\dispatcher.py:409, in _DispatcherBase._compile_for_args..error_rewrite(e, issue_type)
407 raise e
408 else:
--> 409 raise e.with_traceback(None)

UnsupportedError: Failed in nopython mode pipeline (step: analyzing bytecode)
Use of unsupported opcode (<34>) found

File ".......pyenv\pyenv-win\versions\3.11.0b4\Lib\site-packages\vectorbt\portfolio\nb.py", line 1956:
def simulate_from_signal_func_nb(target_shape: tp.Shape,

last_val_price = np.full(target_shape[1], np.nan, dtype=np.float_)
if use_stops:
^

The biggest thank you in advance!

esc commented 1 month ago

@timqaa it seems like you are using a beta release of Python. Numba may not be compatible. Can you use a non-beta release instead?