This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
"""合约公式层:永续合约撮合核心业务公式的纯函数实现。
|
||||
|
||||
本模块全部为无副作用纯函数,入参/out 参一律使用 Decimal(通过 ``to_decimal``
|
||||
转换),杜绝 float 二进制精度误差 —— 这是合约资金类断言的正确性前提。
|
||||
用例侧用这些函数计算"期望值",再与被测系统(HTTP 响应/落库/缓存)比对。
|
||||
"""
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
#: 持仓方向:做多( bid / side == 1)
|
||||
SIDE_LONG = 1
|
||||
#: 持仓方向:做空( ask / side == 2)
|
||||
SIDE_SHORT = -1
|
||||
|
||||
|
||||
def to_decimal(value) -> Decimal:
|
||||
"""把任意数值(含字符串形式的数值)转为 Decimal。
|
||||
|
||||
走 ``str()`` 而非 ``Decimal(float)``,避免 float 先行引入精度误差。
|
||||
"""
|
||||
if isinstance(value, Decimal):
|
||||
return value
|
||||
return Decimal(str(value))
|
||||
|
||||
|
||||
def side_to_direction(side) -> int:
|
||||
"""把下单方向(bid/ask 或 1/2)规整为持仓方向 1(多)/ -1(空)。"""
|
||||
if side in ("bid", "BUY", 1, "1"):
|
||||
return SIDE_LONG
|
||||
if side in ("ask", "SELL", 2, "2"):
|
||||
return SIDE_SHORT
|
||||
raise ValueError("非法的持仓方向: %r" % (side,))
|
||||
|
||||
|
||||
def open_value(price, quantity) -> Decimal:
|
||||
"""开仓价值 = 开仓价格 * 数量。"""
|
||||
return to_decimal(price) * to_decimal(quantity)
|
||||
|
||||
|
||||
def init_margin(price, quantity, margin_rate) -> Decimal:
|
||||
"""起始(初始)保证金 = 开仓价值 * 起始保证金率(杠杆倍数取反)。"""
|
||||
return open_value(price, quantity) * to_decimal(margin_rate)
|
||||
|
||||
|
||||
def fee(price, quantity, maker_fee_rate, taker_fee_rate) -> Decimal:
|
||||
"""手续费 = 开仓价值 * max(maker 费率, taker 费率)。
|
||||
|
||||
冻结场景按"孰高"预冻结,撮合后按实际角色多退少不补。
|
||||
"""
|
||||
value = open_value(price, quantity)
|
||||
maker_fee = value * to_decimal(maker_fee_rate)
|
||||
taker_fee = value * to_decimal(taker_fee_rate)
|
||||
return max(maker_fee, taker_fee)
|
||||
|
||||
|
||||
def frozen_margin(price, quantity, margin_rate, maker_fee_rate, taker_fee_rate) -> Decimal:
|
||||
"""下单冻结保证金 = 起始保证金 + max(maker, taker) 手续费。
|
||||
|
||||
对应限价委托进入订单簿后的资金冻结口径。
|
||||
"""
|
||||
return (
|
||||
init_margin(price, quantity, margin_rate)
|
||||
+ fee(price, quantity, maker_fee_rate, taker_fee_rate)
|
||||
)
|
||||
|
||||
|
||||
def maintain_margin(open_price, quantity, maintain_rate) -> Decimal:
|
||||
"""维持保证金 = 开仓价值 * 维持保证金率。"""
|
||||
return open_value(open_price, quantity) * to_decimal(maintain_rate)
|
||||
|
||||
|
||||
def unrealized_pnl(side, open_price, mark_price, quantity) -> Decimal:
|
||||
"""未实现盈亏 = 持仓方向 * (标记价格 - 开仓均价) * 数量。"""
|
||||
direction = side_to_direction(side)
|
||||
return direction * (to_decimal(mark_price) - to_decimal(open_price)) * to_decimal(quantity)
|
||||
|
||||
|
||||
def margin_balance(extra_margin, side, open_price, mark_price, quantity, margin_rate) -> Decimal:
|
||||
"""逐仓保证金余额 = 额外保证金 + 起始保证金 + 未实现盈亏。"""
|
||||
return (
|
||||
to_decimal(extra_margin)
|
||||
+ init_margin(open_price, quantity, margin_rate)
|
||||
+ unrealized_pnl(side, open_price, mark_price, quantity)
|
||||
)
|
||||
|
||||
|
||||
def is_burst(extra_margin, side, open_price, mark_price, quantity,
|
||||
margin_rate, maintain_rate) -> bool:
|
||||
"""爆仓(强平触发)判断:保证金余额 <= 维持保证金 即触发。"""
|
||||
balance = margin_balance(extra_margin, side, open_price, mark_price, quantity, margin_rate)
|
||||
return balance <= maintain_margin(open_price, quantity, maintain_rate)
|
||||
|
||||
|
||||
def bankruptcy_price(side, open_price, quantity, margin_rate, extra_margin="0") -> Decimal:
|
||||
"""破产价格:保证金余额恰好归零时的标记价格(逐仓口径)。
|
||||
|
||||
做多: p = open * (1 - margin_rate) - extra / qty
|
||||
做空: p = open * (1 + margin_rate) + extra / qty
|
||||
"""
|
||||
open_price = to_decimal(open_price)
|
||||
quantity = to_decimal(quantity)
|
||||
margin_rate = to_decimal(margin_rate)
|
||||
extra = to_decimal(extra_margin)
|
||||
if side_to_direction(side) == SIDE_LONG:
|
||||
return open_price * (1 - margin_rate) - extra / quantity
|
||||
return open_price * (1 + margin_rate) + extra / quantity
|
||||
|
||||
|
||||
def liquidation_price(side, open_price, quantity, margin_rate,
|
||||
maintain_rate, extra_margin="0") -> Decimal:
|
||||
"""强平价格:保证金余额恰好等于维持保证金时的标记价格(逐仓口径)。
|
||||
|
||||
令 extra + init + upnl(mark) == maintain,反解 mark:
|
||||
mark = open + direction * (maintain - extra - init) / qty
|
||||
标记价格越过该价位(沿亏损方向)即触发强平。
|
||||
"""
|
||||
direction = side_to_direction(side)
|
||||
open_price = to_decimal(open_price)
|
||||
quantity = to_decimal(quantity)
|
||||
init = init_margin(open_price, quantity, margin_rate)
|
||||
maintain = maintain_margin(open_price, quantity, maintain_rate)
|
||||
gap = (maintain - to_decimal(extra_margin) - init) / quantity
|
||||
return open_price + direction * gap
|
||||
Reference in New Issue
Block a user