| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- use std::str::FromStr;
- use rust_decimal::Decimal;
- use rust_decimal_macros::dec;
- use serde_json::Value;
- use exchanges::response_base::ResponseData;
- use crate::{MarketOrder, SpecialDepth, SpecialTicker};
- // 处理特殊Ticker信息
- pub fn handle_book_ticker(res_data: &ResponseData) -> SpecialDepth {
- let bp = Decimal::from_str((*res_data).data["b"].as_str().unwrap()).unwrap();
- let bq = Decimal::from_str((*res_data).data["B"].as_str().unwrap()).unwrap();
- let ap = Decimal::from_str((*res_data).data["a"].as_str().unwrap()).unwrap();
- let aq = Decimal::from_str((*res_data).data["A"].as_str().unwrap()).unwrap();
- let mp = (bp + ap) * dec!(0.5);
- let t = Decimal::from_str(&(*res_data).data["u"].to_string()).unwrap();
- let create_at = (*res_data).data["E"].as_i64().unwrap() * 1000;
- let ticker_info = SpecialTicker { sell: ap, buy: bp, mid_price: mp, t, create_at };
- let depth_info = vec![bp, bq, ap, aq];
- SpecialDepth {
- name: (*res_data).label.clone(),
- depth: depth_info,
- ticker: ticker_info,
- t,
- create_at,
- }
- }
- // 格式化深度信息
- pub fn format_depth_items(value: Value) -> Vec<MarketOrder> {
- let mut depth_items: Vec<MarketOrder> = vec![];
- for value in value.as_array().unwrap() {
- depth_items.push(MarketOrder {
- price: Decimal::from_str(value[0].as_str().unwrap()).unwrap(),
- amount: Decimal::from_str(value[1].as_str().unwrap()).unwrap(),
- })
- }
- return depth_items;
- }
|