diff --git a/rqalpha/environment.py b/rqalpha/environment.py index a2e8b8758..a27302ffb 100644 --- a/rqalpha/environment.py +++ b/rqalpha/environment.py @@ -183,6 +183,10 @@ def _get_transaction_cost_decider(self, order_book_id): def get_trade_tax(self, trade): return self._get_transaction_cost_decider(trade.order_book_id).get_trade_tax(trade) + + def get_stock_commission_and_tax(self) -> tuple[float, float]: + decider = self._transaction_cost_decider_dict[INSTRUMENT_TYPE.CS] + return decider.commission_rate, decider.tax_rate def get_trade_commission(self, trade): return self._get_transaction_cost_decider(trade.order_book_id).get_trade_commission(trade) diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py index 6c8fe0057..9c16ddbad 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py @@ -359,6 +359,15 @@ def order_target_portfolio( total_percent = sum(p for p, *__ in target.values()) if total_percent > 1 and not np.isclose(total_percent, 1): raise RQInvalidArgument(_("total percent should be lower than 1, current: {}").format(total_percent)) + + cash_buffer = 1 + if total_percent == 1: + # 在此处形成的订单不包含交易费用,需要预留一点余额以供交易费用使用 + # 1 - (股票佣金 * 佣金倍率 + 印花税 * 印花税倍率) + commission_rate, tax_rate = env.get_stock_commission_and_tax() + commission_multiplier = env.config.mod.sys_transaction_cost.stock_commission_multiplier + tax_multiplier = env.config.mod.sys_transaction_cost.tax_multiplier + cash_buffer = 1 - (commission_rate * commission_multiplier + tax_rate * tax_multiplier) account = env.portfolio.accounts[DEFAULT_ACCOUNT_TYPE.STOCK] @@ -372,7 +381,7 @@ def order_target_portfolio( order_book_id, quantity, SIDE.SELL, MarketOrder(), POSITION_EFFECT.CLOSE )) - account_value = account.total_value + account_value = account.total_value * cash_buffer close_orders, open_orders = [], [] for order_book_id, (target_percent, open_style, close_style, last_price) in target.items(): open_price = _get_order_style_price(order_book_id, open_style) diff --git a/setup.cfg b/setup.cfg index 7421f2443..817f0164b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,7 +5,7 @@ [metadata] name = rqalpha -version = 5.4.1 +version = 5.4.2 [versioneer] VCS = git