a
This commit is contained in:
parent
b0e7ae8ef2
commit
b48f555524
|
|
@ -238,10 +238,11 @@ class PositionManager:
|
||||||
logger.info(f"{symbol} [平仓] 更新数据库状态为已平仓 (ID: {trade_id})...")
|
logger.info(f"{symbol} [平仓] 更新数据库状态为已平仓 (ID: {trade_id})...")
|
||||||
# 获取当前价格作为平仓价格
|
# 获取当前价格作为平仓价格
|
||||||
ticker = await self.client.get_ticker_24h(symbol)
|
ticker = await self.client.get_ticker_24h(symbol)
|
||||||
exit_price = ticker['price'] if ticker else position_info['entryPrice']
|
exit_price = float(ticker['price']) if ticker else float(position_info['entryPrice'])
|
||||||
|
|
||||||
entry_price = position_info['entryPrice']
|
# 确保所有值都是float类型
|
||||||
quantity = position_info['quantity']
|
entry_price = float(position_info['entryPrice'])
|
||||||
|
quantity = float(position_info['quantity'])
|
||||||
if position_info['side'] == 'BUY':
|
if position_info['side'] == 'BUY':
|
||||||
pnl = (exit_price - entry_price) * quantity
|
pnl = (exit_price - entry_price) * quantity
|
||||||
pnl_percent = ((exit_price - entry_price) / entry_price) * 100
|
pnl_percent = ((exit_price - entry_price) / entry_price) * 100
|
||||||
|
|
@ -287,13 +288,13 @@ class PositionManager:
|
||||||
|
|
||||||
if order:
|
if order:
|
||||||
logger.info(f"{symbol} [平仓] ✓ 平仓订单已提交 (订单ID: {order.get('orderId', 'N/A')})")
|
logger.info(f"{symbol} [平仓] ✓ 平仓订单已提交 (订单ID: {order.get('orderId', 'N/A')})")
|
||||||
# 获取平仓价格
|
# 获取平仓价格(确保是float类型)
|
||||||
ticker = await self.client.get_ticker_24h(symbol)
|
ticker = await self.client.get_ticker_24h(symbol)
|
||||||
if not ticker:
|
if not ticker:
|
||||||
logger.warning(f"无法获取 {symbol} 价格,使用订单价格")
|
logger.warning(f"无法获取 {symbol} 价格,使用订单价格")
|
||||||
exit_price = float(order.get('avgPrice', 0)) or float(order.get('price', 0))
|
exit_price = float(order.get('avgPrice', 0)) or float(order.get('price', 0))
|
||||||
else:
|
else:
|
||||||
exit_price = ticker['price']
|
exit_price = float(ticker['price'])
|
||||||
|
|
||||||
# 更新数据库记录
|
# 更新数据库记录
|
||||||
if DB_AVAILABLE and Trade and symbol in self.active_positions:
|
if DB_AVAILABLE and Trade and symbol in self.active_positions:
|
||||||
|
|
@ -302,18 +303,20 @@ class PositionManager:
|
||||||
if trade_id:
|
if trade_id:
|
||||||
try:
|
try:
|
||||||
logger.info(f"正在更新 {symbol} 平仓记录到数据库 (ID: {trade_id})...")
|
logger.info(f"正在更新 {symbol} 平仓记录到数据库 (ID: {trade_id})...")
|
||||||
# 计算盈亏
|
# 计算盈亏(确保所有值都是float类型)
|
||||||
entry_price = position_info['entryPrice']
|
entry_price = float(position_info['entryPrice'])
|
||||||
|
quantity_float = float(quantity)
|
||||||
|
exit_price_float = float(exit_price)
|
||||||
if position_info['side'] == 'BUY':
|
if position_info['side'] == 'BUY':
|
||||||
pnl = (exit_price - entry_price) * quantity
|
pnl = (exit_price_float - entry_price) * quantity_float
|
||||||
pnl_percent = ((exit_price - entry_price) / entry_price) * 100
|
pnl_percent = ((exit_price_float - entry_price) / entry_price) * 100
|
||||||
else: # SELL
|
else: # SELL
|
||||||
pnl = (entry_price - exit_price) * quantity
|
pnl = (entry_price - exit_price_float) * quantity_float
|
||||||
pnl_percent = ((entry_price - exit_price) / entry_price) * 100
|
pnl_percent = ((entry_price - exit_price_float) / entry_price) * 100
|
||||||
|
|
||||||
Trade.update_exit(
|
Trade.update_exit(
|
||||||
trade_id=trade_id,
|
trade_id=trade_id,
|
||||||
exit_price=exit_price,
|
exit_price=exit_price_float,
|
||||||
exit_reason=reason,
|
exit_reason=reason,
|
||||||
pnl=pnl,
|
pnl=pnl,
|
||||||
pnl_percent=pnl_percent
|
pnl_percent=pnl_percent
|
||||||
|
|
@ -610,11 +613,11 @@ class PositionManager:
|
||||||
|
|
||||||
# 获取当前价格作为平仓价格
|
# 获取当前价格作为平仓价格
|
||||||
ticker = await self.client.get_ticker_24h(symbol)
|
ticker = await self.client.get_ticker_24h(symbol)
|
||||||
exit_price = ticker['price'] if ticker else trade['entry_price']
|
exit_price = float(ticker['price']) if ticker else float(trade['entry_price'])
|
||||||
|
|
||||||
# 计算盈亏
|
# 计算盈亏(确保所有值都是float类型,避免Decimal类型问题)
|
||||||
entry_price = trade['entry_price']
|
entry_price = float(trade['entry_price'])
|
||||||
quantity = trade['quantity']
|
quantity = float(trade['quantity'])
|
||||||
if trade['side'] == 'BUY':
|
if trade['side'] == 'BUY':
|
||||||
pnl = (exit_price - entry_price) * quantity
|
pnl = (exit_price - entry_price) * quantity
|
||||||
pnl_percent = ((exit_price - entry_price) / entry_price) * 100
|
pnl_percent = ((exit_price - entry_price) / entry_price) * 100
|
||||||
|
|
@ -876,14 +879,16 @@ class PositionManager:
|
||||||
if not position_info:
|
if not position_info:
|
||||||
return
|
return
|
||||||
|
|
||||||
entry_price = position_info['entryPrice']
|
# 确保所有值都是float类型
|
||||||
quantity = position_info['quantity']
|
entry_price = float(position_info['entryPrice'])
|
||||||
|
quantity = float(position_info['quantity'])
|
||||||
|
current_price_float = float(current_price)
|
||||||
|
|
||||||
# 计算当前盈亏
|
# 计算当前盈亏
|
||||||
if position_info['side'] == 'BUY':
|
if position_info['side'] == 'BUY':
|
||||||
pnl_percent = ((current_price - entry_price) / entry_price) * 100
|
pnl_percent = ((current_price_float - entry_price) / entry_price) * 100
|
||||||
else: # SELL
|
else: # SELL
|
||||||
pnl_percent = ((entry_price - current_price) / entry_price) * 100
|
pnl_percent = ((entry_price - current_price_float) / entry_price) * 100
|
||||||
|
|
||||||
# 更新最大盈利
|
# 更新最大盈利
|
||||||
if pnl_percent > position_info.get('maxProfit', 0):
|
if pnl_percent > position_info.get('maxProfit', 0):
|
||||||
|
|
@ -980,14 +985,14 @@ class PositionManager:
|
||||||
if trade_id:
|
if trade_id:
|
||||||
try:
|
try:
|
||||||
if position_info['side'] == 'BUY':
|
if position_info['side'] == 'BUY':
|
||||||
pnl = (current_price - entry_price) * quantity
|
pnl = (current_price_float - entry_price) * quantity
|
||||||
else: # SELL
|
else: # SELL
|
||||||
pnl = (entry_price - current_price) * quantity
|
pnl = (entry_price - current_price_float) * quantity
|
||||||
|
|
||||||
logger.info(f"{symbol} [自动平仓] 更新数据库记录 (ID: {trade_id})...")
|
logger.info(f"{symbol} [自动平仓] 更新数据库记录 (ID: {trade_id})...")
|
||||||
Trade.update_exit(
|
Trade.update_exit(
|
||||||
trade_id=trade_id,
|
trade_id=trade_id,
|
||||||
exit_price=current_price,
|
exit_price=current_price_float,
|
||||||
exit_reason=exit_reason,
|
exit_reason=exit_reason,
|
||||||
pnl=pnl,
|
pnl=pnl,
|
||||||
pnl_percent=pnl_percent
|
pnl_percent=pnl_percent
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user