Browse Source

止损单、撤销止损单的接口统一加入。

skyfffire 1 year ago
parent
commit
b968bed008

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "as-rust"
-version = "1.5.6"
+version = "1.6.0"
 edition = "2021"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

+ 38 - 26
exchanges/tests/gate_swap_test.rs

@@ -10,8 +10,8 @@ use tracing::{error, info, trace};
 use exchanges::gate_swap_rest::GateSwapRest;
 use exchanges::gate_swap_ws::{GateSwapLogin, GateSwapSubscribeType, GateSwapWs, GateSwapWsType};
 
-const ACCESS_KEY: &str = "4181c882718a95e72122ac1d52c88533";
-const SECRET_KEY: &str = "de82d1507b843ff08d81a0e9b878b721359f274937216b307834b570b676fa3c";
+const ACCESS_KEY: &str = "";
+const SECRET_KEY: &str = "";
 
 //ws-订阅公共频道信息
 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -106,30 +106,31 @@ async fn price_order_test() {
     global::log_utils::init_log_with_info();
 
     let mut rest = get_rest();
-    let params = json!({
-        "initial": {
-            "contract": "XRP_USDT",
-            "price": "0",
-            "tif": "ioc",
-            "text": "t-1232131abc",
-            "reduce_only": true,
-            "auto_size": "close_long"
-        },
-        "trigger": {
-            // [平多:close-long-position, 平空:close-short-position]
-            "order_type": "close-long-position",
-            // 一般都默认用0
-            "strategy_type": 0,
-            // [0 - 最新成交价,1 - 标记价格,2 - 指数价格]
-            "price_type": 0,
-            // [1: 引用价格大于等于我们传的价格,2:引用价格小于等于我们传的价格]
-            // 在止损的情况下:
-            //     1 可以理解为向上突破止损位(一般是给空单用)
-            //     2 可以理解为向下突破止损位(一般是给多单用)
-            "rule": 2,
-            // 我们传的价格,在止损的情况在就是止损触发价格
-            "price": "0.5700",
-        }
+    let mut params = json!({});
+
+    params["initial"] = json!({
+        "contract": "XRP_USDT",
+        "price": "0",
+        "tif": "ioc",
+        "reduce_only": true,
+        // [平多:close_long, 平空:close_short]
+        "auto_size": "close_long"
+    });
+
+    params["trigger"] = json!({
+        // [平多:close-long-position, 平空:close-short-position]
+        "order_type": "close-long-position",
+        // 一般都默认用0
+        "strategy_type": 0,
+        // [0 - 最新成交价,1 - 标记价格,2 - 指数价格]
+        "price_type": 0,
+        // [1: 引用价格大于等于我们传的价格,2:引用价格小于等于我们传的价格]
+        // 在止损的情况下:
+        //     1 可以理解为向上突破触发价(一般是给空单用)
+        //     2 可以理解为向下突破触发价(一般是给多单用)
+        "rule": 2,
+        // 订单触发价格
+        "price": "0.5600",
     });
 
     let response_data = rest.place_price_order("usdt".to_string(), params).await;
@@ -142,6 +143,17 @@ async fn price_order_test() {
     }
 }
 
+#[tokio::test]
+async fn price_order_cancel_test() {
+    global::log_utils::init_log_with_info();
+
+    let mut rest = get_rest();
+
+    // 这边取消订单只能使用系统返回的
+    let rst = rest.cancel_price_order("usdt".to_string(), "58002898".to_string()).await;
+    info!(?rst);
+}
+
 //rest-查询合约账户变更历史
 #[tokio::test]
 async fn rest_account_book_test() {

+ 8 - 0
standard/src/binance_spot.rs

@@ -106,6 +106,14 @@ impl Platform for BinanceSpot {
 
     async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error> { Err(Error::new(ErrorKind::NotFound, "binance_spot:该交易所方法未实现".to_string())) }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "binance_spot:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "binance_spot:该交易所方法未实现".to_string()))
+    }
+
     async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "binance_spot:该交易所方法未实现".to_string())) }
 
     async fn set_dual_leverage(&mut self, _leverage: &str) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "binance_spot:该交易所方法未实现".to_string())) }

+ 9 - 0
standard/src/binance_swap.rs

