lib.rs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. use std::collections::{BTreeMap, HashMap};
  2. use std::fmt;
  3. use std::fmt::Formatter;
  4. use std::io::{Error};
  5. use async_trait::async_trait;
  6. use rust_decimal::Decimal;
  7. use serde_json::Value;
  8. use serde::{Serialize, Deserialize};
  9. use tokio::time::Instant;
  10. use global::trace_stack::TraceStack;
  11. use crate::exchange::ExchangeEnum;
  12. // 引入工具模块
  13. pub mod utils;
  14. // 引入exchange模块
  15. pub mod exchange;
  16. pub mod exchange_struct_handler;
  17. // 引入binance模块
  18. mod binance_swap;
  19. mod binance_spot;
  20. pub mod binance_swap_handle;
  21. pub mod binance_spot_handle;
  22. // 引入gate模块
  23. mod gate_swap;
  24. mod gate_spot;
  25. pub mod gate_swap_handle;
  26. pub mod gate_spot_handle;
  27. mod kucoin_swap;
  28. mod kucoin_swap_handle;
  29. mod okx_swap;
  30. pub mod okx_swap_handle;
  31. mod bitget_spot;
  32. pub mod bitget_spot_handle;
  33. mod kucoin_spot;
  34. pub mod kucoin_spot_handle;
  35. mod bybit_swap;
  36. mod bybit_swap_handle;
  37. mod bitget_swap;
  38. mod bitget_swap_handle;
  39. mod coinex_swap;
  40. mod coinex_swap_handle;
  41. mod htx_swap_handle;
  42. mod htx_swap;
  43. mod bingx_swap;
  44. mod bingx_swap_handle;
  45. mod mexc_swap;
  46. mod mexc_swap_handle;
  47. mod bitmart_swap;
  48. mod bitmart_swap_handle;
  49. mod coinsph_swap;
  50. mod coinsph_swap_handle;
  51. mod phemex_swap;
  52. mod phemex_swap_handle;
  53. mod woo_swap;
  54. mod woo_swap_handle;
  55. mod cointr_swap;
  56. mod cointr_swap_handle;
  57. /// OrderCommand结构体(下单指令)
  58. /// - `cancel(HashMap<String, Vec<String>)`: 取消订单指令 `{"order_name": [c_id, o_id]}`;
  59. /// - `check(HashMap<String, Vec<String>>)`: balance挂单的冻结数量 `{"order_name": [数量,方向,价格,c_id]}`;
  60. /// - `limits_open(HashMap<String, Vec<String>>)`: 总计交易币数量 `{"order_name": [c_id, o_id]}`;
  61. /// - `limits_close(HashMap<String, Vec<String>>)`: 可用交易币数量 `{"order_name": [c_id, o_id]}`;
  62. #[derive(Debug, Clone, PartialEq, Eq)]
  63. pub struct OrderCommand {
  64. // 取消订单指令,数据结构例子:
  65. pub cancel: HashMap<String, Vec<String>>,
  66. // 检验指令,数据结构例子:(暂没找到例子)
  67. pub check: HashMap<String, Vec<String>>,
  68. // 限开指令,数据结构例子:(暂没找到例子)
  69. pub limits_open: HashMap<String, Vec<String>>,
  70. // 限收指令,数据结构例子:(暂没找到例子)
  71. pub limits_close: HashMap<String, Vec<String>>,
  72. }
  73. impl OrderCommand {
  74. pub fn new() -> OrderCommand {
  75. OrderCommand {
  76. cancel: Default::default(),
  77. check: Default::default(),
  78. limits_open: Default::default(),
  79. limits_close: Default::default(),
  80. }
  81. }
  82. pub fn is_not_empty(&self) -> bool {
  83. let is_empty = self.limits_close.is_empty() && self.limits_open.is_empty() && self.cancel.is_empty() && self.check.is_empty();
  84. if is_empty { false } else { true }
  85. }
  86. }
  87. impl fmt::Display for OrderCommand {
  88. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  89. write!(f, "订单触发数据: cancel: {:?}, check: {:?}, limits_open: {:?}, limits_close: {:?}",
  90. self.cancel,
  91. self.check,
  92. self.limits_open,
  93. self.limits_close
  94. )
  95. }
  96. }
  97. /// 持仓模式枚举
  98. /// - `Both`:单持仓方向
  99. /// - `LONG`:多仓
  100. /// - `SHORT`:空仓
  101. #[derive(Debug, Clone, PartialEq, Eq)]
  102. pub enum PositionModeEnum {
  103. Both,
  104. Long,
  105. Short,
  106. }
  107. /// Account结构体(账户信息)
  108. /// - `coin(String)`: 货币;
  109. /// - `balance(Decimal)`: 总计计价币数量;
  110. /// - `available_balance(Decimal)`: 可用计价币数量;
  111. /// - `frozen_balance(Decimal)`: balance挂单的冻结数量
  112. /// - `stocks(Decimal)`: 总计交易币数量
  113. /// - `available_stocks(Decimal)`: 可用交易币数量
  114. /// - `frozen_stocks(Decimal)`: stocks挂单的冻结数量
  115. #[derive(Debug, Clone, PartialEq, Eq)]
  116. pub struct Account {
  117. pub coin: String,
  118. pub balance: Decimal,
  119. pub available_balance: Decimal,
  120. pub frozen_balance: Decimal,
  121. pub stocks: Decimal,
  122. pub available_stocks: Decimal,
  123. pub frozen_stocks: Decimal,
  124. }
  125. impl Account {
  126. pub fn new() -> Account {
  127. Account {
  128. coin: "".to_string(),
  129. balance: Default::default(),
  130. available_balance: Default::default(),
  131. frozen_balance: Default::default(),
  132. stocks: Default::default(),
  133. available_stocks: Default::default(),
  134. frozen_stocks: Default::default(),
  135. }
  136. }
  137. }
  138. /// 交易结构体(订单流)
  139. /// - `id(String)`: id
  140. /// - `time(Decimal)`: 交易更新时间戳(ms)
  141. /// - `size(Decimal)`: 交易量,负数是卖方
  142. /// - `price(Decimal)`: 成交价格
  143. /// - `symbol(String)`: 成交符号
  144. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  145. pub struct Trade {
  146. pub id: String,
  147. pub time: Decimal,
  148. pub size: Decimal,
  149. pub price: Decimal,
  150. pub symbol: String
  151. }
  152. /// 特殊压缩结构体(订单流)
  153. /// - `0(Decimal)`: id
  154. /// - `1(Decimal)`: 交易更新时间戳(ms)
  155. /// - `2(Decimal)`: 交易量,负数是卖方
  156. /// - `3(Decimal)`: 成交价格
  157. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  158. pub struct SpecialTrade(pub Vec<String>);
  159. impl SpecialTrade {
  160. // 获取内部 Vec<Decimal> 的引用
  161. pub fn inner(&self) -> &Vec<String> {
  162. &self.0
  163. }
  164. // 获取内部 Vec<Decimal> 的可变引用
  165. pub fn inner_mut(&mut self) -> &mut Vec<String> {
  166. &mut self.0
  167. }
  168. // 索引访问特定元素
  169. pub fn get(&self, index: usize) -> Option<&String> {
  170. self.0.get(index)
  171. }
  172. pub fn new(trade: &Trade) -> SpecialTrade {
  173. SpecialTrade(vec![trade.id.clone(), trade.time.to_string(), trade.size.to_string(), trade.price.to_string()])
  174. }
  175. }
  176. /// Depth结构体(市场深度)
  177. /// - `time(Decimal)`: 深度更新时间戳(ms);
  178. /// - `symbol(String)`: 币对符号;
  179. /// - `asks(Vec<MarketOrder>)`: 卖方深度列表;
  180. /// - `bids(Vec<MarketOrder>)`: 买方深度列表;
  181. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  182. pub struct Depth {
  183. pub time: Decimal,
  184. pub symbol: String,
  185. pub asks: Vec<OrderBook>,
  186. pub bids: Vec<OrderBook>,
  187. }
  188. impl Depth {
  189. pub fn new() -> Depth {
  190. Depth {
  191. time: Decimal::ZERO,
  192. symbol: String::new(),
  193. asks: vec![],
  194. bids: vec![],
  195. }
  196. }
  197. }
  198. /// 特殊Depth结构体(市场深度),用于存放到本地数据库中
  199. /// - `a(Vec<Vec<Decimal>>)`: asks;
  200. /// - `b(Vec<Vec<Decimal>>)`: bids;
  201. /// - `t(Decimal)`: 数据生成时间
  202. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  203. pub struct SpecialDepth {
  204. pub b: Vec<Vec<Decimal>>,
  205. pub a: Vec<Vec<Decimal>>,
  206. pub t: Decimal,
  207. }
  208. impl SpecialDepth {
  209. pub fn new(depth: &Depth) -> SpecialDepth {
  210. let bids = depth.bids.iter().map(|ob| vec![ob.price, ob.amount]).collect::<Vec<_>>();
  211. let asks = depth.asks.iter().map(|ob| vec![ob.price, ob.amount]).collect::<Vec<_>>();
  212. SpecialDepth {
  213. b: bids,
  214. a: asks,
  215. t: depth.time,
  216. }
  217. }
  218. }
  219. // 简易深度信息
  220. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  221. pub struct SimpleDepth {
  222. pub time: Decimal,
  223. pub size: Decimal,
  224. pub b1: Decimal,
  225. pub a1: Decimal,
  226. pub symbol: String,
  227. }
  228. impl SimpleDepth {
  229. pub fn new(depth: &Depth) -> SimpleDepth {
  230. let mut total_size = Decimal::ZERO;
  231. for ask in &depth.asks {
  232. total_size += ask.price * ask.amount;
  233. }
  234. for bid in &depth.bids {
  235. total_size += bid.price * bid.amount;
  236. }
  237. total_size.rescale(2);
  238. SimpleDepth {
  239. time: depth.time,
  240. size: total_size,
  241. b1: depth.bids[0].price,
  242. a1: depth.asks[0].price,
  243. symbol: depth.symbol.clone(),
  244. }
  245. }
  246. }
  247. /// 特殊Ticker结构体(市场行情)
  248. /// - `sell(Decimal)`: 卖一价
  249. /// - `buy(Decimal)`: 买一价
  250. /// - `mid_price(Decimal)`: 平均价
  251. /// - `t(Decimal)`: 数据更新id
  252. /// - `create_at(i64)`: 数据生成时间
  253. #[derive(Debug, Clone, PartialEq, Eq, Default)]
  254. pub struct SpecialTicker {
  255. pub sell: Decimal,
  256. pub buy: Decimal,
  257. pub mid_price: Decimal,
  258. pub t: Decimal,
  259. pub create_at: i64
  260. }
  261. impl SpecialTicker {
  262. pub fn new() -> SpecialTicker {
  263. SpecialTicker {
  264. sell: Default::default(),
  265. buy: Default::default(),
  266. mid_price: Default::default(),
  267. t: Default::default(),
  268. create_at: 0,
  269. }
  270. }
  271. }
  272. /// OrderBook结构体(市场深度单)
  273. /// - `price(Decimal)`: 价格
  274. /// - `amount(Decimal)`: 数量
  275. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  276. pub struct OrderBook {
  277. pub price: Decimal,
  278. pub amount: Decimal,
  279. }
  280. impl OrderBook {
  281. pub fn new() -> OrderBook {
  282. OrderBook {
  283. price: Default::default(),
  284. amount: Default::default(),
  285. }
  286. }
  287. }
  288. /// Record结构体(标准的OHLC结构)
  289. /// - `time(i64)`: 时间戳;
  290. /// - `open(Decimal)`: 开盘价;
  291. /// - `high(Decimal)`: 最高价;
  292. /// - `low(Decimal):` 最低价;
  293. /// - `close(Decimal)`: 收盘价;
  294. /// - `volume(Decimal)`: 交易量;
  295. /// - `symbol(String)`: 交易对;
  296. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  297. pub struct Record {
  298. pub time: Decimal,
  299. pub open: Decimal,
  300. pub high: Decimal,
  301. pub low: Decimal,
  302. pub close: Decimal,
  303. pub volume: Decimal,
  304. pub symbol: String
  305. }
  306. impl Record {
  307. pub fn new() -> Record {
  308. Record {
  309. time: Default::default(),
  310. open: Default::default(),
  311. high: Default::default(),
  312. low: Default::default(),
  313. close: Default::default(),
  314. volume: Default::default(),
  315. symbol: "".to_string(),
  316. }
  317. }
  318. }
  319. /// 特殊Order结构体(订单)
  320. /// - `name<String>`: 平台信息;
  321. /// - `order<Vec<Order>>`: 订单信息数组;
  322. #[derive(Debug, Clone, PartialEq, Eq)]
  323. pub struct SpecialOrder {
  324. pub name: String,
  325. pub order: Vec<Order>,
  326. }
  327. impl SpecialOrder {
  328. pub fn new() -> SpecialOrder {
  329. SpecialOrder {
  330. name: "".to_string(),
  331. order: vec![],
  332. }
  333. }
  334. }
  335. /// Order结构体(订单)
  336. /// - `id(String)`: 交易单唯一标识
  337. /// - `custom_id(String)`: 自定义Id
  338. /// - `price(Decimal)`: 下单价格
  339. /// - `amount(Decimal)`: 下单数量
  340. /// - `deal_amount(Decimal)`: 成交数量
  341. /// - `avg_price(Decimal)`: 成交均价
  342. /// - `status(String)`: 订单状态
  343. /// - `order_type(String)`: 订单类型
  344. #[derive(Debug, Clone, PartialEq, Eq)]
  345. pub struct Order {
  346. pub id: String,
  347. pub custom_id: String,
  348. pub price: Decimal,
  349. pub amount: Decimal,
  350. pub deal_amount: Decimal,
  351. pub avg_price: Decimal,
  352. pub status: String,
  353. pub order_type: String,
  354. pub trace_stack: TraceStack,
  355. }
  356. impl Order {
  357. pub fn new() -> Order {
  358. Order {
  359. id: "".to_string(),
  360. custom_id: "".to_string(),
  361. price: Default::default(),
  362. amount: Default::default(),
  363. deal_amount: Default::default(),
  364. avg_price: Default::default(),
  365. status: "".to_string(),
  366. order_type: "".to_string(),
  367. trace_stack: TraceStack::new(0, Instant::now()),
  368. }
  369. }
  370. }
  371. /// Ticker结构体(市场行情)
  372. /// - `time(i64)`: 毫秒级别时间戳
  373. /// - `high(Decimal)`: 最高价
  374. /// - `low(Decimal)`: 最低价
  375. /// - `sell(Decimal)`: 卖一价
  376. /// - `buy(Decimal)`: 买一价
  377. /// - `last(Decimal)`: 最后成交价
  378. /// - `volume(Decimal)`: 最近成交量
  379. #[derive(Debug, Clone, PartialEq, Eq)]
  380. pub struct Ticker {
  381. pub time: i64,
  382. pub high: Decimal,
  383. pub low: Decimal,
  384. pub sell: Decimal,
  385. pub buy: Decimal,
  386. pub last: Decimal,
  387. pub volume: Decimal,
  388. }
  389. impl Ticker {
  390. pub fn new() -> Ticker {
  391. Ticker {
  392. time: 0,
  393. high: Default::default(),
  394. low: Default::default(),
  395. sell: Default::default(),
  396. buy: Default::default(),
  397. last: Default::default(),
  398. volume: Default::default(),
  399. }
  400. }
  401. }
  402. /// Market结构体(交易品种的市场信息)
  403. /// - `symbol(String)`: 交易对
  404. /// - `base_asset(String)`: 交易币
  405. /// - `quote_asset(String)`: 计价币
  406. /// - `tick_size(Decimal)`: 价格最小变动数值
  407. /// - `amount_size(Decimal)`: 下单量最小变动数值
  408. /// - `price_precision(Decimal)`: 价格精度
  409. /// - `amount_precision(Decimal)`: 下单量精度
  410. /// - `min_qty(Decimal)`: 最小下单量
  411. /// - `max_qty(Decimal)`: 最大下单量
  412. /// - `min_notional(Decimal)`: 最小下单金额
  413. /// - `max_notional(Decimal)`: 最大下单金额
  414. /// - `ct_val(Decimal)`: 合约价值
  415. #[derive(Debug, Clone, PartialEq, Eq)]
  416. pub struct Market {
  417. pub symbol: String,
  418. pub base_asset: String,
  419. pub quote_asset: String,
  420. pub tick_size: Decimal,
  421. pub amount_size: Decimal,
  422. pub price_precision: Decimal,
  423. pub amount_precision: Decimal,
  424. pub min_qty: Decimal,
  425. pub max_qty: Decimal,
  426. pub min_notional: Decimal,
  427. pub max_notional: Decimal,
  428. pub ct_val: Decimal,
  429. }
  430. impl Market {
  431. pub fn new() -> Market {
  432. Market {
  433. symbol: "".to_string(),
  434. base_asset: "".to_string(),
  435. quote_asset: "".to_string(),
  436. tick_size: Default::default(),
  437. amount_size: Default::default(),
  438. price_precision: Default::default(),
  439. amount_precision: Default::default(),
  440. min_qty: Default::default(),
  441. max_qty: Default::default(),
  442. min_notional: Default::default(),
  443. max_notional: Default::default(),
  444. ct_val: Default::default(),
  445. }
  446. }
  447. }
  448. /// Position结构体(仓位信息)
  449. /// - `symbol(String)`: 币对
  450. /// - `margin_level(Decimal)`: 持仓杆杠大小
  451. /// - `amount(Decimal)`: 持仓量
  452. /// - `frozen_amount(Decimal)`: 仓位冻结量
  453. /// - `price(Decimal)`: 持仓均价
  454. /// - `profit(Decimal)`: 持仓浮动盈亏
  455. /// - `position_mode(PositionModeEnum)`: 持仓模式
  456. /// - `margin(Decimal)`: 仓位占用的保证金
  457. #[derive(Debug, Clone, PartialEq, Eq)]
  458. pub struct Position {
  459. pub symbol: String,
  460. pub margin_level: Decimal,
  461. pub amount: Decimal,
  462. pub frozen_amount: Decimal,
  463. pub price: Decimal,
  464. pub profit: Decimal,
  465. pub position_mode: PositionModeEnum,
  466. pub margin: Decimal,
  467. }
  468. impl Position {
  469. pub fn new() -> Position {
  470. Position {
  471. symbol: "".to_string(),
  472. margin_level: Default::default(),
  473. amount: Default::default(),
  474. frozen_amount: Default::default(),
  475. price: Default::default(),
  476. profit: Default::default(),
  477. position_mode: PositionModeEnum::Both,
  478. margin: Default::default(),
  479. }
  480. }
  481. }
  482. /// 交易所统一方法接口
  483. ///
  484. /// 使用方法前需实例化
  485. /// ```rust
  486. /// use std::collections::BTreeMap;
  487. /// use standard::exchange::{Exchange, ExchangeEnum};
  488. ///
  489. /// let mut params:BTreeMap<String,String> = BTreeMap::new();
  490. /// params.insert("access_key".to_string(), "your_access_key".to_string());
  491. /// params.insert("access_key".to_string(), "your_secret_key".to_string());
  492. /// // let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
  493. /// ```
  494. /// 获取当前交易所交易模式
  495. /// - fn get_self_exchange(&self) -> ExchangeEnum;
  496. /// ```rust
  497. /// # use std::collections::BTreeMap;
  498. /// # use standard::exchange::{Exchange, ExchangeEnum};
  499. /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
  500. /// # params.insert("access_key".to_string(), "your_access_key".to_string());
  501. /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
  502. /// # // let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
  503. ///
  504. /// // exchange.get_self_exchange();
  505. /// ```
  506. /// 获取当前是否使用高速模式
  507. /// - fn get_self_is_colo(&self) -> bool;
  508. /// ```rust
  509. /// # use std::collections::BTreeMap;
  510. /// # use standard::exchange::{Exchange, ExchangeEnum};
  511. /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
  512. /// # params.insert("access_key".to_string(), "your_access_key".to_string());
  513. /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
  514. /// # // let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
  515. ///
  516. /// // exchange.get_self_is_colo();
  517. /// ```
  518. /// 获取当前是否使用登录
  519. /// - fn get_self_is_login(&self) -> bool;
  520. /// ```rust
  521. /// # use std::collections::BTreeMap;
  522. /// # use standard::exchange::{Exchange, ExchangeEnum};
  523. /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
  524. /// # params.insert("access_key".to_string(), "your_access_key".to_string());
  525. /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
  526. /// # // let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
  527. ///
  528. /// // exchange.get_self_is_login();
  529. /// ```
  530. /// 获取登录params信息
  531. /// - fn get_self_params(&self) -> BTreeMap<String, String>;
  532. /// ```rust
  533. /// # use std::collections::BTreeMap;
  534. /// # use standard::exchange::{Exchange, ExchangeEnum};
  535. /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
  536. /// # params.insert("access_key".to_string(), "your_access_key".to_string());
  537. /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
  538. /// # // let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
  539. ///
  540. /// // exchange.get_self_params();
  541. /// ```
  542. /// 获取账号信息
  543. /// - async fn get_account(&self, symbol: &str) -> Result<Account, Error>;
  544. /// ```rust
  545. /// # use std::collections::BTreeMap;
  546. /// # use standard::exchange::{Exchange, ExchangeEnum};
  547. /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
  548. /// # params.insert("access_key".to_string(), "your_access_key".to_string());
  549. /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
  550. /// # // let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
  551. ///
  552. /// // exchange.get_account()
  553. /// ```
  554. /// 订阅账号信息
  555. /// ```rust
  556. /// # use std::collections::BTreeMap;
  557. /// # use standard::exchange::{Exchange, ExchangeEnum};
  558. /// # let mut params:BTreeMap<String,String> = BTreeMap::new();
  559. /// # params.insert("access_key".to_string(), "your_access_key".to_string());
  560. /// # params.insert("access_key".to_string(), "your_secret_key".to_string());
  561. /// # // let exchange = Exchange::new(ExchangeEnum::BinanceSwap, "BTC_USDT".to_string(), true, params);
  562. ///
  563. /// // exchange.subscribe_account();
  564. /// ```
  565. #[async_trait]
  566. pub trait Platform {
  567. fn clone_box(&self) -> Box<dyn Platform + Send + Sync>;
  568. // 获取当前交易所交易模式
  569. fn get_self_exchange(&self) -> ExchangeEnum;
  570. // 获取交易对
  571. fn get_self_symbol(&self) -> String;
  572. // 获取当前是否使用高速模式
  573. fn get_self_is_colo(&self) -> bool;
  574. // 获取登录params信息
  575. fn get_self_params(&self) -> BTreeMap<String, String>;
  576. // 获取market信息
  577. fn get_self_market(&self) -> Market;
  578. // 获取请求时间
  579. fn get_request_delays(&self) -> Vec<i64>;
  580. // 获取请求平均时间
  581. fn get_request_avg_delay(&self) -> Decimal;
  582. // 获取请求最大时间
  583. fn get_request_max_delay(&self) -> i64;
  584. // 获取服务器时间
  585. async fn get_server_time(&mut self) -> Result<String, Error>;
  586. // 获取账号信息
  587. async fn get_account(&mut self) -> Result<Account, Error>;
  588. // 获取现货账号信息
  589. async fn get_spot_account(&mut self) -> Result<Vec<Account>, Error>;
  590. // 获取持仓信息
  591. async fn get_position(&mut self) -> Result<Vec<Position>, Error>;
  592. // 获取所有持仓
  593. async fn get_positions(&mut self) -> Result<Vec<Position>, Error>;
  594. // 获取市场行情
  595. async fn get_ticker(&mut self) -> Result<Ticker, Error>;
  596. // 获取市场行情自定义交易对
  597. async fn get_ticker_symbol(&mut self, symbol: String) -> Result<Ticker, Error>;
  598. // 查询所有的市场信息
  599. async fn get_market(&mut self) -> Result<Market, Error>;
  600. // 查询所有的市场信息自定义交易对
  601. async fn get_market_symbol(&mut self, symbol: String) -> Result<Market, Error>;
  602. // 查询订单详情
  603. async fn get_order_detail(&mut self, order_id: &str, custom_id: &str) -> Result<Order, Error>;
  604. // 获取订单列表
  605. async fn get_orders_list(&mut self, status: &str) -> Result<Vec<Order>, Error>;
  606. // 下单接口
  607. async fn take_order(&mut self, custom_id: &str, origin_side: &str, price: Decimal, amount: Decimal) -> Result<Order, Error>;
  608. // 下单接口自定义交易对
  609. async fn take_order_symbol(&mut self, symbol: String, ct_val: Decimal, custom_id: &str, origin_side: &str, price: Decimal, amount: Decimal) -> Result<Order, Error>;
  610. // 撤销订单
  611. async fn cancel_order(&mut self, order_id: &str, custom_id: &str) -> Result<Order, Error>;
  612. // 批量撤销订单
  613. async fn cancel_orders(&mut self) -> Result<Vec<Order>, Error>;
  614. // 撤销所有订单
  615. async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error>;
  616. /// 下一个止损单
  617. /// - stop_price: 触发价
  618. /// - price: 委托价,0就市价委托
  619. /// - side: 止损哪个方向[long多单,short空单]
  620. async fn take_stop_loss_order(&mut self, stop_price: Decimal, price: Decimal, side: &str) -> Result<Value, Error>;
  621. /// 撤销止损订单
  622. /// - order_id: 需要撤销的id
  623. async fn cancel_stop_loss_order(&mut self, order_id: &str) -> Result<Value, Error>;
  624. // 设置持仓模式
  625. async fn set_dual_mode(&mut self, coin: &str, is_dual_mode: bool) -> Result<String, Error>;
  626. // 更新双持仓模式下杠杆
  627. async fn set_dual_leverage(&mut self, leverage: &str) -> Result<String, Error>;
  628. // 设置自动追加保证金
  629. async fn set_auto_deposit_status(&mut self, status: bool) -> Result<String, Error>;
  630. // 交易账户互转
  631. async fn wallet_transfers(&mut self, coin: &str, from: &str, to: &str, amount: Decimal) -> Result<String, Error>;
  632. // 指令下单
  633. async fn command_order(&mut self, order_command: &mut OrderCommand, trace_stack: &TraceStack);
  634. }