Преглед на файлове

binance的rest获取k线

skyffire преди 8 месеца
родител
ревизия
c02fdd507d
променени са 2 файла, в които са добавени 41 реда и са изтрити 2 реда
  1. 10 0
      exchanges/src/binance_swap_rest.rs
  2. 31 2
      standard/src/binance_swap.rs

+ 10 - 0
exchanges/src/binance_swap_rest.rs

@@ -90,6 +90,16 @@ impl BinanceSwapRest {
         ).await;
         data
     }
+    // 获取k线
+    pub async fn get_records(&mut self, params: Value) -> ResponseData {
+        let data = self.request("GET".to_string(),
+                                "".to_string(),
+                                format!("/fapi/v1/klines"),
+                                false,
+                                params,
+        ).await;
+        data
+    }
 
 
     //账户信息

+ 31 - 2
standard/src/binance_swap.rs

@@ -193,8 +193,37 @@ impl Platform for BinanceSwap {
         }
     }
 
-    async fn get_record(&mut self, _interval: String) -> Result<Vec<Record>, Error> {
-        Err(Error::new(ErrorKind::NotFound, "binance_usdt_swap:该交易所方法未实现".to_string()))
+    async fn get_record(&mut self, interval: String) -> Result<Vec<Record>, Error> {
+        let symbol_format = utils::format_symbol(self.symbol.clone(), "");
+
+        let params = json!({
+            "symbol": symbol_format.clone(),
+            "interval": "USDT-FUTURES",
+            "granularity": interval,
+            "limit": "500"
+        });
+
+        let res_data = self.request.get_records(params).await;
+        if res_data.code == 200 {
+            let mut records: Vec<Record> = vec![];
+            info!("{}", res_data.data.to_string());
+            for record_value in res_data.data.as_array().unwrap() {
+                records.push(Record {
+                    time: Decimal::from_str(record_value[0].as_str().unwrap()).unwrap(),
+                    open: Decimal::from_str(record_value[1].as_str().unwrap()).unwrap(),
+                    high: Decimal::from_str(record_value[2].as_str().unwrap()).unwrap(),
+                    low: Decimal::from_str(record_value[3].as_str().unwrap()).unwrap(),
+                    close: Decimal::from_str(record_value[4].as_str().unwrap()).unwrap(),
+                    volume: Decimal::from_str(record_value[5].as_str().unwrap()).unwrap(),
+                    symbol: symbol_format.clone(),
+                });
+            }
+
+            records.reverse();
+            Ok(records)
+        } else {
+            Err(Error::new(ErrorKind::Other, res_data.to_string()))
+        }
     }
 
     async fn get_ticker_symbol(&mut self, symbol: String) -> Result<Ticker, Error> {