Skip to content

Commit

Permalink
Merge pull request #891 from ricequant/RQSDK-746
Browse files Browse the repository at this point in the history
Rqsdk 746
  • Loading branch information
Cuizi7 authored Jul 19, 2024
2 parents 3238f91 + e56445e commit f1183dd
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
6 changes: 6 additions & 0 deletions rqalpha/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
from rqalpha.utils.logger import system_log, user_log, user_system_log
from rqalpha.core.global_var import GlobalVars
from rqalpha.utils.i18n import gettext as _
from rqalpha.const import SIDE
if TYPE_CHECKING:
from rqalpha.model.order import Order



class Environment(object):
Expand Down Expand Up @@ -183,6 +185,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_transaction_cost_with_value(self, value: float) -> float:
side = SIDE.BUY if value >= 0 else SIDE.SELL
return self._transaction_cost_decider_dict[INSTRUMENT_TYPE.CS].get_transaction_cost_with_value(abs(value), side)

def get_trade_commission(self, trade):
return self._get_transaction_cost_decider(trade.order_book_id).get_trade_commission(trade)
Expand Down
17 changes: 10 additions & 7 deletions rqalpha/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from rqalpha.model.order import Order
from rqalpha.model.trade import Trade
from rqalpha.model.instrument import Instrument
from rqalpha.const import POSITION_DIRECTION, TRADING_CALENDAR_TYPE, INSTRUMENT_TYPE
from rqalpha.const import POSITION_DIRECTION, TRADING_CALENDAR_TYPE, INSTRUMENT_TYPE, SIDE


class AbstractPosition(with_metaclass(abc.ABCMeta)):
Expand Down Expand Up @@ -675,25 +675,28 @@ class AbstractTransactionCostDecider((with_metaclass(abc.ABCMeta))):
订单税费计算接口,通过实现次接口可以定义不同市场、不同合约的个性化税费计算逻辑。
"""
@abc.abstractmethod
def get_trade_tax(self, trade):
# type: (Trade) -> float
def get_trade_tax(self, trade: Trade) -> float:
"""
计算指定交易应付的印花税
"""
raise NotImplementedError

@abc.abstractmethod
def get_trade_commission(self, trade):
# type: (Trade) -> float
def get_trade_commission(self, trade: Trade) -> float:
"""
计算指定交易应付的佣金
"""
raise NotImplementedError

@abc.abstractmethod
def get_order_transaction_cost(self, order):
# type: (Order) -> float
def get_order_transaction_cost(self, order: Order) -> float:
"""
计算指定订单应付的交易成本(税 + 费)
"""
raise NotImplementedError

def get_transaction_cost_with_value(self, value: float, side: SIDE) -> float:
"""
计算指定价格交易应付的交易成本(税 + 费)
"""
raise NotImplementedError
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 @@ -372,7 +372,16 @@ def order_target_portfolio(
order_book_id, quantity, SIDE.SELL, MarketOrder(), POSITION_EFFECT.CLOSE
))

account_value = account.total_value
account_value = account.total_value
if total_percent == 1:
# 在此处形成的订单不包含交易费用,需要预留一点余额以供交易费用使用
estimate_transaction_cost = 0
for order_book_id, (target_percent, open_style, close_style, last_price) in target.items():
current_value = current_quantities.get(order_book_id, 0) * last_price
change_value = target_percent * account_value - current_value
estimate_transaction_cost += env.get_transaction_cost_with_value(change_value)
account_value = account_value - estimate_transaction_cost

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
8 changes: 8 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_transaction_cost/deciders.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def get_order_transaction_cost(self, order):
commission = self._get_order_commission(order.order_book_id, order.side, order.frozen_price, order.quantity)
tax = self._get_tax(order.order_book_id, order.side, order.frozen_price * order.quantity)
return tax + commission

def get_transaction_cost_with_value(self, value: float, side: SIDE) -> float:
raise NotImplementedError


class CNStockTransactionCostDecider(StockTransactionCostDecider):
Expand All @@ -99,6 +102,11 @@ def set_tax_rate(self, event):
else:
self.tax_rate = 0.0005

def get_transaction_cost_with_value(self, value: float, side: SIDE) -> float:
tax = value * self.tax_rate * self.tax_multiplier if side == SIDE.SELL else 0
commission = max(value * self.commission_rate * self.commission_multiplier, self.min_commission)
return tax + commission


class CNFutureTransactionCostDecider(AbstractTransactionCostDecider):
def __init__(self, commission_multiplier):
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
Binary file modified tests/outs/test_f_mean_reverting.pkl
Binary file not shown.

0 comments on commit f1183dd

Please sign in to comment.