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::bybit_swap_rest::BybitSwapRest; use exchanges::bybit_swap_ws::{BybitSwapLogin, BybitSwapSubscribeType, BybitSwapWs, BybitSwapWsType}; const ACCESS_KEY: &str = ""; const SECRET_KEY: &str = ""; //ws-订阅公共频道信息 #[tokio::test(flavor = "multi_thread", worker_threads = 5)] async fn ws_custom_subscribe_pu() { 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 mut ws = get_ws(None, BybitSwapWsType::Public).await; ws.set_symbols(vec!["BTC_USDT".to_string()]); ws.set_subscribe(vec![ // BybitSwapSubscribeType::PuOrderBook1, // BybitSwapSubscribeType::PuOrderBook50, // BybitSwapSubscribeType::PuBlicTrade, BybitSwapSubscribeType::PuTickers, ]); 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; } //ws-订阅私有频道信息 #[tokio::test(flavor = "multi_thread", worker_threads = 5)] async fn ws_custom_subscribe_pr() { 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 logparam = BybitSwapLogin { api_key: ACCESS_KEY.to_string(), secret_key: SECRET_KEY.to_string(), }; let mut ws = get_ws(Option::from(logparam), BybitSwapWsType::Private).await; ws.set_symbols(vec!["BTC_USDT".to_string()]); ws.set_subscribe(vec![ BybitSwapSubscribeType::PrPosition, // BybitSwapSubscribeType::PrExecution, // BybitSwapSubscribeType::PrOrder, // BybitSwapSubscribeType::PrWallet, ]); 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_get_server_time_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_server_time().await; println!("Bybit--服務器時間--{:?}", req_data); } //rest-查詢最新行情信息 #[tokio::test] async fn rest_get_tickers_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_tickers("DOGEUSDT".to_string()).await; println!("Bybit--查詢最新行情信息--{:?}", req_data); } //rest-查詢市場價格K線數據 #[tokio::test] async fn rest_get_kline_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_kline("DOGEUSDT".to_string()).await; println!("Bybit--查詢市場價格K線數據--{:?}", req_data); } //rest-查詢公告 #[tokio::test] async fn rest_get_announcements_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_announcements().await; println!("Bybit--查詢公告--{:?}", req_data); } //rest-查詢可交易產品的規格信息 #[tokio::test] async fn rest_get_instruments_info_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_instruments_info("BTCUSDT".to_string()).await; println!("Bybit--查詢可交易產品的規格信息--{:?}", req_data); } //rest-查詢錢包餘額 #[tokio::test] async fn rest_get_account_balance_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_account_balance("USDT".to_string()).await; println!("Bybit--查詢錢包餘額--{:?}", req_data); } //rest-查看持仓信息 #[tokio::test] async fn rest_get_positions_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_positions("DOGEUSDT".to_string(), "".to_string()).await; println!("Bybit--查看持仓信息--{:?}", req_data); } //rest-设置持仓模式 #[tokio::test] async fn rest_set_position_mode_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.set_position_mode("DOGEUSDT".to_string(), 3).await; println!("Bybit--设置持仓模式--{:?}", req_data); } //rest-設置槓桿 #[tokio::test] async fn rest_set_leverage_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.set_leverage( "DOGEUSDT".to_string(), "1".to_string()).await; println!("Bybit--設置槓桿--{:?}", req_data); } //rest-創建委託單 #[tokio::test] async fn rest_swap_order_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let params = serde_json::json!({ "category":"linear", "symbol":"DOGEUSDT", "orderType":"Limit", "side":"Buy", "qty":"1", "price":"0.085", }); let req_data = ret.swap_order(params).await; println!("Bybit--創建委託單--{:?}", req_data); } //rest-查詢實時委託單 #[tokio::test] async fn rest_get_order_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_order("LINKUSDT".to_string(), "".to_string(), "".to_string()).await; println!("Bybit--查詢實時委託單--{:?}", req_data); } //rest-撤单 #[tokio::test] async fn rest_cancel_order_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.cancel_order("DOGEUSDT".to_string(), "1d3ea16f-cf1c-4dab-9a79-d441a2dea549".to_string(), "".to_string()).await; println!("Bybit--撤单--{:?}", req_data); } //rest-撤銷所有訂單 #[tokio::test] async fn rest_cancel_orders_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.cancel_orders("DOGEUSDT".to_string()).await; println!("Bybit--撤銷所有訂單--{:?}", req_data); } async fn get_ws(btree_map: Option, type_v: BybitSwapWsType) -> BybitSwapWs { let ku_ws = BybitSwapWs::new(false, btree_map, type_v); ku_ws } fn get_rest() -> BybitSwapRest { 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 bybit_exc = BybitSwapRest::new(false, btree_map.clone()); bybit_exc }