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

Qlib simulator refinement #1244

Merged
merged 23 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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"]
10 changes: 10 additions & 0 deletions qlib/backtest/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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.
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
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.

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
12 changes: 12 additions & 0 deletions qlib/rl/order_execution/objects.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)
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