exchange_disguise.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. use std::collections::BTreeMap;
  2. use std::sync::Arc;
  3. use std::sync::atomic::AtomicBool;
  4. use rust_decimal::Decimal;
  5. use tokio::sync::Mutex;
  6. use global::trace_stack::TraceStack;
  7. use standard::SpecialDepth;
  8. use crate::binance_spot::reference_binance_spot_run;
  9. use crate::binance_usdt_swap::reference_binance_swap_run;
  10. use crate::bitget_spot::bitget_spot_run;
  11. use crate::bybit_usdt_swap::bybit_swap_run;
  12. use crate::gate_swap::gate_swap_run;
  13. use crate::kucoin_spot::kucoin_spot_run;
  14. use crate::kucoin_swap::kucoin_swap_run;
  15. use crate::okx_usdt_swap::okex_swap_run;
  16. use crate::quant::Quant;
  17. // 交易交易所启动
  18. pub async fn run_transactional_exchange(bool_v1 :Arc<AtomicBool>,
  19. exchange_name: String,
  20. quant_arc: Arc<Mutex<Quant>>,
  21. name: String,
  22. symbols: Vec<String>,
  23. is_colo: bool,
  24. exchange_params: BTreeMap<String, String>) {
  25. match exchange_name.as_str() {
  26. "gate_usdt_swap" => {
  27. gate_swap_run(bool_v1, true, quant_arc, name, symbols, is_colo, exchange_params).await;
  28. }
  29. "kucoin_usdt_swap" => {
  30. kucoin_swap_run(bool_v1, true, quant_arc, name, symbols, is_colo, exchange_params).await;
  31. },
  32. "okex_usdt_swap" => {
  33. okex_swap_run(bool_v1,true, quant_arc, name, symbols, is_colo, exchange_params).await;
  34. },
  35. "bitget_spot" => {
  36. bitget_spot_run(bool_v1,true, quant_arc, name, symbols, is_colo, exchange_params).await;
  37. },
  38. "bybit_usdt_swap" => {
  39. bybit_swap_run(bool_v1,true, quant_arc, name, symbols, is_colo, exchange_params).await;
  40. }
  41. _ => {
  42. let msg = format!("不支持的交易交易所:{}", exchange_name);
  43. panic!("{}", msg);
  44. }
  45. }
  46. }
  47. // 参考交易所启动
  48. pub async fn run_reference_exchange(bool_v1: Arc<AtomicBool>,
  49. exchange_name: String,
  50. quant_arc: Arc<Mutex<Quant>>,
  51. name: String,
  52. symbols: Vec<String>,
  53. is_colo: bool,
  54. exchange_params: BTreeMap<String, String>) {
  55. match exchange_name.as_str() {
  56. "binance_usdt_swap" => {
  57. reference_binance_swap_run(bool_v1, quant_arc, name, symbols, is_colo, exchange_params).await;
  58. },
  59. "binance_spot" => {
  60. reference_binance_spot_run(bool_v1, quant_arc, name, symbols, is_colo, exchange_params).await;
  61. },
  62. "gate_usdt_swap" => {
  63. gate_swap_run(bool_v1, false, quant_arc, name, symbols, is_colo, exchange_params).await;
  64. },
  65. "okex_usdt_swap" => {
  66. okex_swap_run(bool_v1, false, quant_arc, name, symbols, is_colo, exchange_params).await;
  67. },
  68. "kucoin_usdt_swap" => {
  69. kucoin_swap_run(bool_v1, false, quant_arc, name, symbols, is_colo, exchange_params).await;
  70. },
  71. "kucoin_spot" => {
  72. kucoin_spot_run(bool_v1, false, quant_arc, name, symbols, is_colo, exchange_params).await;
  73. },
  74. "bitget_spot" => {
  75. bitget_spot_run(bool_v1, false, quant_arc, name, symbols, is_colo, exchange_params).await;
  76. },
  77. "bybit_usdt_swap" => {
  78. bybit_swap_run(bool_v1, false, quant_arc, name, symbols, is_colo, exchange_params).await;
  79. },
  80. _ => {
  81. let msg = format!("不支持的参考交易所:{}", exchange_name);
  82. panic!("{}", msg);
  83. }
  84. }
  85. }
  86. pub async fn on_special_depth(bot_arc: Arc<Mutex<Quant>>,
  87. update_flag_u: &mut Decimal,
  88. label: String,
  89. trace_stack: TraceStack,
  90. special_depth: SpecialDepth) {
  91. if special_depth.t > *update_flag_u {
  92. *update_flag_u = special_depth.t;
  93. let mut quant = bot_arc.lock().await;
  94. quant._update_ticker(special_depth.ticker, label.clone());
  95. quant._update_depth(special_depth.depth.clone(), label.clone(), trace_stack);
  96. quant.local_depths.insert(special_depth.name, special_depth.depth);
  97. }
  98. }
  99. // pub async fn on_trade(trade: OriginalTradeBa,
  100. // bot_arc_clone: Arc<Mutex<Quant>>) {
  101. // let mut bot = bot_arc_clone.lock().await;
  102. // // 1. 塞入数据到bot
  103. // bot.trades.push(trade.clone());
  104. // // 2. 长度检查
  105. // if bot.trades.len() > bot.recall_max_count {
  106. // bot.trades.remove(0);
  107. // }
  108. // // 3. 如果少于100条,不进行判断
  109. // if bot.trades.len() < 100 {
  110. // return;
  111. // }
  112. // // 求最近的多空总和
  113. // let mut long_sum = Decimal::ZERO;
  114. // let mut short_sum = Decimal::ZERO;
  115. // let last_trade_t = trade.t.clone();
  116. // let mut rev = bot.trades.clone();
  117. // rev.reverse();
  118. // for trade_o in rev {
  119. // // 如果该元素已过期,我们是按时间顺序插入的,说明前面的应该都过期了,跳出循环,停止检测
  120. // if trade_o.t < last_trade_t - bot.recall_time {
  121. // continue;
  122. // }
  123. //
  124. // // 卖出订单
  125. // if trade_o.m {
  126. // short_sum += trade_o.q;
  127. // } else {
  128. // long_sum += trade_o.q;
  129. // }
  130. // }
  131. //
  132. // // 做多主动性
  133. // if (long_sum / (long_sum + short_sum)) > bot.long_volume_rate {
  134. // if bot.side != "long".to_string() {
  135. // bot.side = "long".to_string();
  136. // }
  137. // } else if (short_sum / (long_sum + short_sum)) > bot.short_volume_rate {
  138. // if bot.side != "short".to_string() {
  139. // bot.side = "short".to_string();
  140. // }
  141. // } else {
  142. // if bot.side != "normal".to_string() {
  143. // bot.side = "normal".to_string();
  144. // }
  145. // }
  146. // }
  147. pub async fn on_order() {}
  148. pub async fn on_position() {}
  149. pub async fn on_account() {}