@@ -549,6 +549,22 @@ def _attach_position_meta(order, position_side=None, offset=None, **kwargs):
549549 order .addinfo (** kwargs )
550550 return order
551551
552+ @staticmethod
553+ def _close_commission_role (offset ):
554+ offset_text = str (offset or "" ).strip ().lower ()
555+ if offset_text in {"close_today" , "closetoday" }:
556+ return "close_today"
557+ if offset_text in {"close_yesterday" , "closeyesterday" }:
558+ return "close_yesterday"
559+ return "close"
560+
561+ @staticmethod
562+ def _getcommission_role (comminfo , size , price , role ):
563+ try :
564+ return comminfo .getcommission (size , price , role = role )
565+ except TypeError :
566+ return comminfo .getcommission (size , price )
567+
552568 @staticmethod
553569 def _position_storage_key (data ):
554570 return data
@@ -1643,7 +1659,12 @@ def _execute(self, order, ago=None, price=None, cash=None, position=None, dtcoc=
16431659 cash += closecash + pnl * comminfo .stocklike
16441660 # Calculate and subtract commission
16451661 # Commission when closing position
1646- closedcomm = comminfo .getcommission (closed , price )
1662+ closedcomm = self ._getcommission_role (
1663+ comminfo ,
1664+ closed ,
1665+ price ,
1666+ self ._close_commission_role (getattr (order .info , "offset" , None )),
1667+ )
16471668 # Cash equals cash minus closing commission
16481669 cash -= closedcomm
16491670 # If ago is not None
@@ -1675,7 +1696,7 @@ def _execute(self, order, ago=None, price=None, cash=None, position=None, dtcoc=
16751696 # Subtract cash obtained after opening
16761697 cash -= opencash # original behavior
16771698 # Commission for opening
1678- openedcomm = cinfocomp . getcommission ( opened , price )
1699+ openedcomm = self . _getcommission_role ( cinfocomp , opened , price , "open" )
16791700 # Cash obtained after subtracting opening commission
16801701 cash -= openedcomm
16811702 # If cash is less than 0, opening position is not possible
@@ -1824,7 +1845,12 @@ def _execute_dual_side(self, order, ago=None, price=None, cash=None, position=No
18241845 if closedvalue > 0 :
18251846 closecash /= comminfo .get_leverage ()
18261847 cash += closecash + pnl * comminfo .stocklike
1827- closedcomm = comminfo .getcommission (closed , price )
1848+ closedcomm = self ._getcommission_role (
1849+ comminfo ,
1850+ closed ,
1851+ price ,
1852+ self ._close_commission_role (getattr (order .info , "offset" , None )),
1853+ )
18281854 cash -= closedcomm
18291855 if ago is not None :
18301856 cash += comminfo .cashadjust (- closed , signed_position .adjbase , price )
@@ -1843,7 +1869,7 @@ def _execute_dual_side(self, order, ago=None, price=None, cash=None, position=No
18431869 if openedvalue > 0 :
18441870 opencash /= comminfo .get_leverage ()
18451871 cash -= opencash
1846- openedcomm = cinfocomp . getcommission ( opened , price )
1872+ openedcomm = self . _getcommission_role ( cinfocomp , opened , price , "open" )
18471873 cash -= openedcomm
18481874 if cash < 0.0 :
18491875 opened = 0
0 commit comments