Skip to content

Commit

Permalink
修复order_target_portfolio由于交易费用导致资金不够用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-Dongzhao committed Jul 18, 2024
1 parent d703cff commit e48904d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions rqalpha/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[metadata]
name = rqalpha
version = 5.4.1
version = 5.4.2

[versioneer]
VCS = git
Expand Down

0 comments on commit e48904d

Please sign in to comment.