import numpy as np from pandas import DataFrame, Series from daily_data import get_daily_data def calculate_rsi(data: DataFrame, period: int = 5) -> Series: """ Calculate the RSI and return it as a Series without modifying the original DataFrame. """ delta = data['Close'].diff() gain = np.where(delta > 0, delta, 0) loss = np.where(delta < 0, -delta, 0) alpha = 1 / period avg_gain = Series(gain).ewm(alpha = alpha, adjust = False).mean() avg_loss = Series(loss).ewm(alpha = alpha, adjust = False).mean() rs = avg_gain / avg_loss return 100 - (100 / (1 + rs)) def signals(data: DataFrame) -> Series: """ Generate swing trading signals based on the Connors VIX Reversal 2 strategy. Returns a Series with 'L' for long signals and 'N' for no signal. """ start_date = data['Date'].min() end_date = data['Date'].max() vix_data = get_daily_data(symbol = 'VIX.XO', start_date = start_date, end_date = end_date) vix_rsi_5 = calculate_rsi(vix_data, period = 5) vix_rsi_above_70 = vix_rsi_5.shift(1) > 70 vix_close_below_prev_close = vix_data['Close'] < vix_data['Close'].shift(1) signals = Series('N', index = data.index) signals[vix_rsi_above_70 & vix_close_below_prev_close] = 'L' return signals