| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- use std::str::FromStr;
- use rust_decimal::Decimal;
- use rust_decimal::prelude::FromPrimitive;
- use serde_json::Value;
- use exchanges::response_base::ResponseData;
- use crate::{OrderBook, Trade, Record};
- pub fn handle_records(value: &Value) -> Vec<Record> {
- let symbol = value["arg"]["instId"].as_str().unwrap().to_string().replace("-", "_");
- let records_list = value["data"].as_array().unwrap();
- let mut records = vec![];
- for record_value in records_list {
- records.push(Record {
- time: Decimal::from_str(record_value[0].as_str().unwrap()).unwrap(),
- open: Decimal::from_str(record_value[1].as_str().unwrap()).unwrap(),
- high: Decimal::from_str(record_value[2].as_str().unwrap()).unwrap(),
- low: Decimal::from_str(record_value[3].as_str().unwrap()).unwrap(),
- close: Decimal::from_str(record_value[4].as_str().unwrap()).unwrap(),
- volume: Decimal::from_str(record_value[5].as_str().unwrap()).unwrap(),
- symbol: symbol.clone(),
- });
- }
- records
- }
- pub fn format_depth_items(value: &Value) -> Vec<OrderBook> {
- let mut depth_items: Vec<OrderBook> = vec![];
- for value in value.as_array().unwrap() {
- let price = Decimal::from_f64(value[0].as_f64().unwrap()).unwrap();
- let size = Decimal::from_f64(value[1].as_f64().unwrap()).unwrap();
- let value = price * size;
- depth_items.push(OrderBook {
- price,
- size,
- value,
- })
- }
- return depth_items;
- }
- pub fn format_trade_items(res_data: &ResponseData) -> Vec<Trade> {
- let result = res_data.data["data"].as_array().unwrap();
- let mut trades = vec![];
- for item in result {
- let side = item["side"].as_str().unwrap() == "buy";
- let size = if side {
- Decimal::from_str(item["sz"].as_str().unwrap()).unwrap()
- } else {
- -Decimal::from_str(item["sz"].as_str().unwrap()).unwrap()
- };
- let price = Decimal::from_str(item["px"].as_str().unwrap().to_string().as_str()).unwrap();
- let value = (size * price).abs();
- trades.push(Trade {
- id: item["tradeId"].as_str().unwrap().to_string(),
- time: Decimal::from_str(item["ts"].as_str().unwrap()).unwrap(),
- size,
- price,
- value,
- symbol: item["instId"].as_str().unwrap().to_string().replace("-", "_"),
- })
- }
- trades
- }
|