修复审计P1:基数纠偏用开仓止损并清空币数量、Gate保证金/手动平仓成交价/全仓实盘空仓校验、OKX·Binance占位函数、快照超时。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
import unittest
|
||||
|
||||
from lib.trade.trade_margin_record_lib import (
|
||||
apply_repaired_margin_capital,
|
||||
coin_amount_to_margin_usdt,
|
||||
looks_like_coin_amount_as_margin,
|
||||
margin_from_risk_amount,
|
||||
@@ -34,7 +35,6 @@ class TestTradeMarginRecord(unittest.TestCase):
|
||||
)
|
||||
|
||||
def test_coin_to_margin(self):
|
||||
# 仅作数学兜底函数;展示层不得优先用它虚增高额
|
||||
self.assertAlmostEqual(
|
||||
coin_amount_to_margin_usdt(0.12, trigger_price=64054.1, leverage=20),
|
||||
384.32,
|
||||
@@ -42,7 +42,6 @@ class TestTradeMarginRecord(unittest.TestCase):
|
||||
)
|
||||
|
||||
def test_margin_from_risk(self):
|
||||
# risk = 110 * 20 * 445.9 / 64054.1 ≈ 15.32
|
||||
risk = 110.0 * 20.0 * 445.9 / 64054.1
|
||||
out = margin_from_risk_amount(
|
||||
risk, trigger_price=64054.1, stop_loss=64500.0, leverage=20
|
||||
@@ -68,6 +67,18 @@ class TestTradeMarginRecord(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(out, 117.71)
|
||||
|
||||
def test_resolve_rejects_inflated_plan_via_risk(self):
|
||||
risk = 110.0 * 20.0 * 445.9 / 64054.1
|
||||
out = resolve_trade_record_margin_usdt(
|
||||
exchange_margin_usdt=None,
|
||||
plan_margin_capital=384.32,
|
||||
leverage=20,
|
||||
trigger_price=64054.1,
|
||||
stop_loss=64500.0,
|
||||
risk_amount=risk,
|
||||
)
|
||||
self.assertAlmostEqual(out, 110.0, places=1)
|
||||
|
||||
def test_resolve_uses_risk_when_no_plan(self):
|
||||
risk = 110.0 * 20.0 * 445.9 / 64054.1
|
||||
out = resolve_trade_record_margin_usdt(
|
||||
@@ -89,6 +100,19 @@ class TestTradeMarginRecord(unittest.TestCase):
|
||||
)
|
||||
self.assertIsNone(out)
|
||||
|
||||
def test_repair_prefers_initial_stop_loss(self):
|
||||
# 当前止损已移近(保本),若误用会把反推基数抬高;应使用开仓止损
|
||||
risk = 110.0 * 20.0 * 445.9 / 64054.1
|
||||
out = repair_stored_margin_capital(
|
||||
384.32,
|
||||
trigger_price=64054.1,
|
||||
leverage=20,
|
||||
stop_loss=64080.0,
|
||||
initial_stop_loss=64500.0,
|
||||
risk_amount=risk,
|
||||
)
|
||||
self.assertAlmostEqual(out, 110.0, places=1)
|
||||
|
||||
def test_repair_stored_uses_risk_not_coin_times_price(self):
|
||||
risk = 110.0 * 20.0 * 445.9 / 64054.1
|
||||
self.assertAlmostEqual(
|
||||
@@ -102,7 +126,6 @@ class TestTradeMarginRecord(unittest.TestCase):
|
||||
110.0,
|
||||
places=1,
|
||||
)
|
||||
# 曾被错误「纠偏」成 384 的入库值,用风险金额压回
|
||||
self.assertAlmostEqual(
|
||||
repair_stored_margin_capital(
|
||||
384.32,
|
||||
@@ -120,13 +143,21 @@ class TestTradeMarginRecord(unittest.TestCase):
|
||||
),
|
||||
108.97,
|
||||
)
|
||||
# 无风险金额时:绝不把 0.12 换成 384
|
||||
self.assertIsNone(
|
||||
repair_stored_margin_capital(
|
||||
0.12, trigger_price=64054.1, leverage=20
|
||||
)
|
||||
)
|
||||
|
||||
def test_apply_clears_coin_like_when_unrepairable(self):
|
||||
item = {"margin_capital": 0.12, "trigger_price": 64054.1, "leverage": 20}
|
||||
apply_repaired_margin_capital(
|
||||
item,
|
||||
trigger_price=64054.1,
|
||||
leverage=20,
|
||||
)
|
||||
self.assertIsNone(item["margin_capital"])
|
||||
|
||||
def test_sanitize_rejects_oversized_collateral(self):
|
||||
out = sanitize_exchange_initial_margin(
|
||||
384.32, notional=2200.0, order_leverage=20, coin_amount=0.034
|
||||
|
||||
Reference in New Issue
Block a user