Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
2 changes: 1 addition & 1 deletion qlib/backtest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,4 @@ def format_decisions(
return res


__all__ = ["Order", "backtest"]
__all__ = ["Order", "backtest", "get_strategy_executor"]
5 changes: 5 additions & 0 deletions qlib/backtest/decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ def parse_dir(direction: Union[str, int, np.integer, OrderDir, np.ndarray]) -> U
else:
raise NotImplementedError(f"This type of input is not supported")

@property
def key(self) -> tuple:
Comment thread
lihuoran marked this conversation as resolved.
"""A hashable & unique key to identify this order. Usually used as the key in a dict."""
return self.stock_id, self.start_time.replace(hour=0, minute=0, second=0), self.direction
Comment thread
you-n-g marked this conversation as resolved.
Outdated


class OrderHelper:
"""
Expand Down
12 changes: 11 additions & 1 deletion qlib/backtest/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __init__(
self.track_data = track_data
self._trade_exchange = trade_exchange
self.level_infra = LevelInfrastructure()
self.level_infra.reset_infra(common_infra=common_infra)
self.level_infra.reset_infra(common_infra=common_infra, executor=self)
self._settle_type = settle_type
self.reset(start_time=start_time, end_time=end_time, common_infra=common_infra)
if common_infra is None:
Expand All @@ -124,6 +124,9 @@ def __init__(
self.dealt_order_amount: Dict[str, float] = defaultdict(float)
self.deal_day = None

# whether the current executor is collecting data
self.is_collecting = False
Comment thread
you-n-g marked this conversation as resolved.
Outdated

def reset_common_infra(self, common_infra: CommonInfrastructure, copy_trade_account: bool = False) -> None:
"""
reset infrastructure for trading
Expand Down Expand Up @@ -256,6 +259,8 @@ def collect_data(
object
trade decision
"""
self.is_collecting = True

if self.track_data:
yield trade_decision

Expand Down Expand Up @@ -296,6 +301,8 @@ def collect_data(

if return_value is not None:
return_value.update({"execute_result": res})

self.is_collecting = False
return res

def get_all_executors(self) -> List[BaseExecutor]:
Expand Down Expand Up @@ -473,6 +480,9 @@ def _collect_data(
# do nothing and just step forward
sub_cal.step()

# Lef inner strategy know that the outer level execution is done.
Comment thread
lihuoran marked this conversation as resolved.
Outdated
self.inner_strategy.post_upper_level_exe_step()

return execute_result, {"inner_order_indicators": inner_order_indicators, "decision_list": decision_list}

def post_inner_exe_step(self, inner_exe_res: List[object]) -> None:
Expand Down
9 changes: 7 additions & 2 deletions qlib/backtest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
from ..data.data import Cal


SAOE_DATA_KEY = "saoe_data"
Comment thread
lihuoran marked this conversation as resolved.
Outdated


class TradeCalendarManager:
"""
Manager for trading calendar
Expand Down Expand Up @@ -235,7 +238,9 @@ def update(self, other: BaseInfrastructure) -> None:

class CommonInfrastructure(BaseInfrastructure):
def get_support_infra(self) -> Set[str]:
return {"trade_account", "trade_exchange"}
# SAOE_DATA_KEY is used to store SAOE (single asset order execution) information that should be shared by
Comment thread
you-n-g marked this conversation as resolved.
Outdated
# all strategies. It should be dict.
return {"trade_account", "trade_exchange", SAOE_DATA_KEY}


class LevelInfrastructure(BaseInfrastructure):
Expand All @@ -248,7 +253,7 @@ def get_support_infra(self) -> Set[str]:
sub_level_infra:
- **NOTE**: this will only work after _init_sub_trading !!!
"""
return {"trade_calendar", "sub_level_infra", "common_infra"}
return {"trade_calendar", "sub_level_infra", "common_infra", "executor"}

def reset_cal(
self,
Expand Down
2 changes: 1 addition & 1 deletion qlib/data/dataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,4 @@ def _prepare_seg(self, slc: slice, **kwargs) -> TSDataSampler:
return tsds


__all__ = ["Optional"]
__all__ = ["Optional", "Dataset", "DatasetH"]
2 changes: 1 addition & 1 deletion qlib/rl/aux_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations

from typing import Optional, TYPE_CHECKING, Generic, TypeVar
from typing import TYPE_CHECKING, Generic, Optional, TypeVar

from qlib.typehint import final

Expand Down
1 change: 1 addition & 0 deletions qlib/rl/data/exchange_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd

from qlib.backtest import Exchange, Order

from .pickle_styled import IntradayBacktestData


Expand Down
20 changes: 0 additions & 20 deletions qlib/rl/from_neutrader/config.py

This file was deleted.

109 changes: 0 additions & 109 deletions qlib/rl/from_neutrader/feature.py

This file was deleted.

12 changes: 12 additions & 0 deletions qlib/rl/order_execution/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from typing import TypeVar

import numpy as np
import pandas as pd

FINEST_GRANULARITY = "1min"
COARSEST_GRANULARITY = "1day"
ONE_SEC = pd.Timedelta("1s") # use 1 second to exclude the right interval point
float_or_ndarray = TypeVar("float_or_ndarray", float, np.ndarray)
4 changes: 2 additions & 2 deletions qlib/rl/order_execution/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
from qlib.constant import EPS
from qlib.rl.data import pickle_styled
from qlib.rl.interpreter import ActionInterpreter, StateInterpreter
from qlib.rl.order_execution.state import SAOEState
from qlib.typehint import TypedDict

from .simulator_simple import SAOEState

__all__ = [
"FullHistoryStateInterpreter",
"CurrentStepStateInterpreter",
"CategoricalActionInterpreter",
"TwapRelativeActionInterpreter",
"FullHistoryObs",
]


Expand Down
1 change: 1 addition & 0 deletions qlib/rl/order_execution/policy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from __future__ import annotations

from pathlib import Path
Expand Down
3 changes: 1 addition & 2 deletions qlib/rl/order_execution/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import numpy as np

from qlib.rl.order_execution.state import SAOEMetrics, SAOEState
from qlib.rl.reward import Reward

from .simulator_simple import SAOEMetrics, SAOEState

__all__ = ["PAPenaltyReward"]


Expand Down
Loading