handle_info.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. use std::cmp::Ordering;
  2. use std::str::FromStr;
  3. use rust_decimal::{Decimal};
  4. use rust_decimal::prelude::FromPrimitive;
  5. use rust_decimal_macros::dec;
  6. use tracing::{error};
  7. use exchanges::response_base::ResponseData;
  8. use global::public_params;
  9. use crate::exchange::ExchangeEnum;
  10. use crate::{binance_swap_handle, gate_swap_handle, bybit_swap_handle, kucoin_handle};
  11. use crate::{Account, MarketOrder, Position, SpecialDepth, SpecialOrder, SpecialTicker};
  12. #[allow(dead_code)]
  13. pub struct HandleSwapInfo;
  14. pub struct DepthParam {
  15. pub depth_asks: Vec<MarketOrder>,
  16. pub depth_bids: Vec<MarketOrder>,
  17. pub t: Decimal,
  18. pub create_at: i64
  19. }
  20. #[allow(dead_code)]
  21. impl HandleSwapInfo {
  22. // 处理账号信息
  23. pub fn handle_account_info(exchange: ExchangeEnum, res_data: &ResponseData, symbol: &String) -> Account {
  24. match exchange {
  25. // ExchangeEnum::BinanceSwap => {
  26. // error!("暂未提供此交易所方法!handle_account_info:{:?}", exchange);
  27. // panic!("暂未提供此交易所方法!handle_account_info:{:?}", exchange);
  28. // }
  29. ExchangeEnum::GateSwap => {
  30. gate_swap_handle::handle_account_info(res_data, symbol)
  31. }
  32. ExchangeEnum::KucoinSwap => {
  33. kucoin_handle::handle_account_info(res_data, symbol)
  34. }
  35. // ExchangeEnum::KucoinSpot => {
  36. // kucoin_spot_handle::handle_account_info(res_data, symbol)
  37. // }
  38. // ExchangeEnum::OkxSwap => {
  39. // okx_handle::handle_account_info(res_data, symbol)
  40. // }
  41. // ExchangeEnum::BitgetSpot => {
  42. // bitget_spot_handle::handle_account_info(res_data, symbol)
  43. // },
  44. ExchangeEnum::BybitSwap => {
  45. bybit_swap_handle::handle_account_info(res_data, symbol)
  46. }
  47. _ => {
  48. error!("未找到该交易所!handle_account_info: {:?}",exchange);
  49. panic!("未找到该交易所!handle_account_info: {:?}", exchange);
  50. }
  51. }
  52. }
  53. // 处理特殊Ticket信息
  54. pub fn handle_book_ticker(exchange: ExchangeEnum, res_data: &ResponseData) -> SpecialDepth {
  55. match exchange {
  56. // ExchangeEnum::BinanceSpot => {
  57. // binance_spot_handle::handle_special_ticker(res_data)
  58. // }
  59. ExchangeEnum::BinanceSwap => {
  60. binance_swap_handle::handle_book_ticker(res_data)
  61. }
  62. ExchangeEnum::GateSwap => {
  63. gate_swap_handle::handle_book_ticker(res_data)
  64. }
  65. ExchangeEnum::KucoinSwap => {
  66. kucoin_handle::handle_book_ticker(res_data)
  67. }
  68. // ExchangeEnum::KucoinSpot => {
  69. // kucoin_spot_handle::handle_special_ticker(res_data)
  70. // }
  71. // ExchangeEnum::OkxSwap => {
  72. // okx_handle::handle_special_ticker(res_data)
  73. // }
  74. // ExchangeEnum::BitgetSpot => {
  75. // bitget_spot_handle::handle_special_ticker(res_data)
  76. // },
  77. ExchangeEnum::BybitSwap => {
  78. bybit_swap_handle::handle_ticker(res_data)
  79. }
  80. }
  81. }
  82. // 处理position信息
  83. pub fn handle_position(exchange: ExchangeEnum, res_data: &ResponseData, ct_val: &Decimal) -> Vec<Position> {
  84. match exchange {
  85. ExchangeEnum::BinanceSwap => {
  86. error!("暂未提供此交易所方法!handle_position:{:?}", exchange);
  87. panic!("暂未提供此交易所方法!handle_position:{:?}", exchange);
  88. }
  89. ExchangeEnum::GateSwap => {
  90. gate_swap_handle::handle_position(res_data, ct_val)
  91. }
  92. ExchangeEnum::KucoinSwap => {
  93. kucoin_handle::handle_position(res_data, ct_val)
  94. }
  95. // ExchangeEnum::KucoinSpot => {
  96. // error!("暂未提供此交易所方法!handle_position:{:?}", exchange);
  97. // panic!("暂未提供此交易所方法!handle_position:{:?}", exchange);
  98. // }
  99. // ExchangeEnum::OkxSwap => {
  100. // okx_handle::handle_position(res_data, ct_val)
  101. // }
  102. // ExchangeEnum::BitgetSpot => {
  103. // error!("暂未提供此交易所方法!handle_position:{:?}", exchange);
  104. // panic!("暂未提供此交易所方法!handle_position:{:?}", exchange);
  105. // },
  106. ExchangeEnum::BybitSwap => {
  107. bybit_swap_handle::handle_position(res_data, ct_val)
  108. }
  109. }
  110. }
  111. // 处理订单信息
  112. pub fn handle_order(exchange: ExchangeEnum, res_data: ResponseData, ct_val: Decimal) -> SpecialOrder {
  113. match exchange {
  114. ExchangeEnum::BinanceSwap => {
  115. error!("暂未提供此交易所方法!handle_order:{:?}", exchange);
  116. panic!("暂未提供此交易所方法!handle_order:{:?}", exchange);
  117. }
  118. ExchangeEnum::GateSwap => {
  119. gate_swap_handle::handle_order(res_data, ct_val)
  120. }
  121. ExchangeEnum::KucoinSwap => {
  122. kucoin_handle::handle_order(res_data, ct_val)
  123. }
  124. // ExchangeEnum::KucoinSpot => {
  125. // kucoin_spot_handle::handle_order(res_data, ct_val)
  126. // }
  127. // ExchangeEnum::OkxSwap => {
  128. // okx_handle::handle_order(res_data, ct_val)
  129. // }
  130. // ExchangeEnum::BitgetSpot => {
  131. // bitget_spot_handle::handle_order(res_data, ct_val)
  132. // },
  133. ExchangeEnum::BybitSwap => {
  134. bybit_swap_handle::handle_order(res_data, ct_val)
  135. }
  136. }
  137. }
  138. // 处理深度信息
  139. pub fn handle_special_depth(exchange: ExchangeEnum, res_data: &ResponseData) -> SpecialDepth {
  140. let label = res_data.label.clone();
  141. // 格式化
  142. let mut format_depth = format_depth(exchange, res_data);
  143. // 运算、组装
  144. make_special_depth(label, &mut format_depth.depth_asks, &mut format_depth.depth_bids, format_depth.t, format_depth.create_at)
  145. }
  146. }
  147. pub fn make_special_depth(label: String, depth_asks: &mut Vec<MarketOrder>, depth_bids: &mut Vec<MarketOrder>, t: Decimal, create_at: i64) -> SpecialDepth{
  148. depth_asks.sort_by(|a, b| a.price.partial_cmp(&b.price).unwrap_or(Ordering::Equal));
  149. depth_bids.sort_by(|a, b| b.price.partial_cmp(&a.price).unwrap_or(Ordering::Equal));
  150. // TODO 不排序的话,有4us可以省下来。
  151. let mp = (depth_asks[0].price + depth_bids[0].price) * dec!(0.5);
  152. let step = (public_params::EFF_RANGE * mp / Decimal::from_usize(public_params::LEVEL).unwrap()).round_dp(mp.scale());
  153. let mut ap = Vec::new();
  154. let mut bp = Vec::new();
  155. let mut av: Vec<Decimal> = Vec::new();
  156. let mut bv: Vec<Decimal> = Vec::new();
  157. for i in 0..public_params::LEVEL {
  158. let price = (depth_asks[0].price + step * Decimal::from_f64(i as f64).unwrap()).round_dp(depth_asks[0].price.scale());
  159. ap.push(price);
  160. }
  161. for i in 0..public_params::LEVEL {
  162. let price = (depth_bids[0].price - step * Decimal::from_f64(i as f64).unwrap()).round_dp(depth_bids[0].price.scale());
  163. bp.push(price);
  164. }
  165. let mut ap_price_tag = depth_asks[0].price + step;
  166. let mut ap_index = 0;
  167. for item in depth_asks.iter() {
  168. let price = item.price;
  169. let amount = item.amount;
  170. if av.get(ap_index).is_none() { av.push(Decimal::ZERO) };
  171. if price < ap_price_tag {
  172. av[ap_index] += amount;
  173. } else {
  174. ap_price_tag += step;
  175. ap_index += 1;
  176. if ap_index == public_params::LEVEL {
  177. break;
  178. }
  179. av[ap_index] += amount
  180. }
  181. }
  182. let mut bp_price_tag = depth_bids[0].price - step;
  183. let mut bp_index = 0;
  184. for item in depth_bids.iter() {
  185. let price = item.price;
  186. let amount = item.amount;
  187. if bv.get(bp_index).is_none() { bv.push(Decimal::ZERO) };
  188. if price > bp_price_tag {
  189. bv[bp_index] += amount;
  190. } else {
  191. bp_price_tag -= step;
  192. bp_index += 1;
  193. if bp_index == public_params::LEVEL {
  194. break;
  195. }
  196. bv[bp_index] += amount
  197. }
  198. }
  199. let ticker_info = SpecialTicker { sell: depth_asks[0].price, buy: depth_bids[0].price, mid_price: mp, t, create_at };
  200. let depth_info = bp.iter().cloned().chain(bv.iter().cloned()).chain(ap.iter().cloned()).chain(av.iter().cloned()).collect();
  201. SpecialDepth {
  202. name: label,
  203. depth: depth_info,
  204. ticker: ticker_info,
  205. t,
  206. create_at,
  207. }
  208. }
  209. pub fn format_depth(exchange: ExchangeEnum, res_data: &ResponseData) -> DepthParam {
  210. let depth_asks: Vec<MarketOrder>;
  211. let depth_bids: Vec<MarketOrder>;
  212. let t: Decimal;
  213. let create_at: i64;
  214. match exchange {
  215. // ExchangeEnum::BinanceSpot => {
  216. // depth_asks = binance_swap_handle::format_depth_items(res_data_json["asks"].clone());
  217. // depth_bids = binance_swap_handle::format_depth_items(res_data_json["bids"].clone());
  218. // t = Decimal::from_str(&res_data_json["lastUpdateId"].to_string()).unwrap();
  219. // create_at = 0;
  220. // }
  221. ExchangeEnum::BinanceSwap => {
  222. depth_asks = binance_swap_handle::format_depth_items(res_data.data["a"].clone());
  223. depth_bids = binance_swap_handle::format_depth_items(res_data.data["b"].clone());
  224. t = Decimal::from_str(&res_data.data["u"].to_string()).unwrap();
  225. create_at = res_data.data["E"].as_i64().unwrap() * 1000;
  226. }
  227. ExchangeEnum::GateSwap => {
  228. depth_asks = gate_swap_handle::format_depth_items(&res_data.data["asks"]);
  229. depth_bids = gate_swap_handle::format_depth_items(&res_data.data["bids"]);
  230. // todo! 有id可以取 保证与py一致
  231. t = Decimal::from_str(&res_data.data["t"].to_string()).unwrap();
  232. create_at = res_data.data["t"].as_i64().unwrap() * 1000;
  233. }
  234. ExchangeEnum::KucoinSwap => {
  235. depth_asks = kucoin_handle::format_depth_items(res_data.data["asks"].clone());
  236. depth_bids = kucoin_handle::format_depth_items(res_data.data["bids"].clone());
  237. t = Decimal::from_str(&res_data.data["sequence"].to_string()).unwrap();
  238. create_at = res_data.data["ts"].as_i64().unwrap() * 1000;
  239. }
  240. // ExchangeEnum::KucoinSpot => {
  241. // depth_asks = kucoin_spot_handle::format_depth_items(res_data_json["asks"].clone());
  242. // depth_bids = kucoin_spot_handle::format_depth_items(res_data_json["bids"].clone());
  243. // t = Decimal::from_str(&res_data_json["timestamp"].to_string()).unwrap();
  244. // create_at = res_data_json["timestamp"].as_i64().unwrap() * 1000;
  245. // }
  246. // ExchangeEnum::OkxSwap => {
  247. // depth_asks = okx_handle::format_depth_items(res_data_json[0]["asks"].clone());
  248. // depth_bids = okx_handle::format_depth_items(res_data_json[0]["bids"].clone());
  249. // t = Decimal::from_str(&res_data_json[0]["seqId"].to_string()).unwrap();
  250. // create_at = res_data_json[0]["ts"].as_str().unwrap().parse::<i64>().unwrap() * 1000;
  251. // }
  252. // ExchangeEnum::BitgetSpot => {
  253. // depth_asks = bitget_spot_handle::format_depth_items(res_data_json[0]["asks"].clone());
  254. // depth_bids = bitget_spot_handle::format_depth_items(res_data_json[0]["bids"].clone());
  255. // t = Decimal::from_str(res_data_json[0]["ts"].as_str().unwrap()).unwrap();
  256. // create_at = res_data_json[0]["ts"].as_str().unwrap().parse::<i64>().unwrap() * 1000;
  257. // }
  258. ExchangeEnum::BybitSwap => {
  259. depth_asks = bybit_swap_handle::format_depth_items(res_data.data["a"].clone());
  260. depth_bids = bybit_swap_handle::format_depth_items(res_data.data["b"].clone());
  261. t = Decimal::from_i64(res_data.reach_time).unwrap();
  262. create_at = res_data.reach_time * 1000;
  263. }
  264. }
  265. DepthParam{
  266. depth_asks,
  267. depth_bids,
  268. t,
  269. create_at
  270. }
  271. }