kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.04k stars 987 forks source link

Implement 2 strategies at the same time #1135

Open matei219 opened 2 months ago

matei219 commented 2 months ago

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)