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::okx_swap_rest::OkxSwapRest; use exchanges::okx_swap_ws::{OkxSwapLogin, OkxSwapSubscribeType, OkxSwapWs, OkxSwapWsType}; const ACCESS_KEY: &str = ""; const SECRET_KEY: &str = ""; const PASS_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, OkxSwapWsType::Public).await; ws.set_symbols(vec!["BTC_USDT".to_string()]); ws.set_subscribe(vec![ // OkxSwapSubscribeType::PuBooks5, // OkxSwapSubscribeType::Putrades, OkxSwapSubscribeType::PuBooks50L2tbt, // OkxSwapSubscribeType::PuIndexTickers, ]); 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 = 2)] async fn ws_custom_subscribe_bu() { 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, OkxSwapWsType::Business).await; ws.set_symbols(vec!["BTC-USD".to_string()]); ws.set_subscribe(vec![ OkxSwapSubscribeType::BuIndexCandle30m, ]); 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 btree_map = OkxSwapLogin { api_key: ACCESS_KEY.to_string(), secret_key: SECRET_KEY.to_string(), passphrase: PASS_KEY.to_string(), }; let mut ws = get_ws(Option::from(btree_map), OkxSwapWsType::Private).await; ws.set_symbols(vec!["BTC-USDT".to_string()]); ws.set_subscribe(vec![ OkxSwapSubscribeType::PrAccount("USDT".to_string()), OkxSwapSubscribeType::PrOrders, OkxSwapSubscribeType::PrPositions, OkxSwapSubscribeType::PrBalanceAndPosition, ]); 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; // // let mut is_shutdown_arc = Arc::new(AtomicBool::new(true)); // 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()); // btree_map.insert("pass_key".to_string(), PASS_KEY.to_string()); // // let (tx, mut rx) = channel(1024); // let mut ws = get_ws(btree_map, OkxWsType::Private, tx).await; // ws.set_subscribe(vec![ // OkxSubscribeType::PrBalanceAndPosition, // // OkxSubscribeType::PrAccount("USDT".to_string()), // OkxSubscribeType::PrOrders, // OkxSubscribeType::PrPositions, // ]); // // let t1 = tokio::spawn(async move { // ws.custom_subscribe(is_shutdown_arc, vec!["BTC-USDT".to_string()]).await; // }); // // let t2 = tokio::spawn(async move { // loop { // if let Ok(received) = rx.try_recv() { // trace!( "age: {:?}", received); // } // } // }); // try_join!(t1,t2).unwrap(); } //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("BTC-USDT".to_string(), "3333".to_string(), "".to_string()).await; println!("okx--订单查询--{:?}", req_data); } //rest-未完成的订单 #[tokio::test] async fn rest_get_incomplete_order_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_incomplete_order("BTC-USDT".to_string()).await; println!("okx--未完成的订单--{:?}", req_data); } //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!("okx--获取系统时间--{:?}", 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("SWA1P".to_string()).await; println!("okx--查看持仓信息--{:?}", req_data); } //rest-获取单个产品行情信息 #[tokio::test] async fn rest_get_ticker_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_ticker("BTC-USDT".to_string()).await; println!("okx--获取单个产品行情信息--{:?}", req_data); } //rest-查看账户余额 #[tokio::test] async fn rest_get_balance_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_balance("BTC,ETH".to_string()).await; println!("okx--查看账户余额--{:?}", req_data); } //rest-获取交易产品基础信息 #[tokio::test] async fn rest_get_instruments_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_instruments().await; println!("okx--获取交易产品基础信息--{:?}", req_data); } //rest-获取成交明细(近三天) #[tokio::test] async fn rest_get_trade_fills_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_trade_fills("".to_string()).await; println!("okx--获取成交明细(近三天)--{:?}", 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("BTC-USD".to_string(), "1111".to_string(), "".to_string()).await; println!("okx--撤单--{:?}", 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("BTC-USDT".to_string(), "5".to_string()).await; println!("okx--设置杠杆倍数--{:?}", 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().await; println!("okx--设置持仓模式--{:?}", req_data); } //rest-获取历史订单记录(近七天) #[tokio::test] async fn rest_get_orders_history_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_orders_history("".to_string(), "".to_string(), "filled".to_string(), "".to_string(), "".to_string(), "".to_string(), ).await; println!("okx--获取历史订单记录--{:?}", req_data); } //rest-获取历史成交数据(近七天) #[tokio::test] async fn rest_get_trades_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_trades("".to_string(), "".to_string(), "".to_string(), "".to_string(), "".to_string(), "100".to_string(), ).await; println!("okx--获取历史成交数据--{:?}", req_data); } async fn get_ws(btree_map: Option, type_v: OkxSwapWsType) -> OkxSwapWs { let ku_ws = OkxSwapWs::new(false, btree_map, type_v); ku_ws } fn get_rest() -> OkxSwapRest { 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()); btree_map.insert("pass_key".to_string(), PASS_KEY.to_string()); let okx_exc = OkxSwapRest::new(false, btree_map.clone()); okx_exc }