vectorbt
vectorbt copied to clipboard
How to use run_combs in combination with other signals?
Hi,
I want to use run_combs to test, for example, different fast and slow windows of the MACD line. However, the signal I want to generate is when the MACD line crosses above the signal line for example. How do I do this?
For just one combination, my code would be:
macd = vbt.MACD.run(
close=df['close'],
fast_window=12,
slow_window=26,
signal_window=9,
)
pf = vbt.Portfolio.from_signals(
close=df['close'],
entries = macd.macd_crossed_above(macd.signal)
exits = macd.macd_crossed_below(macd.signal) ,
freq = '1D'
)
How would I do this for different combinations of fast, slow and signal window? I tried it with MA and the example below works:
windows = np.arange(2, 101)
price = df['close']
fast_ma, slow_ma= vbt.MA.run_combs(close=price, window=windows, r=2, short_names=['fast', 'slow'])
entries = fast_ma.ma_crossed_above(slow_ma)
exits = fast_ma.ma_crossed_below(slow_ma)
pf_kwargs = dict(size=1, fees=0, freq='1D', direction='Both', init_cash=1000)
pf = vbt.Portfolio.from_signals(price, entries, exits, **pf_kwargs)
However, when I try it for MACD, I get an error:
fast_windows = np.arange(20, 30)
slow_windows = np.arange(10, 20)
signal_windows = np.arange(9, 15)
price = brent_df['close']
macd, signal = vbt.MACD.run_combs(
close=price,
fast_window=fast_windows,
slow_window=slow_windows,
signal=signal_windows,
r=3
)
entries = macd.macd_crossed_above(signal)
exits = macd.macd_crossed_below(signal)