From de2658a8dbb032769a2ed4d6c346c632d660cbde Mon Sep 17 00:00:00 2001 From: bxdd Date: Thu, 13 May 2021 22:39:19 +0800 Subject: [PATCH] fix rule_strategy bug --- qlib/contrib/backtest/account.py | 1 - qlib/contrib/strategy/rule_strategy.py | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/qlib/contrib/backtest/account.py b/qlib/contrib/backtest/account.py index 5e2e03ea0..df7614979 100644 --- a/qlib/contrib/backtest/account.py +++ b/qlib/contrib/backtest/account.py @@ -153,7 +153,6 @@ class Account: # if stock is sold out, no stock price information in Position, then we should update account first, then update current position # if stock is bought, there is no stock in current position, update current, then update account # The cost will be substracted from the cash at last. So the trading logic can ignore the cost calculation - trade_amount = trade_val / trade_price if order.direction == Order.SELL: # sell stock self.update_state_from_order(order, trade_val, cost, trade_price) diff --git a/qlib/contrib/strategy/rule_strategy.py b/qlib/contrib/strategy/rule_strategy.py index 3b66b0f1e..222c56568 100644 --- a/qlib/contrib/strategy/rule_strategy.py +++ b/qlib/contrib/strategy/rule_strategy.py @@ -138,6 +138,9 @@ class SBBStrategyBase(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: + _order_amount = self.trade_amount[(order.stock_id, order.direction)] if _order_amount: self.trade_amount[(order.stock_id, order.direction)] -= _order_amount @@ -167,6 +170,10 @@ class SBBStrategyBase(RuleStrategy, OrderEnhancement): * 2 * _amount_trade_unit ) + if order.direction == order.SELL: + if self.trade_amount[(order.stock_id, order.direction)] > 1e-5 and _order_amount is None: + _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