mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-14 00:06:58 +08:00
initial account by position
This commit is contained in:
committed by
you-n-g
parent
9b38e62f21
commit
ca14e36f7a
@@ -8,6 +8,7 @@ from .account import Account
|
|||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ..strategy.base import BaseStrategy
|
from ..strategy.base import BaseStrategy
|
||||||
|
from .position import Position
|
||||||
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
|
||||||
@@ -95,7 +96,7 @@ def get_exchange(
|
|||||||
|
|
||||||
|
|
||||||
def create_account_instance(
|
def create_account_instance(
|
||||||
start_time, end_time, benchmark: str, account: float, pos_type: str = "Position"
|
start_time, end_time, benchmark: str, account: Union[float, int, Position], pos_type: str = "Position"
|
||||||
) -> Account:
|
) -> Account:
|
||||||
"""
|
"""
|
||||||
# TODO: is very strange pass benchmark_config in the account(maybe for report)
|
# TODO: is very strange pass benchmark_config in the account(maybe for report)
|
||||||
@@ -109,13 +110,23 @@ def create_account_instance(
|
|||||||
end time of the benchmark
|
end time of the benchmark
|
||||||
benchmark : str
|
benchmark : str
|
||||||
the benchmark for reporting
|
the benchmark for reporting
|
||||||
account : Union[float, str]
|
account : Union[float, int, Position]
|
||||||
information for describing how to creating the account
|
information for describing how to creating the account
|
||||||
For `float`
|
For `float` or `int`:
|
||||||
Using Account with a normal position
|
Using Account with only initial cash
|
||||||
For `str`:
|
For `Position`:
|
||||||
Using account with a specific Position
|
Using Account with a Position
|
||||||
"""
|
"""
|
||||||
|
if(type(account) in (int, float)):
|
||||||
|
pos_kwargs = {"init_cash": account}
|
||||||
|
elif(type(account) is Position):
|
||||||
|
pos_kwargs = {
|
||||||
|
"init_cash": account.position["cash"],
|
||||||
|
"position_dict": account.position,
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
raise ValueError("account must be in (int, float, Position)")
|
||||||
|
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"init_cash": account,
|
"init_cash": account,
|
||||||
"benchmark_config": {
|
"benchmark_config": {
|
||||||
@@ -125,6 +136,7 @@ def create_account_instance(
|
|||||||
},
|
},
|
||||||
"pos_type": pos_type,
|
"pos_type": pos_type,
|
||||||
}
|
}
|
||||||
|
kwargs.update(pos_kwargs)
|
||||||
return Account(**kwargs)
|
return Account(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ class Account:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
init_cash: float = 1e9,
|
init_cash: float = 1e9,
|
||||||
|
position_dict: dict = {},
|
||||||
freq: str = "day",
|
freq: str = "day",
|
||||||
benchmark_config: dict = {},
|
benchmark_config: dict = {},
|
||||||
pos_type: str = "Position",
|
pos_type: str = "Position",
|
||||||
@@ -74,7 +75,7 @@ class Account:
|
|||||||
):
|
):
|
||||||
self._pos_type = pos_type
|
self._pos_type = pos_type
|
||||||
self._port_metr_enabled = port_metr_enabled
|
self._port_metr_enabled = port_metr_enabled
|
||||||
self.init_vars(init_cash, freq, benchmark_config)
|
self.init_vars(init_cash, position_dict, freq, benchmark_config)
|
||||||
|
|
||||||
def is_port_metr_enabled(self):
|
def is_port_metr_enabled(self):
|
||||||
"""
|
"""
|
||||||
@@ -82,14 +83,17 @@ class Account:
|
|||||||
"""
|
"""
|
||||||
return self._port_metr_enabled and not self.current.skip_update()
|
return self._port_metr_enabled and not self.current.skip_update()
|
||||||
|
|
||||||
def init_vars(self, init_cash, freq: str, benchmark_config: dict):
|
def init_vars(self, init_cash, position_dict, freq: str, benchmark_config: dict):
|
||||||
|
|
||||||
# init cash
|
# init cash
|
||||||
self.init_cash = init_cash
|
self.init_cash = init_cash
|
||||||
self.current: BasePosition = init_instance_by_config(
|
self.current: BasePosition = init_instance_by_config(
|
||||||
{
|
{
|
||||||
"class": self._pos_type,
|
"class": self._pos_type,
|
||||||
"kwargs": {"cash": init_cash},
|
"kwargs": {
|
||||||
|
"cash": init_cash,
|
||||||
|
"position_dict": position_dict,
|
||||||
|
},
|
||||||
"module_path": "qlib.backtest.position",
|
"module_path": "qlib.backtest.position",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -199,13 +199,13 @@ class Position(BasePosition):
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, cash=0, position_dict={}, now_account_value=0):
|
def __init__(self, cash=0, position_dict={}):
|
||||||
# NOTE: The position dict must be copied!!!
|
# NOTE: The position dict must be copied!!!
|
||||||
# Otherwise the initial value
|
# Otherwise the initial value
|
||||||
self.init_cash = cash
|
self.init_cash = cash
|
||||||
self.position = position_dict.copy()
|
self.position = position_dict.copy()
|
||||||
self.position["cash"] = cash
|
self.position["cash"] = cash
|
||||||
self.position["now_account_value"] = now_account_value
|
self.position["now_account_value"] = self.calculate_value()
|
||||||
|
|
||||||
def _init_stock(self, stock_id, amount, price=None):
|
def _init_stock(self, stock_id, amount, price=None):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user