chore: 初始化版本控制,添加 .gitignore 纳入撮合自动化测试工程
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
"""合约公式层纯函数单测:数据驱动 + Decimal 精度演示。
|
||||
|
||||
公式层是全部业务断言的"期望值来源",本身必须先被验证:
|
||||
- 期望值手算写死在 data/formula.yml,防公式实现自证;
|
||||
- 金额全部 Decimal,顺带用 float 反例说明为什么禁止 float。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from common.decimal_assert import assert_decimal_equal
|
||||
from common.read_yaml import read_yaml
|
||||
from formula.contract_formula import to_decimal, unrealized_pnl
|
||||
from formula import contract_formula as formula
|
||||
|
||||
DATA = read_yaml("data/formula.yml")
|
||||
MAKER = "0.0002"
|
||||
TAKER = "0.0005"
|
||||
|
||||
|
||||
class TestFormula:
|
||||
|
||||
@pytest.mark.parametrize("case", DATA["frozenMargin"])
|
||||
def test_frozen_margin(self, case):
|
||||
actual = formula.frozen_margin(
|
||||
case["price"], case["quantity"], case["margin_rate"], MAKER, TAKER)
|
||||
assert_decimal_equal(actual, case["expected"])
|
||||
|
||||
@pytest.mark.parametrize("case", DATA["unrealizedPnl"])
|
||||
def test_unrealized_pnl(self, case):
|
||||
actual = unrealized_pnl(
|
||||
case["side"], case["open_price"], case["mark_price"], case["quantity"])
|
||||
assert_decimal_equal(actual, case["expected"])
|
||||
|
||||
@pytest.mark.parametrize("case", DATA["bankruptcyPrice"])
|
||||
def test_bankruptcy_price(self, case):
|
||||
actual = formula.bankruptcy_price(
|
||||
case["side"], case["open_price"], case["quantity"],
|
||||
case["margin_rate"], case["extra_margin"])
|
||||
assert_decimal_equal(actual, case["expected"])
|
||||
|
||||
@pytest.mark.parametrize("case", DATA["liquidationPrice"])
|
||||
def test_liquidation_price(self, case):
|
||||
actual = formula.liquidation_price(
|
||||
case["side"], case["open_price"], case["quantity"],
|
||||
case["margin_rate"], case["maintain_rate"], case["extra_margin"])
|
||||
assert_decimal_equal(actual, case["expected"])
|
||||
|
||||
@pytest.mark.parametrize("case", DATA["isBurst"])
|
||||
def test_is_burst(self, case):
|
||||
actual = formula.is_burst(
|
||||
case["extra_margin"], case["side"], case["open_price"],
|
||||
case["mark_price"], case["quantity"],
|
||||
case["margin_rate"], case["maintain_rate"])
|
||||
assert actual is case["expected"]
|
||||
|
||||
def test_is_burst_boundary_is_closed(self):
|
||||
"""边界语义:保证金余额恰好等于维持保证金时,<= 判定触发强平。"""
|
||||
# 做多 10 倍杠杆、强平价 90.5,恰好触及即触发
|
||||
assert formula.is_burst("0", "bid", 100, "90.5", 10, "0.1", "0.005") is True
|
||||
# 高出强平价一线即安全
|
||||
assert formula.is_burst("0", "bid", 100, "90.6", 10, "0.1", "0.005") is False
|
||||
|
||||
def test_decimal_precision_over_float(self):
|
||||
"""为什么金额断言必须用 Decimal:float 存在二进制精度误差。"""
|
||||
# float:0.1 + 0.2 != 0.3(经典精度陷阱)
|
||||
assert 0.1 + 0.2 != 0.3
|
||||
# Decimal:精确相等
|
||||
assert to_decimal("0.1") + to_decimal("0.2") == to_decimal("0.3")
|
||||
# 大额乘法同理,float 末位会漂移
|
||||
assert to_decimal("95.5") * to_decimal("2") * to_decimal("0.1") == to_decimal("19.1")
|
||||
Reference in New Issue
Block a user