diff --git a/strategies/rsi_power_zones.py b/strategies/rsi_power_zones.py new file mode 100644 index 0000000..06d04b1 --- /dev/null +++ b/strategies/rsi_power_zones.py @@ -0,0 +1,38 @@ +import numpy as np + +from pandas import DataFrame, Series + +def calculate_moving_average(data: DataFrame, window: int = 200) -> Series: + """ + Calculate the moving average and return it as a Series. + """ + return data['Close'].rolling(window=window).mean() + +def calculate_rsi(data: DataFrame, period: int = 4) -> Series: + """ + Calculate the RSI and return it as a Series. + """ + 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: + """ + Calculate signals for the RSI PowerZones strategy. + Returns a Series with 'L' for long signals and 'N' otherwise. + """ + ma_200 = calculate_moving_average(data, window = 200) + rsi_4 = calculate_rsi(data, period = 4) + + above_ma_200 = data['Close'] > ma_200 + rsi_below_30 = rsi_4 < 30 + + conditions = above_ma_200 & rsi_below_30 + return Series(np.where(conditions, 'L', 'N'), index = data.index) \ No newline at end of file