|
|
@@ -1,8 +1,8 @@
|
|
|
use anyhow::Result;
|
|
|
use rust_decimal::Decimal;
|
|
|
use std::time::{Duration, Instant};
|
|
|
-use tokio::time::{sleep};
|
|
|
-use tracing::info;
|
|
|
+use tokio::time::sleep;
|
|
|
+use tracing::{info, warn};
|
|
|
use crate::data_manager::DataManager;
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
@@ -45,16 +45,24 @@ impl Strategy {
|
|
|
|
|
|
match self.state.clone() {
|
|
|
StrategyState::Idle => {
|
|
|
- self.handle_idle_state(dm).await?;
|
|
|
+ if let Err(e) = self.handle_idle_state(dm).await {
|
|
|
+ warn!("空闲状态处理失败: {}", e);
|
|
|
+ }
|
|
|
}
|
|
|
StrategyState::WaitingLimitOrder { order_id, price, last_order_time } => {
|
|
|
- self.handle_waiting_limit_order(dm, order_id, price, last_order_time).await?;
|
|
|
+ if let Err(e) = self.handle_waiting_limit_order(dm, order_id, price, last_order_time).await {
|
|
|
+ warn!("等待限价单状态处理失败: {}", e);
|
|
|
+ }
|
|
|
}
|
|
|
StrategyState::CancellingOrder { order_id, price, last_order_time } => {
|
|
|
- self.handle_cancelling_order(dm, order_id, price, last_order_time).await?;
|
|
|
+ if let Err(e) = self.handle_cancelling_order(dm, order_id, price, last_order_time).await {
|
|
|
+ warn!("撤单状态处理失败: {}", e);
|
|
|
+ }
|
|
|
}
|
|
|
StrategyState::ExecutingMarketOrder { last_order_time } => {
|
|
|
- self.handle_executing_market_order(dm, last_order_time).await?;
|
|
|
+ if let Err(e) = self.handle_executing_market_order(dm, last_order_time).await {
|
|
|
+ warn!("市价单执行状态处理失败: {}", e);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -74,15 +82,22 @@ impl Strategy {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- let order_id = self.place_limit_buy_order(dm.best_bid, self.order_quantity).await?;
|
|
|
-
|
|
|
- self.state = StrategyState::WaitingLimitOrder {
|
|
|
- order_id,
|
|
|
- price: dm.best_bid,
|
|
|
- last_order_time: Instant::now(),
|
|
|
- };
|
|
|
-
|
|
|
- Ok(())
|
|
|
+ // 尝试下单,只有成功才转换状态
|
|
|
+ match self.place_limit_buy_order(dm.best_bid, self.order_quantity).await {
|
|
|
+ Ok(order_id) => {
|
|
|
+ info!("限价买单下单成功,订单ID: {}", order_id);
|
|
|
+ self.state = StrategyState::WaitingLimitOrder {
|
|
|
+ order_id,
|
|
|
+ price: dm.best_bid,
|
|
|
+ last_order_time: Instant::now(),
|
|
|
+ };
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ warn!("下限价买单失败: {}", e);
|
|
|
+ Err(e)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 状态2&3: 等待限价单成交
|
|
|
@@ -96,10 +111,19 @@ impl Strategy {
|
|
|
info!("等待限价单成交,订单ID: {}, 价格: {}", order_id, price);
|
|
|
|
|
|
// 检查订单是否已成交
|
|
|
- if self.check_order_filled(&order_id).await? {
|
|
|
- info!("限价单已成交,准备执行市价单");
|
|
|
- self.state = StrategyState::ExecutingMarketOrder { last_order_time };
|
|
|
- return Ok(());
|
|
|
+ match self.check_order_filled(&order_id).await {
|
|
|
+ Ok(true) => {
|
|
|
+ info!("限价单已成交,准备执行市价单");
|
|
|
+ self.state = StrategyState::ExecutingMarketOrder { last_order_time };
|
|
|
+ return Ok(());
|
|
|
+ }
|
|
|
+ Ok(false) => {
|
|
|
+ // 订单未成交,继续检查价格
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ warn!("检查订单成交状态失败: {}", e);
|
|
|
+ return Err(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 检查价格是否变化
|
|
|
@@ -125,21 +149,32 @@ impl Strategy {
|
|
|
) -> Result<()> {
|
|
|
info!("撤单中,订单ID: {}", order_id);
|
|
|
|
|
|
- self.cancel_order(&order_id).await?;
|
|
|
+ // 尝试撤单
|
|
|
+ if let Err(e) = self.cancel_order(&order_id).await {
|
|
|
+ warn!("撤单操作失败: {}", e);
|
|
|
+ return Err(e);
|
|
|
+ }
|
|
|
|
|
|
// 撤单后暂停几秒再检查
|
|
|
sleep(Duration::from_millis(2000)).await;
|
|
|
|
|
|
// 撤单后检查是否有成交
|
|
|
- if self.check_order_partially_filled(&order_id).await? {
|
|
|
- info!("撤单时发现有成交,准备执行市价单");
|
|
|
- self.state = StrategyState::ExecutingMarketOrder { last_order_time };
|
|
|
- } else {
|
|
|
- info!("撤单完成,无成交,返回空闲状态");
|
|
|
- self.state = StrategyState::Idle;
|
|
|
+ match self.check_order_partially_filled(&order_id).await {
|
|
|
+ Ok(true) => {
|
|
|
+ info!("撤单时发现有成交,准备执行市价单");
|
|
|
+ self.state = StrategyState::ExecutingMarketOrder { last_order_time };
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+ Ok(false) => {
|
|
|
+ info!("撤单完成,无成交,返回空闲状态");
|
|
|
+ self.state = StrategyState::Idle;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ warn!("检查订单部分成交状态失败: {}", e);
|
|
|
+ Err(e)
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- Ok(())
|
|
|
}
|
|
|
|
|
|
// 状态4: 执行市价单
|
|
|
@@ -150,15 +185,34 @@ impl Strategy {
|
|
|
) -> Result<()> {
|
|
|
info!("执行市价卖单");
|
|
|
|
|
|
- let order_id = self.place_market_sell_order(self.order_quantity).await?;
|
|
|
+ // 尝试下市价单
|
|
|
+ let order_id = match self.place_market_sell_order(self.order_quantity).await {
|
|
|
+ Ok(id) => {
|
|
|
+ info!("市价卖单下单成功,订单ID: {}", id);
|
|
|
+ id
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ warn!("下市价卖单失败: {}", e);
|
|
|
+ return Err(e);
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
// 等待市价单成交(市价单通常立即成交)
|
|
|
- if self.check_order_filled(&order_id).await? {
|
|
|
- info!("市价单已成交,返回空闲状态");
|
|
|
- self.state = StrategyState::Idle;
|
|
|
+ match self.check_order_filled(&order_id).await {
|
|
|
+ Ok(true) => {
|
|
|
+ info!("市价单已成交,返回空闲状态");
|
|
|
+ self.state = StrategyState::Idle;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+ Ok(false) => {
|
|
|
+ warn!("市价单未成交,保持当前状态");
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ warn!("检查市价单成交状态失败: {}", e);
|
|
|
+ Err(e)
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- Ok(())
|
|
|
}
|
|
|
|
|
|
// 获取上次下单时间
|
|
|
@@ -207,4 +261,4 @@ impl Strategy {
|
|
|
// TODO: 实现具体的查询逻辑
|
|
|
Ok(false)
|
|
|
}
|
|
|
-}
|
|
|
+}
|