|
|
@@ -1,14 +1,12 @@
|
|
|
-use std::cmp::Ordering;
|
|
|
use std::collections::BTreeMap;
|
|
|
use std::io::{Error, ErrorKind};
|
|
|
use std::result::Result;
|
|
|
use async_trait::async_trait;
|
|
|
+use rust_decimal::Decimal;
|
|
|
use rust_decimal_macros::dec;
|
|
|
use serde_json::json;
|
|
|
-use crate::{Platform, ExchangeEnum, Account, Depth, MarketOrder, Position, PositionModeEnum, utils, Ticker, Market, Order};
|
|
|
+use crate::{Platform, ExchangeEnum, Account, Position, PositionModeEnum, Ticker, Market, Order};
|
|
|
use exchanges::binance_usdt_swap_rest::BinanceUsdtSwapRest;
|
|
|
-use exchanges::binance_usdt_swap_ws::BinanceUsdtSwapWs;
|
|
|
-use exchanges::response_base::ResponseData;
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub struct BinanceSwap {
|
|
|
@@ -17,7 +15,6 @@ pub struct BinanceSwap {
|
|
|
is_login: bool,
|
|
|
params: BTreeMap<String, String>,
|
|
|
request: BinanceUsdtSwapRest,
|
|
|
- wss: BinanceUsdtSwapWs,
|
|
|
}
|
|
|
|
|
|
impl BinanceSwap {
|
|
|
@@ -28,7 +25,6 @@ impl BinanceSwap {
|
|
|
is_login,
|
|
|
params: params.clone(),
|
|
|
request: BinanceUsdtSwapRest::new(is_colo, is_login, params.clone()),
|
|
|
- wss: BinanceUsdtSwapWs::new(is_colo, is_login, params.clone()),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -98,113 +94,129 @@ impl Platform for BinanceSwap {
|
|
|
todo!()
|
|
|
}
|
|
|
|
|
|
- // 获取深度信息
|
|
|
- fn get_depth(&self) -> Depth {
|
|
|
- let depth_asks = vec![MarketOrder { price: dec!(1.0), amount: dec!(0.0) }];
|
|
|
- let depth_bids = vec![MarketOrder { price: dec!(0.0), amount: dec!(0.0) }];
|
|
|
-
|
|
|
- Depth {
|
|
|
- time: 0,
|
|
|
- asks: depth_asks,
|
|
|
- bids: depth_bids,
|
|
|
- }
|
|
|
+ async fn set_dual_mode(&self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
|
|
|
+ todo!()
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- fn take_order(&self) {
|
|
|
+ async fn set_dual_leverage(&self, _symbol: &str, _leverage: &str) -> Result<String, Error> {
|
|
|
todo!()
|
|
|
}
|
|
|
|
|
|
- fn get_order_list(&self) {
|
|
|
+ async fn wallet_transfers(&self, _coin: &str, _from: &str, _to: &str, _amount: Decimal) -> Result<String, Error> {
|
|
|
todo!()
|
|
|
}
|
|
|
|
|
|
- fn cancel_order(&self) {
|
|
|
+ async fn take_order(&self, _custom_id: &str, _origin_side: &str, _coin: &str, _symbol: &str, _amount: Decimal) -> Result<Order, Error> {
|
|
|
todo!()
|
|
|
}
|
|
|
|
|
|
- fn subscribe_account(&self) -> Account {
|
|
|
- Account {
|
|
|
- balance: dec!(1.1),
|
|
|
- available_balance: dec!(0.0),
|
|
|
- frozen_balance: dec!(0.0),
|
|
|
- stocks: dec!(0.0),
|
|
|
- available_stocks: dec!(0.0),
|
|
|
- frozen_stocks: dec!(0.0),
|
|
|
- }
|
|
|
- }
|
|
|
// 订阅深度信息
|
|
|
- fn subscribe_depth(&self, symbol: &str) {
|
|
|
- ;
|
|
|
- let symbol_format = &utils::format_symbol(symbol, "");
|
|
|
- let get_res_data = move |res_data: ResponseData| {
|
|
|
- async move {
|
|
|
- if res_data.code == "200" && res_data.channel == "depth" {
|
|
|
- let res_data_str = res_data.data;
|
|
|
- let res_data_json: serde_json::Value = serde_json::from_str(&*res_data_str).unwrap();
|
|
|
- let mut depth_asks: Vec<MarketOrder> = parse_depth_items(&res_data_json["asks"]);
|
|
|
- let mut depth_bids: Vec<MarketOrder> = parse_depth_items(&res_data_json["bids"]);
|
|
|
- depth_asks.sort_by(|a, b| a.price.partial_cmp(&b.price).unwrap_or(Ordering::Equal));
|
|
|
- depth_bids.sort_by(|a, b| b.price.partial_cmp(&a.price).unwrap_or(Ordering::Equal));
|
|
|
- let result = Depth {
|
|
|
- time: 123456,
|
|
|
- asks: depth_asks,
|
|
|
- bids: depth_bids,
|
|
|
- };
|
|
|
- println!("subscribe_depth: {:?}", result);
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- self.wss.custom_subscribe(vec![symbol_format], 0, 0, get_res_data)
|
|
|
- }
|
|
|
+ // fn subscribe_depth(&self, symbol: &str) {
|
|
|
+ // let symbol_format = &utils::format_symbol(symbol, "");
|
|
|
+ // let get_res_data = move |res_data: ResponseData| {
|
|
|
+ // async move {
|
|
|
+ // if res_data.code == "200" && res_data.channel == "depth" {
|
|
|
+ // let res_data_str = res_data.data;
|
|
|
+ // let res_data_json: serde_json::Value = serde_json::from_str(&*res_data_str).unwrap();
|
|
|
+ // let mut depth_asks: Vec<MarketOrder> = parse_depth_items(&res_data_json["asks"]);
|
|
|
+ // let mut depth_bids: Vec<MarketOrder> = parse_depth_items(&res_data_json["bids"]);
|
|
|
+ // depth_asks.sort_by(|a, b| a.price.partial_cmp(&b.price).unwrap_or(Ordering::Equal));
|
|
|
+ // depth_bids.sort_by(|a, b| b.price.partial_cmp(&a.price).unwrap_or(Ordering::Equal));
|
|
|
+ // let result = Depth {
|
|
|
+ // time: 123456,
|
|
|
+ // asks: depth_asks,
|
|
|
+ // bids: depth_bids,
|
|
|
+ // };
|
|
|
+ // println!("subscribe_depth: {:?}", result);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+ // self.wss.custom_subscribe(vec![symbol_format], 0, 0, get_res_data)
|
|
|
+ // }
|
|
|
|
|
|
// 订阅深度信息
|
|
|
- fn subscribe_special_depth(&self, symbol: &str) {
|
|
|
- let symbol_format = &utils::format_symbol(symbol, "");
|
|
|
- let get_res_data = move |res_data: ResponseData| {
|
|
|
- async move {
|
|
|
- if res_data.code == "200" && res_data.channel == "depth" {
|
|
|
- let res_data_str = res_data.data;
|
|
|
- let res_data_json: serde_json::Value = serde_json::from_str(&*res_data_str).unwrap();
|
|
|
- let mut depth_asks: Vec<MarketOrder> = parse_depth_items(&res_data_json["asks"]);
|
|
|
- let mut depth_bids: Vec<MarketOrder> = parse_depth_items(&res_data_json["bids"]);
|
|
|
- depth_asks.sort_by(|a, b| a.price.partial_cmp(&b.price).unwrap_or(Ordering::Equal));
|
|
|
- depth_bids.sort_by(|a, b| b.price.partial_cmp(&a.price).unwrap_or(Ordering::Equal));
|
|
|
- let mp = (depth_asks[0].price + depth_bids[0].price) * dec!(0.5);
|
|
|
- // let step = round(mp * utils::EFF_RANGE / utils::LEVEL, self.decimal);
|
|
|
- // TODO 引入有问题
|
|
|
-
|
|
|
-
|
|
|
- let result = Depth {
|
|
|
- time: 123456,
|
|
|
- asks: depth_asks,
|
|
|
- bids: depth_bids,
|
|
|
- };
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- self.wss.custom_subscribe(vec![symbol_format], 0, 0, get_res_data)
|
|
|
- }
|
|
|
-
|
|
|
- fn subscribe_kline(&self) {
|
|
|
- let get_res_data = move |res_data: ResponseData| {
|
|
|
- async move {
|
|
|
- println!("?????{:?}", res_data);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- self.wss.kline(vec![&"BTCUSDT"], get_res_data);
|
|
|
- }
|
|
|
+ // fn subscribe_special_depth(&self, symbol: &str) {
|
|
|
+ // let symbol_format = &utils::format_symbol(symbol, "");
|
|
|
+ // let get_res_data = move |res_data: ResponseData| {
|
|
|
+ // async move {
|
|
|
+ // if res_data.code == "200" && res_data.channel == "depth" {
|
|
|
+ // let res_data_str = res_data.data;
|
|
|
+ // let res_data_json: serde_json::Value = serde_json::from_str(&*res_data_str).unwrap();
|
|
|
+ // let mut depth_asks: Vec<MarketOrder> = parse_depth_items(&res_data_json["asks"]);
|
|
|
+ // let mut depth_bids: Vec<MarketOrder> = parse_depth_items(&res_data_json["bids"]);
|
|
|
+ // depth_asks.sort_by(|a, b| a.price.partial_cmp(&b.price).unwrap_or(Ordering::Equal));
|
|
|
+ // depth_bids.sort_by(|a, b| b.price.partial_cmp(&a.price).unwrap_or(Ordering::Equal));
|
|
|
+ // let mp = (depth_asks[0].price + depth_bids[0].price) * dec!(0.5);
|
|
|
+ // let step = (public_params::EFF_RANGE * mp / Decimal::from_usize(public_params::LEVEL).unwrap()).round();
|
|
|
+ // let mut ap = Vec::new();
|
|
|
+ // let mut bp = Vec::new();
|
|
|
+ // let mut av: Vec<Decimal> = Vec::new();
|
|
|
+ // let mut bv: Vec<Decimal> = Vec::new();
|
|
|
+ // for i in 0..public_params::LEVEL {
|
|
|
+ // let price = (depth_asks[0].price + step * Decimal::from_f64(i as f64).unwrap()).round();
|
|
|
+ // ap.push(price);
|
|
|
+ // }
|
|
|
+ // for i in 0..public_params::LEVEL {
|
|
|
+ // let price = (depth_asks[0].price - step * Decimal::from_f64(i as f64).unwrap()).round();
|
|
|
+ // bp.push(price);
|
|
|
+ // }
|
|
|
+ // let mut ap_price_tag = depth_asks[0].price + step;
|
|
|
+ // let mut ap_index = 0;
|
|
|
+ // for item in depth_asks.iter() {
|
|
|
+ // let price = item.price;
|
|
|
+ // let amount = item.amount;
|
|
|
+ // println!("{} {}", price, amount);
|
|
|
+ // if price > ap_price_tag {
|
|
|
+ // av[ap_index] += amount;
|
|
|
+ // } else {
|
|
|
+ // ap_price_tag += step;
|
|
|
+ // ap_index += 1;
|
|
|
+ // if ap_index == public_params::LEVEL {
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ // av[ap_index] += amount
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // let mut bp_price_tag = depth_bids[0].price - step;
|
|
|
+ // let mut bp_index = 0;
|
|
|
+ // for item in depth_bids.iter() {
|
|
|
+ // let price = item.price;
|
|
|
+ // let amount = item.amount;
|
|
|
+ // if price > bp_price_tag {
|
|
|
+ // bv[bp_index] += amount;
|
|
|
+ // } else {
|
|
|
+ // bp_price_tag -= step;
|
|
|
+ // bp_index += 1;
|
|
|
+ // if bp_index == public_params::LEVEL {
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ // bv[bp_index] += amount
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // let aa: Vec<&Decimal> = bp.iter().clone().chain(bv.iter().clone()).chain(ap.iter().clone()).chain(av.iter().clone()).collect::<Vec<_>>();
|
|
|
+ // println!("{:?}", aa);
|
|
|
+ // let result = Depth {
|
|
|
+ // time: 123456,
|
|
|
+ // asks: depth_asks,
|
|
|
+ // bids: depth_bids,
|
|
|
+ // };
|
|
|
+ // println!("subscribe_special_depth: {:?}", result)
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+ // self.wss.custom_subscribe(vec![symbol_format], 0, 0, get_res_data)
|
|
|
+ // }
|
|
|
}
|
|
|
|
|
|
-fn parse_depth_items(value: &serde_json::Value) -> Vec<MarketOrder> {
|
|
|
- let mut depth_items: Vec<MarketOrder> = vec![];
|
|
|
- for value in value.as_array().unwrap() {
|
|
|
- depth_items.push(MarketOrder {
|
|
|
- price: value[0].as_str().unwrap_or("0").parse().unwrap_or(dec!(0)),
|
|
|
- amount: value[1].as_str().unwrap_or("0").parse().unwrap_or(dec!(0)),
|
|
|
- })
|
|
|
- }
|
|
|
- return depth_items;
|
|
|
-}
|
|
|
+// fn parse_depth_items(value: &serde_json::Value) -> Vec<MarketOrder> {
|
|
|
+// let mut depth_items: Vec<MarketOrder> = vec![];
|
|
|
+// for value in value.as_array().unwrap() {
|
|
|
+// depth_items.push(MarketOrder {
|
|
|
+// price: value[0].as_str().unwrap_or("0").parse().unwrap_or(dec!(0)),
|
|
|
+// amount: value[1].as_str().unwrap_or("0").parse().unwrap_or(dec!(0)),
|
|
|
+// })
|
|
|
+// }
|
|
|
+// return depth_items;
|
|
|
+// }
|