Pārlūkot izejas kodu

基本修复了所有bug。

skyfffire 2 gadi atpakaļ
vecāks
revīzija
53637e950b

+ 2 - 1
.gitignore

@@ -2,4 +2,5 @@
 /.idea
 
 Cargo.lock
-config.toml
+config.toml
+*.log

+ 1 - 1
exchanges/src/response_base.rs

@@ -1,7 +1,7 @@
 /**交易所返回数据处理之后,同意保存格式,为了内部其他接口调用*/
 pub enum ChannelType {}
 
-#[derive(Debug)]
+#[derive(Debug, Clone)]
 pub struct ResponseData {
     pub label: String,
     pub code: String,

+ 4 - 4
src/main.rs

@@ -60,12 +60,12 @@ async fn main() {
 
                         let mut order_info = OrderInfo {
                             symbol: "".to_string(),
-                            amount: Default::default(),
+                            amount: order.amount.abs(),
                             side: "".to_string(),
-                            price: Default::default(),
+                            price: order.price,
                             client_id: order.custom_id,
-                            filled_price: Default::default(),
-                            filled: Default::default(),
+                            filled_price: order.avg_price,
+                            filled: order.deal_amount.abs(),
                             order_id: order.id,
                             local_time: 0,
                             create_time: 0,

+ 9 - 3
standard/src/gate_handle.rs

@@ -1,7 +1,7 @@
 use std::str::FromStr;
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
-use tracing::error;
+use tracing::{error, info};
 use exchanges::response_base::ResponseData;
 use crate::{Account, MarketOrder, Order, Position, PositionModeEnum, SpecialDepth, SpecialOrder};
 use crate::exchange::ExchangeEnum;
@@ -80,6 +80,8 @@ pub fn handle_order(res_data: ResponseData, amount_size: Decimal) -> SpecialOrde
 }
 
 pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Order {
+    info!("format-order-start, gate_handle");
+    info!(?order);
     let status = order["status"].as_str().unwrap_or("");
     let text = order["text"].as_str().unwrap_or("");
     let size = Decimal::from_str(&order["size"].to_string()).unwrap();
@@ -88,7 +90,7 @@ pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Orde
     let amount = size * amount_size;
     let deal_amount = (size - left) * amount_size;
     let custom_status = if status == "finished" { "REMOVE".to_string() } else if status == "open" { "NEW".to_string() } else { panic!("Gate:格式化订单状态错误!\nformat_order_item:status={}", status) };
-    Order {
+    let rst_order = Order {
         id: order["id"].to_string(),
         custom_id: text.replace("t-my-custom-id_", ""),
         price: Decimal::from_str(&order["price"].to_string()).unwrap(),
@@ -97,7 +99,11 @@ pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Orde
         avg_price: Decimal::from_str(&order["fill_price"].to_string()).unwrap(),
         status: custom_status,
         order_type: "limit".to_string(),
-    }
+    };
+
+    info!(?rst_order);
+    info!("format-order-end, gate_handle");
+    return rst_order;
 }
 
 // 处理特殊深度数据

+ 15 - 4
standard/src/gate_swap.rs

@@ -8,8 +8,9 @@ use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
 use rust_decimal_macros::dec;
 use serde_json::{json};
 use futures::stream::FuturesUnordered;
-use futures::{TryStreamExt};
-use tracing::error;
+use futures::{TryFutureExt, TryStreamExt};
+use futures::future::err;
+use tracing::{error, info};
 use crate::{Platform, ExchangeEnum, Account, Position, Ticker, Market, Order, OrderCommand, PositionModeEnum};
 use exchanges::gate_swap_rest::GateSwapRest;
 
@@ -392,6 +393,11 @@ impl Platform for GateSwap {
                         result_sd.send(result).await.unwrap();
                     }
                     Err(error) => {
+                        let mut err_order = Order::new();
+                        err_order.custom_id = (*cid).clone();
+                        err_order.status = "REMOVE".to_string();
+
+                        result_sd.send(err_order).await.unwrap();
                         err_sd.send(error).await.unwrap();
                     }
                 }
@@ -449,6 +455,8 @@ pub fn format_position_item(position: &serde_json::Value, amount_size: Decimal)
 }
 
 pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Order {
+    info!("format-order-start, gate_swap");
+    info!(?order);
     let status = order["status"].as_str().unwrap_or("");
     let text = order["text"].as_str().unwrap_or("");
     let size = Decimal::from_str(&order["size"].to_string()).unwrap();
@@ -457,7 +465,7 @@ pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Orde
     let amount = size * amount_size;
     let deal_amount = (size - left) * amount_size;
     let custom_status = if status == "finished" { "REMOVE".to_string() } else if status == "open" { "NEW".to_string() } else { panic!("Gate:格式化订单状态错误!\nformat_order_item:status={}", status) };
-    Order {
+    let rst_order = Order {
         id: order["id"].to_string(),
         custom_id: text.replace("t-my-custom-id_", ""),
         price: Decimal::from_str(order["price"].as_str().unwrap()).unwrap(),
@@ -466,5 +474,8 @@ pub fn format_order_item(order: serde_json::Value, amount_size: Decimal) -> Orde
         avg_price: Decimal::from_str(&order["fill_price"].as_str().unwrap()).unwrap(),
         status: custom_status,
         order_type: "limit".to_string(),
-    }
+    };
+    info!(?rst_order);
+    info!("format-order-end, gate_swap");
+    return rst_order;
 }

