|
|
@@ -1,4 +1,7 @@
|
|
|
use std::collections::BTreeMap;
|
|
|
+use std::io::Error;
|
|
|
+use async_trait::async_trait;
|
|
|
+use rust_decimal::Decimal;
|
|
|
use crate::exchange::ExchangeEnum;
|
|
|
|
|
|
// 引入exchange模块
|
|
|
@@ -12,54 +15,122 @@ mod gate_spot;
|
|
|
// 引入工具模块
|
|
|
pub mod utils;
|
|
|
|
|
|
-/// Account结构体
|
|
|
-/// - `balance(f64)`: 可用计价币数量;
|
|
|
-/// - `frozen_balance(f64)`: balance挂单的冻结数量
|
|
|
-/// - `stocks(f64)`: 可用交易币数量
|
|
|
-/// - `frozen_stocks(f64)`: stocks挂单的冻结数量
|
|
|
+/// Account结构体(账户信息)
|
|
|
+/// - `balance(Decimal)`: 可用计价币数量;
|
|
|
+/// - `frozen_balance(Decimal)`: balance挂单的冻结数量
|
|
|
+/// - `stocks(Decimal)`: 可用交易币数量
|
|
|
+/// - `frozen_stocks(Decimal)`: stocks挂单的冻结数量
|
|
|
#[derive(Debug)]
|
|
|
pub struct Account {
|
|
|
- pub balance: f64,
|
|
|
- pub frozen_balance: f64,
|
|
|
- pub stocks: f64,
|
|
|
- pub frozen_stocks: f64,
|
|
|
+ pub balance: Decimal,
|
|
|
+ pub frozen_balance: Decimal,
|
|
|
+ pub stocks: Decimal,
|
|
|
+ pub frozen_stocks: Decimal,
|
|
|
}
|
|
|
|
|
|
-/// Depth结构体
|
|
|
+/// Depth结构体(市场深度)
|
|
|
/// - `time(i64)`: 深度更新时间戳(ms);
|
|
|
-/// - `asks(Vec<DepthItem>)`: 卖方深度列表;
|
|
|
-/// - `bids(Vec<DepthItem>)`: 买方深度列表;
|
|
|
+/// - `asks(Vec<MarketOrder>)`: 卖方深度列表;
|
|
|
+/// - `bids(Vec<MarketOrder>)`: 买方深度列表;
|
|
|
#[derive(Debug)]
|
|
|
pub struct Depth {
|
|
|
pub time: i64,
|
|
|
- pub asks: Vec<DepthItem>,
|
|
|
- pub bids: Vec<DepthItem>,
|
|
|
+ pub asks: Vec<MarketOrder>,
|
|
|
+ pub bids: Vec<MarketOrder>,
|
|
|
}
|
|
|
|
|
|
-/// DepthItem结构体
|
|
|
-/// - `price(f64)`: 价格
|
|
|
-/// - `amount(f64)`: 数量
|
|
|
+/// MarketOrder结构体(市场深度单)
|
|
|
+/// - `price(Decimal)`: 价格
|
|
|
+/// - `amount(Decimal)`: 数量
|
|
|
#[derive(Debug)]
|
|
|
-pub struct DepthItem {
|
|
|
- pub price: f64,
|
|
|
- pub amount: f64,
|
|
|
+pub struct MarketOrder {
|
|
|
+ pub price: Decimal,
|
|
|
+ pub amount: Decimal,
|
|
|
}
|
|
|
|
|
|
-/// Record结构体
|
|
|
+/// Record结构体(标准的OHLC结构)
|
|
|
/// - `time(i64)`: 时间戳;
|
|
|
-/// - `open(f64)`: 开盘价;
|
|
|
-/// - `high(f64)`: 最高价;
|
|
|
-/// - `low(f64):` 最低价;
|
|
|
-/// - `close(f64)`: 收盘价;
|
|
|
-/// - `volume(f64)`: 交易量;
|
|
|
+/// - `open(Decimal)`: 开盘价;
|
|
|
+/// - `high(Decimal)`: 最高价;
|
|
|
+/// - `low(Decimal):` 最低价;
|
|
|
+/// - `close(Decimal)`: 收盘价;
|
|
|
+/// - `volume(Decimal)`: 交易量;
|
|
|
#[derive(Debug)]
|
|
|
pub struct Record {
|
|
|
pub time: i64,
|
|
|
- pub open: f64,
|
|
|
- pub high: f64,
|
|
|
- pub low: f64,
|
|
|
- pub close: f64,
|
|
|
- pub volume: f64,
|
|
|
+ pub open: Decimal,
|
|
|
+ pub high: Decimal,
|
|
|
+ pub low: Decimal,
|
|
|
+ pub close: Decimal,
|
|
|
+ pub volume: Decimal,
|
|
|
+}
|
|
|
+
|
|
|
+/// Order结构体(订单)
|
|
|
+/// - `id(String)`: 交易单唯一标识
|
|
|
+/// - `custom_id(String)`: 自定义Id
|
|
|
+/// - `price(Decimal)`: 下单价格
|
|
|
+/// - `amount(Decimal)`: 下单数量
|
|
|
+/// - `deal_amount(Decimal)`: 成交数量
|
|
|
+/// - `avg_price(Decimal)`: 成交均价
|
|
|
+/// - `status(String)`: 订单状态
|
|
|
+/// - `order_type(String)`: 订单类型
|
|
|
+#[derive(Debug)]
|
|
|
+pub struct Order {
|
|
|
+ pub id: String,
|
|
|
+ pub custom_id: String,
|
|
|
+ pub price: Decimal,
|
|
|
+ pub amount: Decimal,
|
|
|
+ pub deal_amount: Decimal,
|
|
|
+ pub avg_price: Decimal,
|
|
|
+ pub status: String,
|
|
|
+ pub order_type: String,
|
|
|
+}
|
|
|
+
|
|
|
+/// Ticker结构体(市场行情)
|
|
|
+/// - `time(i64)`: 毫秒级别时间戳
|
|
|
+/// - `high(Decimal)`: 最高价
|
|
|
+/// - `low(Decimal)`: 最低价
|
|
|
+/// - `sell(Decimal)`: 卖一价
|
|
|
+/// - `buy(Decimal)`: 买一价
|
|
|
+/// - `last(Decimal)`: 最后成交价
|
|
|
+/// - `volume(Decimal)`: 最近成交量
|
|
|
+pub struct Ticker {
|
|
|
+ pub time: i64,
|
|
|
+ pub high: Decimal,
|
|
|
+ pub low: Decimal,
|
|
|
+ pub sell: Decimal,
|
|
|
+ pub buy: Decimal,
|
|
|
+ pub last: Decimal,
|
|
|
+ pub volume: Decimal,
|
|
|
+}
|
|
|
+
|
|
|
+/// Market结构体(交易品种的市场信息)
|
|
|
+/// - `symbol(String)`: 交易对
|
|
|
+/// - `base_asset(String)`: 交易币
|
|
|
+/// - `quote_asset(String)`: 计价币
|
|
|
+/// - `tick_size(Decimal)`: 价格最小变动数值
|
|
|
+/// - `amount_size(Decimal)`: 下单量最小变动数值
|
|
|
+/// - `price_precision(Decimal)`: 价格精度
|
|
|
+/// - `amount_precision(Decimal)`: 下单量精度
|
|
|
+/// - `min_qty(Decimal)`: 最小下单量
|
|
|
+/// - `max_qty(Decimal)`: 最大下单量
|
|
|
+/// - `min_notional(Decimal)`: 最小下单金额
|
|
|
+/// - `max_notional(Decimal)`: 最大下单金额
|
|
|
+/// - `ct_val(Decimal)`: 合约价值
|
|
|
+#[derive(Debug)]
|
|
|
+pub struct Market {
|
|
|
+ pub symbol: String,
|
|
|
+ pub base_asset: String,
|
|
|
+ pub quote_asset: String,
|
|
|
+ pub tick_size: Decimal,
|
|
|
+ pub amount_size: Decimal,
|
|
|
+ pub price_precision: Decimal,
|
|
|
+ pub amount_precision: Decimal,
|
|
|
+ pub min_qty: Decimal,
|
|
|
+ pub max_qty: Decimal,
|
|
|
+ pub min_notional: Decimal,
|
|
|
+ pub max_notional: Decimal,
|
|
|
+ pub ct_val: Decimal,
|
|
|
}
|
|
|
|
|
|
/// 交易所统一方法接口
|
|
|
@@ -74,8 +145,44 @@ pub struct Record {
|
|
|
/// params.insert("access_key".to_string(), "your_secret_key".to_string());
|
|
|
/// let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
|
|
|
/// ```
|
|
|
+/// 获取当前交易所交易模式
|
|
|
+/// - fn get_self_exchange(&self) -> ExchangeEnum;
|
|
|
+/// ```rust
|
|
|
+/// # use std::collections::BTreeMap;
|
|
|
+/// # 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_self_exchange();
|
|
|
+/// ```
|
|
|
+/// 获取当前是否使用高速模式
|
|
|
+/// - fn get_self_is_colo(&self) -> bool;
|
|
|
+/// ```rust
|
|
|
+/// # use std::collections::BTreeMap;
|
|
|
+/// # 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_self_is_colo();
|
|
|
+/// ```
|
|
|
+/// 获取当前是否使用登录
|
|
|
+/// - fn get_self_is_login(&self) -> bool;
|
|
|
+/// ```rust
|
|
|
+/// # use std::collections::BTreeMap;
|
|
|
+/// # 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_self_is_login();
|
|
|
+/// ```
|
|
|
/// 获取登录params信息
|
|
|
-/// - fn get_params_info(&self) -> BTreeMap<String,String>;
|
|
|
+/// - fn get_self_params(&self) -> BTreeMap<String, String>;
|
|
|
/// ```rust
|
|
|
/// # use std::collections::BTreeMap;
|
|
|
/// # use standard::exchange::{Exchange, ExchangeEnum};
|
|
|
@@ -84,9 +191,10 @@ pub struct Record {
|
|
|
/// # params.insert("access_key".to_string(), "your_secret_key".to_string());
|
|
|
/// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
|
|
|
///
|
|
|
-/// exchange.get_params_info();
|
|
|
+/// exchange.get_self_params();
|
|
|
/// ```
|
|
|
/// 获取账号信息
|
|
|
+/// - async fn get_account(&self, symbol: &str) -> Result<Account, Error>;
|
|
|
/// ```rust
|
|
|
/// # use std::collections::BTreeMap;
|
|
|
/// # use standard::exchange::{Exchange, ExchangeEnum};
|
|
|
@@ -95,7 +203,7 @@ pub struct Record {
|
|
|
/// # params.insert("access_key".to_string(), "your_secret_key".to_string());
|
|
|
/// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
|
|
|
///
|
|
|
-/// exchange.get_account("BTC_USDT".to_string());
|
|
|
+/// exchange.get_account("BTC_USDT");
|
|
|
/// ```
|
|
|
/// 订阅账号信息
|
|
|
/// ```rust
|
|
|
@@ -108,6 +216,7 @@ pub struct Record {
|
|
|
///
|
|
|
/// exchange.subscribe_account();
|
|
|
/// ```
|
|
|
+#[async_trait]
|
|
|
pub trait Platform {
|
|
|
// 获取当前交易所交易模式
|
|
|
fn get_self_exchange(&self) -> ExchangeEnum;
|
|
|
@@ -118,7 +227,7 @@ pub trait Platform {
|
|
|
// 获取登录params信息
|
|
|
fn get_self_params(&self) -> BTreeMap<String, String>;
|
|
|
// 获取账号信息
|
|
|
- fn get_account(&self, symbol: &str) -> Account;
|
|
|
+ async fn get_account(&self, symbol: &str) -> Result<Account, Error>;
|
|
|
// 获取深度信息
|
|
|
fn get_depth(&self) -> Depth;
|
|
|
// 下单接口
|