Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions office/api/finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def t0(buy_price: float, sale_price: float, shares: int, w_rate: float = 2.5 / 1
Returns:
float: profit after T+0 trading / 做T后的收益金额
"""
# Decimal 不能和 float 直接相乘,否则金额超过 RATE_LINE 时会抛 TypeError
w_rate = Decimal(str(w_rate))

buy_money = Decimal(str(buy_price)) * shares # 买入的价格
base_rate = min_rate if buy_money <= RATE_LINE else buy_money * w_rate

Expand Down
33 changes: 33 additions & 0 deletions tests/test_utils/test_finance_t0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Tests for office.api.finance.t0."""

import pytest

from office.api.finance import t0


def test_t0_large_trade_does_not_raise_type_error():
"""买入金额超过 RATE_LINE 时,佣金按比例计算,不应抛 TypeError。

回归用例:此前 buy_money 是 Decimal,w_rate 是 float,两者相乘会报
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'。
"""
result = t0(buy_price=100, sale_price=110, shares=700)

# 77000 - 70000 - 70000*0.00025 - 77000*0.00025 - 77000*0.001
assert float(result) == pytest.approx(6886.25)


def test_t0_small_trade_uses_min_commission():
"""金额低于 RATE_LINE 时,买卖两边都按最低手续费 min_rate 计算。"""
result = t0(buy_price=11.99, sale_price=12.26, shares=700)

# 8582 - 8393 - 5 - 5 - 8582*0.001
assert float(result) == pytest.approx(170.418)


def test_t0_respects_custom_commission_rate():
"""自定义 w_rate 时按比例计算佣金,且不受 float/Decimal 混算影响。"""
result = t0(buy_price=100, sale_price=110, shares=700, w_rate=1 / 10000)

# 77000 - 70000 - 70000*0.0001 - 77000*0.0001 - 77000*0.001
assert float(result) == pytest.approx(6908.3)