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

stocks/dps bug fixes #2614

Merged
merged 6 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions openbb_terminal/stocks/dark_pool_shorts/dps_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def call_ctb(self, other_args: List[str]):
)
if ns_parser:
ibkr_view.display_cost_to_borrow(
num_stocks=ns_parser.number,
limit=ns_parser.number,
export=ns_parser.export,
)

Expand Down Expand Up @@ -229,7 +229,7 @@ def call_prom(self, other_args: List[str]):
if ns_parser:
finra_view.darkpool_otc(
input_limit=ns_parser.n_num,
promising=ns_parser.limit,
limit=ns_parser.limit,
tier=ns_parser.tier,
export=ns_parser.export,
)
Expand Down
67 changes: 34 additions & 33 deletions openbb_terminal/stocks/dark_pool_shorts/finra_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def getFINRAweeks(tier: str = "T1", is_ats: bool = True) -> List:
@log_start_end(log=logger)
def getFINRAdata_offset(
start_date: str,
symbol: str = "",
tier: str = "T1",
symbol: str = "",
is_ats: bool = True,
offset: int = 0,
) -> requests.Response:
Expand All @@ -75,10 +75,10 @@ def getFINRAdata_offset(
----------
start_date: str
Weekly data to get FINRA data, in YYYY-MM-DD format
symbol: str
Stock ticker to get data from
tier: str
Stock tier between T1, T2, or OTCE
symbol: str
Stock ticker to get data from
is_ats: bool
ATS data if true, NON-ATS otherwise
offset: int
Expand Down Expand Up @@ -259,35 +259,36 @@ def getATSdata(limit: int = 1000, tier_ats: str = "T1") -> Tuple[pd.DataFrame, D
# df_ats = df_ats.append(df_ats_week, ignore_index=True)
df_ats = pd.concat([df_ats, df_ats_week], ignore_index=True)

df_ats = df_ats.sort_values("weekStartDate")
df_ats["weekStartDateInt"] = pd.to_datetime(df_ats["weekStartDate"]).apply(
lambda x: x.timestamp()
)
if not df_ats.empty:
df_ats = df_ats.sort_values("weekStartDate")
df_ats["weekStartDateInt"] = pd.to_datetime(df_ats["weekStartDate"]).apply(
lambda x: x.timestamp()
)

console.print(f"Processing regression on {limit} promising tickers ...")

d_ats_reg = {}
# set(df_ats['issueSymbolIdentifier'].values) this would be iterating through all tickers
# but that is extremely time consuming for little reward. A little filtering is done to
# speed up search for best ATS tickers
for symbol in list(
df_ats.groupby("issueSymbolIdentifier")["totalWeeklyShareQuantity"]
.sum()
.sort_values()[-limit:]
.index
):
try:
slope = stats.linregress(
df_ats[df_ats["issueSymbolIdentifier"] == symbol][
"weekStartDateInt"
].values,
df_ats[df_ats["issueSymbolIdentifier"] == symbol][
"totalWeeklyShareQuantity"
].values,
)[0]
d_ats_reg[symbol] = slope
except Exception: # nosec B110
pass
console.print(f"Processing regression on {limit} promising tickers ...")

d_ats_reg = {}
# set(df_ats['issueSymbolIdentifier'].values) this would be iterating through all tickers
# but that is extremely time consuming for little reward. A little filtering is done to
# speed up search for best ATS tickers
for symbol in list(
df_ats.groupby("issueSymbolIdentifier")["totalWeeklyShareQuantity"]
.sum()
.sort_values()[-limit:]
.index
):
try:
slope = stats.linregress(
df_ats[df_ats["issueSymbolIdentifier"] == symbol][
"weekStartDateInt"
].values,
df_ats[df_ats["issueSymbolIdentifier"] == symbol][
"totalWeeklyShareQuantity"
].values,
)[0]
d_ats_reg[symbol] = slope
except Exception: # nosec B110
pass

return df_ats, d_ats_reg

Expand All @@ -314,7 +315,7 @@ def getTickerFINRAdata(symbol: str) -> Tuple[pd.DataFrame, pd.DataFrame]:
for tier in tiers:
for d_week in getFINRAweeks(tier, is_ats=True):
status_code, response = getFINRAdata(
d_week["weekStartDate"], tier, symbol, True
d_week["weekStartDate"], symbol, tier, True
)
if status_code == 200:
if response:
Expand All @@ -333,7 +334,7 @@ def getTickerFINRAdata(symbol: str) -> Tuple[pd.DataFrame, pd.DataFrame]:
for tier in tiers:
for d_week in getFINRAweeks(tier, is_ats=False):
status_code, response = getFINRAdata(
d_week["weekStartDate"], tier, symbol, False
d_week["weekStartDate"], symbol, tier, False
)
if status_code == 200:
if response:
Expand Down
33 changes: 20 additions & 13 deletions openbb_terminal/stocks/dark_pool_shorts/finra_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,23 @@ def darkpool_otc(
# TODO: Improve command logic to be faster and more useful
df_ats, d_ats_reg = finra_model.getATSdata(input_limit, tier)

symbols = list(
dict(sorted(d_ats_reg.items(), key=lambda item: item[1], reverse=True)).keys()
)[:limit]

plot_dark_pools_ats(df_ats, symbols, external_axes)
console.print("")

export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"prom",
df_ats,
)
if not df_ats.empty and d_ats_reg:

symbols = list(
dict(
sorted(d_ats_reg.items(), key=lambda item: item[1], reverse=True)
).keys()
)[:limit]

plot_dark_pools_ats(df_ats, symbols, external_axes)
console.print("")

export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"prom",
df_ats,
)
else:
console.print("[red]Could not get data[/red]\n")
return
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def test_call_func_expect_queue(expected_queue, queue, func):
],
dict(
num=1,
promising=2,
limit=2,
tier="T1",
export="csv",
),
Expand Down