From 3ab9df250453af29b6fa3bdf1a0e095cf64a3618 Mon Sep 17 00:00:00 2001 From: Huoran Li Date: Thu, 21 Jul 2022 09:31:33 +0800 Subject: [PATCH] . . . --- .../order_execution/from_neutrader/feature.py | 20 +++++------ qlib/rl/order_execution/simulator_qlib.py | 11 +++--- qlib/rl/order_execution/simulator_simple.py | 4 ++- .../tests/test_simulator_qlib.py | 35 +++++++++---------- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/qlib/rl/order_execution/from_neutrader/feature.py b/qlib/rl/order_execution/from_neutrader/feature.py index 40e62c8c0..ba0e5fff4 100644 --- a/qlib/rl/order_execution/from_neutrader/feature.py +++ b/qlib/rl/order_execution/from_neutrader/feature.py @@ -5,7 +5,7 @@ from typing import List, Optional import pandas as pd import qlib -from qlib.config import QlibConfig, REG_CN +from qlib.config import REG_CN from qlib.contrib.ops.high_freq import BFillNan, Cut, Date, DayCumsum, DayLast, FFillNan, IsInf, IsNull, Select from qlib.data.dataset import DatasetH @@ -71,12 +71,12 @@ class DataWrapper: return data -def init_qlib(config: QlibConfig, part: Optional[str] = None) -> None: +def init_qlib(config: dict, part: Optional[str] = None) -> None: global _dataset provider_uri_map = { - "day": config.provider_uri_day.as_posix(), - "1min": config.provider_uri_1min.as_posix(), + "day": config["provider_uri_day"].as_posix(), + "1min": config["provider_uri_1min"].as_posix(), } qlib.init( region=REG_CN, @@ -114,11 +114,11 @@ def init_qlib(config: QlibConfig, part: Optional[str] = None) -> None: # this won't work if it's put outside in case of multiprocessing if part is None: - feature_path = config.feature_root_dir / "feature.pkl" - backtest_path = config.feature_root_dir / "backtest.pkl" + feature_path = config["feature_root_dir"] / "feature.pkl" + backtest_path = config["feature_root_dir"] / "backtest.pkl" else: - feature_path = config.feature_root_dir / "feature" / (part + ".pkl") - backtest_path = config.feature_root_dir / "backtest" / (part + ".pkl") + feature_path = config["feature_root_dir"] / "feature" / (part + ".pkl") + backtest_path = config["feature_root_dir"] / "backtest" / (part + ".pkl") with feature_path.open("rb") as f: print(feature_path) @@ -129,7 +129,7 @@ def init_qlib(config: QlibConfig, part: Optional[str] = None) -> None: _dataset = DataWrapper( feature_dataset, backtest_dataset, - config.feature_columns_today, - config.feature_columns_yesterday, + config["feature_columns_today"], + config["feature_columns_yesterday"], _internal=True, ) diff --git a/qlib/rl/order_execution/simulator_qlib.py b/qlib/rl/order_execution/simulator_qlib.py index 30512c709..6b6498d92 100644 --- a/qlib/rl/order_execution/simulator_qlib.py +++ b/qlib/rl/order_execution/simulator_qlib.py @@ -12,7 +12,6 @@ import pandas as pd from qlib.backtest.decision import BaseTradeDecision, Order, OrderHelper, TradeDecisionWO, TradeRange, TradeRangeByTime from qlib.backtest.executor import BaseExecutor, NestedExecutor from qlib.backtest.utils import CommonInfrastructure -from qlib.config import QlibConfig from qlib.constant import EPS from qlib.rl.data.pickle_styled import QlibIntradayBacktestData from qlib.rl.order_execution.from_neutrader.config import ExchangeConfig @@ -31,7 +30,7 @@ from qlib.strategy.base import BaseStrategy class DecomposedStrategy(BaseStrategy): def __init__(self) -> None: - super(DecomposedStrategy, self).__init__() + super().__init__() self.execute_order: Optional[Order] = None self.execute_result: List[Tuple[Order, float, float, float]] = [] @@ -91,7 +90,7 @@ class SingleOrderStrategy(BaseStrategy): class StateMaintainer: def __init__(self, order: Order, tick_index: pd.DatetimeIndex, twap_price: float) -> None: - super(StateMaintainer, self).__init__() + super().__init__() self.position = order.amount self._order = order @@ -224,16 +223,16 @@ class StateMaintainer: ) -class QlibSimulator(Simulator[Order, SAOEState, float]): +class SingleAssetQlibSimulator(Simulator[Order, SAOEState, float]): def __init__( self, order: Order, time_per_step: str, - qlib_config: QlibConfig, + qlib_config: dict, inner_executor_fn: Callable[[str, CommonInfrastructure], BaseExecutor], exchange_config: ExchangeConfig, ) -> None: - super(QlibSimulator, self).__init__( + super().__init__( initial=None, # TODO ) diff --git a/qlib/rl/order_execution/simulator_simple.py b/qlib/rl/order_execution/simulator_simple.py index ab4b88031..c7efa0578 100644 --- a/qlib/rl/order_execution/simulator_simple.py +++ b/qlib/rl/order_execution/simulator_simple.py @@ -16,6 +16,8 @@ from qlib.rl.simulator import Simulator from qlib.rl.utils import LogLevel from qlib.typehint import TypedDict +# TODO: Integrating Qlib's native data with simulator_simple + __all__ = ["SAOEMetrics", "SAOEState", "SingleAssetOrderExecution"] ONE_SEC = pd.Timedelta("1s") # use 1 second to exclude the right interval point @@ -158,7 +160,7 @@ class SingleAssetOrderExecution(Simulator[Order, SAOEState, float]): deal_price_type: DealPriceType = "close", vol_threshold: Optional[float] = None, ) -> None: - super(SingleAssetOrderExecution, self).__init__(initial=order) + super().__init__(initial=order) self.order = order self.ticks_per_step: int = ticks_per_step diff --git a/qlib/rl/order_execution/tests/test_simulator_qlib.py b/qlib/rl/order_execution/tests/test_simulator_qlib.py index 2bb63bb8a..0e7c3227d 100644 --- a/qlib/rl/order_execution/tests/test_simulator_qlib.py +++ b/qlib/rl/order_execution/tests/test_simulator_qlib.py @@ -5,10 +5,9 @@ import pandas as pd from qlib.backtest.decision import Order, OrderDir from qlib.backtest.executor import NestedExecutor, SimulatorExecutor from qlib.backtest.utils import CommonInfrastructure -from qlib.config import QlibConfig from qlib.contrib.strategy import TWAPStrategy from qlib.rl.order_execution import CategoricalActionInterpreter -from qlib.rl.order_execution.simulator_qlib import ExchangeConfig, QlibSimulator +from qlib.rl.order_execution.simulator_qlib import ExchangeConfig, SingleAssetQlibSimulator TOTAL_POSITION = 2100.0 @@ -27,7 +26,7 @@ def get_order() -> Order: ) -def get_simulator(order: Order) -> QlibSimulator: +def get_simulator(order: Order) -> SingleAssetQlibSimulator: def _inner_executor_fn(time_per_step: str, common_infra: CommonInfrastructure) -> NestedExecutor: return NestedExecutor( time_per_step=time_per_step, @@ -45,21 +44,19 @@ def get_simulator(order: Order) -> QlibSimulator: ) # fmt: off - qlib_config = QlibConfig( - { - "provider_uri_day": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_1d"), - "provider_uri_1min": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_1min"), - "feature_root_dir": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_handler_stock"), - "feature_columns_today": [ - "$open", "$high", "$low", "$close", "$vwap", "$bid", "$ask", "$volume", - "$bidV", "$bidV1", "$bidV3", "$bidV5", "$askV", "$askV1", "$askV3", "$askV5", - ], - "feature_columns_yesterday": [ - "$open_1", "$high_1", "$low_1", "$close_1", "$vwap_1", "$bid_1", "$ask_1", "$volume_1", - "$bidV_1", "$bidV1_1", "$bidV3_1", "$bidV5_1", "$askV_1", "$askV1_1", "$askV3_1", "$askV5_1", - ], - } - ) + qlib_config = { + "provider_uri_day": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_1d"), + "provider_uri_1min": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_1min"), + "feature_root_dir": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_handler_stock"), + "feature_columns_today": [ + "$open", "$high", "$low", "$close", "$vwap", "$bid", "$ask", "$volume", + "$bidV", "$bidV1", "$bidV3", "$bidV5", "$askV", "$askV1", "$askV3", "$askV5", + ], + "feature_columns_yesterday": [ + "$open_1", "$high_1", "$low_1", "$close_1", "$vwap_1", "$bid_1", "$ask_1", "$volume_1", + "$bidV_1", "$bidV1_1", "$bidV3_1", "$bidV5_1", "$askV_1", "$askV1_1", "$askV3_1", "$askV5_1", + ], + } # fmt: on exchange_config = ExchangeConfig( @@ -78,7 +75,7 @@ def get_simulator(order: Order) -> QlibSimulator: generate_report=False, ) - return QlibSimulator( + return SingleAssetQlibSimulator( order=order, time_per_step="30min", qlib_config=qlib_config,