Skip to content

Commit

Permalink
add c_order_percent for chinese market fee (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertandking authored Dec 10, 2023
1 parent f58ef39 commit 4ba5be7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/pybroker/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class FeeMode(Enum):
"""

ORDER_PERCENT = "order_percent"
C_ORDER_PERCENT = "c_order_percent"
PER_ORDER = "per_order"
PER_SHARE = "per_share"

Expand Down
14 changes: 12 additions & 2 deletions src/pybroker/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,22 @@ def __init__(
self._entry_id: int = 0
self._trade_id: int = 0

def _calculate_fees(self, fill_price: Decimal, shares: Decimal) -> Decimal:
def _calculate_fees(self, fill_price: Decimal, shares: Decimal, type: Literal["buy", "sell"]) -> Decimal:
fees = Decimal()
if self._fee_mode is None or self._fee_amount is None:
return fees
if self._fee_mode == FeeMode.ORDER_PERCENT:
fees = self._fee_amount / Decimal(100) * fill_price * shares
elif self._fee_mode == FeeMode.C_ORDER_PERCENT:
if type == "buy":
fees = self._fee_amount / Decimal(100) * fill_price * shares
if fees < Decimal(5.0):
fees = Decimal(5.0)
else:
fees = self._fee_amount / Decimal(100) * fill_price * shares
if fees < Decimal(5.0):
fees = Decimal(5.0)
fees = fees + Decimal(0.0001) * fill_price * shares
elif self._fee_mode == FeeMode.PER_ORDER:
fees = self._fee_amount
elif self._fee_mode == FeeMode.PER_SHARE:
Expand Down Expand Up @@ -402,7 +412,7 @@ def _add_order(
shares: Decimal,
) -> Order:
self._order_id += 1
fees = self._calculate_fees(fill_price, shares)
fees = self._calculate_fees(fill_price, shares, type)
order = Order(
id=self._order_id,
date=date,
Expand Down

0 comments on commit 4ba5be7

Please sign in to comment.