|
|
@@ -36,6 +36,7 @@ pub struct Strategy {
|
|
|
order_quantity: Decimal, // 写死的订单数量
|
|
|
filled_quantity: Decimal, // 成交数量
|
|
|
min_order_interval_ms: u128, // 最小下单间隔(毫秒)
|
|
|
+ min_query_order_interval_ms: u128, // 最小查单间隔(毫秒)
|
|
|
rest_client: Arc<Mutex<ExtendedRestClient>>, // rest客户端
|
|
|
}
|
|
|
|
|
|
@@ -43,9 +44,10 @@ impl Strategy {
|
|
|
pub fn new(client_am: Arc<Mutex<ExtendedRestClient>>) -> Strategy {
|
|
|
Strategy {
|
|
|
state: StrategyState::Idle,
|
|
|
- order_quantity: dec!(0.001),
|
|
|
+ order_quantity: dec!(0.0001),
|
|
|
filled_quantity: Decimal::ZERO,
|
|
|
min_order_interval_ms: 200,
|
|
|
+ min_query_order_interval_ms: 200,
|
|
|
rest_client: client_am,
|
|
|
}
|
|
|
}
|
|
|
@@ -79,7 +81,7 @@ impl Strategy {
|
|
|
|
|
|
// 状态1: 空闲状态,下限价买单
|
|
|
async fn handle_idle_state(&mut self, dm: &DataManager) -> Result<()> {
|
|
|
- info!("进入空闲状态,准备下买单");
|
|
|
+ info!("============================ 进入空闲状态,准备下买单 ===========================");
|
|
|
|
|
|
// 检查是否满足下单时间间隔
|
|
|
if let Some(last_time) = self.get_last_order_time() {
|
|
|
@@ -116,7 +118,12 @@ impl Strategy {
|
|
|
price: Decimal,
|
|
|
last_order_time: Instant,
|
|
|
) -> Result<()> {
|
|
|
- info!("等待限价单成交,订单ID: {}, 价格: {}", order_id, price);
|
|
|
+ // info!("等待限价单成交,订单ID: {}, 价格: {}", order_id, price);
|
|
|
+ // 下单之后过一会再检查订单
|
|
|
+ let elapsed = last_order_time.elapsed().as_millis();
|
|
|
+ if elapsed < self.min_query_order_interval_ms {
|
|
|
+ return Ok(());
|
|
|
+ }
|
|
|
|
|
|
// 检查订单是否已成交
|
|
|
match self.check_order_filled(&order_id).await {
|
|
|
@@ -188,13 +195,13 @@ impl Strategy {
|
|
|
// 状态4: 执行市价单
|
|
|
async fn handle_executing_market_order(
|
|
|
&mut self,
|
|
|
- _dm: &DataManager,
|
|
|
+ dm: &DataManager,
|
|
|
_last_order_time: Instant,
|
|
|
) -> Result<()> {
|
|
|
info!("执行市价卖单");
|
|
|
|
|
|
// 尝试下市价单
|
|
|
- let order_id = match self.place_market_sell_order(self.filled_quantity).await {
|
|
|
+ let order_id = match self.place_market_sell_order(dm.best_bid, self.filled_quantity).await {
|
|
|
Ok(id) => {
|
|
|
info!("市价卖单下单成功,订单ID: {}", id);
|
|
|
id
|
|
|
@@ -259,7 +266,7 @@ impl Strategy {
|
|
|
}
|
|
|
|
|
|
/// 下市价卖单
|
|
|
- async fn place_market_sell_order(&self, quantity: Decimal) -> Result<String> {
|
|
|
+ async fn place_market_sell_order(&self, price: Decimal, quantity: Decimal) -> Result<String> {
|
|
|
info!("下市价卖单: 数量={}", quantity);
|
|
|
let mut client = self.rest_client.lock().await;
|
|
|
|
|
|
@@ -268,7 +275,7 @@ impl Strategy {
|
|
|
"MARKET",
|
|
|
"SELL",
|
|
|
quantity.to_string().as_str(),
|
|
|
- "-1").await;
|
|
|
+ price.to_string().as_str()).await;
|
|
|
|
|
|
// 解析下单结果并返回
|
|
|
self.match_create_order_result(&create_result)
|
|
|
@@ -313,7 +320,7 @@ impl Strategy {
|
|
|
/// - `require_full_fill`: true 表示只接受完全成交,false 表示部分成交也接受
|
|
|
async fn check_order_filled_status(&mut self, order_id: &str, require_full_fill: bool) -> Result<bool> {
|
|
|
let check_type = if require_full_fill { "完全成交" } else { "部分成交" };
|
|
|
- info!("检查订单是否{}: {}", check_type, order_id);
|
|
|
+ // info!("检查订单是否{}: {}", check_type, order_id);
|
|
|
|
|
|
let data = self.get_order_result(order_id).await?;
|
|
|
|
|
|
@@ -341,7 +348,7 @@ impl Strategy {
|
|
|
.map_err(|e| anyhow!("查单-解析 'data.filledQty' 为 Decimal 失败: {}, 值: {}", e, v))
|
|
|
)?;
|
|
|
|
|
|
- self.filled_quantity = filled_qty;
|
|
|
+ self.filled_quantity = filled_qty.normalize();
|
|
|
info!("订单 {} 已{},成交数量: {}", order_id, check_type, filled_qty);
|
|
|
Ok(true)
|
|
|
} else {
|
|
|
@@ -398,7 +405,7 @@ impl Strategy {
|
|
|
|
|
|
// 获取order的id
|
|
|
let id = data.get("id")
|
|
|
- .and_then(|v| v.as_str())
|
|
|
+ .and_then(|v| v.as_i64())
|
|
|
.ok_or_else(|| anyhow!("下单-获取 'data.id' 失败,原始JSON:{}", value_str))?;
|
|
|
|
|
|
Ok(id.to_string())
|