fix(trend): correct DCA triggers and partial-position PnL across exchanges

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-07 17:09:22 +08:00
parent 9257a8051f
commit 84abf7e7f7
5 changed files with 122 additions and 265 deletions
+34
View File
@@ -24,6 +24,40 @@ def calc_risk_fraction(direction: str, entry_price: float, stop_loss: float) ->
return None
def trend_effective_margin_capital(plan: dict) -> float:
"""按已开仓张数占计划总张数比例折算保证金(首仓/部分补仓时的盈亏估算)。"""
try:
plan_margin = float(plan.get("plan_margin_capital") or 0)
target = float(plan.get("target_order_amount") or 0)
open_amt = float(plan.get("order_amount_open") or 0)
except (TypeError, ValueError):
return float((plan or {}).get("plan_margin_capital") or 0)
if plan_margin <= 0:
return 0.0
if target > 0 and open_amt > 0:
return round(plan_margin * min(1.0, open_amt / target), 8)
try:
first = float(plan.get("first_order_amount") or 0)
except (TypeError, ValueError):
first = 0.0
if target > 0 and first > 0:
return round(plan_margin * min(1.0, first / target), 8)
return plan_margin
def trend_dca_level_reached(direction: str, mark_price: float, level: float) -> bool:
"""做空:价升触达/越过档位即应补仓;做多:价跌触达/越过档位。"""
d = (direction or "long").strip().lower()
try:
pf = float(mark_price)
lv = float(level)
except (TypeError, ValueError):
return False
if d == "long":
return pf <= lv
return pf >= lv
def validate_trend_bounds(direction: str, stop_loss: float, add_upper: float) -> Optional[str]:
direction = (direction or "long").strip().lower()
if direction == "long":