data_providers.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. use serde::{Deserialize, Serialize};
  2. pub mod binance;
  3. pub mod bybit;
  4. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
  5. pub enum StreamType {
  6. Kline {
  7. exchange: Exchange,
  8. ticker: Ticker,
  9. timeframe: Timeframe,
  10. },
  11. DepthAndTrades {
  12. exchange: Exchange,
  13. ticker: Ticker,
  14. },
  15. None,
  16. }
  17. // data types
  18. #[derive(Debug, Clone, Copy, Default)]
  19. pub struct Order {
  20. pub price: f32,
  21. pub qty: f32,
  22. }
  23. #[derive(Debug, Clone, Default)]
  24. pub struct Depth {
  25. pub time: i64,
  26. pub bids: Vec<Order>,
  27. pub asks: Vec<Order>,
  28. }
  29. #[derive(Debug, Clone, Default)]
  30. pub struct LocalDepthCache {
  31. pub last_update_id: i64,
  32. pub time: i64,
  33. pub bids: Vec<Order>,
  34. pub asks: Vec<Order>,
  35. }
  36. impl LocalDepthCache {
  37. pub fn new() -> Self {
  38. Self {
  39. last_update_id: 0,
  40. time: 0,
  41. bids: Vec::new(),
  42. asks: Vec::new(),
  43. }
  44. }
  45. pub fn fetched(&mut self, new_depth: LocalDepthCache) {
  46. self.last_update_id = new_depth.last_update_id;
  47. self.time = new_depth.time;
  48. self.bids = new_depth.bids;
  49. self.asks = new_depth.asks;
  50. }
  51. pub fn update_depth_cache(&mut self, new_depth: LocalDepthCache) {
  52. self.last_update_id = new_depth.last_update_id;
  53. self.time = new_depth.time;
  54. for order in new_depth.bids.iter() {
  55. if order.qty == 0.0 {
  56. self.bids.retain(|x| x.price != order.price);
  57. } else if let Some(existing_order) = self.bids.iter_mut().find(|x| x.price == order.price) {
  58. existing_order.qty = order.qty;
  59. } else {
  60. self.bids.push(*order);
  61. }
  62. }
  63. for order in new_depth.asks.iter() {
  64. if order.qty == 0.0 {
  65. self.asks.retain(|x| x.price != order.price);
  66. } else if let Some(existing_order) = self.asks.iter_mut().find(|x| x.price == order.price) {
  67. existing_order.qty = order.qty;
  68. } else {
  69. self.asks.push(*order);
  70. }
  71. }
  72. }
  73. pub fn get_depth(&self) -> Depth {
  74. Depth {
  75. time: self.time,
  76. bids: self.bids.clone(),
  77. asks: self.asks.clone(),
  78. }
  79. }
  80. pub fn get_fetch_id(&self) -> i64 {
  81. self.last_update_id
  82. }
  83. }
  84. #[derive(Default, Debug, Clone, Copy)]
  85. pub struct Trade {
  86. pub time: i64,
  87. pub is_sell: bool,
  88. pub price: f32,
  89. pub qty: f32,
  90. }
  91. #[derive(Default, Debug, Clone, Copy)]
  92. pub struct Kline {
  93. pub time: u64,
  94. pub open: f32,
  95. pub high: f32,
  96. pub low: f32,
  97. pub close: f32,
  98. pub volume: (f32, f32),
  99. }
  100. #[derive(Default, Debug, Clone, Copy)]
  101. pub struct FeedLatency {
  102. pub time: i64,
  103. pub depth_latency: i64,
  104. pub trade_latency: Option<i64>,
  105. }
  106. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
  107. pub struct TickMultiplier(pub u16);
  108. impl std::fmt::Display for TickMultiplier {
  109. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  110. write!(f, "{}x", self.0)
  111. }
  112. }
  113. impl TickMultiplier {
  114. pub fn multiply_with_min_tick_size(&self, min_tick_size: f32) -> f32 {
  115. self.0 as f32 * min_tick_size
  116. }
  117. }
  118. // connection types
  119. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
  120. pub enum Exchange {
  121. BinanceFutures,
  122. BybitLinear,
  123. }
  124. impl std::fmt::Display for Exchange {
  125. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  126. write!(
  127. f,
  128. "{}",
  129. match self {
  130. Exchange::BinanceFutures => "Binance Futures",
  131. Exchange::BybitLinear => "Bybit Linear",
  132. }
  133. )
  134. }
  135. }
  136. impl Exchange {
  137. pub const ALL: [Exchange; 2] = [Exchange::BinanceFutures, Exchange::BybitLinear];
  138. }
  139. impl std::fmt::Display for Ticker {
  140. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  141. write!(
  142. f,
  143. "{}",
  144. match self {
  145. Ticker::BTCUSDT => "BTCUSDT",
  146. Ticker::ETHUSDT => "ETHUSDT",
  147. Ticker::SOLUSDT => "SOLUSDT",
  148. Ticker::LTCUSDT => "LTCUSDT",
  149. }
  150. )
  151. }
  152. }
  153. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
  154. pub enum Ticker {
  155. BTCUSDT,
  156. ETHUSDT,
  157. SOLUSDT,
  158. LTCUSDT,
  159. }
  160. impl Ticker {
  161. pub const ALL: [Ticker; 4] = [Ticker::BTCUSDT, Ticker::ETHUSDT, Ticker::SOLUSDT, Ticker::LTCUSDT];
  162. }
  163. impl Ticker {
  164. /// Returns the string representation of the ticker in lowercase
  165. ///
  166. /// e.g. BTCUSDT -> "btcusdt"
  167. pub fn get_string(&self) -> String {
  168. match self {
  169. Ticker::BTCUSDT => "btcusdt".to_string(),
  170. Ticker::ETHUSDT => "ethusdt".to_string(),
  171. Ticker::SOLUSDT => "solusdt".to_string(),
  172. Ticker::LTCUSDT => "ltcusdt".to_string(),
  173. }
  174. }
  175. }
  176. impl std::fmt::Display for Timeframe {
  177. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  178. write!(
  179. f,
  180. "{}",
  181. match self {
  182. Timeframe::M1 => "1m",
  183. Timeframe::M3 => "3m",
  184. Timeframe::M5 => "5m",
  185. Timeframe::M15 => "15m",
  186. Timeframe::M30 => "30m",
  187. }
  188. )
  189. }
  190. }
  191. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
  192. pub enum Timeframe {
  193. M1,
  194. M3,
  195. M5,
  196. M15,
  197. M30,
  198. }
  199. impl Timeframe {
  200. pub const ALL: [Timeframe; 5] = [Timeframe::M1, Timeframe::M3, Timeframe::M5, Timeframe::M15, Timeframe::M30];
  201. pub fn to_minutes(&self) -> u16 {
  202. match self {
  203. Timeframe::M1 => 1,
  204. Timeframe::M3 => 3,
  205. Timeframe::M5 => 5,
  206. Timeframe::M15 => 15,
  207. Timeframe::M30 => 30,
  208. }
  209. }
  210. }
  211. #[derive(Debug)]
  212. pub enum BinanceWsState {
  213. Connected(binance::market_data::Connection),
  214. Disconnected,
  215. }
  216. impl Default for BinanceWsState {
  217. fn default() -> Self {
  218. Self::Disconnected
  219. }
  220. }
  221. #[derive(Debug)]
  222. pub enum BybitWsState {
  223. Connected(bybit::market_data::Connection),
  224. Disconnected,
  225. }
  226. impl Default for BybitWsState {
  227. fn default() -> Self {
  228. Self::Disconnected
  229. }
  230. }
  231. pub enum UserWsState {
  232. Connected(binance::user_data::Connection),
  233. Disconnected,
  234. }
  235. impl Default for UserWsState {
  236. fn default() -> Self {
  237. Self::Disconnected
  238. }
  239. }
  240. #[derive(Debug, Clone)]
  241. pub enum MarketEvents {
  242. Binance(binance::market_data::Event),
  243. Bybit(bybit::market_data::Event),
  244. }
  245. #[derive(thiserror::Error, Debug)]
  246. pub enum StreamError {
  247. #[error("FetchError: {0}")]
  248. FetchError(#[from] reqwest::Error),
  249. #[error("ParseError: {0}")]
  250. ParseError(String),
  251. #[error("StreamError: {0}")]
  252. WebsocketError(String),
  253. #[error("UnknownError: {0}")]
  254. UnknownError(String),
  255. }