|
|
@@ -1,9 +1,8 @@
|
|
|
use std::collections::BTreeMap;
|
|
|
-use crate::binance_swap::BinanceSwap;
|
|
|
-use crate::binance_spot::BinanceSpot;
|
|
|
-use crate::gate_spot::GateSpot;
|
|
|
-use crate::gate_swap::GateSwap;
|
|
|
+use crate::exchange::ExchangeEnum;
|
|
|
|
|
|
+// 引入exchange模块
|
|
|
+pub mod exchange;
|
|
|
// 引入binance模块
|
|
|
mod binance_swap;
|
|
|
mod binance_spot;
|
|
|
@@ -13,12 +12,11 @@ mod gate_spot;
|
|
|
// 引入工具模块
|
|
|
pub mod utils;
|
|
|
|
|
|
-
|
|
|
/// Account结构体
|
|
|
-/// - balance(f64): 可用计价币数量;
|
|
|
-/// - frozen_balance(f64): balance挂单的冻结数量
|
|
|
-/// - stocks(f64): 可用交易币数量
|
|
|
-/// - frozen_stocks(f64): stocks挂单的冻结数量
|
|
|
+/// - `balance(f64)`: 可用计价币数量;
|
|
|
+/// - `frozen_balance(f64)`: balance挂单的冻结数量
|
|
|
+/// - `stocks(f64)`: 可用交易币数量
|
|
|
+/// - `frozen_stocks(f64)`: stocks挂单的冻结数量
|
|
|
#[derive(Debug)]
|
|
|
pub struct Account {
|
|
|
pub balance: f64,
|
|
|
@@ -27,12 +25,49 @@ pub struct Account {
|
|
|
pub frozen_stocks: f64,
|
|
|
}
|
|
|
|
|
|
+/// Depth结构体
|
|
|
+/// - `time(i64)`: 深度更新时间戳(ms);
|
|
|
+/// - `asks(Vec<DepthItem>)`: 卖方深度列表;
|
|
|
+/// - `bids(Vec<DepthItem>)`: 买方深度列表;
|
|
|
+#[derive(Debug)]
|
|
|
+pub struct Depth {
|
|
|
+ pub time: i64,
|
|
|
+ pub asks: Vec<DepthItem>,
|
|
|
+ pub bids: Vec<DepthItem>,
|
|
|
+}
|
|
|
+
|
|
|
+/// DepthItem结构体
|
|
|
+/// - `price(f64)`: 价格
|
|
|
+/// - `amount(f64)`: 数量
|
|
|
+#[derive(Debug)]
|
|
|
+pub struct DepthItem {
|
|
|
+ pub price: f64,
|
|
|
+ pub amount: f64,
|
|
|
+}
|
|
|
+
|
|
|
+/// Record结构体
|
|
|
+/// - `time(i64)`: 时间戳;
|
|
|
+/// - `open(f64)`: 开盘价;
|
|
|
+/// - `high(f64)`: 最高价;
|
|
|
+/// - `low(f64):` 最低价;
|
|
|
+/// - `close(f64)`: 收盘价;
|
|
|
+/// - `volume(f64)`: 交易量;
|
|
|
+#[derive(Debug)]
|
|
|
+pub struct Record {
|
|
|
+ pub time: i64,
|
|
|
+ pub open: f64,
|
|
|
+ pub high: f64,
|
|
|
+ pub low: f64,
|
|
|
+ pub close: f64,
|
|
|
+ pub volume: f64,
|
|
|
+}
|
|
|
+
|
|
|
/// 交易所统一方法接口
|
|
|
///
|
|
|
/// 使用方法前需实例化
|
|
|
/// ```rust
|
|
|
/// use std::collections::BTreeMap;
|
|
|
-/// use standard::{Exchange, ExchangeEnum};
|
|
|
+/// use standard::exchange::{Exchange, ExchangeEnum};
|
|
|
///
|
|
|
/// let mut params:BTreeMap<String,String> = BTreeMap::new();
|
|
|
/// params.insert("access_key".to_string(), "your_access_key".to_string());
|
|
|
@@ -43,7 +78,7 @@ pub struct Account {
|
|
|
/// - fn get_params_info(&self) -> BTreeMap<String,String>;
|
|
|
/// ```rust
|
|
|
/// # use std::collections::BTreeMap;
|
|
|
-/// # use standard::{Exchange, ExchangeEnum};
|
|
|
+/// # use standard::exchange::{Exchange, ExchangeEnum};
|
|
|
/// # 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());
|
|
|
@@ -54,18 +89,18 @@ pub struct Account {
|
|
|
/// 获取账号信息
|
|
|
/// ```rust
|
|
|
/// # use std::collections::BTreeMap;
|
|
|
-/// # use standard::{Exchange, ExchangeEnum};
|
|
|
+/// # use standard::exchange::{Exchange, ExchangeEnum};
|
|
|
/// # 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, true, true, params);
|
|
|
///
|
|
|
-/// exchange.get_account();
|
|
|
+/// exchange.get_account("BTC_USDT".to_string());
|
|
|
/// ```
|
|
|
/// 订阅账号信息
|
|
|
/// ```rust
|
|
|
/// # use std::collections::BTreeMap;
|
|
|
-/// # use standard::{Exchange, ExchangeEnum};
|
|
|
+/// # use standard::exchange::{Exchange, ExchangeEnum};
|
|
|
/// # 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());
|
|
|
@@ -74,77 +109,27 @@ pub struct Account {
|
|
|
/// exchange.subscribe_account();
|
|
|
/// ```
|
|
|
pub trait Platform {
|
|
|
- fn get_params_info(&self) -> BTreeMap<String, String>;
|
|
|
- fn get_account(&self) -> Account;
|
|
|
+ // 获取当前交易所交易模式
|
|
|
+ fn get_self_exchange(&self) -> ExchangeEnum;
|
|
|
+ // 获取当前是否使用高速模式
|
|
|
+ fn get_self_is_colo(&self) -> bool;
|
|
|
+ // 获取当前是否使用登录
|
|
|
+ fn get_self_is_login(&self) -> bool;
|
|
|
+ // 获取登录params信息
|
|
|
+ fn get_self_params(&self) -> BTreeMap<String, String>;
|
|
|
+ // 获取账号信息
|
|
|
+ fn get_account(&self, symbol: &str) -> Account;
|
|
|
+ // 获取深度信息
|
|
|
+ fn get_depth(&self) -> Depth;
|
|
|
// 下单接口
|
|
|
fn take_order(&self);
|
|
|
// 获取订单列表
|
|
|
fn get_order_list(&self);
|
|
|
// 取消订单
|
|
|
fn cancel_order(&self);
|
|
|
- fn subscribe_account(&self);
|
|
|
+ // 订阅账号信息
|
|
|
+ fn subscribe_account(&self) -> Account;
|
|
|
+ // 订阅深度信息
|
|
|
+ fn subscribe_depth(&self) -> Depth;
|
|
|
fn subscribe_kline(&self);
|
|
|
}
|
|
|
-
|
|
|
-/// 交易所交易模式枚举
|
|
|
-/// - `BinanceSwap`: Binance交易所;
|
|
|
-/// - `BinanceSpot`: Binance交易所;
|
|
|
-/// - `GateSwap`: Gate交易所;
|
|
|
-/// - `GateSpot`: Gate交易所;
|
|
|
-#[derive(Debug)]
|
|
|
-pub enum ExchangeEnum {
|
|
|
- BinanceSwap,
|
|
|
- BinanceSpot,
|
|
|
- GateSwap,
|
|
|
- GateSpot,
|
|
|
-}
|
|
|
-
|
|
|
-/// Exchange结构体
|
|
|
-///
|
|
|
-/// 方法:
|
|
|
-/// - 创建Exchange:
|
|
|
-///
|
|
|
-/// new(platform: [PlatformEnum], is_colo: bool, is_login:bool, params: BTreeMap<String, String>) -> Box\<dyn Platform\>
|
|
|
-/// - platform([PlatformEnum]): 交易所平台枚举
|
|
|
-/// - is_colo(bool): 是否开始告诉模式
|
|
|
-/// - is_login(bool): 是否需要登录
|
|
|
-/// - params(BTreeMap\<String, String\>): 登录所需参数
|
|
|
-///
|
|
|
-/// 示例参数值:
|
|
|
-///
|
|
|
-/// | 交易所枚举 | params参数示例 BTreeMap<String,String> |
|
|
|
-/// | --- | --- |
|
|
|
-/// | BinanceSwap | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
|
|
|
-/// | BinanceSpot | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
|
|
|
-/// | GateSwap | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
|
|
|
-/// | GateSpot | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
|
|
|
-/// ```rust
|
|
|
-/// use std::collections::BTreeMap;
|
|
|
-/// use standard::{Exchange, ExchangeEnum};
|
|
|
-///
|
|
|
-/// 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, true, true, params);
|
|
|
-pub struct Exchange {}
|
|
|
-
|
|
|
-impl Exchange {
|
|
|
- pub fn new(exchange: ExchangeEnum, is_colo: bool, is_login: bool, params: BTreeMap<String, String>) -> Box<dyn Platform> {
|
|
|
- match exchange {
|
|
|
- ExchangeEnum::BinanceSwap => {
|
|
|
- Box::new(BinanceSwap::new(is_colo, is_login, params))
|
|
|
- }
|
|
|
- ExchangeEnum::BinanceSpot => {
|
|
|
- Box::new(BinanceSpot::new(is_colo, is_login, params))
|
|
|
- }
|
|
|
- ExchangeEnum::GateSwap => {
|
|
|
- Box::new(GateSpot::new(is_colo, is_login, params))
|
|
|
- }
|
|
|
- ExchangeEnum::GateSpot => {
|
|
|
- Box::new(GateSwap::new(is_colo, is_login, params))
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|