|
|
@@ -6,12 +6,12 @@ use rust_decimal::Decimal;
|
|
|
use tokio::{spawn, time};
|
|
|
use tokio::sync::Mutex;
|
|
|
use tokio::time::Instant;
|
|
|
-use tracing::info;
|
|
|
use exchanges::bybit_swap_ws::{BybitSwapLogin, BybitSwapSubscribeType, BybitSwapWs, BybitSwapWsType};
|
|
|
use exchanges::response_base::ResponseData;
|
|
|
use global::trace_stack::TraceStack;
|
|
|
use standard::exchange::ExchangeEnum::BybitSwap;
|
|
|
use crate::core::Core;
|
|
|
+use crate::exchange_disguise::on_special_depth;
|
|
|
use crate::model::OrderInfo;
|
|
|
|
|
|
// 1交易、0参考 bybit 合约 启动
|
|
|
@@ -30,11 +30,11 @@ pub async fn bybit_swap_run(is_shutdown_arc: Arc<AtomicBool>,
|
|
|
ws_public.set_subscribe(vec![
|
|
|
BybitSwapSubscribeType::PuTickers
|
|
|
]);
|
|
|
- if is_trade {
|
|
|
- ws_public.set_subscribe(vec![
|
|
|
- BybitSwapSubscribeType::PuBlicTrade
|
|
|
- ]);
|
|
|
- }
|
|
|
+ // if is_trade {
|
|
|
+ // ws_public.set_subscribe(vec![
|
|
|
+ // BybitSwapSubscribeType::PuBlicTrade
|
|
|
+ // ]);
|
|
|
+ // }
|
|
|
// 挂起公共ws
|
|
|
let write_tx_am_public = Arc::new(Mutex::new(write_tx_public));
|
|
|
let is_shutdown_clone_public = Arc::clone(&is_shutdown_arc);
|
|
|
@@ -85,7 +85,7 @@ pub async fn bybit_swap_run(is_shutdown_arc: Arc<AtomicBool>,
|
|
|
let run_symbol_clone = run_symbol.clone();
|
|
|
|
|
|
async move {
|
|
|
- on_private_data(core_arc_clone.clone(), &ct_val, data, &run_symbol_clone).await;
|
|
|
+ on_private_data(core_arc_clone.clone(), &ct_val, &run_symbol_clone, data).await;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -110,114 +110,120 @@ pub async fn bybit_swap_run(is_shutdown_arc: Arc<AtomicBool>,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-async fn on_private_data(core_arc_clone: Arc<Mutex<Core>>,
|
|
|
- ct_val: &Decimal,
|
|
|
- data: ResponseData,
|
|
|
- run_symbol: &String) {
|
|
|
+async fn on_private_data(core_arc_clone: Arc<Mutex<Core>>, ct_val: &Decimal, run_symbol: &String, data: ResponseData) {
|
|
|
let mut trace_stack = TraceStack::new(data.time, data.ins);
|
|
|
trace_stack.on_after_span_line();
|
|
|
|
|
|
- if data.code != "200".to_string() {
|
|
|
+ if data.code.as_str() != "200" {
|
|
|
return;
|
|
|
}
|
|
|
- if data.channel == "wallet" {
|
|
|
- let account = standard::handle_info::HandleSwapInfo::handle_account_info(BybitSwap, &data, run_symbol);
|
|
|
- {
|
|
|
+
|
|
|
+ match data.channel.as_str() {
|
|
|
+ "wallet" => {
|
|
|
+ let account = standard::handle_info::HandleSwapInfo::handle_account_info(BybitSwap, &data, run_symbol);
|
|
|
let mut core = core_arc_clone.lock().await;
|
|
|
core.update_equity(account).await;
|
|
|
}
|
|
|
- } else if data.channel == "order" {
|
|
|
- let orders = standard::handle_info::HandleSwapInfo::handle_order(BybitSwap, data.clone(), ct_val.clone());
|
|
|
- trace_stack.on_after_format();
|
|
|
+ "order" => {
|
|
|
+ let orders = standard::handle_info::HandleSwapInfo::handle_order(BybitSwap, data.clone(), ct_val.clone());
|
|
|
+ trace_stack.on_after_format();
|
|
|
|
|
|
- let mut order_infos:Vec<OrderInfo> = Vec::new();
|
|
|
- for order in orders.order {
|
|
|
- if order.status == "NULL" {
|
|
|
- continue;
|
|
|
+ let mut order_infos:Vec<OrderInfo> = Vec::new();
|
|
|
+ for order in orders.order {
|
|
|
+ if order.status == "NULL" {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ let order_info = OrderInfo {
|
|
|
+ symbol: "".to_string(),
|
|
|
+ amount: order.amount.abs(),
|
|
|
+ side: "".to_string(),
|
|
|
+ price: order.price,
|
|
|
+ client_id: order.custom_id,
|
|
|
+ filled_price: order.avg_price,
|
|
|
+ filled: order.deal_amount.abs(),
|
|
|
+ order_id: order.id,
|
|
|
+ local_time: 0,
|
|
|
+ create_time: 0,
|
|
|
+ status: order.status,
|
|
|
+ fee: Default::default(),
|
|
|
+ trace_stack: TraceStack::new(0, Instant::now()),
|
|
|
+ };
|
|
|
+ order_infos.push(order_info);
|
|
|
}
|
|
|
- let order_info = OrderInfo {
|
|
|
- symbol: "".to_string(),
|
|
|
- amount: order.amount.abs(),
|
|
|
- side: "".to_string(),
|
|
|
- price: order.price,
|
|
|
- client_id: order.custom_id,
|
|
|
- filled_price: order.avg_price,
|
|
|
- filled: order.deal_amount.abs(),
|
|
|
- order_id: order.id,
|
|
|
- local_time: 0,
|
|
|
- create_time: 0,
|
|
|
- status: order.status,
|
|
|
- fee: Default::default(),
|
|
|
- trace_stack: TraceStack::new(0, Instant::now()),
|
|
|
- };
|
|
|
- order_infos.push(order_info);
|
|
|
- }
|
|
|
|
|
|
- {
|
|
|
let mut core = core_arc_clone.lock().await;
|
|
|
core.update_order(order_infos, trace_stack).await;
|
|
|
}
|
|
|
- } else if data.channel == "position" {
|
|
|
- let positions = standard::handle_info::HandleSwapInfo::handle_position(BybitSwap, &data, ct_val);
|
|
|
- {
|
|
|
+ "position" => {
|
|
|
+ let positions = standard::handle_info::HandleSwapInfo::handle_position(BybitSwap, &data, ct_val);
|
|
|
let mut core = core_arc_clone.lock().await;
|
|
|
core.update_position(positions).await;
|
|
|
}
|
|
|
+ _ => {}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-async fn on_public_data(_core_arc_clone: Arc<Mutex<Core>>,
|
|
|
- _update_flag_u: &mut Decimal,
|
|
|
- data: ResponseData) {
|
|
|
- let mut trace_stack = TraceStack::new(data.time, data.ins);
|
|
|
- trace_stack.on_after_span_line();
|
|
|
-
|
|
|
- if data.code != "200".to_string() {
|
|
|
+async fn on_public_data(core_arc_clone: Arc<Mutex<Core>>, update_flag_u: &mut Decimal, response: ResponseData) {
|
|
|
+ if response.code.as_str() != "200" {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- info!(?data);
|
|
|
+ let mut trace_stack = TraceStack::new(response.time, response.ins);
|
|
|
+ trace_stack.on_after_span_line();
|
|
|
+
|
|
|
+ match response.channel.as_str() {
|
|
|
+ "tickers" => {
|
|
|
+ trace_stack.set_source("bybit_usdt_swap.tickers".to_string());
|
|
|
|
|
|
- if data.channel == "orderbook" {
|
|
|
- // let mut is_update = false;
|
|
|
- // let data_type = data.data_type.clone();
|
|
|
- // let label = data.label.clone();
|
|
|
- // if data_type == "delta" {
|
|
|
- // is_update = true;
|
|
|
- // }
|
|
|
- // let mut depth_format: DepthParam = format_depth(BybitSwap, &data);
|
|
|
- // // 是增量更新
|
|
|
- // if is_update {
|
|
|
- // update_order_book(depth_asks, depth_bids, depth_format.depth_asks, depth_format.depth_bids);
|
|
|
- // } else { // 全量
|
|
|
- // depth_asks.clear();
|
|
|
- // depth_asks.append(&mut depth_format.depth_asks);
|
|
|
- // depth_bids.clear();
|
|
|
- // depth_bids.append(&mut depth_format.depth_bids);
|
|
|
- // }
|
|
|
- // let depth = make_special_depth(label.clone(), depth_asks, depth_bids, depth_format.t, depth_format.create_at);
|
|
|
- // trace_stack.on_before_network(depth_format.create_at.clone());
|
|
|
- // trace_stack.on_after_format();
|
|
|
- //
|
|
|
- // on_special_depth(core_arc_clone, update_flag_u, &label, &mut trace_stack, &depth).await;
|
|
|
- } else if data.channel == "trade" {
|
|
|
- // let mut core = core_arc_clone.lock().await;
|
|
|
- // let str = data.label.clone();
|
|
|
- // if core.is_update.contains_key(&data.label) && *core.is_update.get(str.as_str()).unwrap(){
|
|
|
- // *max_buy = Decimal::ZERO;
|
|
|
- // *min_sell = Decimal::ZERO;
|
|
|
- // core.is_update.remove(str.as_str());
|
|
|
- // }
|
|
|
- // let trades: Vec<OriginalTradeBy> = serde_json::from_str(data.data.as_str()).unwrap();
|
|
|
- // for trade in trades {
|
|
|
- // if trade.p > *max_buy || *max_buy == Decimal::ZERO{
|
|
|
- // *max_buy = trade.p
|
|
|
- // }
|
|
|
- // if trade.p < *min_sell || *min_sell == Decimal::ZERO{
|
|
|
- // *min_sell = trade.p
|
|
|
- // }
|
|
|
- // }
|
|
|
- // core.max_buy_min_sell_cache.insert(data.label, vec![*max_buy, *min_sell]);
|
|
|
+ // 处理ticker信息
|
|
|
+ let special_depth = standard::handle_info::HandleSwapInfo::handle_book_ticker(BybitSwap, &response);
|
|
|
+ trace_stack.on_after_format();
|
|
|
+
|
|
|
+ on_special_depth(core_arc_clone, update_flag_u, &response.label, &mut trace_stack, &special_depth).await;
|
|
|
+ }
|
|
|
+ "orderbook" => {
|
|
|
+ // let mut is_update = false;
|
|
|
+ // let data_type = data.data_type.clone();
|
|
|
+ // let label = data.label.clone();
|
|
|
+ // if data_type == "delta" {
|
|
|
+ // is_update = true;
|
|
|
+ // }
|
|
|
+ // let mut depth_format: DepthParam = format_depth(BybitSwap, &data);
|
|
|
+ // // 是增量更新
|
|
|
+ // if is_update {
|
|
|
+ // update_order_book(depth_asks, depth_bids, depth_format.depth_asks, depth_format.depth_bids);
|
|
|
+ // } else { // 全量
|
|
|
+ // depth_asks.clear();
|
|
|
+ // depth_asks.append(&mut depth_format.depth_asks);
|
|
|
+ // depth_bids.clear();
|
|
|
+ // depth_bids.append(&mut depth_format.depth_bids);
|
|
|
+ // }
|
|
|
+ // let depth = make_special_depth(label.clone(), depth_asks, depth_bids, depth_format.t, depth_format.create_at);
|
|
|
+ // trace_stack.on_before_network(depth_format.create_at.clone());
|
|
|
+ // trace_stack.on_after_format();
|
|
|
+ //
|
|
|
+ // on_special_depth(core_arc_clone, update_flag_u, &label, &mut trace_stack, &depth).await;
|
|
|
+ }
|
|
|
+ "trade" => {
|
|
|
+ // let mut core = core_arc_clone.lock().await;
|
|
|
+ // let str = data.label.clone();
|
|
|
+ // if core.is_update.contains_key(&data.label) && *core.is_update.get(str.as_str()).unwrap(){
|
|
|
+ // *max_buy = Decimal::ZERO;
|
|
|
+ // *min_sell = Decimal::ZERO;
|
|
|
+ // core.is_update.remove(str.as_str());
|
|
|
+ // }
|
|
|
+ // let trades: Vec<OriginalTradeBy> = serde_json::from_str(data.data.as_str()).unwrap();
|
|
|
+ // for trade in trades {
|
|
|
+ // if trade.p > *max_buy || *max_buy == Decimal::ZERO{
|
|
|
+ // *max_buy = trade.p
|
|
|
+ // }
|
|
|
+ // if trade.p < *min_sell || *min_sell == Decimal::ZERO{
|
|
|
+ // *min_sell = trade.p
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // core.max_buy_min_sell_cache.insert(data.label, vec![*max_buy, *min_sell]);
|
|
|
+ }
|
|
|
+ _ => {}
|
|
|
}
|
|
|
}
|
|
|
|