1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-15 16:56:54 +08:00

Rename util functions

This commit is contained in:
Huoran Li
2022-07-26 11:25:01 +08:00
parent 80b2006a1f
commit e864bba47f
2 changed files with 19 additions and 19 deletions

View File

@@ -18,12 +18,12 @@ from qlib.rl.from_neutrader.config import ExchangeConfig
from qlib.rl.from_neutrader.feature import init_qlib from qlib.rl.from_neutrader.feature import init_qlib
from qlib.rl.order_execution.simulator_simple import SAOEMetrics, SAOEState from qlib.rl.order_execution.simulator_simple import SAOEMetrics, SAOEState
from qlib.rl.order_execution.utils import ( from qlib.rl.order_execution.utils import (
_convert_tick_str_to_int, convert_tick_str_to_int,
_dataframe_append, dataframe_append,
_get_common_infra, get_common_infra,
_get_portfolio_and_indicator, get_portfolio_and_indicator,
_get_ticks_slice, get_ticks_slice,
_price_advantage, price_advantage,
) )
from qlib.rl.simulator import Simulator from qlib.rl.simulator import Simulator
from qlib.strategy.base import BaseStrategy from qlib.strategy.base import BaseStrategy
@@ -138,7 +138,7 @@ class StateMaintainer:
assert market_price.shape == market_volume.shape == exec_vol.shape assert market_price.shape == market_volume.shape == exec_vol.shape
self.history_exec = _dataframe_append( self.history_exec = dataframe_append(
self.history_exec, self.history_exec,
self._collect_multi_order_metric( self._collect_multi_order_metric(
order=self._order, order=self._order,
@@ -150,7 +150,7 @@ class StateMaintainer:
), ),
) )
self.history_steps = _dataframe_append( self.history_steps = dataframe_append(
self.history_steps, self.history_steps,
[ [
self._collect_single_order_metric( self._collect_single_order_metric(
@@ -237,7 +237,7 @@ class StateMaintainer:
trade_value=float(np.sum(market_price * exec_vol)), trade_value=float(np.sum(market_price * exec_vol)),
position=self.position - exec_sum, position=self.position - exec_sum,
ffr=float(exec_sum / order.amount), ffr=float(exec_sum / order.amount),
pa=_price_advantage(exec_avg_price, self._twap_price, order.direction), pa=price_advantage(exec_avg_price, self._twap_price, order.direction),
) )
@@ -264,7 +264,7 @@ class SingleAssetQlibSimulator(Simulator[Order, SAOEState, float]):
self._exchange_config = exchange_config self._exchange_config = exchange_config
self._time_per_step = time_per_step self._time_per_step = time_per_step
self._ticks_per_step = _convert_tick_str_to_int(time_per_step) self._ticks_per_step = convert_tick_str_to_int(time_per_step)
self._executor: Optional[NestedExecutor] = None self._executor: Optional[NestedExecutor] = None
self._collect_data_loop: Optional[Generator] = None self._collect_data_loop: Optional[Generator] = None
@@ -280,7 +280,7 @@ class SingleAssetQlibSimulator(Simulator[Order, SAOEState, float]):
init_qlib(self._qlib_config, instrument) init_qlib(self._qlib_config, instrument)
common_infra = _get_common_infra( common_infra = get_common_infra(
self._exchange_config, self._exchange_config,
trade_date=pd.Timestamp(self._order_date), trade_date=pd.Timestamp(self._order_date),
codes=[instrument], codes=[instrument],
@@ -297,7 +297,7 @@ class SingleAssetQlibSimulator(Simulator[Order, SAOEState, float]):
exchange = self._inner_executor.trade_exchange exchange = self._inner_executor.trade_exchange
self._ticks_index = pd.DatetimeIndex([e[1] for e in list(exchange.quote_df.index)]) self._ticks_index = pd.DatetimeIndex([e[1] for e in list(exchange.quote_df.index)])
self._ticks_for_order = _get_ticks_slice( self._ticks_for_order = get_ticks_slice(
self._ticks_index, self._ticks_index,
self._order.start_time, self._order.start_time,
self._order.end_time, self._order.end_time,
@@ -344,7 +344,7 @@ class SingleAssetQlibSimulator(Simulator[Order, SAOEState, float]):
except StopIteration: except StopIteration:
self._done = True self._done = True
_, all_indicators = _get_portfolio_and_indicator(self._executor) _, all_indicators = get_portfolio_and_indicator(self._executor)
self._maintainer.update( self._maintainer.update(
inner_executor=self._inner_executor, inner_executor=self._inner_executor,

View File

@@ -17,7 +17,7 @@ from qlib.rl.order_execution.simulator_simple import ONE_SEC, _float_or_ndarray
from qlib.utils.time import Freq from qlib.utils.time import Freq
def _get_common_infra( def get_common_infra(
config: ExchangeConfig, config: ExchangeConfig,
trade_date: pd.Timestamp, trade_date: pd.Timestamp,
codes: List[str], codes: List[str],
@@ -51,14 +51,14 @@ def _get_common_infra(
return CommonInfrastructure(trade_account=trade_account, trade_exchange=exchange) return CommonInfrastructure(trade_account=trade_account, trade_exchange=exchange)
def _convert_tick_str_to_int(time_per_step: str) -> int: def convert_tick_str_to_int(time_per_step: str) -> int:
d = { d = {
"30min": 30, "30min": 30,
} }
return d[time_per_step] return d[time_per_step]
def _get_ticks_slice( def get_ticks_slice(
ticks_index: pd.DatetimeIndex, ticks_index: pd.DatetimeIndex,
start: pd.Timestamp, start: pd.Timestamp,
end: pd.Timestamp, end: pd.Timestamp,
@@ -69,7 +69,7 @@ def _get_ticks_slice(
return ticks_index[ticks_index.slice_indexer(start, end)] return ticks_index[ticks_index.slice_indexer(start, end)]
def _dataframe_append(df: pd.DataFrame, other: Any) -> pd.DataFrame: def dataframe_append(df: pd.DataFrame, other: Any) -> pd.DataFrame:
# dataframe.append is deprecated # dataframe.append is deprecated
other_df = pd.DataFrame(other).set_index("datetime") other_df = pd.DataFrame(other).set_index("datetime")
other_df.index.name = "datetime" other_df.index.name = "datetime"
@@ -78,7 +78,7 @@ def _dataframe_append(df: pd.DataFrame, other: Any) -> pd.DataFrame:
return res return res
def _price_advantage( def price_advantage(
exec_price: _float_or_ndarray, exec_price: _float_or_ndarray,
baseline_price: float, baseline_price: float,
direction: OrderDir | int, direction: OrderDir | int,
@@ -101,7 +101,7 @@ def _price_advantage(
return cast(_float_or_ndarray, res_wo_nan) return cast(_float_or_ndarray, res_wo_nan)
def _get_portfolio_and_indicator(executor: BaseExecutor) -> Tuple[dict, dict]: def get_portfolio_and_indicator(executor: BaseExecutor) -> Tuple[dict, dict]:
all_executors = executor.get_all_executors() all_executors = executor.get_all_executors()
all_portfolio_metrics = { all_portfolio_metrics = {
"{}{}".format(*Freq.parse(_executor.time_per_step)): _executor.trade_account.get_portfolio_metrics() "{}{}".format(*Freq.parse(_executor.time_per_step)): _executor.trade_account.get_portfolio_metrics()