polakowo / vectorbt

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

error on generate_ohlc_stop_exits - TypeError: cannot index array(float64, 1d, C) with 2 indices: UniTuple(int64 x 2) #178

Closed agustinvinao closed 3 years ago

agustinvinao commented 3 years ago

Hi,

I'm trying to create my exist signals with the following code:

cols = ['Open', 'High', 'Low', 'Close', 'Volume']
symbols = ["BTC/EUR"]
price = vbt.CCXTData.download(symbols, exchange="kraken", missing_index='drop', timeframe="15m").get(cols)
bbands = vbt.BBANDS.run(price["Close"], window=bbands_period, alpha=int(bbands_stddev), short_name="bb")
entries = bbands.lower_above(price["Low"], crossover=True, level_name="crossed", allow_multiple=False)
out_dict = {}
exits = entries.vbt.signals.generate_ohlc_stop_exits(
    price['Open'],
    price['High'],
    price['Low'],
    price['Close'],
    out_dict=out_dict,
    sl_stop=0.2,
    ts_stop=0.2,
    tp_stop=0.2)

and this shows the following error:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function setitem>) found for signature:

 >>> setitem(array(float64, 1d, C), UniTuple(int64 x 2), float64)

There are 16 candidate implementations:
      - Of which 14 did not match due to:
      Overload of function 'setitem': File: <numerous>: Line N/A.
        With argument(s): '(array(float64, 1d, C), UniTuple(int64 x 2), float64)':
       No match.
      - Of which 2 did not match due to:
      Overload in function 'SetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 171.
        With argument(s): '(array(float64, 1d, C), UniTuple(int64 x 2), float64)':
       Rejected as the implementation raised a specific error:
         TypeError: cannot index array(float64, 1d, C) with 2 indices: UniTuple(int64 x 2)
  raised from /opt/conda/lib/python3.8/site-packages/numba/core/typing/arraydecl.py:84

During: typing of setitem at /opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py (676)

File "../../../../opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py", line 676:
def ohlc_stop_choice_nb(from_i: int,
    <source elided>
                exit_signal = True
                hit_price_out[i, col] = curr_sl_stop_price
                ^

During: resolving callee type: type(CPUDispatcher(<function ohlc_stop_choice_nb at 0x7efc86965c10>))
During: typing of call at /opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py (95)

During: resolving callee type: type(CPUDispatcher(<function ohlc_stop_choice_nb at 0x7efc86965c10>))
During: typing of call at /opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py (95)

File "../../../../opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py", line 95:
def generate_ex_nb(entries: tp.Array2d, wait: int, exit_choice_func_nb: tp.SignalChoiceFunc, *args) -> tp.Array2d:
    <source elided>
                # Run the UDF
                idxs = exit_choice_func_nb(from_i, to_i, col, *args)
                ^

During: resolving callee type: type(CPUDispatcher(<function generate_ex_nb at 0x7efc869d60d0>))
During: typing of call at /opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py (768)

During: resolving callee type: type(CPUDispatcher(<function generate_ex_nb at 0x7efc869d60d0>))
During: typing of call at /opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py (768)

File "../../../../opt/conda/lib/python3.8/site-packages/vectorbt/signals/nb.py", line 768:
def generate_ohlc_stop_ex_nb(entries: tp.Array2d,
    <source elided>
    temp_idx_arr = np.empty((entries.shape[0],), dtype=np.int_)
    return generate_ex_nb(
    ^

My entries Series is a boolean value as the function expects, where can I find a working example of generate_ohlc_stop_exits to see it?

My goal is to create stop loss signals for my data. Im using the docker image polakowo/vectorbt-full

thank you

polakowo commented 3 years ago

Update to 0.19.0, the issue is resolved there.

agustinvinao commented 3 years ago

All working with the latest docker image, thank you for your quick response, and congrats for the amazing work.

polakowo commented 3 years ago

@agustinvinao also note that the latest version has stop signals implemented directly into Portfolio.from_signals, so you can just do Portfolio.from_signals(..., sl_stop=0.2, sl_trail=True, tp_stop=0.2). This is the preferred way.