1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-10 14:26:56 +08:00

update rule_startegy & add README, notebook for multi-level trading

This commit is contained in:
bxdd
2021-05-14 01:51:43 +08:00
parent de2658a8db
commit ea60e608ba
5 changed files with 358 additions and 12 deletions

View File

@@ -127,8 +127,7 @@ class BaseExecutor(BaseTradeCalendar):
self.track_data = track_data
def get_init_state(self):
init_state = {"current": self.trade_account.current}
return init_state
raise NotImplementedError("get_init_state in not implemeted!")
def execute(self, **kwargs):
raise NotImplementedError("execute is not implemented!")
@@ -180,9 +179,12 @@ class SplitExecutor(BaseExecutor):
if generate_report:
self.trade_exchange = common_faculty.trade_exchange if trade_exchange is None else trade_exchange
self.sub_env = init_instance_by_config(sub_env, accept_types=BaseExecutor)
self.sub_strategy = init_instance_by_config(sub_strategy, accept_types=self.BaseStrategy)
def get_init_state(self):
init_state = {"current": self.trade_account.current}
return init_state
def _init_sub_trading(self, order_list):
trade_start_time, trade_end_time = self._get_calendar_time(self.trade_index)
self.sub_env.reset(start_time=trade_start_time, end_time=trade_end_time)
@@ -263,6 +265,10 @@ class SimulatorExecutor(BaseExecutor):
)
self.trade_exchange = common_faculty.trade_exchange if trade_exchange is None else trade_exchange
def get_init_state(self):
init_state = {"current": self.trade_account.current, "trade_info": []}
return init_state
def execute(self, order_list):
super(SimulatorExecutor, self).step()
trade_start_time, trade_end_time = self._get_calendar_time(self.trade_index)

View File

@@ -36,6 +36,10 @@ class TWAPStrategy(RuleStrategy, OrderEnhancement):
def generate_order_list(self, execute_state):
super(TWAPStrategy, self).step()
trade_info = execute_state.get("trade_info")
for order, _, _, _ in trade_info:
self.trade_amount[(order.stock_id, order.direction)] -= order.deal_amount
trade_start_time, trade_end_time = self._get_calendar_time(self.trade_index)
order_list = []
for order in self.trade_order_list:
@@ -56,7 +60,15 @@ class TWAPStrategy(RuleStrategy, OrderEnhancement):
// (self.trade_len - self.trade_index)
* _amount_trade_unit
)
if order.direction == order.SELL:
if self.trade_amount[(order.stock_id, order.direction)] > 1e-5 and (
_order_amount is None or self.trade_index == self.trade_len - 1
):
_order_amount = self.trade_amount[(order.stock_id, order.direction)]
if _order_amount:
_order_amount = min(_order_amount, self.trade_amount[(order.stock_id, order.direction)])
_order = Order(
stock_id=order.stock_id,
amount=_order_amount,
@@ -106,8 +118,11 @@ class SBBStrategyBase(RuleStrategy, OrderEnhancement):
def generate_order_list(self, execute_state):
super(SBBStrategyBase, self).step()
if not self.trade_order_list:
return []
trade_info = execute_state.get("trade_info")
for order, _, _, _ in trade_info:
self.trade_amount[(order.stock_id, order.direction)] -= order.deal_amount
trade_start_time, trade_end_time = self._get_calendar_time(self.trade_index)
pred_start_time, pred_end_time = self._get_calendar_time(self.trade_index, shift=1)
order_list = []
@@ -139,11 +154,12 @@ class SBBStrategyBase(RuleStrategy, OrderEnhancement):
* _amount_trade_unit
)
if order.direction == order.SELL:
if self.trade_amount[(order.stock_id, order.direction)] > 1e-5 and _order_amount is None:
if self.trade_amount[(order.stock_id, order.direction)] > 1e-5 and (
_order_amount is None or self.trade_index == self.trade_len - 1
):
_order_amount = self.trade_amount[(order.stock_id, order.direction)]
if _order_amount:
self.trade_amount[(order.stock_id, order.direction)] -= _order_amount
_order = Order(
stock_id=order.stock_id,
amount=_order_amount,
@@ -171,12 +187,13 @@ class SBBStrategyBase(RuleStrategy, OrderEnhancement):
* _amount_trade_unit
)
if order.direction == order.SELL:
if self.trade_amount[(order.stock_id, order.direction)] > 1e-5 and _order_amount is None:
if self.trade_amount[(order.stock_id, order.direction)] >= 1e-5 and (
_order_amount is None or self.trade_index == self.trade_len - 1
):
_order_amount = self.trade_amount[(order.stock_id, order.direction)]
if _order_amount:
_order_amount = min(_order_amount, self.trade_amount[(order.stock_id, order.direction)])
self.trade_amount[(order.stock_id, order.direction)] -= _order_amount
if self.trade_index % 2 == 1:
if (
_pred_trend == self.TREND_SHORT