1
This commit is contained in:
parent
9fceadcb27
commit
9944d9ea86
@ -268,14 +268,16 @@ class PaperTradingService:
|
||||
return result
|
||||
|
||||
# === 检查总杠杆是否超限 ===
|
||||
# 计算当前已用保证金(持仓+挂单)
|
||||
current_used_margin = len(self.active_orders) * self.margin_per_order
|
||||
# 新订单增加的保证金
|
||||
# 计算当前实际的已用保证金和持仓价值(使用订单实际值)
|
||||
current_used_margin = sum(order.margin for order in self.active_orders.values())
|
||||
current_total_position_value = sum(order.quantity for order in self.active_orders.values())
|
||||
|
||||
# 新订单增加的保证金和持仓价值
|
||||
new_margin = margin
|
||||
# 新订单的总持仓价值 = 保证金 × 杠杆
|
||||
new_position_value = new_margin * self.leverage
|
||||
# 新增后的总持仓价值
|
||||
new_total_position_value = (current_used_margin * self.leverage) + new_position_value
|
||||
new_position_value = position_value
|
||||
|
||||
# 新增后的总持仓价值和总杠杆
|
||||
new_total_position_value = current_total_position_value + new_position_value
|
||||
|
||||
# 获取当前账户余额
|
||||
db = db_service.get_session()
|
||||
@ -300,7 +302,7 @@ class PaperTradingService:
|
||||
|
||||
if new_total_leverage > self.max_total_leverage:
|
||||
msg = f"总杠杆超限!当前 {new_total_leverage:.1f}x,上限 {self.max_total_leverage}x"
|
||||
logger.info(f"{msg}: {symbol} | 当前持仓价值: ${current_used_margin * self.leverage:,.0f} | "
|
||||
logger.info(f"{msg}: {symbol} | 当前持仓价值: ${current_total_position_value:,.0f} | "
|
||||
f"新订单持仓价值: ${new_position_value:,.0f} | 总持仓价值: ${new_total_position_value:,.0f} | "
|
||||
f"账户余额: ${current_balance:,.0f}")
|
||||
result['message'] = msg
|
||||
@ -1783,9 +1785,14 @@ class PaperTradingService:
|
||||
Returns:
|
||||
账户状态信息,包括余额、已用保证金、可用保证金等
|
||||
"""
|
||||
# 计算已用保证金(每个活跃订单占用固定保证金)
|
||||
# 计算已用保证金和总仓位价值(使用订单实际值,而非固定值)
|
||||
active_count = len(self.active_orders)
|
||||
used_margin = active_count * self.margin_per_order
|
||||
used_margin = 0
|
||||
total_position_value = 0
|
||||
|
||||
for order in self.active_orders.values():
|
||||
used_margin += order.margin # 订单实际保证金
|
||||
total_position_value += order.quantity # 订单实际持仓价值
|
||||
|
||||
# 计算已实现盈亏(从历史订单)
|
||||
db = db_service.get_session()
|
||||
@ -1812,7 +1819,6 @@ class PaperTradingService:
|
||||
current_balance = self.initial_balance + realized_pnl
|
||||
|
||||
# 计算当前总杠杆(持仓价值 / 账户余额)
|
||||
total_position_value = used_margin * self.leverage
|
||||
current_total_leverage = total_position_value / current_balance if current_balance > 0 else 0
|
||||
|
||||
# 计算可用保证金
|
||||
|
||||
Loading…
Reference in New Issue
Block a user