修复交易记录基数虚高:禁止币×价÷杠杆发明保证金,优先计划保证金/风险金额反推。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-13 00:12:02 +08:00
parent 5ad22a57da
commit 35b70777b8
4 changed files with 163 additions and 42 deletions
+68 -18
View File
@@ -1,4 +1,4 @@
"""交易记录基数(保证金)口径:币数量误记为基数时的修复."""
"""交易记录基数(保证金)口径:币数量误记 / 虚高纠偏."""
from __future__ import annotations
import unittest
@@ -6,6 +6,7 @@ import unittest
from lib.trade.trade_margin_record_lib import (
coin_amount_to_margin_usdt,
looks_like_coin_amount_as_margin,
margin_from_risk_amount,
repair_stored_margin_capital,
resolve_trade_record_margin_usdt,
sanitize_exchange_initial_margin,
@@ -33,13 +34,21 @@ class TestTradeMarginRecord(unittest.TestCase):
)
def test_coin_to_margin(self):
# 0.12 BTC * 64054 / 20 ≈ 384.32
# 仅作数学兜底函数;展示层不得优先用它虚增高额
self.assertAlmostEqual(
coin_amount_to_margin_usdt(0.12, trigger_price=64054.1, leverage=20),
384.32,
places=2,
)
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
)
self.assertAlmostEqual(out, 110.0, places=1)
def test_resolve_prefers_plan_when_exchange_is_coin(self):
out = resolve_trade_record_margin_usdt(
exchange_margin_usdt=0.12,
@@ -50,31 +59,60 @@ class TestTradeMarginRecord(unittest.TestCase):
)
self.assertEqual(out, 117.71)
def test_resolve_repairs_coin_when_no_plan(self):
def test_resolve_prefers_plan_over_inflated_exchange(self):
out = resolve_trade_record_margin_usdt(
exchange_margin_usdt=384.32,
plan_margin_capital=117.71,
leverage=20,
trigger_price=64054.1,
)
self.assertEqual(out, 117.71)
def test_resolve_uses_risk_when_no_plan(self):
risk = 110.0 * 20.0 * 445.9 / 64054.1
out = resolve_trade_record_margin_usdt(
exchange_margin_usdt=0.12,
plan_margin_capital=None,
leverage=20,
trigger_price=64054.1,
stop_loss=64500.0,
risk_amount=risk,
)
self.assertAlmostEqual(out, 110.0, places=1)
def test_resolve_does_not_invent_coin_times_price(self):
out = resolve_trade_record_margin_usdt(
exchange_margin_usdt=0.12,
plan_margin_capital=None,
leverage=20,
trigger_price=64054.1,
)
self.assertEqual(out, 384.32)
self.assertIsNone(out)
def test_resolve_uses_plan_when_cannot_repair(self):
out = resolve_trade_record_margin_usdt(
exchange_margin_usdt=0.05,
plan_margin_capital=100.0,
leverage=None,
trigger_price=None,
base_amount=0.05,
)
self.assertEqual(out, 100.0)
def test_repair_stored_display(self):
self.assertEqual(
def test_repair_stored_uses_risk_not_coin_times_price(self):
risk = 110.0 * 20.0 * 445.9 / 64054.1
self.assertAlmostEqual(
repair_stored_margin_capital(
0.12, trigger_price=64054.1, leverage=20
0.12,
trigger_price=64054.1,
leverage=20,
stop_loss=64500.0,
risk_amount=risk,
),
384.32,
110.0,
places=1,
)
# 曾被错误「纠偏」成 384 的入库值,用风险金额压回
self.assertAlmostEqual(
repair_stored_margin_capital(
384.32,
trigger_price=64054.1,
leverage=20,
stop_loss=64500.0,
risk_amount=risk,
),
110.0,
places=1,
)
self.assertEqual(
repair_stored_margin_capital(
@@ -82,6 +120,18 @@ 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_sanitize_rejects_oversized_collateral(self):
out = sanitize_exchange_initial_margin(
384.32, notional=2200.0, order_leverage=20, coin_amount=0.034
)
self.assertAlmostEqual(out, 110.0, places=1)
def test_sanitize_exchange_margin(self):
out = sanitize_exchange_initial_margin(