coinex_swap_handle.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. use std::str::FromStr;
  2. use rust_decimal::Decimal;
  3. use rust_decimal::prelude::FromPrimitive;
  4. use exchanges::response_base::ResponseData;
  5. use crate::{Trade};
  6. // 处理账号信息
  7. // pub fn handle_account_info(res_data: &ResponseData, symbol: &String) -> Account {
  8. // let res_data_json = res_data.data["balance_list"].as_array().unwrap();
  9. // format_account_info(res_data_json, symbol)
  10. // }
  11. // pub fn format_account_info(data: &Vec<Value>, symbol: &String) -> Account {
  12. // let symbol_upper = symbol.to_uppercase();
  13. // let symbol_array: Vec<&str> = symbol_upper.split("_").collect();
  14. // let balance_info = data.iter().find(|&item| item["ccy"].as_str().unwrap().contains(symbol_array[1]));
  15. //
  16. // match balance_info {
  17. // None => {
  18. // error!("Coinex:格式化账号信息错误!\nformat_account_info: data={:?}", data);
  19. // panic!("Coinex:格式化账号信息错误!\nformat_account_info: data={:?}", data)
  20. // }
  21. // Some(value) => {
  22. // let frozen_balance= Decimal::from_str(&value["frozen"].as_str().unwrap()).unwrap();
  23. // let available_balance = Decimal::from_str(&value["available"].as_str().unwrap()).unwrap();
  24. // let margin = Decimal::from_str(&value["margin"].as_str().unwrap()).unwrap();
  25. // let profit_unreal = Decimal::from_str(&value["unrealized_pnl"].as_str().unwrap()).unwrap();
  26. // let balance = frozen_balance + available_balance + margin + profit_unreal;
  27. // Account {
  28. // coin: symbol_array[1].to_string(),
  29. // balance,
  30. // available_balance: Decimal::ZERO,
  31. // frozen_balance: Decimal::ZERO,
  32. // stocks: Decimal::ZERO,
  33. // available_stocks: Decimal::ZERO,
  34. // frozen_stocks: Decimal::ZERO,
  35. // }
  36. // }
  37. // }
  38. // }
  39. // 处理position信息
  40. // pub fn handle_position(res_data: &ResponseData, ct_val: &Decimal) -> Vec<Position> {
  41. // let res_data_json = &res_data.data["position"];
  42. // let position = format_position_item(res_data_json, ct_val);
  43. // vec![position]
  44. // }
  45. // pub fn format_position_item(position: &Value, ct_val: &Decimal) -> Position {
  46. // let position_mode = match position["side"].as_str().unwrap_or("") {
  47. // "long" => PositionModeEnum::Long,
  48. // "short" => PositionModeEnum::Short,
  49. // _ => {
  50. // error!("Coinex:格式化持仓模式错误!\nformat_position_item:position={:?}", position);
  51. // panic!("Coinex:格式化持仓模式错误!\nformat_position_item:position={:?}", position)
  52. // }
  53. // };
  54. // let size = Decimal::from_str(&position["open_interest"].as_str().unwrap()).unwrap();
  55. // let amount = size * ct_val;
  56. // Position {
  57. // symbol: position["market"].as_str().unwrap().to_string(),
  58. // margin_level: Decimal::from_str(&position["leverage"].as_str().unwrap()).unwrap(),
  59. // amount,
  60. // frozen_amount: Decimal::ZERO,
  61. // price: Decimal::from_str(&position["avg_entry_price"].as_str().unwrap()).unwrap(),
  62. // profit: Decimal::from_str(&position["unrealized_pnl"].as_str().unwrap()).unwrap(),
  63. // position_mode,
  64. // margin: Decimal::from_str(&position["ath_margin_size"].as_str().unwrap()).unwrap(),
  65. // }
  66. // }
  67. // 处理order信息
  68. // pub fn handle_order(res_data: ResponseData, ct_val: Decimal) -> SpecialOrder {
  69. // let status = res_data.data["event"].as_str().unwrap();
  70. // let res_data_json = &res_data.data["order"];
  71. // let order_info = format_order_item(res_data_json, ct_val, status);
  72. //
  73. // SpecialOrder {
  74. // name: res_data.tag,
  75. // order: vec![order_info],
  76. // }
  77. // }
  78. // pub fn format_order_item(order: &Value, ct_val: Decimal, status: &str) -> Order {
  79. // let text = order["client_id"].as_str().unwrap_or("");
  80. // let size = Decimal::from_str(order["amount"].as_str().unwrap()).unwrap();
  81. // let left = Decimal::from_str(order["unfilled_amount"].as_str().unwrap()).unwrap();
  82. // // 已成交量
  83. // let filled_amount = size - left;
  84. // // 成交额
  85. // let filled_value = Decimal::from_str(order["filled_value"].as_str().unwrap()).unwrap();
  86. // // 成交均价
  87. // let mut avg_price = Decimal::ZERO;
  88. // if filled_amount > Decimal::ZERO{
  89. // avg_price = filled_value/filled_amount;
  90. // }
  91. // let amount = size * ct_val;
  92. // let deal_amount = filled_amount * ct_val;
  93. // let custom_status = if status == "finish" { "REMOVE".to_string() } else if status == "put" || status == "update" { "NEW".to_string() } else {
  94. // "NULL".to_string()
  95. // };
  96. // let rst_order = Order {
  97. // id: order["order_id"].to_string(),
  98. // custom_id: text.replace("t-my-custom-id_", "").replace("t-", ""),
  99. // price: Decimal::from_str(order["price"].as_str().unwrap()).unwrap(),
  100. // amount,
  101. // deal_amount,
  102. // avg_price,
  103. // status: custom_status,
  104. // order_type: "limit".to_string(),
  105. // };
  106. // return rst_order;
  107. // }
  108. pub fn format_trade_items(response: &ResponseData) -> Vec<Trade> {
  109. let symbol = response.data["market"].as_str().unwrap().to_string().replace("USDT", "_USDT");
  110. let result = response.data["deal_list"].as_array().unwrap();
  111. let mut trades = vec![];
  112. for item in result {
  113. let id = format!("{}", item["deal_id"].as_i64().unwrap());
  114. let time = Decimal::from_i64(item["created_at"].as_i64().unwrap()).unwrap();
  115. let mut size = Decimal::from_str(item["amount"].as_str().unwrap()).unwrap();
  116. if item["side"].as_str().unwrap().eq("sell") {
  117. size = size * Decimal::NEGATIVE_ONE;
  118. }
  119. let price = Decimal::from_str(item["price"].as_str().unwrap().to_string().as_str()).unwrap();
  120. let value = (price * size).abs();
  121. let trade = Trade {
  122. id,
  123. time,
  124. size,
  125. price,
  126. value,
  127. symbol: symbol.clone(),
  128. };
  129. trades.push(trade)
  130. }
  131. return trades
  132. }
  133. // 处理特殊Ticket信息
  134. // pub fn handle_ticker(res_data: &ResponseData) -> SpecialDepth {
  135. // let depth = &res_data.data["depth"];
  136. //
  137. // let bp = Decimal::from_str(depth["bids"][0][0].as_str().unwrap()).unwrap();
  138. // let bq = Decimal::from_str(depth["bids"][0][1].as_str().unwrap()).unwrap();
  139. // let ap = Decimal::from_str(depth["asks"][0][0].as_str().unwrap()).unwrap();
  140. // let aq = Decimal::from_str(depth["asks"][0][1].as_str().unwrap()).unwrap();
  141. // let mp = (bp + ap) * dec!(0.5);
  142. // let t = Decimal::from_i64(depth.get("checksum").unwrap().as_i64().unwrap_or(0i64)).unwrap();
  143. // let create_at = depth.get("updated_at").unwrap().as_i64().unwrap() * 1000;
  144. //
  145. // let ticker_info = SpecialTicker { sell: ap, buy: bp, mid_price: mp, t, create_at };
  146. // let depth_info = vec![bp, bq, ap, aq];
  147. //
  148. // SpecialDepth {
  149. // name: (*res_data).tag.clone(),
  150. // depth: depth_info,
  151. // ticker: ticker_info,
  152. // t,
  153. // create_at,
  154. // }
  155. // }
  156. // pub fn format_depth_items(value: &Value) -> Vec<MarketOrder> {
  157. // if value.is_null() {
  158. // return vec![];
  159. // }
  160. // let mut depth_items: Vec<MarketOrder> = vec![];
  161. // for value in value.as_array().unwrap() {
  162. // let values = value.as_array().unwrap();
  163. // depth_items.push(MarketOrder {
  164. // price: Decimal::from_str(values[0].as_str().unwrap()).unwrap(),
  165. // amount: Decimal::from_str(values[1].as_str().unwrap()).unwrap(),
  166. // })
  167. // }
  168. // return depth_items;
  169. // }