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

Implement 2 strategies at the same time #1135

Open
matei219 opened this issue Apr 9, 2024 · 1 comment
Open

Implement 2 strategies at the same time #1135

matei219 opened this issue Apr 9, 2024 · 1 comment

Comments

@matei219
Copy link

matei219 commented Apr 9, 2024

I'm trying to add a new condition to the strategy in the guide, however I'm not getting the expected result. Can you help me indicate what I am doing wrong? I'm trying to combine SMA and RVOL at the same time. Thanks.

from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import GOOG
import pandas as pd
import numpy as np
import yfinance as yf

MSFT = yf.download(["MSFT"], start="2019-01-01", end="2024-01-01")
def SMA(values, n):
"""
Return simple moving average of values, at
each step taking into account n previous values.
"""
return pd.Series(values).rolling(n).mean()

def RVOL(volume, window):
"""
Calculate Relative Volume (RVOL) using a rolling window.

Args:
    volume (np.array): Array of volume data.
    window (int): Window size for calculating the average volume.

Returns:
    np.array: RVOL values calculated using the rolling window.
"""
avg_volume = np.convolve(volume, np.ones(window)/window, mode='valid')
rvol_values = volume[window-1:] / avg_volume
rvol_values = np.concatenate((np.full(window-1, np.nan), rvol_values))
return rvol_values

class SmaCross(Strategy):
n1 = 20
n2 = 50
threshold = 1.2

def init(self):
    close = self.data.Close
    self.sma1 = self.I(SMA, close, self.n1)
    self.sma2 = self.I(SMA, close, self.n2)
    # Precompute RVOL
    self.rvol = self.I(RVOL, self.data.Volume, 10)  # Adjust the window size as needed

def next(self):
    if crossover(self.sma1, self.sma2) and self.rvol[-1] > self.threshold:
        self.position.close()
        self.buy()
    elif crossover(self.sma2, self.sma1) and self.rvol[-1] > self.threshold:
        self.position.close()
        self.sell()

bt = Backtest(MSFT, SmaCross,
cash=10000, commission=.002,
exclusive_orders=True)

stats = bt.run()
print(stats)

@s-kust
Copy link

s-kust commented Sep 2, 2024

This repository - https://github.com/s-kust/python-backtesting-template - may serve you as a good boilerplate. See the main Strategy class with next function here - https://github.com/s-kust/python-backtesting-template/blob/main/strategy/run_backtest_for_ticker.py

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