Skip to content

Commit

Permalink
minor modifications to relative_strength_index
Browse files Browse the repository at this point in the history
  • Loading branch information
fmilthaler committed Sep 26, 2023
1 parent 370d0b2 commit f3871d9
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions finquant/momentum_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def relative_strength_index(
plotted along with the prices in another sub-graph
for comparison.
Ref: https://www.investopedia.com/terms/r/rsi.asp
Ref: https://www.investopedia.com/terms/r/rsi.asp
:Input
:data: pandas.Series or pandas.DataFrame with stock prices in columns
Expand Down Expand Up @@ -46,24 +46,22 @@ def relative_strength_index(
# get the stock key
stock = data.keys()[0]
# calculate price differences
data["diff"] = data.diff(1)
data["diff"] = data.diff(periods=1)
# calculate gains and losses
data["gain"] = data["diff"].clip(lower=0).round(2)
data["loss"] = data["diff"].clip(upper=0).abs().round(2)
data["gain"] = data["diff"].clip(lower=0)
data["loss"] = data["diff"].clip(upper=0).abs()
# placeholder
wl = window_length
# calculate rolling window mean gains and losses
data["avg_gain"] = data["gain"].rolling(window=wl, min_periods=wl).mean()
data["avg_loss"] = data["loss"].rolling(window=wl, min_periods=wl).mean()
# calculate WMS (wilder smoothing method) averages
for i, row in enumerate(data["avg_gain"].iloc[wl + 1 :]):
data["avg_gain"].iloc[i + wl + 1] = (
data["avg_gain"].iloc[i + wl] * (wl - 1) + data["gain"].iloc[i + wl + 1]
) / wl
for i, row in enumerate(data["avg_loss"].iloc[wl + 1 :]):
data["avg_loss"].iloc[i + wl + 1] = (
data["avg_loss"].iloc[i + wl] * (wl - 1) + data["loss"].iloc[i + wl + 1]
) / wl
lambda_wsm = lambda avg_gain_loss, gain_loss, window_length: (avg_gain_loss * (window_length - 1) + gain_loss) / window_length
# ignore SettingWithCopyWarning for the below operation
with pd.option_context('mode.chained_assignment', None):
for gain_or_loss in ["gain", "loss"]:
for i, row in enumerate(data[f"avg_{gain_or_loss}"].iloc[wl + 1:]):
data[f"avg_{gain_or_loss}"].iloc[i + wl + 1] = lambda_wsm(data[f"avg_{gain_or_loss}"].iloc[i + wl], data[gain_or_loss].iloc[i + wl + 1], wl)
# calculate RS values
data["rs"] = data["avg_gain"] / data["avg_loss"]
# calculate RSI
Expand Down Expand Up @@ -98,6 +96,7 @@ def relative_strength_index(
legend=True,
)
plt.legend()
return data["rsi"]


def macd(
Expand Down

0 comments on commit f3871d9

Please sign in to comment.