mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-18 01:44:34 +08:00
update strategy
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
"""
|
||||
This order generator is for strategies based on WeightStrategyBase
|
||||
"""
|
||||
from ..backtest.position import Position
|
||||
from ..backtest.exchange import Exchange
|
||||
from ...backtest.position import Position
|
||||
from ...backtest.exchange import Exchange
|
||||
import pandas as pd
|
||||
import copy
|
||||
|
||||
@@ -17,8 +17,10 @@ class OrderGenerator:
|
||||
trade_exchange: Exchange,
|
||||
target_weight_position: dict,
|
||||
risk_degree: float,
|
||||
pred_date: pd.Timestamp,
|
||||
trade_date: pd.Timestamp,
|
||||
pred_start_time: pd.Timestamp,
|
||||
pred_end_time: pd.Timestamp,
|
||||
trade_start_time: pd.Timestamp,
|
||||
trade_end_time: pd.Timestamp,
|
||||
) -> list:
|
||||
"""generate_order_list_from_target_weight_position
|
||||
|
||||
@@ -49,8 +51,10 @@ class OrderGenWInteract(OrderGenerator):
|
||||
trade_exchange: Exchange,
|
||||
target_weight_position: dict,
|
||||
risk_degree: float,
|
||||
pred_date: pd.Timestamp,
|
||||
trade_date: pd.Timestamp,
|
||||
pred_start_time: pd.Timestamp,
|
||||
pred_end_time: pd.Timestamp,
|
||||
trade_start_time: pd.Timestamp,
|
||||
trade_end_time: pd.Timestamp,
|
||||
) -> list:
|
||||
"""generate_order_list_from_target_weight_position
|
||||
|
||||
@@ -77,10 +81,10 @@ class OrderGenWInteract(OrderGenerator):
|
||||
# calculate current_tradable_value
|
||||
current_amount_dict = current.get_stock_amount_dict()
|
||||
current_total_value = trade_exchange.calculate_amount_position_value(
|
||||
amount_dict=current_amount_dict, trade_date=trade_date, only_tradable=False
|
||||
amount_dict=current_amount_dict, trade_start_time=trade_start_time, trade_end_time=trade_end_time, only_tradable=False
|
||||
)
|
||||
current_tradable_value = trade_exchange.calculate_amount_position_value(
|
||||
amount_dict=current_amount_dict, trade_date=trade_date, only_tradable=True
|
||||
amount_dict=current_amount_dict, trade_start_time=trade_start_time, trade_end_time=trade_end_time, only_tradable=True
|
||||
)
|
||||
# add cash
|
||||
current_tradable_value += current.get_cash()
|
||||
@@ -93,7 +97,7 @@ class OrderGenWInteract(OrderGenerator):
|
||||
# value. Then just sell all the stocks
|
||||
target_amount_dict = copy.deepcopy(current_amount_dict.copy())
|
||||
for stock_id in list(target_amount_dict.keys()):
|
||||
if trade_exchange.is_stock_tradable(stock_id, trade_date):
|
||||
if trade_exchange.is_stock_tradable(stock_id, trade_start_time=trade_start_time, trade_end_time=trade_end_time):
|
||||
del target_amount_dict[stock_id]
|
||||
else:
|
||||
# consider cost rate
|
||||
@@ -104,12 +108,14 @@ class OrderGenWInteract(OrderGenerator):
|
||||
target_amount_dict = trade_exchange.generate_amount_position_from_weight_position(
|
||||
weight_position=target_weight_position,
|
||||
cash=current_tradable_value,
|
||||
trade_date=trade_date,
|
||||
trade_start_time=trade_start_time,
|
||||
trade_end_time=trade_end_time,
|
||||
)
|
||||
order_list = trade_exchange.generate_order_for_target_amount_position(
|
||||
target_position=target_amount_dict,
|
||||
current_position=current_amount_dict,
|
||||
trade_date=trade_date,
|
||||
trade_start_time=trade_start_time,
|
||||
trade_end_time=trade_end_time,
|
||||
)
|
||||
return order_list
|
||||
|
||||
@@ -123,8 +129,10 @@ class OrderGenWOInteract(OrderGenerator):
|
||||
trade_exchange: Exchange,
|
||||
target_weight_position: dict,
|
||||
risk_degree: float,
|
||||
pred_date: pd.Timestamp,
|
||||
trade_date: pd.Timestamp,
|
||||
pred_start_time: pd.Timestamp,
|
||||
pred_end_time: pd.Timestamp,
|
||||
trade_start_time: pd.Timestamp,
|
||||
trade_end_time: pd.Timestamp,
|
||||
) -> list:
|
||||
"""generate_order_list_from_target_weight_position
|
||||
|
||||
@@ -153,7 +161,7 @@ class OrderGenWOInteract(OrderGenerator):
|
||||
amount_dict = {}
|
||||
for stock_id in target_weight_position:
|
||||
# Current rule will ignore the stock that not hold and cannot be traded at predict date
|
||||
if trade_exchange.is_stock_tradable(stock_id=stock_id, trade_date=pred_date):
|
||||
if trade_exchange.is_stock_tradable(stock_id=stock_id, trade_start_time=trade_start_time, trade_end_time=trade_end_time):
|
||||
amount_dict[stock_id] = (
|
||||
risk_total_value * target_weight_position[stock_id] / trade_exchange.get_close(stock_id, pred_date)
|
||||
)
|
||||
@@ -166,6 +174,7 @@ class OrderGenWOInteract(OrderGenerator):
|
||||
order_list = trade_exchange.generate_order_for_target_amount_position(
|
||||
target_position=amount_dict,
|
||||
current_position=current.get_stock_amount_dict(),
|
||||
trade_date=trade_date,
|
||||
trade_start_time=trade_start_time,
|
||||
trade_end_time=trade_end_time,
|
||||
)
|
||||
return order_list
|
||||
|
||||
Reference in New Issue
Block a user