From 4d140b42297c603bf114896f26864ac78444e7c2 Mon Sep 17 00:00:00 2001 From: Don Date: Fri, 15 May 2026 13:50:49 +0800 Subject: [PATCH 1/2] ix bug:shares purchased through the reinvestment of dividends received on the same day should not be eligible for bonus share distributions --- rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py b/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py index e05ff51fc..6573e3ace 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py @@ -145,8 +145,8 @@ def before_trading(self, trading_date): raise RuntimeError("direction of stock position {} is not supposed to be short".format(self._order_book_id)) data_proxy = self._env.data_proxy self._daily_dividend = self._handle_dividend_book_closure(trading_date, data_proxy) - delta_cash += self._handle_dividend_payable(trading_date) self._daily_split = self._handle_split(trading_date, data_proxy) + delta_cash += self._handle_dividend_payable(trading_date) return delta_cash def apply_trade(self, trade): From 40a1df1683a01eeee02c5103dbaa1cd808241471 Mon Sep 17 00:00:00 2001 From: Don Date: Fri, 15 May 2026 14:15:27 +0800 Subject: [PATCH 2/2] add test case --- .../position_model.py | 1 + .../mod/sys_accounts/test_position_models.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py b/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py index 6573e3ace..79b7b7f1a 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py @@ -145,6 +145,7 @@ def before_trading(self, trading_date): raise RuntimeError("direction of stock position {} is not supposed to be short".format(self._order_book_id)) data_proxy = self._env.data_proxy self._daily_dividend = self._handle_dividend_book_closure(trading_date, data_proxy) + # 需要先执行拆股后再执行分红支付操作,否则当开启分红再投资时,会将当日到账的票也进行拆股 self._daily_split = self._handle_split(trading_date, data_proxy) delta_cash += self._handle_dividend_payable(trading_date) return delta_cash diff --git a/tests/integration_tests/test_api/mod/sys_accounts/test_position_models.py b/tests/integration_tests/test_api/mod/sys_accounts/test_position_models.py index b01c3daac..43da0d065 100644 --- a/tests/integration_tests/test_api/mod/sys_accounts/test_position_models.py +++ b/tests/integration_tests/test_api/mod/sys_accounts/test_position_models.py @@ -101,3 +101,42 @@ def after_trading(context): run_func(config=config, init=init, open_auction=open_auction, handle_bar=handle_bar, after_trading=after_trading) + +def test_dividend_reinvestment_and_splits_on_the_same_day(): + """ + 测试分红再投资和拆股行为在同一天时的逻辑,只要测试点为:在当日分红再投资买入的票不应当参与拆股 + """ + config = _config({ + "base": { + "start_date": "2017-02-01", + "end_date": "2017-03-10", + "frequency": "1d", + "accounts": { + "stock": 10000000 + }, + }, + "extra": { + "log_level": "error", + }, + "mod": { + "sys_accounts": { + "dividend_reinvestment": True + } + } + }) + + def init(context): + context.s1 = "600816.XSHG" + context.fired = False + + def handle_bar(context, bar_dict): + if not context.fired: + order_shares(context.s1, 10000) + context.fired = True + assert get_position(context.s1).quantity == 10000 + if context.now.date() == date(2017, 3, 6): + # 1. 每 1 股拆为 2.2 股 + # 2. 每 10 股分现金 6 元,总共分 6000 元,再投资买入 400 股 + assert get_position(context.s1).quantity == 22400 + + run_func(config=config, init=init, handle_bar=handle_bar) \ No newline at end of file