Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use run_combs in combination with other signals? #720

Open
ETTAN93 opened this issue Jun 16, 2024 · 1 comment
Open

How to use run_combs in combination with other signals? #720

ETTAN93 opened this issue Jun 16, 2024 · 1 comment

Comments

@ETTAN93
Copy link

ETTAN93 commented Jun 16, 2024

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)

image

@quantresearch1
Copy link

It looks like you used 'signal' instead of the expected signal_window argument
I have just tried the below without any issues
macd = vbt.MACD.run(
close=price,
fast_window=np.array([3, 6, 9, 12]),
slow_window=2 * np.array([3, 6, 9, 12]),
signal_window=np.array([1, 3, 5, 9]),
macd_ewm=True,
signal_ewm=True,
)
entries = macd.macd_crossed_above(macd.signal)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants