htx_swap_handle.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // use rust_decimal::Decimal;
  2. // use rust_decimal::prelude::FromPrimitive;
  3. // use serde_json::Value;
  4. // use exchanges::response_base::ResponseData;
  5. // use crate::{Record, Trade};
  6. //
  7. // pub fn format_trade_items(response: &ResponseData) -> Vec<Trade> {
  8. // let symbol = response.data["symbol"].as_str().unwrap().to_string();
  9. // let data = response.data["data"].as_array().unwrap();
  10. // let mut trades = vec![];
  11. //
  12. // for item in data {
  13. // let id = format!("{}", item["id"].as_i64().unwrap());
  14. // let time = Decimal::from_i64(item["ts"].as_i64().unwrap()).unwrap();
  15. // let mut size = Decimal::from_i64(item["amount"].as_i64().unwrap()).unwrap();
  16. // if item["direction"].as_str().unwrap().eq("sell") {
  17. // size = size * Decimal::NEGATIVE_ONE;
  18. // }
  19. // let price = Decimal::from_f64(item["price"].as_f64().unwrap()).unwrap();
  20. //
  21. // let trade = Trade {
  22. // id,
  23. // time,
  24. // size,
  25. // price,
  26. // symbol: symbol.clone(),
  27. // };
  28. //
  29. // trades.push(trade)
  30. // }
  31. //
  32. // return trades
  33. // }
  34. //
  35. // pub fn handle_records(value: &Value) -> Vec<Record> {
  36. // let time = Decimal::from_i64(value["id"].as_i64().unwrap() * 1000).unwrap();
  37. //
  38. // let open = Decimal::from_f64(value["open"].as_f64().unwrap()).unwrap();
  39. // let high = Decimal::from_f64(value["high"].as_f64().unwrap()).unwrap();
  40. // let low = Decimal::from_f64(value["low"].as_f64().unwrap()).unwrap();
  41. // let close = Decimal::from_f64(value["close"].as_f64().unwrap()).unwrap();
  42. // let volume = Decimal::from_f64(value["trade_turnover"].as_f64().unwrap()).unwrap();
  43. // let symbol = value["symbol"].as_str().unwrap().to_string();
  44. //
  45. // vec![Record {
  46. // time,
  47. // open,
  48. // high,
  49. // low,
  50. // close,
  51. // volume,
  52. // symbol,
  53. // }]
  54. // }
  55. //
  56. // // 处理账号信息
  57. // // pub fn handle_account_info(response: &ResponseData, _symbol: &String) -> Account {
  58. // // let mut result = Account::new();
  59. // // let account_infos = response.data.as_array().unwrap();
  60. // // for data in account_infos {
  61. // // if data["margin_asset"].as_str().unwrap() != "USDT" { continue; }
  62. // // let margin_position = Decimal::from_str(&data["margin_position"].to_string()).unwrap();
  63. // //
  64. // // let balance = Decimal::from_str(&data["margin_balance"].to_string()).unwrap();
  65. // // let frozen_balance = Decimal::from_str(&data["margin_frozen"].to_string()).unwrap();
  66. // // let available_balance = balance - margin_position - frozen_balance;
  67. // // // 格式化account信息
  68. // // let account = Account {
  69. // // coin: data["margin_asset"].as_str().unwrap().to_string(),
  70. // // balance,
  71. // // available_balance,
  72. // // frozen_balance,
  73. // // stocks: Default::default(),
  74. // // available_stocks: Default::default(),
  75. // // frozen_stocks: Default::default(),
  76. // // };
  77. // // result = account
  78. // // }
  79. // // return result;
  80. // // }
  81. //
  82. // // 处理order信息
  83. // // pub fn handle_order(res_data: ResponseData, ct_val: Decimal) -> SpecialOrder {
  84. // // let res_data_json = res_data.data;
  85. // // let order_info = vec![format_order_item(res_data_json.clone(), ct_val)];
  86. // // SpecialOrder {
  87. // // name: res_data.tag,
  88. // // order: order_info,
  89. // // }
  90. // // }
  91. //
  92. // // 处理订单信息
  93. // // pub fn format_order_item(order: serde_json::Value, ct_val: Decimal) -> Order {
  94. // // let id = order["order_id"].to_string();
  95. // // let custom_id = order["client_order_id"].to_string();
  96. // // let price = Decimal::from_str(&order["price"].to_string()).unwrap();
  97. // // let amount = Decimal::from_str(&order["volume"].to_string()).unwrap() * ct_val;
  98. // // let deal_amount = Decimal::from_str(&order["trade_volume"].to_string()).unwrap() * ct_val;
  99. // // let avg_price = Decimal::from_f64(order["trade_avg_price"].as_f64().unwrap_or(0.0)).unwrap() * ct_val;
  100. // //
  101. // // let status = order["status"].to_string();
  102. // //
  103. // // let custom_status = if ["6", "7", "11"].contains(&&**&status) {
  104. // // "REMOVE".to_string()
  105. // // } else if ["1", "2", "3", "4", "5"].contains(&&**&status) {
  106. // // "NEW".to_string()
  107. // // } else {
  108. // // "NULL".to_string()
  109. // // };
  110. // // Order {
  111. // // id,
  112. // // custom_id,
  113. // // price,
  114. // // amount,
  115. // // deal_amount,
  116. // // avg_price,
  117. // // status: custom_status,
  118. // // order_type: order["order_price_type"].as_str().unwrap().to_string(),
  119. // // }
  120. // // }
  121. //
  122. // // 格式化深度信息
  123. // // pub fn format_depth_items(value: serde_json::Value) -> Vec<OrderBook> {
  124. // // let mut depth_items: Vec<OrderBook> = vec![];
  125. // // for value in value.as_array().unwrap() {
  126. // // depth_items.push(OrderBook {
  127. // // price: Decimal::from_str(&value[0].to_string()).unwrap(),
  128. // // amount: Decimal::from_str(&value[1].to_string()).unwrap(),
  129. // // })
  130. // // }
  131. // // return depth_items;
  132. // // }
  133. //
  134. // // 处理position信息
  135. // // pub fn handle_position(res_data: &ResponseData, ct_val: &Decimal) -> Vec<Position> {
  136. // // let res_data_json = res_data.data.as_array().unwrap();
  137. // // res_data_json.iter().map(|item| { format_position_item(item, ct_val) }).collect()
  138. // // }
  139. //
  140. // // pub fn format_position_item(position: &serde_json::Value, ct_val: &Decimal) -> Position {
  141. // // let symbol = position["symbol"].as_str().unwrap().to_string();
  142. // // let margin_level = Decimal::from_str(&position["lever_rate"].to_string()).unwrap();
  143. // // let amount = Decimal::from_f64(position["volume"].as_f64().unwrap()).unwrap() * ct_val;
  144. // //
  145. // // let frozen_amount = Decimal::from_f64(position["frozen"].as_f64().unwrap()).unwrap();
  146. // // let price = Decimal::from_f64(position["cost_hold"].as_f64().unwrap()).unwrap();
  147. // // let profit = Decimal::from_f64(position["profit_unreal"].as_f64().unwrap()).unwrap();
  148. // // let position_mode = match position["position_mode"].as_str().unwrap() {
  149. // // "dual_side" => {
  150. // // match position["direction"].as_str().unwrap() {
  151. // // "sell" => {
  152. // // PositionModeEnum::Short
  153. // // }
  154. // // "buy" => {
  155. // // PositionModeEnum::Long
  156. // // }
  157. // // _ => {
  158. // // panic!("htx_usdt_swap: 未知的持仓模式与持仓方向: {}, {}",
  159. // // position["direction"].as_str().unwrap(), position["direction"].as_str().unwrap())
  160. // // }
  161. // // }
  162. // // }
  163. // // "single_side" => {
  164. // // PositionModeEnum::Both
  165. // // }
  166. // // _ => {
  167. // // panic!("htx_usdt_swap: 未知的持仓模式: {}", position["position_mode"].as_str().unwrap())
  168. // // }
  169. // // };
  170. // // let margin = Decimal::from_f64(position["position_margin"].as_f64().unwrap()).unwrap();
  171. // //
  172. // // Position {
  173. // // symbol,
  174. // // margin_level,
  175. // // amount,
  176. // // frozen_amount,
  177. // // price,
  178. // // profit,
  179. // // position_mode,
  180. // // margin,
  181. // // }
  182. // // }