From b708dfc5b0f603a6fbe647c6007857b952da4a47 Mon Sep 17 00:00:00 2001 From: montezdesousa Date: Fri, 23 Sep 2022 13:24:10 +0100 Subject: [PATCH 1/4] fix dps prom and ctb --- .../stocks/dark_pool_shorts/dps_controller.py | 4 +- .../stocks/dark_pool_shorts/finra_model.py | 59 ++++++++++--------- .../stocks/dark_pool_shorts/finra_view.py | 33 +++++++---- 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/openbb_terminal/stocks/dark_pool_shorts/dps_controller.py b/openbb_terminal/stocks/dark_pool_shorts/dps_controller.py index 208ecd83e8db..74ce9bb45db9 100644 --- a/openbb_terminal/stocks/dark_pool_shorts/dps_controller.py +++ b/openbb_terminal/stocks/dark_pool_shorts/dps_controller.py @@ -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, ) @@ -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, ) diff --git a/openbb_terminal/stocks/dark_pool_shorts/finra_model.py b/openbb_terminal/stocks/dark_pool_shorts/finra_model.py index e7aececc6362..4f79b03e6f40 100644 --- a/openbb_terminal/stocks/dark_pool_shorts/finra_model.py +++ b/openbb_terminal/stocks/dark_pool_shorts/finra_model.py @@ -255,39 +255,40 @@ def getATSdata(limit: int = 1000, tier_ats: str = "T1") -> Tuple[pd.DataFrame, D df_ats_week = pd.DataFrame(l_data) df_ats_week["weekStartDate"] = d_week["weekStartDate"] + d_ats_reg = {} + if not df_ats_week.empty: + # df_ats_week["weekStartDate"] = d_week["weekStartDate"] # 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() - ) - - 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 + 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 ...") + + # 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 diff --git a/openbb_terminal/stocks/dark_pool_shorts/finra_view.py b/openbb_terminal/stocks/dark_pool_shorts/finra_view.py index 18bc2f01c02d..157187bfd3d4 100644 --- a/openbb_terminal/stocks/dark_pool_shorts/finra_view.py +++ b/openbb_terminal/stocks/dark_pool_shorts/finra_view.py @@ -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 not d_ats_reg.empty: + + 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 From de2b0c056b3609fc584a279070f55e184dc85aeb Mon Sep 17 00:00:00 2001 From: montezdesousa Date: Fri, 23 Sep 2022 14:57:44 +0100 Subject: [PATCH 2/4] fix prom command --- .../stocks/dark_pool_shorts/finra_model.py | 66 +++++++++---------- .../stocks/dark_pool_shorts/finra_view.py | 2 +- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/openbb_terminal/stocks/dark_pool_shorts/finra_model.py b/openbb_terminal/stocks/dark_pool_shorts/finra_model.py index 4f79b03e6f40..d974a74ed01c 100644 --- a/openbb_terminal/stocks/dark_pool_shorts/finra_model.py +++ b/openbb_terminal/stocks/dark_pool_shorts/finra_model.py @@ -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: @@ -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 @@ -255,40 +255,40 @@ def getATSdata(limit: int = 1000, tier_ats: str = "T1") -> Tuple[pd.DataFrame, D df_ats_week = pd.DataFrame(l_data) df_ats_week["weekStartDate"] = d_week["weekStartDate"] - d_ats_reg = {} - if not df_ats_week.empty: - # df_ats_week["weekStartDate"] = d_week["weekStartDate"] # 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()) - console.print(f"Processing regression on {limit} promising tickers ...") - - # 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 + 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 return df_ats, d_ats_reg diff --git a/openbb_terminal/stocks/dark_pool_shorts/finra_view.py b/openbb_terminal/stocks/dark_pool_shorts/finra_view.py index 157187bfd3d4..61ba5f9ccea4 100644 --- a/openbb_terminal/stocks/dark_pool_shorts/finra_view.py +++ b/openbb_terminal/stocks/dark_pool_shorts/finra_view.py @@ -213,7 +213,7 @@ def darkpool_otc( # TODO: Improve command logic to be faster and more useful df_ats, d_ats_reg = finra_model.getATSdata(input_limit, tier) - if not df_ats.empty and not d_ats_reg.empty: + if not df_ats.empty and d_ats_reg: symbols = list( dict( From e7f346ffda0c2b549f00fe57b90f83cd25cb0751 Mon Sep 17 00:00:00 2001 From: montezdesousa Date: Fri, 23 Sep 2022 17:45:51 +0100 Subject: [PATCH 3/4] fix dpotc --- openbb_terminal/stocks/dark_pool_shorts/finra_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openbb_terminal/stocks/dark_pool_shorts/finra_model.py b/openbb_terminal/stocks/dark_pool_shorts/finra_model.py index d974a74ed01c..3066a79b547a 100644 --- a/openbb_terminal/stocks/dark_pool_shorts/finra_model.py +++ b/openbb_terminal/stocks/dark_pool_shorts/finra_model.py @@ -315,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: @@ -334,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: From e504c3d8ae90216341217f255245244d9b61f04a Mon Sep 17 00:00:00 2001 From: montezdesousa Date: Fri, 23 Sep 2022 18:05:38 +0100 Subject: [PATCH 4/4] fix unit test --- .../stocks/dark_pool_shorts/test_dps_controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/openbb_terminal/stocks/dark_pool_shorts/test_dps_controller.py b/tests/openbb_terminal/stocks/dark_pool_shorts/test_dps_controller.py index 03c46e0571d1..4e025a4d4fb7 100644 --- a/tests/openbb_terminal/stocks/dark_pool_shorts/test_dps_controller.py +++ b/tests/openbb_terminal/stocks/dark_pool_shorts/test_dps_controller.py @@ -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", ),