Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate NeuTrader to Qlib RL #1169

Merged
merged 46 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b184cc4
Refine previous version RL codes
lihuoran Jun 16, 2022
92d4ec4
Polish utils/__init__.py
lihuoran Jun 16, 2022
7535d60
Draft
lihuoran Jun 20, 2022
15340ff
Merge branch 'main' into huoran/qlib_rl
lihuoran Jun 24, 2022
e23504c
Use | instead of Union
lihuoran Jun 24, 2022
9348401
Simulator & action interpreter
lihuoran Jun 27, 2022
a2f7383
Test passed
lihuoran Jun 27, 2022
47252a4
Merge branch 'main' into huoran/qlib_rl
lihuoran Jun 28, 2022
d8858ba
Migrate to SAOEState & new qlib interpreter
lihuoran Jul 8, 2022
09f5106
Black format
lihuoran Jul 8, 2022
11ee76e
. Revert file_storage change
lihuoran Jul 14, 2022
3294e4d
Refactor file structure & renaming functions
lihuoran Jul 14, 2022
a44fbf5
Enrich test cases
lihuoran Jul 15, 2022
aeb54cb
Add QlibIntradayBacktestData
lihuoran Jul 15, 2022
5ff6407
Test interpreter
lihuoran Jul 15, 2022
7d46689
Black format
lihuoran Jul 19, 2022
3ab9df2
.
lihuoran Jul 21, 2022
fae0f77
Merge branch 'main' into huoran/qlib_rl
lihuoran Jul 21, 2022
036e593
Rename receive_execute_result()
lihuoran Jul 21, 2022
53dde51
Use indicator to simplify state update
lihuoran Jul 22, 2022
00def78
Format code
lihuoran Jul 22, 2022
0536672
Modify data path
lihuoran Jul 25, 2022
77966c2
Adjust file structure
lihuoran Jul 26, 2022
85a2cb3
Minor change
lihuoran Jul 26, 2022
a573768
Merge branch 'main' into huoran/qlib_rl
lihuoran Jul 26, 2022
ecb385a
Add copyright message
lihuoran Jul 26, 2022
80b2006
Format code
lihuoran Jul 26, 2022
e864bba
Rename util functions
lihuoran Jul 26, 2022
bad1ae5
Add CI
lihuoran Jul 26, 2022
0caa9a4
Pylint issue
lihuoran Jul 26, 2022
83d8f00
Remove useless code to pass pylint
lihuoran Jul 26, 2022
ccc3f96
Pass mypy
lihuoran Jul 26, 2022
f269274
Mypy issue
lihuoran Jul 26, 2022
e453290
mypy issue
lihuoran Jul 26, 2022
8eb1b01
mypy issue
lihuoran Jul 27, 2022
e2a72b6
Revert "mypy issue"
lihuoran Jul 27, 2022
59e0b80
mypy issue
lihuoran Jul 27, 2022
2fcadfe
mypy issue
lihuoran Jul 27, 2022
54231b1
Fix the numpy version incompatible bug
you-n-g Jul 27, 2022
cbb767e
Fix a minor typing issue
lihuoran Jul 27, 2022
87ef47f
Try to skip python 3.7 test for qlib simulator
lihuoran Jul 27, 2022
c495798
Resolve PR comments by Yuge; solve several CI issues.
lihuoran Jul 27, 2022
362c3ab
Black issue
lihuoran Jul 27, 2022
eb8593b
Fix a low-level type error
lihuoran Jul 28, 2022
8ae62fc
Change data name
lihuoran Jul 28, 2022
a6aa367
Resolve PR comments. Leave TODOs in the code base.
lihuoran Jul 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
.
.

.
  • Loading branch information
lihuoran committed Jul 21, 2022
commit 3ab9df250453af29b6fa3bdf1a0e095cf64a3618
20 changes: 10 additions & 10 deletions qlib/rl/order_execution/from_neutrader/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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

Expand Down Expand Up @@ -71,12 +71,12 @@ def get(self, stock_id: str, date: pd.Timestamp, backtest: bool = False):
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,
Expand Down Expand Up @@ -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)
Expand All @@ -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,
)
11 changes: 5 additions & 6 deletions qlib/rl/order_execution/simulator_qlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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
Expand All @@ -31,7 +30,7 @@

class DecomposedStrategy(BaseStrategy):
lihuoran marked this conversation as resolved.
Show resolved Hide resolved
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]] = []
Expand Down Expand Up @@ -91,7 +90,7 @@ def generate_trade_decision(self, execute_result: list = None) -> TradeDecisionW

class StateMaintainer:
lihuoran marked this conversation as resolved.
Show resolved Hide resolved
lihuoran marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -224,16 +223,16 @@ def _metrics_collect(
)


class QlibSimulator(Simulator[Order, SAOEState, float]):
class SingleAssetQlibSimulator(Simulator[Order, SAOEState, float]):
lihuoran marked this conversation as resolved.
Show resolved Hide resolved
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__(
lihuoran marked this conversation as resolved.
Show resolved Hide resolved
initial=None, # TODO
)

Expand Down
4 changes: 3 additions & 1 deletion qlib/rl/order_execution/simulator_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
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
Expand Down Expand Up @@ -158,7 +160,7 @@ def __init__(
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
Expand Down
35 changes: 16 additions & 19 deletions qlib/rl/order_execution/tests/test_simulator_qlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
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

Expand All @@ -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,
Expand All @@ -45,21 +44,19 @@ def _inner_executor_fn(time_per_step: str, common_infra: CommonInfrastructure) -
)

# 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(
Expand All @@ -78,7 +75,7 @@ def _inner_executor_fn(time_per_step: str, common_infra: CommonInfrastructure) -
generate_report=False,
)

return QlibSimulator(
return SingleAssetQlibSimulator(
order=order,
time_per_step="30min",
qlib_config=qlib_config,
Expand Down