From 2e49a5f7c0f47e157f3512c65f37f1a62ec060d1 Mon Sep 17 00:00:00 2001 From: Dong Zhou Date: Fri, 15 Oct 2021 07:04:47 +0000 Subject: [PATCH] fix order generator --- qlib/contrib/strategy/order_generator.py | 25 +++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/qlib/contrib/strategy/order_generator.py b/qlib/contrib/strategy/order_generator.py index 34102d88a..ee20be947 100644 --- a/qlib/contrib/strategy/order_generator.py +++ b/qlib/contrib/strategy/order_generator.py @@ -82,16 +82,17 @@ 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_start_time=trade_start_time, - trade_end_time=trade_end_time, + start_time=trade_start_time, + end_time=trade_end_time, only_tradable=False, ) current_tradable_value = trade_exchange.calculate_amount_position_value( amount_dict=current_amount_dict, - trade_start_time=trade_start_time, - trade_end_time=trade_end_time, + start_time=trade_start_time, + end_time=trade_end_time, only_tradable=True, ) # add cash @@ -105,9 +106,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_start_time=trade_start_time, trade_end_time=trade_end_time - ): + if trade_exchange.is_stock_tradable(stock_id, start_time=trade_start_time, end_time=trade_end_time): del target_amount_dict[stock_id] else: # consider cost rate @@ -118,8 +117,8 @@ class OrderGenWInteract(OrderGenerator): target_amount_dict = trade_exchange.generate_amount_position_from_weight_position( weight_position=target_weight_position, cash=current_tradable_value, - trade_start_time=trade_start_time, - trade_end_time=trade_end_time, + start_time=trade_start_time, + end_time=trade_end_time, ) order_list = trade_exchange.generate_order_for_target_amount_position( target_position=target_amount_dict, @@ -172,13 +171,17 @@ class OrderGenWOInteract(OrderGenerator): 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_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] = ( risk_total_value * 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: amount_dict[stock_id] = ( risk_total_value * target_weight_position[stock_id] / current.get_stock_price(stock_id)