// use rust_decimal::Decimal; // use rust_decimal::prelude::FromPrimitive; // use serde_json::Value; // use exchanges::response_base::ResponseData; // use crate::{Record, Trade}; // // pub fn format_trade_items(response: &ResponseData) -> Vec { // let symbol = response.data["symbol"].as_str().unwrap().to_string(); // let data = response.data["data"].as_array().unwrap(); // let mut trades = vec![]; // // for item in data { // let id = format!("{}", item["id"].as_i64().unwrap()); // let time = Decimal::from_i64(item["ts"].as_i64().unwrap()).unwrap(); // let mut size = Decimal::from_i64(item["amount"].as_i64().unwrap()).unwrap(); // if item["direction"].as_str().unwrap().eq("sell") { // size = size * Decimal::NEGATIVE_ONE; // } // let price = Decimal::from_f64(item["price"].as_f64().unwrap()).unwrap(); // // let trade = Trade { // id, // time, // size, // price, // symbol: symbol.clone(), // }; // // trades.push(trade) // } // // return trades // } // // pub fn handle_records(value: &Value) -> Vec { // let time = Decimal::from_i64(value["id"].as_i64().unwrap() * 1000).unwrap(); // // let open = Decimal::from_f64(value["open"].as_f64().unwrap()).unwrap(); // let high = Decimal::from_f64(value["high"].as_f64().unwrap()).unwrap(); // let low = Decimal::from_f64(value["low"].as_f64().unwrap()).unwrap(); // let close = Decimal::from_f64(value["close"].as_f64().unwrap()).unwrap(); // let volume = Decimal::from_f64(value["trade_turnover"].as_f64().unwrap()).unwrap(); // let symbol = value["symbol"].as_str().unwrap().to_string(); // // vec![Record { // time, // open, // high, // low, // close, // volume, // symbol, // }] // } // // // 处理账号信息 // // pub fn handle_account_info(response: &ResponseData, _symbol: &String) -> Account { // // let mut result = Account::new(); // // let account_infos = response.data.as_array().unwrap(); // // for data in account_infos { // // if data["margin_asset"].as_str().unwrap() != "USDT" { continue; } // // let margin_position = Decimal::from_str(&data["margin_position"].to_string()).unwrap(); // // // // let balance = Decimal::from_str(&data["margin_balance"].to_string()).unwrap(); // // let frozen_balance = Decimal::from_str(&data["margin_frozen"].to_string()).unwrap(); // // let available_balance = balance - margin_position - frozen_balance; // // // 格式化account信息 // // let account = Account { // // coin: data["margin_asset"].as_str().unwrap().to_string(), // // balance, // // available_balance, // // frozen_balance, // // stocks: Default::default(), // // available_stocks: Default::default(), // // frozen_stocks: Default::default(), // // }; // // result = account // // } // // return result; // // } // // // 处理order信息 // // pub fn handle_order(res_data: ResponseData, ct_val: Decimal) -> SpecialOrder { // // let res_data_json = res_data.data; // // let order_info = vec![format_order_item(res_data_json.clone(), ct_val)]; // // SpecialOrder { // // name: res_data.tag, // // order: order_info, // // } // // } // // // 处理订单信息 // // pub fn format_order_item(order: serde_json::Value, ct_val: Decimal) -> Order { // // let id = order["order_id"].to_string(); // // let custom_id = order["client_order_id"].to_string(); // // let price = Decimal::from_str(&order["price"].to_string()).unwrap(); // // let amount = Decimal::from_str(&order["volume"].to_string()).unwrap() * ct_val; // // let deal_amount = Decimal::from_str(&order["trade_volume"].to_string()).unwrap() * ct_val; // // let avg_price = Decimal::from_f64(order["trade_avg_price"].as_f64().unwrap_or(0.0)).unwrap() * ct_val; // // // // let status = order["status"].to_string(); // // // // let custom_status = if ["6", "7", "11"].contains(&&**&status) { // // "REMOVE".to_string() // // } else if ["1", "2", "3", "4", "5"].contains(&&**&status) { // // "NEW".to_string() // // } else { // // "NULL".to_string() // // }; // // Order { // // id, // // custom_id, // // price, // // amount, // // deal_amount, // // avg_price, // // status: custom_status, // // order_type: order["order_price_type"].as_str().unwrap().to_string(), // // } // // } // // // 格式化深度信息 // // pub fn format_depth_items(value: serde_json::Value) -> Vec { // // let mut depth_items: Vec = vec![]; // // for value in value.as_array().unwrap() { // // depth_items.push(OrderBook { // // price: Decimal::from_str(&value[0].to_string()).unwrap(), // // amount: Decimal::from_str(&value[1].to_string()).unwrap(), // // }) // // } // // return depth_items; // // } // // // 处理position信息 // // pub fn handle_position(res_data: &ResponseData, ct_val: &Decimal) -> Vec { // // let res_data_json = res_data.data.as_array().unwrap(); // // res_data_json.iter().map(|item| { format_position_item(item, ct_val) }).collect() // // } // // // pub fn format_position_item(position: &serde_json::Value, ct_val: &Decimal) -> Position { // // let symbol = position["symbol"].as_str().unwrap().to_string(); // // let margin_level = Decimal::from_str(&position["lever_rate"].to_string()).unwrap(); // // let amount = Decimal::from_f64(position["volume"].as_f64().unwrap()).unwrap() * ct_val; // // // // let frozen_amount = Decimal::from_f64(position["frozen"].as_f64().unwrap()).unwrap(); // // let price = Decimal::from_f64(position["cost_hold"].as_f64().unwrap()).unwrap(); // // let profit = Decimal::from_f64(position["profit_unreal"].as_f64().unwrap()).unwrap(); // // let position_mode = match position["position_mode"].as_str().unwrap() { // // "dual_side" => { // // match position["direction"].as_str().unwrap() { // // "sell" => { // // PositionModeEnum::Short // // } // // "buy" => { // // PositionModeEnum::Long // // } // // _ => { // // panic!("htx_usdt_swap: 未知的持仓模式与持仓方向: {}, {}", // // position["direction"].as_str().unwrap(), position["direction"].as_str().unwrap()) // // } // // } // // } // // "single_side" => { // // PositionModeEnum::Both // // } // // _ => { // // panic!("htx_usdt_swap: 未知的持仓模式: {}", position["position_mode"].as_str().unwrap()) // // } // // }; // // let margin = Decimal::from_f64(position["position_margin"].as_f64().unwrap()).unwrap(); // // // // Position { // // symbol, // // margin_level, // // amount, // // frozen_amount, // // price, // // profit, // // position_mode, // // margin, // // } // // }