1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-06 20:41:09 +08:00

add infra interface & fix no KeyboardInterpret bug

This commit is contained in:
bxdd
2021-05-31 20:40:11 +08:00
parent bf3b757294
commit 60e082e446
10 changed files with 120 additions and 72 deletions

View File

@@ -7,6 +7,7 @@ from .executor import BaseExecutor
from .backtest import backtest as backtest_func
from .backtest import collect_data as data_generator
from .utils import CommonInfrastructure
from ..strategy.base import BaseStrategy
from ..utils import init_instance_by_config
from ..log import get_module_logger
@@ -101,10 +102,7 @@ def get_strategy_executor(
)
trade_exchange = get_exchange(**exchange_kwargs)
common_infra = {
"trade_account": trade_account,
"trade_exchange": trade_exchange,
}
common_infra = CommonInfrastructure(trade_account=trade_account, trade_exchange=trade_exchange)
trade_strategy = init_instance_by_config(strategy, accept_types=BaseStrategy, common_infra=common_infra)
trade_executor = init_instance_by_config(executor, accept_types=BaseExecutor, common_infra=common_infra)

View File

@@ -9,7 +9,7 @@ from ..utils.resam import parse_freq
from .order import Order
from .exchange import Exchange
from .utils import TradeCalendarManager
from .utils import TradeCalendarManager, CommonInfrastructure, LevelInfrastructure
class BaseExecutor:
@@ -23,7 +23,7 @@ class BaseExecutor:
generate_report: bool = False,
verbose: bool = False,
track_data: bool = False,
common_infra: dict = {},
common_infra: CommonInfrastructure = None,
**kwargs,
):
"""
@@ -39,7 +39,7 @@ class BaseExecutor:
whether to generate trade_decision, will be used when making data for multi-level training
- If `self.track_data` is true, when making data for training, the input `trade_decision` of `execute` will be generated by `collect_data`
- Else, `trade_decision` will not be generated
common_infra : dict, optional:
common_infra : CommonInfrastructure, optional:
common infrastructure for backtesting, may including:
- trade_account : Account, optional
trade account for trading
@@ -63,11 +63,11 @@ class BaseExecutor:
else:
self.common_infra.update(common_infra)
if "trade_account" in common_infra:
if common_infra.has("trade_account"):
self.trade_account = copy.copy(common_infra.get("trade_account"))
self.trade_account.reset(freq=self.time_per_step, init_report=True)
def reset(self, track_data: bool = None, common_infra: dict = None, **kwargs):
def reset(self, track_data: bool = None, common_infra: CommonInfrastructure = None, **kwargs):
"""
- reset `start_time` and `end_time`, used in trade calendar
- reset `track_data`, used when making data for multi-level training
@@ -88,7 +88,7 @@ class BaseExecutor:
self.reset_common_infra(common_infra)
def get_level_infra(self):
return {"trade_calendar": self.trade_calendar}
return LevelInfrastructure(trade_calendar=self.trade_calendar)
def finished(self):
return self.trade_calendar.finished()
@@ -138,7 +138,7 @@ class NestedExecutor(BaseExecutor):
verbose: bool = False,
track_data: bool = False,
trade_exchange: Exchange = None,
common_infra: dict = {},
common_infra: CommonInfrastructure = None,
**kwargs,
):
"""
@@ -182,7 +182,7 @@ class NestedExecutor(BaseExecutor):
"""
super(NestedExecutor, self).reset_common_infra(common_infra)
if self.generate_report and "trade_exchange" in common_infra:
if self.generate_report and common_infra.has("trade_exchange"):
self.trade_exchange = common_infra.get("trade_exchange")
self.inner_executor.reset_common_infra(common_infra)
@@ -257,7 +257,7 @@ class SimulatorExecutor(BaseExecutor):
verbose: bool = False,
track_data: bool = False,
trade_exchange: Exchange = None,
common_infra: dict = {},
common_infra: CommonInfrastructure = None,
**kwargs,
):
"""
@@ -286,7 +286,7 @@ class SimulatorExecutor(BaseExecutor):
- reset trade_exchange
"""
super(SimulatorExecutor, self).reset_common_infra(common_infra)
if "trade_exchange" in common_infra:
if common_infra.has("trade_exchange"):
self.trade_exchange = common_infra.get("trade_exchange")
def execute(self, trade_decision):

View File

@@ -2,6 +2,7 @@
# Licensed under the MIT License.
import pandas as pd
import warnings
from typing import Union
from ..utils.resam import get_resam_calendar
@@ -96,3 +97,46 @@ class TradeCalendarManager:
def get_all_time(self):
"""Get the start_time and end_time for trading"""
return self.start_time, self.end_time
class BaseInfrastructure:
def __init__(self, **kwargs):
self.reset_infra(**kwargs)
def get_support_infra(self):
raise NotImplementedError("`get_support_infra` is not implemented!")
def reset_infra(self, **kwargs):
support_infra = self.get_support_infra()
for k, v in kwargs.items():
if k in support_infra:
setattr(self, k, v)
else:
warnings.warn(f"{k} is ignored in `reset_infra`!")
def get(self, infra_name):
if hasattr(self, infra_name):
return getattr(self, infra_name)
else:
warnings.warn(f"infra {infra_name} is not found!")
def has(self, infra_name):
if infra_name in self.get_support_infra() and hasattr(self, infra_name):
return True
else:
return False
def update(self, other):
support_infra = other.get_support_infra()
infra_dict = {_infra: getattr(other, _infra) for _infra in support_infra if hasattr(other, _infra)}
self.reset_infra(**infra_dict)
class CommonInfrastructure(BaseInfrastructure):
def get_support_infra(self):
return ["trade_account", "trade_exchange"]
class LevelInfrastructure(BaseInfrastructure):
def get_support_infra(self):
return ["trade_calendar"]