mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-11 23:06:58 +08:00
.
. .
This commit is contained in:
@@ -5,7 +5,7 @@ from typing import List, Optional
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
import qlib
|
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.contrib.ops.high_freq import BFillNan, Cut, Date, DayCumsum, DayLast, FFillNan, IsInf, IsNull, Select
|
||||||
from qlib.data.dataset import DatasetH
|
from qlib.data.dataset import DatasetH
|
||||||
|
|
||||||
@@ -71,12 +71,12 @@ class DataWrapper:
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def init_qlib(config: QlibConfig, part: Optional[str] = None) -> None:
|
def init_qlib(config: dict, part: Optional[str] = None) -> None:
|
||||||
global _dataset
|
global _dataset
|
||||||
|
|
||||||
provider_uri_map = {
|
provider_uri_map = {
|
||||||
"day": config.provider_uri_day.as_posix(),
|
"day": config["provider_uri_day"].as_posix(),
|
||||||
"1min": config.provider_uri_1min.as_posix(),
|
"1min": config["provider_uri_1min"].as_posix(),
|
||||||
}
|
}
|
||||||
qlib.init(
|
qlib.init(
|
||||||
region=REG_CN,
|
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
|
# this won't work if it's put outside in case of multiprocessing
|
||||||
|
|
||||||
if part is None:
|
if part is None:
|
||||||
feature_path = config.feature_root_dir / "feature.pkl"
|
feature_path = config["feature_root_dir"] / "feature.pkl"
|
||||||
backtest_path = config.feature_root_dir / "backtest.pkl"
|
backtest_path = config["feature_root_dir"] / "backtest.pkl"
|
||||||
else:
|
else:
|
||||||
feature_path = config.feature_root_dir / "feature" / (part + ".pkl")
|
feature_path = config["feature_root_dir"] / "feature" / (part + ".pkl")
|
||||||
backtest_path = config.feature_root_dir / "backtest" / (part + ".pkl")
|
backtest_path = config["feature_root_dir"] / "backtest" / (part + ".pkl")
|
||||||
|
|
||||||
with feature_path.open("rb") as f:
|
with feature_path.open("rb") as f:
|
||||||
print(feature_path)
|
print(feature_path)
|
||||||
@@ -129,7 +129,7 @@ def init_qlib(config: QlibConfig, part: Optional[str] = None) -> None:
|
|||||||
_dataset = DataWrapper(
|
_dataset = DataWrapper(
|
||||||
feature_dataset,
|
feature_dataset,
|
||||||
backtest_dataset,
|
backtest_dataset,
|
||||||
config.feature_columns_today,
|
config["feature_columns_today"],
|
||||||
config.feature_columns_yesterday,
|
config["feature_columns_yesterday"],
|
||||||
_internal=True,
|
_internal=True,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import pandas as pd
|
|||||||
from qlib.backtest.decision import BaseTradeDecision, Order, OrderHelper, TradeDecisionWO, TradeRange, TradeRangeByTime
|
from qlib.backtest.decision import BaseTradeDecision, Order, OrderHelper, TradeDecisionWO, TradeRange, TradeRangeByTime
|
||||||
from qlib.backtest.executor import BaseExecutor, NestedExecutor
|
from qlib.backtest.executor import BaseExecutor, NestedExecutor
|
||||||
from qlib.backtest.utils import CommonInfrastructure
|
from qlib.backtest.utils import CommonInfrastructure
|
||||||
from qlib.config import QlibConfig
|
|
||||||
from qlib.constant import EPS
|
from qlib.constant import EPS
|
||||||
from qlib.rl.data.pickle_styled import QlibIntradayBacktestData
|
from qlib.rl.data.pickle_styled import QlibIntradayBacktestData
|
||||||
from qlib.rl.order_execution.from_neutrader.config import ExchangeConfig
|
from qlib.rl.order_execution.from_neutrader.config import ExchangeConfig
|
||||||
@@ -31,7 +30,7 @@ from qlib.strategy.base import BaseStrategy
|
|||||||
|
|
||||||
class DecomposedStrategy(BaseStrategy):
|
class DecomposedStrategy(BaseStrategy):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super(DecomposedStrategy, self).__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.execute_order: Optional[Order] = None
|
self.execute_order: Optional[Order] = None
|
||||||
self.execute_result: List[Tuple[Order, float, float, float]] = []
|
self.execute_result: List[Tuple[Order, float, float, float]] = []
|
||||||
@@ -91,7 +90,7 @@ class SingleOrderStrategy(BaseStrategy):
|
|||||||
|
|
||||||
class StateMaintainer:
|
class StateMaintainer:
|
||||||
def __init__(self, order: Order, tick_index: pd.DatetimeIndex, twap_price: float) -> None:
|
def __init__(self, order: Order, tick_index: pd.DatetimeIndex, twap_price: float) -> None:
|
||||||
super(StateMaintainer, self).__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.position = order.amount
|
self.position = order.amount
|
||||||
self._order = order
|
self._order = order
|
||||||
@@ -224,16 +223,16 @@ class StateMaintainer:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class QlibSimulator(Simulator[Order, SAOEState, float]):
|
class SingleAssetQlibSimulator(Simulator[Order, SAOEState, float]):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
order: Order,
|
order: Order,
|
||||||
time_per_step: str,
|
time_per_step: str,
|
||||||
qlib_config: QlibConfig,
|
qlib_config: dict,
|
||||||
inner_executor_fn: Callable[[str, CommonInfrastructure], BaseExecutor],
|
inner_executor_fn: Callable[[str, CommonInfrastructure], BaseExecutor],
|
||||||
exchange_config: ExchangeConfig,
|
exchange_config: ExchangeConfig,
|
||||||
) -> None:
|
) -> None:
|
||||||
super(QlibSimulator, self).__init__(
|
super().__init__(
|
||||||
initial=None, # TODO
|
initial=None, # TODO
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ from qlib.rl.simulator import Simulator
|
|||||||
from qlib.rl.utils import LogLevel
|
from qlib.rl.utils import LogLevel
|
||||||
from qlib.typehint import TypedDict
|
from qlib.typehint import TypedDict
|
||||||
|
|
||||||
|
# TODO: Integrating Qlib's native data with simulator_simple
|
||||||
|
|
||||||
__all__ = ["SAOEMetrics", "SAOEState", "SingleAssetOrderExecution"]
|
__all__ = ["SAOEMetrics", "SAOEState", "SingleAssetOrderExecution"]
|
||||||
|
|
||||||
ONE_SEC = pd.Timedelta("1s") # use 1 second to exclude the right interval point
|
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",
|
deal_price_type: DealPriceType = "close",
|
||||||
vol_threshold: Optional[float] = None,
|
vol_threshold: Optional[float] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super(SingleAssetOrderExecution, self).__init__(initial=order)
|
super().__init__(initial=order)
|
||||||
|
|
||||||
self.order = order
|
self.order = order
|
||||||
self.ticks_per_step: int = ticks_per_step
|
self.ticks_per_step: int = ticks_per_step
|
||||||
|
|||||||
@@ -5,10 +5,9 @@ import pandas as pd
|
|||||||
from qlib.backtest.decision import Order, OrderDir
|
from qlib.backtest.decision import Order, OrderDir
|
||||||
from qlib.backtest.executor import NestedExecutor, SimulatorExecutor
|
from qlib.backtest.executor import NestedExecutor, SimulatorExecutor
|
||||||
from qlib.backtest.utils import CommonInfrastructure
|
from qlib.backtest.utils import CommonInfrastructure
|
||||||
from qlib.config import QlibConfig
|
|
||||||
from qlib.contrib.strategy import TWAPStrategy
|
from qlib.contrib.strategy import TWAPStrategy
|
||||||
from qlib.rl.order_execution import CategoricalActionInterpreter
|
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
|
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:
|
def _inner_executor_fn(time_per_step: str, common_infra: CommonInfrastructure) -> NestedExecutor:
|
||||||
return NestedExecutor(
|
return NestedExecutor(
|
||||||
time_per_step=time_per_step,
|
time_per_step=time_per_step,
|
||||||
@@ -45,21 +44,19 @@ def get_simulator(order: Order) -> QlibSimulator:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# fmt: off
|
# fmt: off
|
||||||
qlib_config = QlibConfig(
|
qlib_config = {
|
||||||
{
|
"provider_uri_day": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_1d"),
|
||||||
"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"),
|
||||||
"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_root_dir": Path("C:/workspace/NeuTrader/data_sample/cn/qlib_amc_handler_stock"),
|
"feature_columns_today": [
|
||||||
"feature_columns_today": [
|
"$open", "$high", "$low", "$close", "$vwap", "$bid", "$ask", "$volume",
|
||||||
"$open", "$high", "$low", "$close", "$vwap", "$bid", "$ask", "$volume",
|
"$bidV", "$bidV1", "$bidV3", "$bidV5", "$askV", "$askV1", "$askV3", "$askV5",
|
||||||
"$bidV", "$bidV1", "$bidV3", "$bidV5", "$askV", "$askV1", "$askV3", "$askV5",
|
],
|
||||||
],
|
"feature_columns_yesterday": [
|
||||||
"feature_columns_yesterday": [
|
"$open_1", "$high_1", "$low_1", "$close_1", "$vwap_1", "$bid_1", "$ask_1", "$volume_1",
|
||||||
"$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",
|
||||||
"$bidV_1", "$bidV1_1", "$bidV3_1", "$bidV5_1", "$askV_1", "$askV1_1", "$askV3_1", "$askV5_1",
|
],
|
||||||
],
|
}
|
||||||
}
|
|
||||||
)
|
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
exchange_config = ExchangeConfig(
|
exchange_config = ExchangeConfig(
|
||||||
@@ -78,7 +75,7 @@ def get_simulator(order: Order) -> QlibSimulator:
|
|||||||
generate_report=False,
|
generate_report=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
return QlibSimulator(
|
return SingleAssetQlibSimulator(
|
||||||
order=order,
|
order=order,
|
||||||
time_per_step="30min",
|
time_per_step="30min",
|
||||||
qlib_config=qlib_config,
|
qlib_config=qlib_config,
|
||||||
|
|||||||
Reference in New Issue
Block a user