跳轉到

交易成本模型

台股手續費、證交稅和選用的滑價,套用在每一筆模擬成交上。

cjtrade.pkgs.models.cost

Transaction cost model for Taiwan Stock Exchange.

Cost components: - Commission (手續費): 0.1425% per side, minimum NT$20, with broker discount - Transaction tax (證交稅): 0.3% on SELL only (0.1% for ETFs, symbol starts with "0") - Slippage: optional price adjustment to simulate market impact

Usage::

from cjtrade.pkgs.models.cost import CostModel

# Default: full-rate commission, standard tax, no slippage
cm = CostModel()

# Online broker at 六折 discount, with 0.05% slippage
cm = CostModel(commission_discount=0.6, slippage_model="fixed_pct", slippage_pct=0.0005)

CostModel(commission_rate=0.001425, commission_discount=1.0, min_commission=20.0, stock_tax_rate=0.003, etf_tax_rate=0.001, slippage_model='none', slippage_pct=0.0005) dataclass

Parametrisable transaction-cost model for Taiwan stock trading.

Attributes:

Name Type Description
commission_rate float

Statutory maximum rate: 0.1425% per side.

commission_discount float

Broker discount multiplier (1.0 = no discount; 0.6 = 六折).

min_commission float

Minimum charge per order in TWD (default NT$20).

stock_tax_rate float

Transaction tax (證交稅) applied on SELL for ordinary stocks (0.3%).

etf_tax_rate float

Transaction tax on SELL for ETFs (0.1%; symbols starting with "0").

slippage_model 'none' | 'fixed_pct' | 'fixed_tick'

"none" – no price adjustment (default for full-simulation). "fixed_pct" – price moves adversely by slippage_pct × price. "fixed_tick" – price moves adversely by one Taiwan tick.

slippage_pct float

Fractional slippage per side (used only when slippage_model == "fixed_pct"). Default 0.05%.

effective_rate()

Return the commission rate after applying the broker discount.

Source code in src/cjtrade/pkgs/models/cost.py
75
76
77
def effective_rate(self) -> float:
    """Return the commission rate after applying the broker discount."""
    return self.commission_rate * self.commission_discount

calc_commission(value)

Return the commission for a trade of value TWD.

Parameters:

Name Type Description Default
value float

Notional of the trade.

required

Returns:

Type Description
float

At least min_commission, rounded to 2 decimals.

Source code in src/cjtrade/pkgs/models/cost.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def calc_commission(self, value: float) -> float:
    """Return the commission for a trade of `value` TWD.

    Parameters
    ----------
    value : float
        Notional of the trade.

    Returns
    -------
    float
        At least `min_commission`, rounded to 2 decimals.
    """
    return round(max(value * self.effective_rate(), self.min_commission), 2)

calc_tax(value, side, symbol)

Return transaction tax for a trade. Zero for BUY; non-zero for SELL.

Parameters:

Name Type Description Default
value float

Notional of the trade.

required
side str

Only "SELL" is taxed.

required
symbol str

Symbols starting with "0" (ETFs) use etf_tax_rate.

required

Returns:

Type Description
float

Rounded to 2 decimals.

Source code in src/cjtrade/pkgs/models/cost.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def calc_tax(self, value: float, side: str, symbol: str) -> float:
    """Return transaction tax for a trade.  Zero for BUY; non-zero for SELL.

    Parameters
    ----------
    value : float
        Notional of the trade.
    side : str
        Only ``"SELL"`` is taxed.
    symbol : str
        Symbols starting with ``"0"`` (ETFs) use `etf_tax_rate`.

    Returns
    -------
    float
        Rounded to 2 decimals.
    """
    if side != "SELL":
        return 0.0
    rate = self.etf_tax_rate if symbol.startswith("0") else self.stock_tax_rate
    return round(value * rate, 2)

apply_slippage(price, side)

Return price adjusted for slippage (adverse to the trader).

Parameters:

Name Type Description Default
price float

Unadjusted price.

required
side str

"BUY" raises the price; any other value lowers it.

required

Returns:

Type Description
float

Unchanged for slippage_model="none", otherwise rounded to 2 decimals.

Source code in src/cjtrade/pkgs/models/cost.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def apply_slippage(self, price: float, side: str) -> float:
    """Return `price` adjusted for slippage (adverse to the trader).

    Parameters
    ----------
    price : float
        Unadjusted price.
    side : str
        ``"BUY"`` raises the price; any other value lowers it.

    Returns
    -------
    float
        Unchanged for ``slippage_model="none"``, otherwise rounded to 2 decimals.
    """
    if self.slippage_model == "none":
        return price
    if self.slippage_model == "fixed_pct":
        slip = price * self.slippage_pct
    elif self.slippage_model == "fixed_tick":
        slip = _tick_size(price)
    else:
        return price
    return round(price + slip if side == "BUY" else price - slip, 2)

buy_total(price, qty, symbol='')

Return (adj_price, notional, commission, tax) for a BUY.

tax is always 0.0 for BUY; included for API symmetry. notional is computed from the slippage-adjusted adj_price.

Parameters:

Name Type Description Default
price float

Unadjusted price.

required
qty int

Number of shares.

required
symbol str

Unused for BUY; accepted for symmetry with sell_total.

''
Source code in src/cjtrade/pkgs/models/cost.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def buy_total(
    self, price: float, qty: int, symbol: str = ""
) -> tuple[float, float, float, float]:
    """Return ``(adj_price, notional, commission, tax)`` for a BUY.

    *tax* is always ``0.0`` for BUY; included for API symmetry.
    *notional* is computed from the slippage-adjusted *adj_price*.

    Parameters
    ----------
    price : float
        Unadjusted price.
    qty : int
        Number of shares.
    symbol : str, optional
        Unused for BUY; accepted for symmetry with `sell_total`.
    """
    adj = self.apply_slippage(price, "BUY")
    notional = adj * qty
    comm = self.calc_commission(notional)
    return adj, notional, comm, 0.0

sell_total(price, qty, symbol='')

Return (adj_price, notional, commission, tax) for a SELL.

Parameters:

Name Type Description Default
price float

Unadjusted price.

required
qty int

Number of shares.

required
symbol str

Decides the tax rate (see calc_tax).

''
Source code in src/cjtrade/pkgs/models/cost.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def sell_total(
    self, price: float, qty: int, symbol: str = ""
) -> tuple[float, float, float, float]:
    """Return ``(adj_price, notional, commission, tax)`` for a SELL.

    Parameters
    ----------
    price : float
        Unadjusted price.
    qty : int
        Number of shares.
    symbol : str, optional
        Decides the tax rate (see `calc_tax`).
    """
    adj = self.apply_slippage(price, "SELL")
    notional = adj * qty
    comm = self.calc_commission(notional)
    tax  = self.calc_tax(notional, "SELL", symbol)
    return adj, notional, comm, tax