fix: 全仓模式预估风险/盈利按杠杆与可用保证金计算

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-29 00:38:27 +08:00
parent 9cb63c368a
commit e51d7824a7
8 changed files with 147 additions and 15 deletions
+27
View File
@@ -37,3 +37,30 @@ def test_invalid_geometry_returns_none():
def test_pct_mode_rr():
assert _calc_rr_from_pct(2.0, 4.0) == 2.0
assert _calc_rr_from_pct(1.5, 3.0) == 2.0
def _calc_risk_fraction(direction: str, entry: float, sl: float):
if entry <= 0 or sl <= 0:
return None
if direction == "short":
risk = sl - entry
else:
risk = entry - sl
if risk <= 0:
return None
return risk / entry
def _full_margin_risk_u(available: float, buffer: float, leverage: int, direction: str, entry: float, sl: float):
rf = _calc_risk_fraction(direction, entry, sl)
if rf is None:
return None
margin = round(available * buffer, 2)
return round(margin * leverage * rf, 2)
def test_full_margin_risk_short_hype():
# 可用约 23.06U × 0.9 缓冲 × 5x,入场 62.5、止损 63.6
risk = _full_margin_risk_u(23.06, 0.9, 5, "short", 62.5, 63.6)
assert risk is not None
assert 1.5 <= risk <= 2.5