From 0ec6b87d39e9f8fcccc99abb9a5c904bd3e1c8b0 Mon Sep 17 00:00:00 2001 From: "wangwenxi.handsome" Date: Fri, 23 Jul 2021 05:50:41 +0000 Subject: [PATCH] fix little bug --- qlib/backtest/exchange.py | 2 +- qlib/backtest/high_performance_ds.py | 53 +++++++++++++++------------- qlib/backtest/report.py | 6 ++-- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/qlib/backtest/exchange.py b/qlib/backtest/exchange.py index edcd7baaf..5677e855d 100644 --- a/qlib/backtest/exchange.py +++ b/qlib/backtest/exchange.py @@ -15,7 +15,7 @@ from ..config import C, REG_CN from ..utils.resam import resam_ts_data, ts_data_last from ..log import get_module_logger from .order import Order, OrderDir, OrderHelper -from .high_performane_ds import PandasQuote +from .high_performance_ds import PandasQuote class Exchange: diff --git a/qlib/backtest/high_performance_ds.py b/qlib/backtest/high_performance_ds.py index 3e5a9d8e2..8a908fbf0 100644 --- a/qlib/backtest/high_performance_ds.py +++ b/qlib/backtest/high_performance_ds.py @@ -25,6 +25,7 @@ class BaseQuote: Iterable all stock codes """ + raise NotImplementedError(f"Please implement the `get_all_stock` method") def get_data( @@ -119,76 +120,80 @@ class BaseSingleMetric: """ def __init__(self, metric: Union[dict, pd.Series]): - pass + raise NotImplementedError(f"Please implement the `__init__` method") def __add__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__add__` method") def __radd__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": return self + other def __sub__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__sub__` method") def __rsub__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__rsub__` method") def __mul__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__mul__` method") def __truediv__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__truediv__` method") def __eq__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__eq__` method") def __gt__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__gt__` method") def __lt__(self, other: Union["BaseSingleMetric", int, float]) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `__lt__` method") def __len__(self) -> int: - pass + raise NotImplementedError(f"Please implement the `__len__` method") def sum(self) -> float: - pass + raise NotImplementedError(f"Please implement the `sum` method") def mean(self) -> float: - pass + raise NotImplementedError(f"Please implement the `mean` method") def count(self) -> int: - pass + """Return the count of the single metric, NaN is not included. + """ + + raise NotImplementedError(f"Please implement the `count` method") def abs(self) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `abs` method") def astype(self, type: type) -> "BaseSingleMetric": - pass + raise NotImplementedError(f"Please implement the `astype` method") @property def empty(self) -> bool: """If metric is empyt, return True.""" - pass + raise NotImplementedError(f"Please implement the `empty` method") def add(self, other: "BaseSingleMetric", fill_value: float = None) -> "BaseSingleMetric": """Replace np.NaN with fill_value in two metrics and add them.""" - pass + raise NotImplementedError(f"Please implement the `add` method") - def apply(self, map_dict: dict) -> "BaseSingleMetric": + def map(self, map_dict: dict) -> "BaseSingleMetric": """Replace the value of metric according to map_dict.""" - pass + raise NotImplementedError(f"Please implement the `map` method") class BaseOrderIndicator: """ The data structure of order indicator. !!!NOTE: There are two ways to organize the data structure. Please choose a better way. - 1. one way is use BaseSingleMetric to represent each metric. For example, the data - structure of PandasOrderIndicator is Dict[str: PandasSingleMetric]. It uses + 1. One way is using BaseSingleMetric to represent each metric. For example, the data + structure of PandasOrderIndicator is Dict[str, PandasSingleMetric]. It uses PandasSingleMetric based on pd.Series to represent each metric. - 2. the another way doesn't BaseSingleMetric to represent each metric. The data - structure of PandasOrderIndicator is a whole matrix. + 2. The another way doesn't use BaseSingleMetric to represent each metric. The data + structure of PandasOrderIndicator is a whole matrix. It means you are not neccesary + to inherit the BaseSingleMetric. """ def assign(self, col: str, metric: Union[dict, pd.Series]): @@ -367,7 +372,7 @@ class PandasSingleMetric: def add(self, other, fill_value=None): return PandasSingleMetric(self.metric.add(other.metric, fill_value=fill_value)) - def apply(self, map_dict: dict): + def map(self, map_dict: dict): return PandasSingleMetric(self.metric.apply(map_dict)) diff --git a/qlib/backtest/report.py b/qlib/backtest/report.py index 98d8b4f63..375100cba 100644 --- a/qlib/backtest/report.py +++ b/qlib/backtest/report.py @@ -6,8 +6,6 @@ from collections import OrderedDict from logging import warning import pathlib from typing import Dict, List, Tuple, Union, Callable -import warnings -import inspect import numpy as np import pandas as pd @@ -18,7 +16,7 @@ from qlib.backtest.exchange import Exchange from qlib.backtest.order import BaseTradeDecision, Order, OrderDir from qlib.backtest.utils import TradeCalendarManager -from .high_performane_ds import PandasOrderIndicator +from .high_performance_ds import PandasOrderIndicator from ..data import D from ..tests.config import CSI300_BENCH from ..utils.resam import get_higher_eq_freq_feature, resam_ts_data @@ -329,7 +327,7 @@ class Indicator: self.order_indicator.transfer(func, "trade_price") def func_apply(trade_dir): - return trade_dir.apply(Order.parse_dir) + return trade_dir.map(Order.parse_dir) self.order_indicator.transfer(func_apply, "trade_dir")