| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- use std::str::FromStr;
- use rust_decimal::Decimal;
- use rust_decimal::prelude::FromPrimitive;
- use exchanges::response_base::ResponseData;
- use crate::{Trade};
- // 处理账号信息
- // pub fn handle_account_info(res_data: &ResponseData, symbol: &String) -> Account {
- // let res_data_json = res_data.data["balance_list"].as_array().unwrap();
- // format_account_info(res_data_json, symbol)
- // }
- // pub fn format_account_info(data: &Vec<Value>, symbol: &String) -> Account {
- // let symbol_upper = symbol.to_uppercase();
- // let symbol_array: Vec<&str> = symbol_upper.split("_").collect();
- // let balance_info = data.iter().find(|&item| item["ccy"].as_str().unwrap().contains(symbol_array[1]));
- //
- // match balance_info {
- // None => {
- // error!("Coinex:格式化账号信息错误!\nformat_account_info: data={:?}", data);
- // panic!("Coinex:格式化账号信息错误!\nformat_account_info: data={:?}", data)
- // }
- // Some(value) => {
- // let frozen_balance= Decimal::from_str(&value["frozen"].as_str().unwrap()).unwrap();
- // let available_balance = Decimal::from_str(&value["available"].as_str().unwrap()).unwrap();
- // let margin = Decimal::from_str(&value["margin"].as_str().unwrap()).unwrap();
- // let profit_unreal = Decimal::from_str(&value["unrealized_pnl"].as_str().unwrap()).unwrap();
- // let balance = frozen_balance + available_balance + margin + profit_unreal;
- // Account {
- // coin: symbol_array[1].to_string(),
- // balance,
- // available_balance: Decimal::ZERO,
- // frozen_balance: Decimal::ZERO,
- // stocks: Decimal::ZERO,
- // available_stocks: Decimal::ZERO,
- // frozen_stocks: Decimal::ZERO,
- // }
- // }
- // }
- // }
- // 处理position信息
- // pub fn handle_position(res_data: &ResponseData, ct_val: &Decimal) -> Vec<Position> {
- // let res_data_json = &res_data.data["position"];
- // let position = format_position_item(res_data_json, ct_val);
- // vec![position]
- // }
- // pub fn format_position_item(position: &Value, ct_val: &Decimal) -> Position {
- // let position_mode = match position["side"].as_str().unwrap_or("") {
- // "long" => PositionModeEnum::Long,
- // "short" => PositionModeEnum::Short,
- // _ => {
- // error!("Coinex:格式化持仓模式错误!\nformat_position_item:position={:?}", position);
- // panic!("Coinex:格式化持仓模式错误!\nformat_position_item:position={:?}", position)
- // }
- // };
- // let size = Decimal::from_str(&position["open_interest"].as_str().unwrap()).unwrap();
- // let amount = size * ct_val;
- // Position {
- // symbol: position["market"].as_str().unwrap().to_string(),
- // margin_level: Decimal::from_str(&position["leverage"].as_str().unwrap()).unwrap(),
- // amount,
- // frozen_amount: Decimal::ZERO,
- // price: Decimal::from_str(&position["avg_entry_price"].as_str().unwrap()).unwrap(),
- // profit: Decimal::from_str(&position["unrealized_pnl"].as_str().unwrap()).unwrap(),
- // position_mode,
- // margin: Decimal::from_str(&position["ath_margin_size"].as_str().unwrap()).unwrap(),
- // }
- // }
- // 处理order信息
- // pub fn handle_order(res_data: ResponseData, ct_val: Decimal) -> SpecialOrder {
- // let status = res_data.data["event"].as_str().unwrap();
- // let res_data_json = &res_data.data["order"];
- // let order_info = format_order_item(res_data_json, ct_val, status);
- //
- // SpecialOrder {
- // name: res_data.tag,
- // order: vec![order_info],
- // }
- // }
- // pub fn format_order_item(order: &Value, ct_val: Decimal, status: &str) -> Order {
- // let text = order["client_id"].as_str().unwrap_or("");
- // let size = Decimal::from_str(order["amount"].as_str().unwrap()).unwrap();
- // let left = Decimal::from_str(order["unfilled_amount"].as_str().unwrap()).unwrap();
- // // 已成交量
- // let filled_amount = size - left;
- // // 成交额
- // let filled_value = Decimal::from_str(order["filled_value"].as_str().unwrap()).unwrap();
- // // 成交均价
- // let mut avg_price = Decimal::ZERO;
- // if filled_amount > Decimal::ZERO{
- // avg_price = filled_value/filled_amount;
- // }
- // let amount = size * ct_val;
- // let deal_amount = filled_amount * ct_val;
- // let custom_status = if status == "finish" { "REMOVE".to_string() } else if status == "put" || status == "update" { "NEW".to_string() } else {
- // "NULL".to_string()
- // };
- // let rst_order = Order {
- // id: order["order_id"].to_string(),
- // custom_id: text.replace("t-my-custom-id_", "").replace("t-", ""),
- // price: Decimal::from_str(order["price"].as_str().unwrap()).unwrap(),
- // amount,
- // deal_amount,
- // avg_price,
- // status: custom_status,
- // order_type: "limit".to_string(),
- // };
- // return rst_order;
- // }
- pub fn format_trade_items(response: &ResponseData) -> Vec<Trade> {
- let symbol = response.data["market"].as_str().unwrap().to_string().replace("USDT", "_USDT");
- let result = response.data["deal_list"].as_array().unwrap();
- let mut trades = vec![];
- for item in result {
- let id = format!("{}", item["deal_id"].as_i64().unwrap());
- let time = Decimal::from_i64(item["created_at"].as_i64().unwrap()).unwrap();
- let mut size = Decimal::from_str(item["amount"].as_str().unwrap()).unwrap();
- if item["side"].as_str().unwrap().eq("sell") {
- size = size * Decimal::NEGATIVE_ONE;
- }
- let price = Decimal::from_str(item["price"].as_str().unwrap().to_string().as_str()).unwrap();
- let value = (price * size).abs();
- let trade = Trade {
- id,
- time,
- size,
- price,
- value,
- symbol: symbol.clone(),
- };
- trades.push(trade)
- }
- return trades
- }
- // 处理特殊Ticket信息
- // pub fn handle_ticker(res_data: &ResponseData) -> SpecialDepth {
- // let depth = &res_data.data["depth"];
- //
- // let bp = Decimal::from_str(depth["bids"][0][0].as_str().unwrap()).unwrap();
- // let bq = Decimal::from_str(depth["bids"][0][1].as_str().unwrap()).unwrap();
- // let ap = Decimal::from_str(depth["asks"][0][0].as_str().unwrap()).unwrap();
- // let aq = Decimal::from_str(depth["asks"][0][1].as_str().unwrap()).unwrap();
- // let mp = (bp + ap) * dec!(0.5);
- // let t = Decimal::from_i64(depth.get("checksum").unwrap().as_i64().unwrap_or(0i64)).unwrap();
- // let create_at = depth.get("updated_at").unwrap().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).tag.clone(),
- // depth: depth_info,
- // ticker: ticker_info,
- // t,
- // create_at,
- // }
- // }
- // pub fn format_depth_items(value: &Value) -> Vec<MarketOrder> {
- // if value.is_null() {
- // return vec![];
- // }
- // let mut depth_items: Vec<MarketOrder> = vec![];
- // for value in value.as_array().unwrap() {
- // let values = value.as_array().unwrap();
- // depth_items.push(MarketOrder {
- // price: Decimal::from_str(values[0].as_str().unwrap()).unwrap(),
- // amount: Decimal::from_str(values[1].as_str().unwrap()).unwrap(),
- // })
- // }
- // return depth_items;
- // }
|