@@ -346,6 +346,15 @@ impl Platform for BinanceSwap {
 
     async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error> { Err(Error::new(ErrorKind::NotFound, "binance_swap:该交易所方法未实现".to_string())) }
 
+
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "binance_swap:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "binance_swap:该交易所方法未实现".to_string()))
+    }
+
     async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "binance_swap:该交易所方法未实现".to_string())) }
 
     async fn set_dual_leverage(&mut self, _leverage: &str) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "binance_swap:该交易所方法未实现".to_string())) }

+ 8 - 0
standard/src/bitget_spot.rs

@@ -446,6 +446,14 @@ impl Platform for BitgetSpot {
         Err(Error::new(ErrorKind::NotFound, "bitget_spot:该交易所方法未实现".to_string()))
     }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "bitget_spot:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "bitget_spot:该交易所方法未实现".to_string()))
+    }
+
     async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
         Err(Error::new(ErrorKind::NotFound, "bitget_spot:该交易所方法未实现".to_string()))
     }

+ 8 - 0
standard/src/bybit_swap.rs

@@ -509,6 +509,14 @@ impl Platform for BybitSwap {
         }
     }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "bybit_swap:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "bybit_swap:该交易所方法未实现".to_string()))
+    }
+
     // 设置持仓模式
     async fn set_dual_mode(&mut self, _coin: &str, is_dual_mode: bool) -> Result<String, Error> {
         let coin_format = self.symbol_uppercase.clone();

+ 8 - 0
standard/src/gate_spot.rs

@@ -101,6 +101,14 @@ impl Platform for GateSpot {
 
     async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error> { Err(Error::new(ErrorKind::NotFound, "gate_spot:该交易所方法未实现".to_string())) }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "gate_spot:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "gate_spot:该交易所方法未实现".to_string()))
+    }
+
     async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "gate_spot:该交易所方法未实现".to_string())) }
 
     async fn set_dual_leverage(&mut self, _leverage: &str) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "gate_spot:该交易所方法未实现".to_string())) }

+ 8 - 0
standard/src/gate_swap.rs

@@ -483,6 +483,14 @@ impl Platform for GateSwap {
         }
     }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        todo!()
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        todo!()
+    }
+
     // 设置持仓模式
     async fn set_dual_mode(&mut self, coin: &str, is_dual_mode: bool) -> Result<String, Error> {
         let coin_format = coin.to_string().to_lowercase();

+ 8 - 0
standard/src/kucoin_spot.rs

@@ -599,6 +599,14 @@ impl Platform for KucoinSpot {
         }
     }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "kucoin_spot:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "kucoin_spot:该交易所方法未实现".to_string()))
+    }
+
     async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
         Err(Error::new(ErrorKind::NotFound, "kucoin_spot:该交易所方法未实现".to_string()))
     }

+ 8 - 0
standard/src/kucoin_swap.rs

@@ -514,6 +514,14 @@ impl Platform for KucoinSwap {
         }
     }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "kucoin_swap:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "kucoin_swap:该交易所方法未实现".to_string()))
+    }
+
     async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
         Err(Error::new(ErrorKind::NotFound, "kucoin_swap:该交易所方法未实现".to_string()))
     }

+ 8 - 0
standard/src/lib.rs

@@ -558,6 +558,14 @@ pub trait Platform {
     async fn cancel_orders(&mut self) -> Result<Vec<Order>, Error>;
     // 撤销所有订单
     async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error>;
+    /// 下一个止损单
+    /// - stop_price: 触发价
+    /// - price: 委托价,0就市价委托
+    /// - side: 止损哪个方向[long多单,short空单]
+    async fn take_stop_loss_order(&mut self, stop_price: Decimal, price: Decimal, side: &str) -> Result<String, Error>;
+    /// 撤销订单
+    /// - order_id: 需要撤销的id
+    async fn cancel_stop_loss_order(&mut self, order_id: &str) -> Result<String, Error>;
     // 设置持仓模式
     async fn set_dual_mode(&mut self, coin: &str, is_dual_mode: bool) -> Result<String, Error>;
     // 更新双持仓模式下杠杆

+ 8 - 0
standard/src/okx_swap.rs

@@ -891,6 +891,14 @@ impl Platform for OkxSwap {
         Err(Error::new(ErrorKind::NotFound, "okx_swap:该交易所方法未实现".to_string()))
     }
 
+    async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "okx_swap:该交易所方法未实现".to_string()))
+    }
+
+    async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<String, Error> {
+        Err(Error::new(ErrorKind::NotFound, "okx_swap:该交易所方法未实现".to_string()))
+    }
+
     async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
         let res_data = self.request.set_position_mode().await;
         if res_data.code == "200" {