Add script for generating signals based on RSI PowerZones strategy
This commit is contained in:
parent
e52c59652f
commit
b5ffc6044c
38
strategies/rsi_power_zones.py
Normal file
38
strategies/rsi_power_zones.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user