浏览代码

配置!

skyffire 1 年之前
父节点
当前提交
95bfa24b14
共有 2 个文件被更改,包括 30 次插入7 次删除
  1. 27 5
      standard/src/gate_swap.rs
  2. 3 2
      standard/src/lib.rs

+ 27 - 5
standard/src/gate_swap.rs

@@ -7,6 +7,7 @@ use futures::stream::FuturesUnordered;
 use futures::TryStreamExt;
 use rust_decimal::Decimal;
 use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
+use rust_decimal_macros::dec;
 use serde_json::{json};
 use tokio::spawn;
 use tokio::time::Instant;
@@ -536,7 +537,8 @@ impl Platform for GateSwap {
         let response_data = self.request.place_price_order(base_coin, params).await;
         if response_data.code == "200" {
             Ok(PriceOrder {
-                id: response_data.data["id"].to_string(),
+                price_order_id: response_data.data["id"].to_string(),
+                order_id: "".to_string(),
             })
         } else {
             Err(Error::new(ErrorKind::Other, response_data.to_string()))
@@ -551,7 +553,8 @@ impl Platform for GateSwap {
         let response_data = self.request.cancel_price_order(base_coin, order_id.to_string()).await;
         if response_data.code == "200" {
             Ok(PriceOrder {
-                id: order_id.to_string(),
+                price_order_id: order_id.to_string(),
+                order_id: "".to_string(),
             })
         } else {
             Err(Error::new(ErrorKind::Other, response_data.to_string()))
@@ -611,6 +614,7 @@ impl Platform for GateSwap {
             let side = order_command.limits_open[item].get(1).unwrap().clone();
             let price = Decimal::from_str(&*order_command.limits_open[item].get(2).unwrap().clone()).unwrap();
             let cid = order_command.limits_open[item].get(3).unwrap().clone();
+            let stop_price = Decimal::from_str(&*order_command.limits_open[item].get(4).unwrap().clone()).unwrap();
 
             //  order_name: [数量,方向,价格,c_id]
             let mut self_clone = self.clone();
@@ -621,10 +625,28 @@ impl Platform for GateSwap {
                 ts.on_after_send();
 
                 match result {
-                    Ok(mut result) => {
-                        result.trace_stack = ts;
+                    Ok(mut order) => {
+                        order.trace_stack = ts;
 
-                        self_clone.order_sender.send(result).await.unwrap();
+                        self_clone.order_sender.send(order).await.unwrap();
+
+                        // 成功之后下止损单
+                        let price_order_result = self_clone.take_stop_loss_order(
+                            stop_price,
+                            dec!(0),
+                            &side).await;
+
+                        // 下止损单成功之后发到逻辑层处理。
+                        match price_order_result {
+                            Ok(mut price_order) => {
+                                price_order.order_id = order.id.clone();
+
+                                self_clone.price_order_sender.send(price_order).await.unwrap();
+                            }
+                            Err(error) => {
+                                error!("gate swap 止损单下单失败,原因:{:?}。", error);
+                            }
+                        }
                     }
                     Err(error) => {
                         let mut err_order = Order::new();

+ 3 - 2
standard/src/lib.rs

@@ -302,7 +302,8 @@ impl Order {
 
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub struct PriceOrder {
-    pub id: String,
+    pub price_order_id: String,
+    pub order_id: String,
 }
 
 /// Ticker结构体(市场行情)
@@ -559,7 +560,7 @@ pub trait Platform {
     /// 下一个止损单
     /// - stop_price: 触发价
     /// - price: 委托价,0就市价委托
-    /// - side: 止损哪个方向[long多单,short空单]
+    /// - side: 止损哪个方向[kd多单,kk空单]
     async fn take_stop_loss_order(&mut self, stop_price: Decimal, price: Decimal, side: &str) -> Result<PriceOrder, Error>;
     /// 撤销止损订单
     /// - order_id: 需要撤销的id