use std::collections::BTreeMap; use std::sync::Arc; use std::sync::atomic::AtomicBool; use futures_util::StreamExt; use tokio::sync::Mutex; use tracing::trace; use exchanges::gate_swap_rest::GateSwapRest; use exchanges::gate_swap_ws::{GateSwapLogin, GateSwapSubscribeType, GateSwapWs, GateSwapWsType}; const ACCESS_KEY: &str = ""; const SECRET_KEY: &str = ""; //ws-订阅公共频道信息 #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn ws_custom_subscribe() { global::log_utils::init_log_with_trace(); let (write_tx, write_rx) = futures_channel::mpsc::unbounded(); let (read_tx, mut read_rx) = futures_channel::mpsc::unbounded(); let param = GateSwapLogin { api_key: ACCESS_KEY.to_string(), secret: SECRET_KEY.to_string(), }; let mut ws = get_ws(Option::from(param)); ws.set_symbols(vec!["BTC_USDT".to_string()]); ws.set_subscribe(vec![ // GateSwapSubscribeType::PuFuturesOrderBook, GateSwapSubscribeType::PuFuturesCandlesticks, GateSwapSubscribeType::PuFuturesTrades, // GateSwapSubscribeType::PrFuturesBalances("".to_string()), // GateSwapSubscribeType::PrFuturesOrders("".to_string()), // GateSwapSubscribeType::PrFuturesPositions("".to_string()), ]); let write_tx_am = Arc::new(Mutex::new(write_tx)); let is_shutdown_arc = Arc::new(AtomicBool::new(true)); //读取 let _is_shutdown_arc_clone = Arc::clone(&is_shutdown_arc); let _tr = tokio::spawn(async move { trace!("线程-数据读取-开启"); loop { if let Some(data) = read_rx.next().await { trace!("读取数据data:{:?}",data) } } // trace!("线程-数据读取-结束"); }); //写数据 // let bool_v2_clone = Arc::clone(&is_shutdown_arc); // let write_tx_clone = Arc::clone(&write_tx_am); // let su = ws.get_subscription(); // let tw = tokio::spawn(async move { // trace!("线程-数据写入-开始"); // loop { // tokio::time::sleep(Duration::from_millis(20 * 1000)).await; // // let close_frame = CloseFrame { // // code: CloseCode::Normal, // // reason: Cow::Borrowed("Bye bye"), // // }; // // let message = Message::Close(Some(close_frame)); // // // let message = Message::Text(su.clone()); // AbstractWsMode::send_subscribe(write_tx_clone.clone(), message.clone()).await; // trace!("发送指令成功"); // } // trace!("线程-数据写入-结束"); // }); let t1 = tokio::spawn(async move { //链接 let bool_v3_clone = Arc::clone(&is_shutdown_arc); ws.ws_connect_async(bool_v3_clone, &write_tx_am, write_rx, read_tx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)"); trace!("test 唯一线程结束--"); }); tokio::try_join!(t1).unwrap(); trace!("当此结束"); trace!("重启!"); trace!("参考交易所关闭"); return; } //rest-设置持仓模式 #[tokio::test] async fn rest_cancel_order_all_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.cancel_order_all().await; println!("okx--设置持仓模式--{:?}", req_data); } //rest-下一个自动单 #[tokio::test] async fn price_order_test() { global::log_utils::init_log_with_info(); let mut rest = get_rest(); let mut params = json!({}); params["initial"] = json!({ "contract": "XRP_USDT", "price": "0", "tif": "ioc", "reduce_only": true, // [平多:close_long, 平空:close_short] "auto_size": "close_long" }); params["trigger"] = json!({ // [平多:close-long-position, 平空:close-short-position] "order_type": "close-long-position", // 一般都默认用0 "strategy_type": 0, // [0 - 最新成交价,1 - 标记价格,2 - 指数价格] "price_type": 0, // [1: 引用价格大于等于我们传的价格,2:引用价格小于等于我们传的价格] // 在止损的情况下: // 1 可以理解为向上突破触发价(一般是给空单用) // 2 可以理解为向下突破触发价(一般是给多单用) "rule": 2, // 订单触发价格 "price": "0.5600", }); let response_data = rest.place_price_order("usdt".to_string(), params).await; if response_data.code == "200" { let response_obj: serde_json::Value = serde_json::from_str(response_data.data.as_str()).unwrap(); info!("resp={:?}", response_obj.as_object().unwrap()); } else { error!(?response_data); } } #[tokio::test] async fn price_order_cancel_test() { global::log_utils::init_log_with_info(); let mut rest = get_rest(); // 这边取消订单只能使用系统返回的 let rst = rest.cancel_price_order("usdt".to_string(), "58002898".to_string()).await; info!(?rst); } //rest-查询合约账户变更历史 #[tokio::test] async fn rest_account_book_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.account_book("usdt".to_string()).await; println!("okx--查询合约账户变更历史--{:?}", req_data); } fn get_ws(btree_map: Option) -> GateSwapWs { let binance_ws = GateSwapWs::new(false, btree_map, GateSwapWsType::PublicAndPrivate("usdt".to_string())); binance_ws } fn get_rest() -> GateSwapRest { let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string()); btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string()); let ba_exc = GateSwapRest::new(false, btree_map); ba_exc }