From f277a66582eb9f8e65c64380a885105e236929e0 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Sat, 6 Mar 2021 13:01:00 +0530 Subject: [PATCH 01/10] Add .deepsource.toml Signed-off-by: shubhendra --- .deepsource.toml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 000000000..773c9e023 --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,12 @@ +version = 1 + +test_patterns = ["tests/test_*.py"] + +exclude_patterns = ["examples/**"] + +[[analyzers]] +name = "python" +enabled = true + + [analyzers.meta] + runtime_version = "3.x.x" From 07eef183372e7dda39f7b427ff5cf22427a31bc8 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Sat, 6 Mar 2021 13:01:01 +0530 Subject: [PATCH 02/10] Remove length check in favour of truthiness of the object Signed-off-by: shubhendra --- qlib/contrib/backtest/exchange.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qlib/contrib/backtest/exchange.py b/qlib/contrib/backtest/exchange.py index 178950eeb..5a8b8b2b6 100644 --- a/qlib/contrib/backtest/exchange.py +++ b/qlib/contrib/backtest/exchange.py @@ -93,7 +93,7 @@ class Exchange: self.limit_threshold = limit_threshold # TODO: the quote, trade_dates, codes are not necessray. # It is just for performance consideration. - if trade_dates is not None and len(trade_dates): + if trade_dates is not None and trade_dates: start_date, end_date = trade_dates[0], trade_dates[-1] else: self.logger.warning("trade_dates have not been assigned, all dates will be loaded") From 6f034ccb5d289845816fc8c62e0b4a04ef4857fa Mon Sep 17 00:00:00 2001 From: shubhendra Date: Sat, 6 Mar 2021 13:01:02 +0530 Subject: [PATCH 03/10] Remove unnecessary use of comprehension Signed-off-by: shubhendra --- qlib/contrib/online/manager.py | 2 +- qlib/contrib/report/graph.py | 2 +- qlib/workflow/cli.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/qlib/contrib/online/manager.py b/qlib/contrib/online/manager.py index b0be7f076..70b7bad40 100644 --- a/qlib/contrib/online/manager.py +++ b/qlib/contrib/online/manager.py @@ -63,7 +63,7 @@ class UserManager: account_path = self.data_path / user_id strategy_file = self.data_path / user_id / "strategy_{}.pickle".format(user_id) model_file = self.data_path / user_id / "model_{}.pickle".format(user_id) - cur_user_list = [user_id for user_id in self.users] + cur_user_list = list(self.users) if user_id in cur_user_list: raise ValueError("User {} has been loaded".format(user_id)) else: diff --git a/qlib/contrib/report/graph.py b/qlib/contrib/report/graph.py index 70e382fb1..677e767ee 100644 --- a/qlib/contrib/report/graph.py +++ b/qlib/contrib/report/graph.py @@ -161,7 +161,7 @@ class DistplotGraph(BaseGraph): """ _t_df = self._df.dropna() _data_list = [_t_df[_col] for _col in self._name_dict] - _label_list = [_name for _name in self._name_dict.values()] + _label_list = list(self._name_dict.values()) _fig = create_distplot(_data_list, _label_list, show_rug=False, **self._graph_kwargs) return _fig["data"] diff --git a/qlib/workflow/cli.py b/qlib/workflow/cli.py index 6eba96277..879c0aaeb 100644 --- a/qlib/workflow/cli.py +++ b/qlib/workflow/cli.py @@ -16,7 +16,7 @@ def get_path_list(path): if isinstance(path, str): return [path] else: - return [p for p in path] + return list(path) def sys_config(config, config_path): From 5015d218ff726784280dd1913ecd987990a3ea73 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Sat, 6 Mar 2021 13:01:03 +0530 Subject: [PATCH 04/10] Remove methods with unnecessary super delegation. Signed-off-by: shubhendra --- qlib/data/cache.py | 3 --- qlib/data/data.py | 3 --- qlib/data/dataset/__init__.py | 11 ----------- qlib/workflow/expm.py | 3 --- 4 files changed, 20 deletions(-) diff --git a/qlib/data/cache.py b/qlib/data/cache.py index 0174dc63f..2d0b8a7cd 100644 --- a/qlib/data/cache.py +++ b/qlib/data/cache.py @@ -1045,9 +1045,6 @@ class SimpleDatasetCache(DatasetCache): class DatasetURICache(DatasetCache): """Prepared cache mechanism for server.""" - def __init__(self, provider): - super(DatasetURICache, self).__init__(provider) - def _uri(self, instruments, fields, start_time, end_time, freq, disk_cache=1, **kwargs): return hash_args(*self.normalize_uri_args(instruments, fields, freq), disk_cache) diff --git a/qlib/data/data.py b/qlib/data/data.py index 762467da3..000bd1196 100644 --- a/qlib/data/data.py +++ b/qlib/data/data.py @@ -654,9 +654,6 @@ class LocalExpressionProvider(ExpressionProvider): Provide expression data from local data source. """ - def __init__(self): - super().__init__() - def expression(self, instrument, field, start_time=None, end_time=None, freq="day"): expression = self.get_expression_instance(field) start_time = pd.Timestamp(start_time) diff --git a/qlib/data/dataset/__init__.py b/qlib/data/dataset/__init__.py index 8ff8c1210..b87beeeb4 100644 --- a/qlib/data/dataset/__init__.py +++ b/qlib/data/dataset/__init__.py @@ -76,17 +76,6 @@ class DatasetH(Dataset): - The processing is related to data split. """ - def __init__(self, handler: Union[dict, DataHandler], segments: dict): - """ - Parameters - ---------- - handler : Union[dict, DataHandler] - handler will be passed into setup_data. - segments : dict - handler will be passed into setup_data. - """ - super().__init__(handler, segments) - def init(self, handler_kwargs: dict = None, segment_kwargs: dict = None): """ Initialize the DatasetH diff --git a/qlib/workflow/expm.py b/qlib/workflow/expm.py index a50dce7c9..a75c1cf6e 100644 --- a/qlib/workflow/expm.py +++ b/qlib/workflow/expm.py @@ -229,9 +229,6 @@ class MLflowExpManager(ExpManager): Use mlflow to implement ExpManager. """ - def __init__(self, uri, default_exp_name): - super(MLflowExpManager, self).__init__(uri, default_exp_name) - @property def client(self): # Delay the creation of mlflow client in case of creating `mlruns` folder when importing qlib From a62d1a1b360355d26a9e21476a7b76a54f222be1 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Sat, 6 Mar 2021 13:01:04 +0530 Subject: [PATCH 05/10] Use literal syntax to create data structure Signed-off-by: shubhendra --- qlib/contrib/evaluate_portfolio.py | 4 ++-- qlib/utils/__init__.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/qlib/contrib/evaluate_portfolio.py b/qlib/contrib/evaluate_portfolio.py index 04ddd8db0..b3a770e7c 100644 --- a/qlib/contrib/evaluate_portfolio.py +++ b/qlib/contrib/evaluate_portfolio.py @@ -61,7 +61,7 @@ def get_position_value(evaluate_date, position): # load close price for position # position should also consider cash instruments = list(position.keys()) - instruments = list(set(instruments) - set(["cash"])) # filter 'cash' + instruments = list(set(instruments) - {"cash"}) # filter 'cash' fields = ["$close"] close_data_df = D.features( instruments, @@ -80,7 +80,7 @@ def get_position_list_value(positions): instruments = set() for day, position in positions.items(): instruments.update(position.keys()) - instruments = list(set(instruments) - set(["cash"])) # filter 'cash' + instruments = list(set(instruments) - {"cash"}) # filter 'cash' instruments.sort() day_list = list(positions.keys()) day_list.sort() diff --git a/qlib/utils/__init__.py b/qlib/utils/__init__.py index 6640dae2c..3585c5281 100644 --- a/qlib/utils/__init__.py +++ b/qlib/utils/__init__.py @@ -212,7 +212,7 @@ def get_cls_kwargs(config: Union[dict, str], module) -> (type, dict): def init_instance_by_config( - config: Union[str, dict, object], module=None, accept_types: Union[type, Tuple[type]] = tuple([]), **kwargs + config: Union[str, dict, object], module=None, accept_types: Union[type, Tuple[type]] = (), **kwargs ) -> object: """ get initialized instance with config From dc86a6abc5ec9a5423dbfa4e96ad70cb04373699 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Sat, 6 Mar 2021 13:01:05 +0530 Subject: [PATCH 06/10] Refactor unnecessary `else` / `elif` when `if` block has a `continue` statement Signed-off-by: shubhendra --- qlib/contrib/backtest/account.py | 7 +++---- qlib/contrib/backtest/exchange.py | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/qlib/contrib/backtest/account.py b/qlib/contrib/backtest/account.py index d8b285019..a614f08b6 100644 --- a/qlib/contrib/backtest/account.py +++ b/qlib/contrib/backtest/account.py @@ -104,10 +104,9 @@ class Account: # if suspend, no new price to be updated, profit is 0 if trader.check_stock_suspended(code, today): continue - else: - today_close = trader.get_close(code, today) - profit += (today_close - self.current.position[code]["price"]) * self.current.position[code]["amount"] - self.current.update_stock_price(stock_id=code, price=today_close) + today_close = trader.get_close(code, today) + profit += (today_close - self.current.position[code]["price"]) * self.current.position[code]["amount"] + self.current.update_stock_price(stock_id=code, price=today_close) self.rtn += profit # update holding day count self.current.add_count_all() diff --git a/qlib/contrib/backtest/exchange.py b/qlib/contrib/backtest/exchange.py index 5a8b8b2b6..cbb3d7932 100644 --- a/qlib/contrib/backtest/exchange.py +++ b/qlib/contrib/backtest/exchange.py @@ -325,7 +325,7 @@ class Exchange: deal_amount = self.get_real_deal_amount(current_amount, target_amount, factor) if deal_amount == 0: continue - elif deal_amount > 0: + if deal_amount > 0: # buy stock buy_order_list.append( Order( From aab5c5b311b47128a62b32500cb7fab7e3ba485f Mon Sep 17 00:00:00 2001 From: shubhendra Date: Sat, 6 Mar 2021 13:01:05 +0530 Subject: [PATCH 07/10] Refactor the comparison involving `not` Signed-off-by: shubhendra --- qlib/contrib/online/operator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qlib/contrib/online/operator.py b/qlib/contrib/online/operator.py index c8b44f578..d2307dad5 100644 --- a/qlib/contrib/online/operator.py +++ b/qlib/contrib/online/operator.py @@ -148,7 +148,7 @@ class Operator: for user_id, user in um.users.items(): dates, trade_exchange = prepare(um, trade_date, user_id, exchange_config) executor = SimulatorExecutor(trade_exchange=trade_exchange) - if not str(dates[0].date()) == str(pred_date.date()): + if str(dates[0].date()) != str(pred_date.date()): raise ValueError( "The account data is not newest! last trading date {}, today {}".format( dates[0].date(), trade_date.date() From 4fbb5a03c116f22aa243156087579479e6b0dd3c Mon Sep 17 00:00:00 2001 From: shubhendra Date: Fri, 12 Mar 2021 13:05:48 +0530 Subject: [PATCH 08/10] revert fixes that failed unit test --- qlib/contrib/backtest/account.py | 1 + qlib/contrib/backtest/exchange.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/qlib/contrib/backtest/account.py b/qlib/contrib/backtest/account.py index a614f08b6..b2e307fe5 100644 --- a/qlib/contrib/backtest/account.py +++ b/qlib/contrib/backtest/account.py @@ -167,3 +167,4 @@ class Account: def save_account(self, account_path): self.current.save_position(account_path / "position.xlsx", self.last_trade_date) self.report.save_report(account_path / "report.csv") + diff --git a/qlib/contrib/backtest/exchange.py b/qlib/contrib/backtest/exchange.py index cbb3d7932..369b3aef3 100644 --- a/qlib/contrib/backtest/exchange.py +++ b/qlib/contrib/backtest/exchange.py @@ -93,7 +93,7 @@ class Exchange: self.limit_threshold = limit_threshold # TODO: the quote, trade_dates, codes are not necessray. # It is just for performance consideration. - if trade_dates is not None and trade_dates: + if trade_dates is not None and len(trade_dates): start_date, end_date = trade_dates[0], trade_dates[-1] else: self.logger.warning("trade_dates have not been assigned, all dates will be loaded") @@ -325,7 +325,7 @@ class Exchange: deal_amount = self.get_real_deal_amount(current_amount, target_amount, factor) if deal_amount == 0: continue - if deal_amount > 0: + elif deal_amount > 0: # buy stock buy_order_list.append( Order( @@ -423,3 +423,4 @@ class Exchange: raise NotImplementedError("order type {} error".format(order.type)) return trade_val, trade_cost + From 0969c3e7e051c3857c75243cf2fd4cbd5551c331 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Fri, 12 Mar 2021 13:29:20 +0530 Subject: [PATCH 09/10] formatted with black --- qlib/contrib/backtest/account.py | 1 - qlib/contrib/backtest/exchange.py | 1 - 2 files changed, 2 deletions(-) diff --git a/qlib/contrib/backtest/account.py b/qlib/contrib/backtest/account.py index b2e307fe5..a614f08b6 100644 --- a/qlib/contrib/backtest/account.py +++ b/qlib/contrib/backtest/account.py @@ -167,4 +167,3 @@ class Account: def save_account(self, account_path): self.current.save_position(account_path / "position.xlsx", self.last_trade_date) self.report.save_report(account_path / "report.csv") - diff --git a/qlib/contrib/backtest/exchange.py b/qlib/contrib/backtest/exchange.py index 369b3aef3..178950eeb 100644 --- a/qlib/contrib/backtest/exchange.py +++ b/qlib/contrib/backtest/exchange.py @@ -423,4 +423,3 @@ class Exchange: raise NotImplementedError("order type {} error".format(order.type)) return trade_val, trade_cost - From e41373b8adc7686287065811a2ea9d86dfe7bb2e Mon Sep 17 00:00:00 2001 From: Shubhendra Singh Chauhan Date: Fri, 12 Mar 2021 14:10:52 +0530 Subject: [PATCH 10/10] revert fix --- qlib/workflow/expm.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/qlib/workflow/expm.py b/qlib/workflow/expm.py index d50ccbf4a..56fe810c3 100644 --- a/qlib/workflow/expm.py +++ b/qlib/workflow/expm.py @@ -275,6 +275,14 @@ class MLflowExpManager(ExpManager): Use mlflow to implement ExpManager. """ + def __init__(self, uri: Text, default_exp_name: Optional[Text]): + super(MLflowExpManager, self).__init__(uri, default_exp_name) + self._client = None + + def _set_uri(self): + self._client = mlflow.tracking.MlflowClient(tracking_uri=self.uri) + logger.info("{:}".format(self._client)) + @property def client(self): # Delay the creation of mlflow client in case of creating `mlruns` folder when importing qlib