增加挂单成交通知。
This commit is contained in:
parent
01c9d13ce3
commit
6739e0124f
@ -61,9 +61,34 @@ class CryptoAgent:
|
|||||||
|
|
||||||
for result in triggered:
|
for result in triggered:
|
||||||
if self._event_loop and self._event_loop.is_running():
|
if self._event_loop and self._event_loop.is_running():
|
||||||
|
# 根据事件类型选择不同的通知方法
|
||||||
|
event_type = result.get('event_type', 'order_closed')
|
||||||
|
if event_type == 'order_filled':
|
||||||
|
asyncio.run_coroutine_threadsafe(self._notify_order_filled(result), self._event_loop)
|
||||||
|
else:
|
||||||
asyncio.run_coroutine_threadsafe(self._notify_order_closed(result), self._event_loop)
|
asyncio.run_coroutine_threadsafe(self._notify_order_closed(result), self._event_loop)
|
||||||
else:
|
else:
|
||||||
logger.warning(f"无法发送平仓通知: 事件循环不可用")
|
logger.warning(f"无法发送通知: 事件循环不可用")
|
||||||
|
|
||||||
|
async def _notify_order_filled(self, result: Dict[str, Any]):
|
||||||
|
"""发送挂单成交通知"""
|
||||||
|
side_text = "做多" if result.get('side') == 'long' else "做空"
|
||||||
|
grade = result.get('signal_grade', 'N/A')
|
||||||
|
|
||||||
|
message = f"""✅ 挂单成交
|
||||||
|
|
||||||
|
交易对: {result.get('symbol')}
|
||||||
|
方向: {side_text}
|
||||||
|
等级: {grade}
|
||||||
|
挂单价: ${result.get('entry_price', 0):,.2f}
|
||||||
|
成交价: ${result.get('filled_price', 0):,.2f}
|
||||||
|
仓位: ${result.get('quantity', 0):,.0f}
|
||||||
|
止损: ${result.get('stop_loss', 0):,.2f}
|
||||||
|
止盈: ${result.get('take_profit', 0):,.2f}"""
|
||||||
|
|
||||||
|
await self.feishu.send_text(message)
|
||||||
|
await self.telegram.send_message(message)
|
||||||
|
logger.info(f"已发送挂单成交通知: {result.get('order_id')}")
|
||||||
|
|
||||||
async def _notify_order_closed(self, result: Dict[str, Any]):
|
async def _notify_order_closed(self, result: Dict[str, Any]):
|
||||||
"""发送订单平仓通知"""
|
"""发送订单平仓通知"""
|
||||||
|
|||||||
@ -189,7 +189,7 @@ class PaperTradingService:
|
|||||||
current_price: 当前价格
|
current_price: 当前价格
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
触发的订单结果列表(平仓结果)
|
触发的订单结果列表(包括挂单激活和平仓结果)
|
||||||
"""
|
"""
|
||||||
triggered = []
|
triggered = []
|
||||||
|
|
||||||
@ -199,7 +199,9 @@ class PaperTradingService:
|
|||||||
if order.symbol == symbol and order.status == OrderStatus.PENDING
|
if order.symbol == symbol and order.status == OrderStatus.PENDING
|
||||||
]
|
]
|
||||||
for order in pending_orders:
|
for order in pending_orders:
|
||||||
if self._check_pending_entry(order, current_price):
|
result = self._check_pending_entry(order, current_price)
|
||||||
|
if result:
|
||||||
|
triggered.append(result)
|
||||||
logger.info(f"挂单触发入场: {order.order_id} | {symbol} @ ${current_price:,.2f}")
|
logger.info(f"挂单触发入场: {order.order_id} | {symbol} @ ${current_price:,.2f}")
|
||||||
|
|
||||||
# 2. 检查持仓订单是否触发止盈止损
|
# 2. 检查持仓订单是否触发止盈止损
|
||||||
@ -217,12 +219,15 @@ class PaperTradingService:
|
|||||||
|
|
||||||
return triggered
|
return triggered
|
||||||
|
|
||||||
def _check_pending_entry(self, order: PaperOrder, current_price: float) -> bool:
|
def _check_pending_entry(self, order: PaperOrder, current_price: float) -> Optional[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
检查挂单是否触发入场
|
检查挂单是否触发入场
|
||||||
|
|
||||||
做多挂单:价格下跌到入场价时触发(买入)
|
做多挂单:价格下跌到入场价时触发(买入)
|
||||||
做空挂单:价格上涨到入场价时触发(卖出)
|
做空挂单:价格上涨到入场价时触发(卖出)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
如果触发,返回激活结果字典;否则返回 None
|
||||||
"""
|
"""
|
||||||
should_trigger = False
|
should_trigger = False
|
||||||
|
|
||||||
@ -238,10 +243,15 @@ class PaperTradingService:
|
|||||||
if should_trigger:
|
if should_trigger:
|
||||||
return self._activate_pending_order(order, current_price)
|
return self._activate_pending_order(order, current_price)
|
||||||
|
|
||||||
return False
|
return None
|
||||||
|
|
||||||
def _activate_pending_order(self, order: PaperOrder, filled_price: float) -> bool:
|
def _activate_pending_order(self, order: PaperOrder, filled_price: float) -> Optional[Dict[str, Any]]:
|
||||||
"""激活挂单,转为持仓"""
|
"""
|
||||||
|
激活挂单,转为持仓
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
激活结果字典,包含订单信息
|
||||||
|
"""
|
||||||
db = db_service.get_session()
|
db = db_service.get_session()
|
||||||
try:
|
try:
|
||||||
order.status = OrderStatus.OPEN
|
order.status = OrderStatus.OPEN
|
||||||
@ -252,11 +262,24 @@ class PaperTradingService:
|
|||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
logger.info(f"挂单已激活: {order.order_id} | {order.symbol} {order.side.value} @ ${filled_price:,.2f}")
|
logger.info(f"挂单已激活: {order.order_id} | {order.symbol} {order.side.value} @ ${filled_price:,.2f}")
|
||||||
return True
|
|
||||||
|
# 返回激活结果
|
||||||
|
return {
|
||||||
|
'event_type': 'order_filled',
|
||||||
|
'order_id': order.order_id,
|
||||||
|
'symbol': order.symbol,
|
||||||
|
'side': order.side.value,
|
||||||
|
'entry_price': order.entry_price,
|
||||||
|
'filled_price': filled_price,
|
||||||
|
'quantity': order.quantity,
|
||||||
|
'signal_grade': order.signal_grade.value if order.signal_grade else None,
|
||||||
|
'stop_loss': order.stop_loss,
|
||||||
|
'take_profit': order.take_profit
|
||||||
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"激活挂单失败: {e}")
|
logger.error(f"激活挂单失败: {e}")
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return False
|
return None
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user