Răsfoiți Sursa

止损单能挂出去了,其他规则明天再研究了。

skyfffire 1 an în urmă
părinte
comite
36c16f482b
4 a modificat fișierele cu 90 adăugiri și 23 ștergeri
  1. 1 1
      Cargo.toml
  2. 19 2
      exchanges/src/gate_swap_rest.rs
  3. 45 3
      exchanges/tests/gate_swap_test.rs
  4. 25 17
      standard/src/lib.rs

+ 1 - 1
Cargo.toml

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

+ 19 - 2
exchanges/src/gate_swap_rest.rs

@@ -222,7 +222,7 @@ impl GateSwapRest {
         data
     }
     //合约交易下单
-    pub async fn swap_order(&mut self, settle: String, params: serde_json::Value) -> ResponseData {
+    pub async fn swap_order(&mut self, settle: String, params: Value) -> ResponseData {
         let data = self.request("POST".to_string(),
                                 "/api/v4".to_string(),
                                 format!("/futures/{}/orders", settle),
@@ -231,6 +231,23 @@ impl GateSwapRest {
         ).await;
         data
     }
+    // 提交一个自动订单
+    pub async fn place_price_order(&mut self, settle: String, params: Value) -> ResponseData {
+        self.request("POST".to_string(),
+                     "/api/v4".to_string(),
+                     format!("/futures/{}/price_orders", settle),
+                     true,
+                     params.to_string()).await
+    }
+    // 撤销自动订单
+    pub async fn cancel_price_order(&mut self, settle: String, order_id: String) -> ResponseData {
+        self.request("DELETE".to_string(),
+                     "/api/v4".to_string(),
+                     format!("/futures/{}/price_orders/{}", settle, order_id),
+                     true,
+                     "{}".to_string(),
+        ).await
+    }
     //设置持仓模式
     pub async fn setting_dual_mode(&mut self, settle: String, dual_mode: bool) -> ResponseData {
         let params = serde_json::json!({
@@ -585,7 +602,7 @@ impl GateSwapRest {
                             error
                         }
                         Err(e) => {
-                            error!("解析错误:{:?}",e);
+                            error!("gate_swap_rest, 解析错误:{:?}", e);
                             let error = ResponseData::error("".to_string(), format!("json 解析失败:{},相关参数:{}", e, message));
                             error
                         }

+ 45 - 3
exchanges/tests/gate_swap_test.rs

@@ -3,14 +3,15 @@ use std::sync::Arc;
 use std::sync::atomic::AtomicBool;
 
 use futures_util::StreamExt;
+use serde_json::json;
 use tokio::sync::Mutex;
-use tracing::trace;
+use tracing::{error, info, trace};
 
 use exchanges::gate_swap_rest::GateSwapRest;
 use exchanges::gate_swap_ws::{GateSwapLogin, GateSwapSubscribeType, GateSwapWs, GateSwapWsType};
 
-const ACCESS_KEY: &str = "";
-const SECRET_KEY: &str = "";
+const ACCESS_KEY: &str = "4181c882718a95e72122ac1d52c88533";
+const SECRET_KEY: &str = "de82d1507b843ff08d81a0e9b878b721359f274937216b307834b570b676fa3c";
 
 //ws-订阅公共频道信息
 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -99,6 +100,47 @@ async fn rest_cancel_order_all_test() {
     println!("okx--设置持仓模式--{:?}", req_data);
 }
 
+//rest-下一个自动单
+#[tokio::test]
+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 response_data = rest.place_price_order("usdt".to_string(), params).await;
+    if response_data.code == "200" {
+        let response_obj: serde_json::Value = serde_json::from_str(response_data.data.as_str()).unwrap();
+
+        info!("resp={:?}", response_obj.as_object().unwrap());
+    } else {
+        error!(?response_data);
+    }
+}
 
 //rest-查询合约账户变更历史
 #[tokio::test]

+ 25 - 17
standard/src/lib.rs

@@ -51,13 +51,15 @@ pub enum PositionModeEnum {
 /// - `limits_close(HashMap<String, Vec<String>>)`: 可用交易币数量 `{"order_name": [c_id, o_id]}`;
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub struct OrderCommand {
-    // 取消订单指令,数据结构例子:
+    // 取消订单指令
     pub cancel: HashMap<String, Vec<String>>,
-    // 检验指令,数据结构例子:(暂没找到例子)
+    // 检验指令
     pub check: HashMap<String, Vec<String>>,
-    // 限开指令,数据结构例子:(暂没找到例子)
+    // 限价开仓单
     pub limits_open: HashMap<String, Vec<String>>,
-    // 限收指令,数据结构例子:(暂没找到例子)
+    // 止损单
+    pub stop_loss: HashMap<String, Vec<String>>,
+    // 平仓订单
     pub limits_close: HashMap<String, Vec<String>>,
 }
 
@@ -67,6 +69,7 @@ impl OrderCommand {
             cancel: Default::default(),
             check: Default::default(),
             limits_open: Default::default(),
+            stop_loss: Default::default(),
             limits_close: Default::default(),
         }
     }
@@ -426,8 +429,8 @@ impl Position {
 /// let mut params:BTreeMap<String,String> = BTreeMap::new();
 /// params.insert("access_key".to_string(), "your_access_key".to_string());
 /// params.insert("access_key".to_string(), "your_secret_key".to_string());
-/// let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
 /// ```
+/// let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
 /// 获取当前交易所交易模式
 /// - fn get_self_exchange(&self) -> ExchangeEnum;
 /// ```rust
@@ -436,10 +439,10 @@ impl Position {
 /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
 /// # params.insert("access_key".to_string(), "your_access_key".to_string());
 /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// ```
 /// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
-///
 /// exchange.get_self_exchange();
-/// ```
+///
 /// 获取当前是否使用高速模式
 /// - fn get_self_is_colo(&self) -> bool;
 /// ```rust
@@ -448,10 +451,11 @@ impl Position {
 /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
 /// # params.insert("access_key".to_string(), "your_access_key".to_string());
 /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// ```
 /// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
-///
 /// exchange.get_self_is_colo();
-/// ```
+///
+///
 /// 获取当前是否使用登录
 /// - fn get_self_is_login(&self) -> bool;
 /// ```rust
@@ -460,10 +464,11 @@ impl Position {
 /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
 /// # params.insert("access_key".to_string(), "your_access_key".to_string());
 /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// ```
 /// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
-///
 /// exchange.get_self_is_login();
-/// ```
+///
+///
 /// 获取登录params信息
 /// - fn get_self_params(&self) -> BTreeMap<String, String>;
 /// ```rust
@@ -472,10 +477,11 @@ impl Position {
 /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
 /// # params.insert("access_key".to_string(), "your_access_key".to_string());
 /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// ```
 /// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
-///
 /// exchange.get_self_params();
-/// ```
+///
+///
 /// 获取账号信息
 /// - async fn get_account(&self, symbol: &str) -> Result<Account, Error>;
 /// ```rust
@@ -484,10 +490,11 @@ impl Position {
 /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
 /// # params.insert("access_key".to_string(), "your_access_key".to_string());
 /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// ```
 /// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
-///
 /// exchange.get_account()
-/// ```
+///
+///
 /// 订阅账号信息
 /// ```rust
 /// # use std::collections::BTreeMap;
@@ -495,10 +502,11 @@ impl Position {
 /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
 /// # params.insert("access_key".to_string(), "your_access_key".to_string());
 /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// ```
 /// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
-///
 /// exchange.subscribe_account();
-/// ```
+
+
 #[async_trait]
 pub trait Platform {
     fn clone_box(&self) -> Box<dyn Platform + Send + Sync>;