1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-17 17:34:35 +08:00

fix bug in Exchange

This commit is contained in:
bxdd
2021-06-22 15:03:05 +08:00
parent 4ac6e6e246
commit ab97e82484
4 changed files with 23 additions and 14 deletions

View File

@@ -19,7 +19,7 @@ class NestedDecisonExecutionWorkflow:
benchmark = "SH000300" benchmark = "SH000300"
data_handler_config = { data_handler_config = {
"start_time": "2008-01-01", "start_time": "2008-01-01",
"end_time": "2021-05-28", "end_time": "2020-12-31",
"fit_start_time": "2008-01-01", "fit_start_time": "2008-01-01",
"fit_end_time": "2014-12-31", "fit_end_time": "2014-12-31",
"instruments": market, "instruments": market,
@@ -53,7 +53,7 @@ class NestedDecisonExecutionWorkflow:
"segments": { "segments": {
"train": ("2007-01-01", "2014-12-31"), "train": ("2007-01-01", "2014-12-31"),
"valid": ("2015-01-01", "2016-12-31"), "valid": ("2015-01-01", "2016-12-31"),
"test": ("2020-09-01", "2021-05-28"), "test": ("2020-01-01", "2020-12-31"),
}, },
}, },
}, },
@@ -78,12 +78,8 @@ class NestedDecisonExecutionWorkflow:
}, },
}, },
"inner_strategy": { "inner_strategy": {
"class": "SBBStrategyEMA", "class": "TWAPStrategy",
"module_path": "qlib.contrib.strategy.rule_strategy", "module_path": "qlib.contrib.strategy.rule_strategy",
"kwargs": {
"freq": "day",
"instruments": market,
},
}, },
"track_data": True, "track_data": True,
"generate_report": True, "generate_report": True,
@@ -93,8 +89,8 @@ class NestedDecisonExecutionWorkflow:
}, },
}, },
"backtest": { "backtest": {
"start_time": "2020-09-20", "start_time": "2020-01-01",
"end_time": "2021-05-28", "end_time": "2020-12-31",
"account": 100000000, "account": 100000000,
"benchmark": benchmark, "benchmark": benchmark,
"exchange_kwargs": { "exchange_kwargs": {

View File

@@ -1,14 +1,15 @@
# Copyright (c) Microsoft Corporation. # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License. # Licensed under the MIT License.
import copy
from .account import Account from .account import Account
from .exchange import Exchange from .exchange import Exchange
from .executor import BaseExecutor from .executor import BaseExecutor
from .backtest import backtest_loop from .backtest import backtest_loop
from .backtest import collect_data_loop from .backtest import collect_data_loop
from .utils import CommonInfrastructure from .utils import CommonInfrastructure
from .order import Order from .order import Order
from ..strategy.base import BaseStrategy from ..strategy.base import BaseStrategy
from ..utils import init_instance_by_config from ..utils import init_instance_by_config
from ..log import get_module_logger from ..log import get_module_logger
@@ -101,10 +102,15 @@ def get_strategy_executor(
"end_time": end_time, "end_time": end_time,
}, },
) )
exchange_kwargs = copy.copy(exchange_kwargs)
if "start_time" not in exchange_kwargs:
exchange_kwargs["start_time"] = start_time
if "end_time" not in exchange_kwargs:
exchange_kwargs["end_time"] = end_time
trade_exchange = get_exchange(**exchange_kwargs) trade_exchange = get_exchange(**exchange_kwargs)
common_infra = CommonInfrastructure(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_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) trade_executor = init_instance_by_config(executor, accept_types=BaseExecutor, common_infra=common_infra)

View File

@@ -174,8 +174,8 @@ class Exchange:
self.quote = quote_dict self.quote = quote_dict
def _update_limit(self, buy_limit, sell_limit): def _update_limit(self, buy_limit, sell_limit):
self.quote["limit_buy"] = ~self.quote["$change"].lt(buy_limit) self.quote["limit_buy"] = self.quote["$change"].ge(buy_limit)
self.quote["limit_sell"] = ~self.quote["$change"].gt(-sell_limit) self.quote["limit_sell"] = self.quote["$change"].le(-sell_limit)
def check_stock_limit(self, stock_id, start_time, end_time, direction=None): def check_stock_limit(self, stock_id, start_time, end_time, direction=None):
""" """

View File

@@ -7,6 +7,7 @@ import warnings
import pandas as pd import pandas as pd
from pathlib import Path from pathlib import Path
from pprint import pprint from pprint import pprint
from typing import Union, List
from ..contrib.evaluate import indicator_analysis, risk_analysis, indicator_analysis from ..contrib.evaluate import indicator_analysis, risk_analysis, indicator_analysis
from ..data.dataset import DatasetH from ..data.dataset import DatasetH
@@ -295,7 +296,13 @@ class PortAnaRecord(RecordTemp):
artifact_path = "portfolio_analysis" artifact_path = "portfolio_analysis"
def __init__( def __init__(
self, recorder, config, risk_analysis_freq, indicator_analysis_freq, indicator_analysis_method=None, **kwargs self,
recorder,
config,
risk_analysis_freq: Union[List, str] = [],
indicator_analysis_freq: Union[List, str] = [],
indicator_analysis_method=None,
**kwargs,
): ):
""" """
config["strategy"] : dict config["strategy"] : dict