-
Hey,
Thanks for taking time to help me out and to understand the library better. Any help is much appreciated! Code example:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Okay, you need to keep in mind that each column in vectorbt is a separate backtesting instance. Providing multiple columns means vectorbt will execute the same logic on each of those columns separately. This way you can construct thousands of columns and backtest them all at once. In your example, you have only one column that is created by combining
Don't use variable arguments such as Here's how to perform your example correctly: from datetime import datetime
import pandas as pd
from numba import njit
import numpy as np
from vectorbt import _typing as tp
index = pd.Index([
datetime(2020, 1, 1),
datetime(2020, 1, 2),
datetime(2020, 1, 3),
datetime(2020, 1, 4),
datetime(2020, 1, 5),
datetime(2020, 1, 6),
datetime(2020, 1, 7),
datetime(2020, 1, 8),
datetime(2020, 1, 9),
datetime(2020, 1, 10)
])
a = pd.Series([True, False, False, False, False, False, False, True, False, False], index=index, name='a')
b = pd.Series([True, True, True, False, False, True, True, True, True, True], index=index, name='b')
c = pd.Series([True, True, True, False, False, True, True, True, True, True], index=index, name='c')
@njit
def entry_choice_nb(from_i : int, to_i : int, col : int, a: tp.Array2d, b: tp.Array2d, c: tp.Array2d) -> tp.Array1d:
for i in range(from_i, to_i):
if i - 1 >= 0:
if b[i, col] and c[i, col] and a[i - 1, col]:
temp = np.empty((1,), dtype=np.int_)
temp[0] = i
return temp
temp = np.empty(0, dtype=np.int_)
return temp
@njit
def exit_choice_nb(from_i : int, to_i : int, col : int, b: tp.Array2d, c: tp.Array2d) -> tp.Array1d:
for i in range(from_i, to_i):
if not b[i, col] or not c[i, col]:
temp = np.empty((1,), dtype=np.int_)
temp[0] = i
return temp
temp = np.empty(0, dtype=np.int_)
return temp
a_2d = a.to_numpy()[:, None]
b_2d = b.to_numpy()[:, None]
c_2d = c.to_numpy()[:, None]
entries, exits = pd.DataFrame.vbt.signals.generate_both(
a_2d.shape,
entry_choice_func_nb=entry_choice_nb,
entry_args=(a_2d, b_2d, c_2d),
exit_choice_func_nb=exit_choice_nb,
exit_args=(b_2d, c_2d)
)
print('Entries: \n', entries)
print('Exits: \n', exits)
Entries:
0
0 False
1 True
2 False
3 False
4 False
5 False
6 False
7 False
8 True
9 False
Exits:
0
0 False
1 False
2 False
3 True
4 False
5 False
6 False
7 False
8 False
9 False But using entries = b & c & a.shift()
exits = ~b | ~c This will not remove multiple True values in a row though (look at If you want to test this for different data, instead of re-running this pipeline for each data, you can make each of |
Beta Was this translation helpful? Give feedback.
@m4rc3l-h3
Okay, you need to keep in mind that each column in vectorbt is a separate backtesting instance. Providing multiple columns means vectorbt will execute the same logic on each of those columns separately. This way you can construct thousands of columns and backtest them all at once. In your example, you have only one column that is created by combining
a
,b
, andc
, so your shape should bedf.shape[0]
or(df.shape[0], 1)
. But I advise you to not puta
,b
, andc
into one data frame but keep them separately since they represent different features of the same backtesting instance. Later, if you want to run multiple backtests (e.g., to test multiple parameters), you can expand columns…