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
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 accountn
previous values. """ return pd.Series(values).rolling(n).mean()def RVOL(volume, window): """ Calculate Relative Volume (RVOL) using a rolling window.
class SmaCross(Strategy): n1 = 20 n2 = 50 threshold = 1.2
bt = Backtest(MSFT, SmaCross, cash=10000, commission=.002, exclusive_orders=True)
stats = bt.run() print(stats)