JiahengHe 2 years ago
parent
commit
77caa7d48e
1 changed files with 107 additions and 105 deletions
  1. 107 105
      strategy/src/quant.rs

+ 107 - 105
strategy/src/quant.rs

@@ -1168,76 +1168,77 @@ pub async fn run_transaction(quant_arc: Arc<Mutex<Quant>>, name: String, symbols
             match rx.recv().await {
                 Some(data) => {
                     debug!(?data);
-                    if data.code == "200" {
-                        if data.channel == "futures.order_book" {
-                            let depth = standard::gate_handle::handle_special_depth(data);
-                            {
-                                let mut quant = bot_arc_clone.lock().await;
-                                quant._update_depth(depth.depth.clone(), depth.name.clone());
-                                quant._update_ticker(depth.ticker.clone(), depth.name.clone());
-                                quant.local_depths.insert(depth.name, depth.depth);
-                            }
-                        } else if data.channel == "futures.balances" {
-                            let account = standard::gate_handle::handle_account_info(data, run_symbol.clone());
-                            {
-                                let mut quant = bot_arc_clone.lock().await;
-                                quant.update_equity(account);
-                            }
-                        } else if data.channel == "futures.orders" {
-                            let orders = standard::gate_handle::handle_order(data, multiplier.clone());
-                            let mut order_infos:Vec<OrderInfo> = Vec::new();
-                            for order in orders.order{
-                                let mut order_info = OrderInfo{
-                                    symbol: "".to_string(),
-                                    amount: Default::default(),
-                                    side: "".to_string(),
-                                    price: Default::default(),
-                                    client_id: order.custom_id,
-                                    filled_price: Default::default(),
-                                    filled: Default::default(),
-                                    order_id: order.id,
-                                    local_time: 0,
-                                    create_time: 0,
-                                    status: order.status,
-                                    fee: Default::default(),
-                                };
-                                if "REMOVE" == order_info.status {
-                                    order_info.amount = order.amount;
-                                    order_info.price = order.price;
-                                    order_info.filled = order.deal_amount;
-                                    order_info.filled_price = order.avg_price;
-                                }
-                                order_infos.push(order_info);
-                            }
-                            {
-                                let mut quant = bot_arc_clone.lock().await;
-                                quant.update_order(order_infos);
-                            }
-                        } else if data.channel == "futures.positions" {
-                            let positions = standard::gate_handle::handle_position(data, multiplier.clone());
-                            {
-                                let mut quant = bot_arc_clone.lock().await;
-                                quant.update_position(positions);
+                    if data.code != "200".to_string() {
+                        continue;
+                    }
+                    if data.channel == "futures.order_book" {
+                        let depth = standard::gate_handle::handle_special_depth(data);
+                        {
+                            let mut quant = bot_arc_clone.lock().await;
+                            quant._update_depth(depth.depth.clone(), depth.name.clone());
+                            quant._update_ticker(depth.ticker.clone(), depth.name.clone());
+                            quant.local_depths.insert(depth.name, depth.depth);
+                        }
+                    } else if data.channel == "futures.balances" {
+                        let account = standard::gate_handle::handle_account_info(data, run_symbol.clone());
+                        {
+                            let mut quant = bot_arc_clone.lock().await;
+                            quant.update_equity(account);
+                        }
+                    } else if data.channel == "futures.orders" {
+                        let orders = standard::gate_handle::handle_order(data, multiplier.clone());
+                        let mut order_infos:Vec<OrderInfo> = Vec::new();
+                        for order in orders.order{
+                            let mut order_info = OrderInfo{
+                                symbol: "".to_string(),
+                                amount: Default::default(),
+                                side: "".to_string(),
+                                price: Default::default(),
+                                client_id: order.custom_id,
+                                filled_price: Default::default(),
+                                filled: Default::default(),
+                                order_id: order.id,
+                                local_time: 0,
+                                create_time: 0,
+                                status: order.status,
+                                fee: Default::default(),
+                            };
+                            if "REMOVE" == order_info.status {
+                                order_info.amount = order.amount;
+                                order_info.price = order.price;
+                                order_info.filled = order.deal_amount;
+                                order_info.filled_price = order.avg_price;
                             }
-                        } else if data.channel == "futures.trades" {
+                            order_infos.push(order_info);
+                        }
+                        {
+                            let mut quant = bot_arc_clone.lock().await;
+                            quant.update_order(order_infos);
+                        }
+                    } else if data.channel == "futures.positions" {
+                        let positions = standard::gate_handle::handle_position(data, multiplier.clone());
+                        {
                             let mut quant = bot_arc_clone.lock().await;
-                            let str = data.lable.clone();
-                            if quant.is_update.contains_key(&data.lable) && *quant.is_update.get(str.as_str()).unwrap(){
-                                max_buy = Decimal::ZERO;
-                                min_sell = Decimal::ZERO;
-                                quant.is_update.remove(str.as_str());
+                            quant.update_position(positions);
+                        }
+                    } else if data.channel == "futures.trades" {
+                        let mut quant = bot_arc_clone.lock().await;
+                        let str = data.lable.clone();
+                        if quant.is_update.contains_key(&data.lable) && *quant.is_update.get(str.as_str()).unwrap(){
+                            max_buy = Decimal::ZERO;
+                            min_sell = Decimal::ZERO;
+                            quant.is_update.remove(str.as_str());
+                        }
+                        let trades: Vec<OriginalTradeGa> = serde_json::from_str(data.data.as_str()).unwrap();
+                        for trade in trades {
+                            if trade.price > max_buy || max_buy == Decimal::ZERO{
+                                max_buy = trade.price
                             }
-                            let trades: Vec<OriginalTradeGa> = serde_json::from_str(data.data.as_str()).unwrap();
-                            for trade in trades {
-                                if trade.price > max_buy || max_buy == Decimal::ZERO{
-                                    max_buy = trade.price
-                                }
-                                if trade.price < min_sell || min_sell == Decimal::ZERO{
-                                    min_sell = trade.price
-                                }
+                            if trade.price < min_sell || min_sell == Decimal::ZERO{
+                                min_sell = trade.price
                             }
-                            quant.max_buy_min_sell_cache.insert(data.lable, vec![max_buy, min_sell]);
                         }
+                        quant.max_buy_min_sell_cache.insert(data.lable, vec![max_buy, min_sell]);
                     }
                 },
                 None => {
@@ -1267,49 +1268,50 @@ pub async fn run_refer(quant_arc: Arc<Mutex<Quant>>, name: String, symbol: Vec<S
         loop {
             match rx.recv().await {
                 Some(data) => {
-                    if data.code == "200".to_string() {
-                        if data.channel == "aggTrade" {
-                            let trade: OriginalTradeBa = serde_json::from_str(data.data.as_str()).unwrap();
-                            let str = data.lable.clone();
-                            let mut quant = bot_arc_clone.lock().await;
-                            if quant.is_update.contains_key(&data.lable) && *quant.is_update.get(str.as_str()).unwrap(){
-                                max_buy = Decimal::ZERO;
-                                min_sell = Decimal::ZERO;
-                                quant.is_update.remove(str.as_str());
-                            }
-                            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
-                            }
-                            {
-                                quant.max_buy_min_sell_cache.insert(data.lable, vec![max_buy, min_sell]);
-                            }
-                        } else if data.channel == "bookTicker" {
-                            let ticker: OriginalTicker = serde_json::from_str(data.data.as_str()).unwrap();
-                            if ticker.u > update_flag_u {
-                                {
-                                    let mut quant = bot_arc_clone.lock().await;
-                                    quant._update_ticker(SpecialTicker{
-                                        sell: ticker.a.clone(),
-                                        buy: ticker.b.clone(),
-                                        mid_price: Default::default(),
-                                    }, data.lable.clone());
-                                    let depth = vec![ticker.b, ticker.B, ticker.a, ticker.A];
-                                    quant._update_depth(depth.clone(), data.lable.clone());
-                                    quant.local_depths.insert(data.lable.clone(), depth);
-                                }
-                            } else {
-                                update_flag_u = ticker.u;
-                            }
-                        } else if data.channel == "depth" {
-                            let depth = standard::binance_handle::handle_special_depth(data);
+                    if data.code != "200".to_string() {
+                        continue;
+                    }
+                    if data.channel == "aggTrade" {
+                        let trade: OriginalTradeBa = serde_json::from_str(data.data.as_str()).unwrap();
+                        let str = data.lable.clone();
+                        let mut quant = bot_arc_clone.lock().await;
+                        if quant.is_update.contains_key(&data.lable) && *quant.is_update.get(str.as_str()).unwrap(){
+                            max_buy = Decimal::ZERO;
+                            min_sell = Decimal::ZERO;
+                            quant.is_update.remove(str.as_str());
+                        }
+                        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
+                        }
+                        {
+                            quant.max_buy_min_sell_cache.insert(data.lable, vec![max_buy, min_sell]);
+                        }
+                    } else if data.channel == "bookTicker" {
+                        let ticker: OriginalTicker = serde_json::from_str(data.data.as_str()).unwrap();
+                        if ticker.u > update_flag_u {
                             {
                                 let mut quant = bot_arc_clone.lock().await;
-                                quant._update_depth(depth.depth.clone(), depth.name.clone());
-                                quant.local_depths.insert(depth.name, depth.depth);
+                                quant._update_ticker(SpecialTicker{
+                                    sell: ticker.a.clone(),
+                                    buy: ticker.b.clone(),
+                                    mid_price: Default::default(),
+                                }, data.lable.clone());
+                                let depth = vec![ticker.b, ticker.B, ticker.a, ticker.A];
+                                quant._update_depth(depth.clone(), data.lable.clone());
+                                quant.local_depths.insert(data.lable.clone(), depth);
                             }
+                        } else {
+                            update_flag_u = ticker.u;
+                        }
+                    } else if data.channel == "depth" {
+                        let depth = standard::binance_handle::handle_special_depth(data);
+                        {
+                            let mut quant = bot_arc_clone.lock().await;
+                            quant._update_depth(depth.depth.clone(), depth.name.clone());
+                            quant.local_depths.insert(depth.name, depth.depth);
                         }
                     }
                 },