Add script for generating IBS + RSI strategy signals
This commit is contained in:
parent
6247bb48aa
commit
f4c6fcd295
35
strategies/ibs_rsi.py
Normal file
35
strategies/ibs_rsi.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
def calculate_rsi(data: pd.DataFrame, period: int = 21) -> pd.Series:
|
||||||
|
"""
|
||||||
|
Calculate the RSI for a given period 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 = pd.Series(gain).ewm(alpha = alpha, adjust = False).mean()
|
||||||
|
avg_loss = pd.Series(loss).ewm(alpha = alpha, adjust = False).mean()
|
||||||
|
|
||||||
|
rs = avg_gain / avg_loss
|
||||||
|
return 100 - (100 / (1 + rs))
|
||||||
|
|
||||||
|
def calculate_ibs(data: pd.DataFrame) -> pd.Series:
|
||||||
|
"""
|
||||||
|
Calculate the IBS and return it as a Series.
|
||||||
|
"""
|
||||||
|
return (data['Close'] - data['Low']) / (data['High'] - data['Low'])
|
||||||
|
|
||||||
|
def signals(data: pd.DataFrame) -> pd.Series:
|
||||||
|
"""
|
||||||
|
Generate swing trading signals based on the IBS + RSI strategy.
|
||||||
|
|
||||||
|
Returns a Series with 'L' for long signals and 'N' otherwise.
|
||||||
|
"""
|
||||||
|
ibs = calculate_ibs(data)
|
||||||
|
rsi_21 = calculate_rsi(data, period = 21)
|
||||||
|
|
||||||
|
conditions = (ibs < 0.25) & (rsi_21 < 45)
|
||||||
|
return pd.Series(np.where(conditions, 'L', 'N'), index = data.index)
|
Loading…
Reference in New Issue
Block a user