|
|
@@ -1,6 +1,7 @@
|
|
|
use anyhow::Result;
|
|
|
use rust_decimal::Decimal;
|
|
|
use std::time::{Duration, Instant};
|
|
|
+use rust_decimal_macros::dec;
|
|
|
use tokio::time::sleep;
|
|
|
use tracing::{info, warn};
|
|
|
use crate::data_manager::DataManager;
|
|
|
@@ -26,23 +27,22 @@ pub enum StrategyState {
|
|
|
#[allow(dead_code)]
|
|
|
pub struct Strategy {
|
|
|
state: StrategyState,
|
|
|
- order_quantity: Decimal, // 写死的订单数量
|
|
|
- min_order_interval_ms: u128, // 最小下单间隔(毫秒)
|
|
|
+ order_quantity: Decimal, // 写死的订单数量
|
|
|
+ filled_quantity: Decimal, // 成交数量
|
|
|
+ min_order_interval_ms: u128, // 最小下单间隔(毫秒)
|
|
|
}
|
|
|
|
|
|
impl Strategy {
|
|
|
pub fn new() -> Strategy {
|
|
|
Strategy {
|
|
|
state: StrategyState::Idle,
|
|
|
- order_quantity: Decimal::from(100), // 示例数量
|
|
|
- min_order_interval_ms: 1000,
|
|
|
+ order_quantity: dec!(0.001),
|
|
|
+ filled_quantity: Decimal::ZERO,
|
|
|
+ min_order_interval_ms: 200,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pub async fn do_strategy(&mut self, dm: &DataManager) -> Result<()> {
|
|
|
- info!("Current state: {:?}", self.state);
|
|
|
- info!("Best ask: {}, Best bid: {}", dm.best_ask, dm.best_bid);
|
|
|
-
|
|
|
match self.state.clone() {
|
|
|
StrategyState::Idle => {
|
|
|
if let Err(e) = self.handle_idle_state(dm).await {
|
|
|
@@ -156,10 +156,10 @@ impl Strategy {
|
|
|
}
|
|
|
|
|
|
// 撤单后暂停几秒再检查
|
|
|
- sleep(Duration::from_millis(2000)).await;
|
|
|
+ sleep(Duration::from_millis(3000)).await;
|
|
|
|
|
|
// 撤单后检查是否有成交
|
|
|
- match self.check_order_partially_filled(&order_id).await {
|
|
|
+ match self.check_order_filled(&order_id).await {
|
|
|
Ok(true) => {
|
|
|
info!("撤单时发现有成交,准备执行市价单");
|
|
|
self.state = StrategyState::ExecutingMarketOrder { last_order_time };
|
|
|
@@ -186,7 +186,7 @@ impl Strategy {
|
|
|
info!("执行市价卖单");
|
|
|
|
|
|
// 尝试下市价单
|
|
|
- let order_id = match self.place_market_sell_order(self.order_quantity).await {
|
|
|
+ let order_id = match self.place_market_sell_order(self.filled_quantity).await {
|
|
|
Ok(id) => {
|
|
|
info!("市价卖单下单成功,订单ID: {}", id);
|
|
|
id
|
|
|
@@ -197,6 +197,9 @@ impl Strategy {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ // 下单后暂停几秒再检查
|
|
|
+ sleep(Duration::from_millis(3000)).await;
|
|
|
+
|
|
|
// 等待市价单成交(市价单通常立即成交)
|
|
|
match self.check_order_filled(&order_id).await {
|
|
|
Ok(true) => {
|
|
|
@@ -254,11 +257,4 @@ impl Strategy {
|
|
|
// TODO: 实现具体的查询逻辑
|
|
|
Ok(false)
|
|
|
}
|
|
|
-
|
|
|
- /// 检查订单是否有部分成交
|
|
|
- async fn check_order_partially_filled(&self, order_id: &str) -> Result<bool> {
|
|
|
- info!("检查订单是否有部分成交: {}", order_id);
|
|
|
- // TODO: 实现具体的查询逻辑
|
|
|
- Ok(false)
|
|
|
- }
|
|
|
}
|