Skip to content

Commit

Permalink
issues/2: Fix RS formula for negative index returns
Browse files Browse the repository at this point in the history
  • Loading branch information
skyte committed Jun 11, 2022
1 parent 740b56b commit d43314a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions rs_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
def relative_strength(closes: pd.Series, closes_ref: pd.Series):
rs_stock = strength(closes)
rs_ref = strength(closes_ref)
rs = (rs_stock-rs_ref)/rs_ref * 100 # equivalent for percentages: (rs_stock/rs_ref - 1) * 100
rs = (1 + rs_stock) / (1 + rs_ref) * 100
rs = int(rs*100) / 100 # round to 2 decimals
return rs

Expand Down Expand Up @@ -135,7 +135,7 @@ def rankings():

# stocks
df = pd.DataFrame(relative_strengths, columns=[TITLE_RANK, TITLE_TICKER, TITLE_SECTOR, TITLE_INDUSTRY, TITLE_UNIVERSE, TITLE_RS, TITLE_PERCENTILE, TITLE_1M, TITLE_3M, TITLE_6M])
df[TITLE_PERCENTILE] = pd.qcut(df[TITLE_RS], 100, labels=False)
df[TITLE_PERCENTILE] = pd.qcut(df[TITLE_RS], 100, labels=False, duplicates="drop")
df[TITLE_1M] = pd.qcut(df[TITLE_1M], 100, labels=False, duplicates="drop")
df[TITLE_3M] = pd.qcut(df[TITLE_3M], 100, labels=False, duplicates="drop")
df[TITLE_6M] = pd.qcut(df[TITLE_6M], 100, labels=False, duplicates="drop")
Expand Down Expand Up @@ -171,10 +171,10 @@ def getTickers(industries, industry):
df_industries[TITLE_1M] = df_industries.apply(lambda row: getRsAverage(industries, row[TITLE_INDUSTRY], TITLE_1M), axis=1)
df_industries[TITLE_3M] = df_industries.apply(lambda row: getRsAverage(industries, row[TITLE_INDUSTRY], TITLE_3M), axis=1)
df_industries[TITLE_6M] = df_industries.apply(lambda row: getRsAverage(industries, row[TITLE_INDUSTRY], TITLE_6M), axis=1)
df_industries[TITLE_PERCENTILE] = pd.qcut(df_industries[TITLE_RS], 100, labels=False)
df_industries[TITLE_1M] = pd.qcut(df_industries[TITLE_1M], 100, labels=False)
df_industries[TITLE_3M] = pd.qcut(df_industries[TITLE_3M], 100, labels=False)
df_industries[TITLE_6M] = pd.qcut(df_industries[TITLE_6M], 100, labels=False)
df_industries[TITLE_PERCENTILE] = pd.qcut(df_industries[TITLE_RS], 100, labels=False, duplicates="drop")
df_industries[TITLE_1M] = pd.qcut(df_industries[TITLE_1M], 100, labels=False, duplicates="drop")
df_industries[TITLE_3M] = pd.qcut(df_industries[TITLE_3M], 100, labels=False, duplicates="drop")
df_industries[TITLE_6M] = pd.qcut(df_industries[TITLE_6M], 100, labels=False, duplicates="drop")
df_industries[TITLE_TICKERS] = df_industries.apply(lambda row: getTickers(industries, row[TITLE_INDUSTRY]), axis=1)
df_industries = df_industries.sort_values(([TITLE_RS]), ascending=False)
ind_ranks = ind_ranks[:len(df_industries)]
Expand Down

0 comments on commit d43314a

Please sign in to comment.