vectorbt icon indicating copy to clipboard operation
vectorbt copied to clipboard

Loss some exit signals by using SignalFactory.

Open eromoe opened this issue 1 year ago • 2 comments

I want to generate an exit signal exact n tick after an entry .

Test below code

import numpy as np
import pandas as pd
import vectorbt as vbt
from numba import njit

@njit
def choice_func_nb(from_i, to_i, col, n):
    if from_i + n - 1 < to_i:
        return np.array([from_i + n - 1], dtype=np.int_)
    return np.empty(0, dtype=np.int_)  # no index

ExitAfter = vbt.SignalFactory(
    short_name="exit_after",
    mode="chain", 
    param_names=["n"]
).from_choice_func(exit_choice_func=choice_func_nb, exit_settings=dict(pass_params=["n"]))


entries = pd.DataFrame(np.tile([True, False, True, False, True, False, False], 3).reshape(3, -1).T, columns=list('abc'))
ExitAfter.run(entries, n=[2]).exits

I found output is


a | b | c
-- | -- | --
False | False | False
False | False | False
True | True | True
False | False | False
False | False | False   # here I expect all True 
False | False | False
True | True | True

What I expect result is same as entries.shift(2).fillna(False)

a | b | c
-- | -- | --
False | False | False
False | False | False
True | True | True
False | False | False
True | True | True
False | False | False
True | True | True

I want to see what happen and use vscode to debug, but the breakpoints doesn't stop in vbt.SignalFactory.__init__ and vbt.SignalFactory.run , I can't figure it out so ask for help here.

eromoe avatar Jun 21 '23 02:06 eromoe

Exit signals are generated strictly between each pair of entries, there's no option in community version to change this (again, this was redesigned in pro). But you can create your own numba function that does the generation part and wrap it with indicator factory.

polakowo avatar Jun 23 '23 13:06 polakowo

Thank you for clarify

Exit signals are generated strictly between each pair of entries

Since entries are

	a	b	c
-- | -- | --
0	True	True	True              # entry 1
1	False	False	False
2	True	True	True               # entry 2
3	False	False	False
4	True	True	True               # entry 3
5	False	False	False
6	False	False	False

produce exits

a | b | c
-- | -- | --
False | False | False
False | False | False
True | True | True           # exit 1
False | False | False
False | False | False   
False | False | False
True | True | True           # exit 2

Here exit 1 is overlaped with entry 2, it is not betweeen entry 1 and entry 2 , and exit 2 isn't between two entry. I can't get the machenism.

eromoe avatar Jun 27 '23 03:06 eromoe