gate_swap_test.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // use std::collections::BTreeMap;
  2. // use std::sync::Arc;
  3. // use std::sync::atomic::AtomicBool;
  4. //
  5. // use futures_util::StreamExt;
  6. // use tokio::sync::Mutex;
  7. // use tracing::trace;
  8. //
  9. // use exchanges::gate_swap_rest::GateSwapRest;
  10. // use exchanges::gate_swap_ws::{GateSwapLogin, GateSwapSubscribeType, GateSwapWs, GateSwapWsType};
  11. //
  12. // const ACCESS_KEY: &str = "";
  13. // const SECRET_KEY: &str = "";
  14. //
  15. // //ws-订阅公共频道信息
  16. // #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
  17. // async fn ws_custom_subscribe() {
  18. // global::log_utils::init_log_with_trace();
  19. //
  20. // let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
  21. // let (read_tx, mut read_rx) = futures_channel::mpsc::unbounded();
  22. //
  23. // let param = GateSwapLogin {
  24. // api_key: ACCESS_KEY.to_string(),
  25. // secret: SECRET_KEY.to_string(),
  26. // };
  27. //
  28. // let mut ws = get_ws(Option::from(param));
  29. // ws.set_symbols(vec!["BTC_USDT".to_string()]);
  30. // ws.set_subscribe(vec![
  31. // // GateSwapSubscribeType::PuFuturesOrderBook,
  32. // GateSwapSubscribeType::PuFuturesCandlesticks,
  33. // GateSwapSubscribeType::PuFuturesTrades,
  34. //
  35. // // GateSwapSubscribeType::PrFuturesBalances("".to_string()),
  36. // // GateSwapSubscribeType::PrFuturesOrders("".to_string()),
  37. // // GateSwapSubscribeType::PrFuturesPositions("".to_string()),
  38. // ]);
  39. //
  40. //
  41. // let write_tx_am = Arc::new(Mutex::new(write_tx));
  42. // let is_shutdown_arc = Arc::new(AtomicBool::new(true));
  43. //
  44. // //读取
  45. // let _is_shutdown_arc_clone = Arc::clone(&is_shutdown_arc);
  46. // let _tr = tokio::spawn(async move {
  47. // trace!("线程-数据读取-开启");
  48. // loop {
  49. // if let Some(data) = read_rx.next().await {
  50. // trace!("读取数据data:{:?}",data)
  51. // }
  52. // }
  53. // // trace!("线程-数据读取-结束");
  54. // });
  55. //
  56. // //写数据
  57. // // let bool_v2_clone = Arc::clone(&is_shutdown_arc);
  58. // // let write_tx_clone = Arc::clone(&write_tx_am);
  59. // // let su = ws.get_subscription();
  60. // // let tw = tokio::spawn(async move {
  61. // // trace!("线程-数据写入-开始");
  62. // // loop {
  63. // // tokio::time::sleep(Duration::from_millis(20 * 1000)).await;
  64. // // // let close_frame = CloseFrame {
  65. // // // code: CloseCode::Normal,
  66. // // // reason: Cow::Borrowed("Bye bye"),
  67. // // // };
  68. // // // let message = Message::Close(Some(close_frame));
  69. // //
  70. // //
  71. // // let message = Message::Text(su.clone());
  72. // // AbstractWsMode::send_subscribe(write_tx_clone.clone(), message.clone()).await;
  73. // // trace!("发送指令成功");
  74. // // }
  75. // // trace!("线程-数据写入-结束");
  76. // // });
  77. //
  78. // let t1 = tokio::spawn(async move {
  79. // //链接
  80. // let bool_v3_clone = Arc::clone(&is_shutdown_arc);
  81. // ws.ws_connect_async(bool_v3_clone, &write_tx_am, write_rx, read_tx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
  82. // trace!("test 唯一线程结束--");
  83. // });
  84. // tokio::try_join!(t1).unwrap();
  85. // trace!("当此结束");
  86. // trace!("重启!");
  87. // trace!("参考交易所关闭");
  88. // return;
  89. // }
  90. //
  91. //
  92. // //rest-设置持仓模式
  93. // #[tokio::test]
  94. // async fn rest_cancel_order_all_test() {
  95. // global::log_utils::init_log_with_trace();
  96. //
  97. // let mut ret = get_rest();
  98. // let req_data = ret.cancel_order_all().await;
  99. // println!("okx--设置持仓模式--{:?}", req_data);
  100. // }
  101. //
  102. // //rest-下一个自动单
  103. // #[tokio::test]
  104. // async fn price_order_test() {
  105. // global::log_utils::init_log_with_info();
  106. //
  107. // let mut rest = get_rest();
  108. // let mut params = json!({});
  109. //
  110. // params["initial"] = json!({
  111. // "contract": "XRP_USDT",
  112. // "price": "0",
  113. // "tif": "ioc",
  114. // "reduce_only": true,
  115. // // [平多:close_long, 平空:close_short]
  116. // "auto_size": "close_long"
  117. // });
  118. //
  119. // params["trigger"] = json!({
  120. // // [平多:close-long-position, 平空:close-short-position]
  121. // "order_type": "close-long-position",
  122. // // 一般都默认用0
  123. // "strategy_type": 0,
  124. // // [0 - 最新成交价,1 - 标记价格,2 - 指数价格]
  125. // "price_type": 0,
  126. // // [1: 引用价格大于等于我们传的价格,2:引用价格小于等于我们传的价格]
  127. // // 在止损的情况下:
  128. // // 1 可以理解为向上突破触发价(一般是给空单用)
  129. // // 2 可以理解为向下突破触发价(一般是给多单用)
  130. // "rule": 2,
  131. // // 订单触发价格
  132. // "price": "0.5600",
  133. // });
  134. //
  135. // let response_data = rest.place_price_order("usdt".to_string(), params).await;
  136. // if response_data.code == "200" {
  137. // let response_obj: serde_json::Value = serde_json::from_str(response_data.data.as_str()).unwrap();
  138. //
  139. // info!("resp={:?}", response_obj.as_object().unwrap());
  140. // } else {
  141. // error!(?response_data);
  142. // }
  143. // }
  144. //
  145. // #[tokio::test]
  146. // async fn price_order_cancel_test() {
  147. // global::log_utils::init_log_with_info();
  148. //
  149. // let mut rest = get_rest();
  150. //
  151. // // 这边取消订单只能使用系统返回的
  152. // let rst = rest.cancel_price_order("usdt".to_string(), "58002898".to_string()).await;
  153. // info!(?rst);
  154. // }
  155. //
  156. // //rest-查询合约账户变更历史
  157. // #[tokio::test]
  158. // async fn rest_account_book_test() {
  159. // global::log_utils::init_log_with_trace();
  160. //
  161. // let mut ret = get_rest();
  162. // let req_data = ret.account_book("usdt".to_string()).await;
  163. // println!("okx--查询合约账户变更历史--{:?}", req_data);
  164. // }
  165. //
  166. // fn get_ws(btree_map: Option<GateSwapLogin>) -> GateSwapWs {
  167. // let binance_ws = GateSwapWs::new(false,
  168. // btree_map,
  169. // GateSwapWsType::PublicAndPrivate("usdt".to_string()));
  170. // binance_ws
  171. // }
  172. //
  173. //
  174. // fn get_rest() -> GateSwapRest {
  175. // let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
  176. // btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string());
  177. // btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string());
  178. //
  179. // let ba_exc = GateSwapRest::new(false, btree_map);
  180. // ba_exc
  181. // }