|
|
@@ -104,8 +104,23 @@ impl Exchange {
|
|
|
let req_data = self.binance_exc.binance_depth(&real_symbol, &limit.to_string()).await;
|
|
|
let depth_json_str = req_data.data;
|
|
|
let depth: serde_json::Value = serde_json::from_str(&*depth_json_str).unwrap();
|
|
|
- let depth_asks: Vec<DepthItem> = parse_depth_items(&depth["asks"]);
|
|
|
- let depth_bids: Vec<DepthItem> = parse_depth_items(&depth["bids"]);
|
|
|
+ let mut depth_asks: Vec<DepthItem> = vec![];
|
|
|
+ for value in depth["asks"].as_array().unwrap() {
|
|
|
+ let asks_item = DepthItem {
|
|
|
+ price: value[0].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ amount: value[1].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ };
|
|
|
+ depth_asks.push(asks_item)
|
|
|
+ }
|
|
|
+ let mut depth_bids: Vec<DepthItem> = vec![];
|
|
|
+ for value in depth["bids"].as_array().unwrap() {
|
|
|
+ let bids_item = DepthItem {
|
|
|
+ price: value[0].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ amount: value[1].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ };
|
|
|
+ depth_bids.push(bids_item)
|
|
|
+ }
|
|
|
+
|
|
|
let result = Depth {
|
|
|
asks: depth_asks,
|
|
|
bids: depth_bids,
|
|
|
@@ -120,17 +135,17 @@ impl Exchange {
|
|
|
pub async fn get_binance_klines(&self, symbol: &String, interval: &String, limit: &i32) -> Vec<Record> {
|
|
|
let real_symbol = self.get_real_symbol(symbol, "".to_string());
|
|
|
let req_data = self.binance_exc.binance_k(&real_symbol, interval, &limit.to_string()).await;
|
|
|
- let req_data_str = req_data.data;
|
|
|
- let req_data_json: Vec<Vec<serde_json::Value>> = serde_json::from_str(&*req_data_str).unwrap();
|
|
|
+ let klines_json_str = req_data.data;
|
|
|
+ let klines: Vec<Vec<serde_json::Value>> = serde_json::from_str(&*klines_json_str).unwrap();
|
|
|
let mut result: Vec<Record> = vec![];
|
|
|
- for item in req_data_json.iter() {
|
|
|
+ for i in 0..klines.len() {
|
|
|
let record = Record {
|
|
|
- time: item[0].as_i64().unwrap_or(0),
|
|
|
- open: item[1].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- high: item[2].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- low: item[3].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- close: item[4].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- volume: item[5].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ time: klines[i][0].as_i64().unwrap_or(0),
|
|
|
+ open: klines[i][1].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ high: klines[i][2].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ low: klines[i][3].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ close: klines[i][4].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ volume: klines[i][5].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
};
|
|
|
result.push(record);
|
|
|
}
|
|
|
@@ -141,11 +156,11 @@ impl Exchange {
|
|
|
// symbol: 交易币对, "BTC_USDT"
|
|
|
pub async fn get_okx_account(&self, symbol: &String) -> Account {
|
|
|
let real_symbol = self.get_real_symbol(symbol, ",".to_string());
|
|
|
- let req_data = self.okx_exc.okx_acc(&real_symbol).await;
|
|
|
- let req_data_str = req_data.data;
|
|
|
- let req_data_json: Vec<serde_json::Value> = serde_json::from_str(&*req_data_str).unwrap();
|
|
|
let symbol_array: Vec<&str> = symbol.split("_").collect();
|
|
|
- let details = req_data_json[0]["details"].as_array().unwrap();
|
|
|
+ let req_data = self.okx_exc.okx_acc(&real_symbol).await;
|
|
|
+ let account_json_str = req_data.data;
|
|
|
+ let account: Vec<serde_json::Value> = serde_json::from_str(&*account_json_str).unwrap();
|
|
|
+ let details = account[0]["details"].as_array().unwrap();
|
|
|
let default_info = json!({"availBal":"0","fixedBal":"0"});
|
|
|
let stocks_info = details.iter().find(|item| item["ccy"].as_str().unwrap() == symbol_array[0].to_string()).unwrap_or(&default_info);
|
|
|
let balance_info = details.iter().find(|item| item["ccy"].as_str().unwrap() == symbol_array[1].to_string()).unwrap_or(&default_info);
|
|
|
@@ -167,9 +182,9 @@ impl Exchange {
|
|
|
pub async fn place_okx_order(&self, symbol: &String, side: &String, order_type: &String, price: &String, amount: &String) -> String {
|
|
|
let real_symbol = self.get_real_symbol(symbol, "-".to_string());
|
|
|
let req_data = self.okx_exc.okx_order(&real_symbol, "cash", side, order_type, price, amount).await;
|
|
|
- let req_data_str = req_data.data;
|
|
|
- let req_data_json: Vec<serde_json::Value> = serde_json::from_str(&*req_data_str).unwrap();
|
|
|
- let result = req_data_json[0]["ordId"].as_str().unwrap().parse().unwrap();
|
|
|
+ let order_json_str = req_data.data;
|
|
|
+ let result_info: Vec<serde_json::Value> = serde_json::from_str(&*order_json_str).unwrap();
|
|
|
+ let result = result_info[0]["ordId"].as_str().unwrap().parse().unwrap();
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -179,17 +194,18 @@ impl Exchange {
|
|
|
pub async fn get_okx_order(&self, symbol: &String, order_id: &String) -> Order {
|
|
|
let real_symbol = self.get_real_symbol(symbol, "-".to_string());
|
|
|
let req_data = self.okx_exc.okx_get_order(&real_symbol, order_id).await;
|
|
|
- let req_data_str = req_data.data;
|
|
|
- let req_data_json: Vec<serde_json::Value> = serde_json::from_str(&*req_data_str).unwrap();
|
|
|
+ let order_json_str = req_data.data;
|
|
|
+ let result_info: Vec<serde_json::Value> = serde_json::from_str(&*order_json_str).unwrap();
|
|
|
let result = Order {
|
|
|
- id: req_data_json[0]["ordId"].as_str().unwrap().parse().unwrap(),
|
|
|
- price: req_data_json[0]["px"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- amount: req_data_json[0]["sz"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- deal_amount: req_data_json[0]["accFillSz"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- avg_price: req_data_json[0]["avgPx"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- status: req_data_json[0]["state"].as_str().unwrap().parse().unwrap(),
|
|
|
- order_type: req_data_json[0]["instType"].as_str().unwrap().parse().unwrap(),
|
|
|
+ id: result_info[0]["ordId"].as_str().unwrap().parse().unwrap(),
|
|
|
+ price: result_info[0]["px"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ amount: result_info[0]["sz"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ deal_amount: result_info[0]["accFillSz"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ avg_price: result_info[0]["avgPx"].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
+ status: result_info[0]["state"].as_str().unwrap().parse().unwrap(),
|
|
|
+ order_type: result_info[0]["instType"].as_str().unwrap().parse().unwrap(),
|
|
|
};
|
|
|
+ println!("{:?}", result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -199,25 +215,14 @@ impl Exchange {
|
|
|
pub async fn cancel_okx_order(&self, symbol: &String, order_id: &String) -> bool {
|
|
|
let real_symbol = self.get_real_symbol(symbol, "-".to_string());
|
|
|
let req_data = self.okx_exc.okx_revocation_order(&real_symbol, order_id).await;
|
|
|
- let req_data_str = req_data.data;
|
|
|
- let req_data_json: Vec<serde_json::Value> = serde_json::from_str(&*req_data_str).unwrap();
|
|
|
- let order_info = req_data_json[0]["sCode"].as_str().unwrap();
|
|
|
- let result = if order_info == "0" { true } else { false };
|
|
|
+ let order_json_str = req_data.data;
|
|
|
+ let order_info: Vec<serde_json::Value> = serde_json::from_str(&*order_json_str).unwrap();
|
|
|
+ let result_info = order_info[0]["sCode"].as_str().unwrap();
|
|
|
+ let result = if result_info == "0" { true } else { false };
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// 深度信息买单/卖单处理
|
|
|
-fn parse_depth_items(value:&serde_json::Value) -> Vec<DepthItem> {
|
|
|
- let mut depth_items: Vec<DepthItem> = vec![];
|
|
|
- for value in value.as_array().unwrap(){
|
|
|
- depth_items.push(DepthItem {
|
|
|
- price: value[0].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- amount: value[1].as_str().unwrap_or("0").parse().unwrap_or(0.0),
|
|
|
- })
|
|
|
- }
|
|
|
- return depth_items;
|
|
|
-}
|
|
|
|
|
|
// 单元测试集
|
|
|
#[cfg(test)]
|