backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

Implement 2 strategies at the same time

Open matei219 opened this issue 2 years ago • 1 comments

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)

matei219 avatar Apr 09 '24 13:04 matei219

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

s-kust avatar Sep 02 '24 16:09 s-kust