+ 7 - 19
strategy/src/quant.rs

@@ -316,7 +316,6 @@ impl Quant {
         if data.status == "NEW" {
             // 更新oid信息 更新订单 loceltime信息(尤其是查单返回new的情况 必须更新 否则会误触发风控)
             if self.local_orders.contains_key(&data.client_id) {
-                info!(?data);
                 let mut order_info = self.local_orders.get(&data.client_id).unwrap().clone();
                 order_info.order_id = data.order_id;
                 order_info.local_time = Utc::now().timestamp_millis();
@@ -329,7 +328,7 @@ impl Quant {
             }
             // TODO 本地收到移除动作之后,会去继续撤销指定订单,有逻辑问题.
             if self.local_orders.contains_key(&data.client_id) {
-                debug!("删除本地订单, client_id:{}", data.client_id);
+                info!("删除本地订单, client_id:{}", data.client_id);
                 self.local_orders.remove(&data.client_id);
             } else {
                 debug!("该订单不在本地挂单表中, client_id:{}", data.client_id);
@@ -1185,7 +1184,6 @@ pub async fn run_transaction(quant_arc: Arc<Mutex<Quant>>, name: String, symbols
         loop {
             match rx.recv().await {
                 Some(data) => {
-                    debug!(?data);
                     if data.code != "200".to_string() {
                         continue;
                     }
@@ -1204,33 +1202,23 @@ pub async fn run_transaction(quant_arc: Arc<Mutex<Quant>>, name: String, symbols
                             quant.update_equity(account);
                         }
                     } else if data.channel == "futures.orders" {
-                        let orders = standard::gate_handle::handle_order(data, multiplier.clone());
+                        let orders = standard::gate_handle::handle_order(data.clone(), multiplier.clone());
                         let mut order_infos:Vec<OrderInfo> = Vec::new();
                         for order in orders.order {
-                            if order.deal_amount != Decimal::ZERO {
-                                info!(?order);
-                            }
-
-                            let mut order_info = OrderInfo{
+                            let mut order_info = OrderInfo {
                                 symbol: "".to_string(),
-                                amount: Default::default(),
+                                amount: order.amount.abs(),
                                 side: "".to_string(),
-                                price: Default::default(),
+                                price: order.price,
                                 client_id: order.custom_id,
-                                filled_price: Default::default(),
-                                filled: Default::default(),
+                                filled_price: order.avg_price,
+                                filled: order.deal_amount.abs(),
                                 order_id: order.id,
                                 local_time: 0,
                                 create_time: 0,
                                 status: order.status,
                                 fee: Default::default(),
                             };
-                            if "REMOVE" == order_info.status {
-                                order_info.amount = order.amount.abs();
-                                order_info.price = order.price;
-                                order_info.filled = order.deal_amount.abs();
-                                order_info.filled_price = order.avg_price;
-                            }
                             order_infos.push(order_info);
                         }
                         {