Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds relative strength percentile to stocks/ta #2302

Merged
merged 19 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ en:
stocks/ta/cci: commodity channel index
stocks/ta/macd: moving average convergence/divergence
stocks/ta/rsi: relative strength index
stocks/ta/rsp: relative strength percentile
stocks/ta/stoch: stochastic oscillator
stocks/ta/fisher: fisher transform
stocks/ta/cg: centre of gravity
Expand Down
53 changes: 53 additions & 0 deletions openbb_terminal/stocks/technical_analysis/rsp_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
""" Relative Strength Percentile Model """
__docformat__ = "numpy"

import logging
from typing import Tuple

import pandas as pd

from openbb_terminal.decorators import log_start_end


logger = logging.getLogger(__name__)


@log_start_end(log=logger)
def get_rsp(
s_ticker: str = "",
) -> Tuple[pd.DataFrame]:
"""Relative strength percentile [Source: https://github.com/skyte/relative-strength]
Currently takes from https://github.com/soggyomelette/rs-log in order to get desired output

Parameters
----------
s_ticker : str
Stock Ticker

Returns
----------
pd.DataFrame
Dataframe of stock percentile
pd.Dataframe
Dataframe of industry percentile
pd.Dataframe
Raw stock dataframe for export
pd.Dataframe
Raw industry dataframe for export
"""
df_stock_p = pd.read_csv(
"https://raw.githubusercontent.com/soggyomelette/rs-log/main/output/rs_stocks.csv"
)
df_industries_p = pd.read_csv(
"https://raw.githubusercontent.com/soggyomelette/rs-log/main/output/rs_industries.csv"
)
if s_ticker == "":
rsp_stock = pd.DataFrame()
rsp_industry = pd.DataFrame()
else:
rsp_stock = df_stock_p[df_stock_p["Ticker"].str.match(s_ticker)]
for i in range(len(df_industries_p)):
if s_ticker in df_industries_p.iloc[i]["Tickers"]:
rsp_industry = df_industries_p.iloc[[i]]

return (rsp_stock, rsp_industry, df_stock_p, df_industries_p) # type: ignore
72 changes: 72 additions & 0 deletions openbb_terminal/stocks/technical_analysis/rsp_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
""" Relative Strength Percentile View """
__docformat__ = "numpy"

import logging
import os
import pandas as pd

from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import (
export_data,
print_rich_table,
)
from openbb_terminal.stocks.technical_analysis import rsp_model

logger = logging.getLogger(__name__)


@log_start_end(log=logger)
def display_rsp(
s_ticker: str = "",
export: str = "",
tickers_show: bool = False,
):
"""Display Relative Strength Percentile [Source: https://github.com/skyte/relative-strength]

Parameters
----------
s_ticker : str
Stock ticker
export : str
Format of export file
tickers_show : bool
Boolean to check if tickers in the same industry as the stock should be shown
"""

rsp_stock, rsp_industry, df_stock_p, df_industries_p = rsp_model.get_rsp(s_ticker)
if rsp_stock.empty or rsp_industry.empty:
print("Ticker not found")
else:
tickers = pd.DataFrame(rsp_industry["Tickers"])
del rsp_industry["Tickers"]
print_rich_table(
rsp_stock,
headers=list(rsp_stock.columns),
show_index=False,
title="Relative Strength Percentile of Stock (relative to SPY)",
)
print_rich_table(
rsp_industry,
headers=list(rsp_industry.columns),
show_index=False,
title="Relative Strength Percentile of Industry the ticker is part of",
)
if tickers_show:
print_rich_table(
tickers,
headers=list(tickers.columns),
show_index=False,
title="Tickers in same industry as chosen stock",
)
export_data(
export,
os.path.dirname(os.path.abspath(__file__)).replace("common", "stocks"),
"rsp_stock",
df_stock_p,
)
export_data(
export,
os.path.dirname(os.path.abspath(__file__)).replace("common", "stocks"),
"rsp_industry",
df_industries_p,
)
42 changes: 42 additions & 0 deletions openbb_terminal/stocks/technical_analysis/ta_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
finviz_view,
tradingview_model,
tradingview_view,
rsp_view,
)

logger = logging.getLogger(__name__)
Expand All @@ -62,6 +63,7 @@ class TechnicalAnalysisController(StockBaseController):
"cci",
"macd",
"rsi",
"rsp",
"stoch",
"fisher",
"cg",
Expand Down Expand Up @@ -133,6 +135,7 @@ def print_help(self):
mt.add_cmd("cci")
mt.add_cmd("macd")
mt.add_cmd("rsi")
mt.add_cmd("rsp")
mt.add_cmd("stoch")
mt.add_cmd("fisher")
mt.add_cmd("cg")
Expand Down Expand Up @@ -749,6 +752,45 @@ def call_rsi(self, other_args: List[str]):
export=ns_parser.export,
)

@log_start_end(log=logger)
def call_rsp(self, other_args: List[str]):
"""Process rsp command"""
parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="rsp",
description="""
IBD Style Relative Strength Percentile Ranking of Stocks (i.e. 0-100 Score).
Ranks stocks on the basis of relative strength as calculated by Investor's
Business Daily (Yearly performance of stock (most recent quarter is weighted
double) divided by yearly performance of reference index (here, we use SPY)
Export table to view the entire ranking
Data taken from https://github.com/skyte/relative-strength
""",
)

parser.add_argument(
"-t",
"--tickers",
action="store_true",
default=False,
dest="disp_tickers",
help="Show other tickers in the industry the stock is part of",
)

ns_parser = self.parse_known_args_and_warn(
parser,
other_args,
EXPORT_BOTH_RAW_DATA_AND_FIGURES,
)

if ns_parser:
rsp_view.display_rsp(
s_ticker=self.ticker,
export=ns_parser.export,
tickers_show=ns_parser.disp_tickers,
)

@log_start_end(log=logger)
def call_stoch(self, other_args: List[str]):
"""Process stoch command"""
Expand Down
1 change: 1 addition & 0 deletions scripts/test_stocks_ta.openbb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ vwap
cci
macd
rsi
rsp
stoch
fisher
cg
Expand Down
Loading