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

fix order generator

This commit is contained in:
Dong Zhou
2021-10-15 07:04:47 +00:00
parent 3ab5721448
commit 2e49a5f7c0

View File

@@ -82,16 +82,17 @@ class OrderGenWInteract(OrderGenerator):
""" """
# calculate current_tradable_value # calculate current_tradable_value
current_amount_dict = current.get_stock_amount_dict() current_amount_dict = current.get_stock_amount_dict()
current_total_value = trade_exchange.calculate_amount_position_value( current_total_value = trade_exchange.calculate_amount_position_value(
amount_dict=current_amount_dict, amount_dict=current_amount_dict,
trade_start_time=trade_start_time, start_time=trade_start_time,
trade_end_time=trade_end_time, end_time=trade_end_time,
only_tradable=False, only_tradable=False,
) )
current_tradable_value = trade_exchange.calculate_amount_position_value( current_tradable_value = trade_exchange.calculate_amount_position_value(
amount_dict=current_amount_dict, amount_dict=current_amount_dict,
trade_start_time=trade_start_time, start_time=trade_start_time,
trade_end_time=trade_end_time, end_time=trade_end_time,
only_tradable=True, only_tradable=True,
) )
# add cash # add cash
@@ -105,9 +106,7 @@ class OrderGenWInteract(OrderGenerator):
# value. Then just sell all the stocks # value. Then just sell all the stocks
target_amount_dict = copy.deepcopy(current_amount_dict.copy()) target_amount_dict = copy.deepcopy(current_amount_dict.copy())
for stock_id in list(target_amount_dict.keys()): for stock_id in list(target_amount_dict.keys()):
if trade_exchange.is_stock_tradable( if trade_exchange.is_stock_tradable(stock_id, start_time=trade_start_time, end_time=trade_end_time):
stock_id, trade_start_time=trade_start_time, trade_end_time=trade_end_time
):
del target_amount_dict[stock_id] del target_amount_dict[stock_id]
else: else:
# consider cost rate # consider cost rate
@@ -118,8 +117,8 @@ class OrderGenWInteract(OrderGenerator):
target_amount_dict = trade_exchange.generate_amount_position_from_weight_position( target_amount_dict = trade_exchange.generate_amount_position_from_weight_position(
weight_position=target_weight_position, weight_position=target_weight_position,
cash=current_tradable_value, cash=current_tradable_value,
trade_start_time=trade_start_time, start_time=trade_start_time,
trade_end_time=trade_end_time, end_time=trade_end_time,
) )
order_list = trade_exchange.generate_order_for_target_amount_position( order_list = trade_exchange.generate_order_for_target_amount_position(
target_position=target_amount_dict, target_position=target_amount_dict,
@@ -172,13 +171,17 @@ class OrderGenWOInteract(OrderGenerator):
for stock_id in target_weight_position: for stock_id in target_weight_position:
# Current rule will ignore the stock that not hold and cannot be traded at predict date # Current rule will ignore the stock that not hold and cannot be traded at predict date
if trade_exchange.is_stock_tradable( if trade_exchange.is_stock_tradable(
stock_id=stock_id, trade_start_time=trade_start_time, trade_end_time=trade_end_time stock_id=stock_id, start_time=trade_start_time, end_time=trade_end_time
) and trade_exchange.is_stock_tradable(
stock_id=stock_id, start_time=pred_start_time, end_time=pred_end_time
): ):
amount_dict[stock_id] = ( amount_dict[stock_id] = (
risk_total_value risk_total_value
* target_weight_position[stock_id] * target_weight_position[stock_id]
/ trade_exchange.get_close(stock_id, trade_start_time=pred_start_time, trade_end_time=pred_end_time) / trade_exchange.get_close(stock_id, start_time=pred_start_time, end_time=pred_end_time)
) )
# TODO: Qlib use None to represent trading suspension. So last close price can't be the estimated trading price.
# Maybe a close price with forward fill will be a better solution.
elif stock_id in current_stock: elif stock_id in current_stock:
amount_dict[stock_id] = ( amount_dict[stock_id] = (
risk_total_value * target_weight_position[stock_id] / current.get_stock_price(stock_id) risk_total_value * target_weight_position[stock_id] / current.get_stock_price(stock_id)