JiahengHe 2 жил өмнө
parent
commit
0586f225df
1 өөрчлөгдсөн 18 нэмэгдсэн , 12 устгасан
  1. 18 12
      src/main.rs

+ 18 - 12
src/main.rs

@@ -62,14 +62,15 @@ pub struct Bot {
     mid_price: f64,
     ask: f64,
     bid: f64,
-    spread_list_limit: usize
+    spread_list_limit: usize,
+    exchange:Exchange
 }
 
 impl Bot {
     fn new(spread_list: Vec<f64>, symbol: String, limit: i32, short_interval: String,
            rl_start: f64, rl_end: f64, quantity_max: f64, amount_decimal_places: usize,
            order_info_map: HashMap<String, OrderInfo>, last_buy_time: i64, buy_time_limit: i64,
-           cancel_time_limit: i64, price_decimal_places: usize, spread_list_limit: usize) -> Bot {
+           cancel_time_limit: i64, price_decimal_places: usize, spread_list_limit: usize, exchange:Exchange) -> Bot {
         Bot {
             spread_list,
             symbol,
@@ -87,7 +88,8 @@ impl Bot {
             mid_price: 0.0,
             ask: 0.0,
             bid: 0.0,
-            spread_list_limit
+            spread_list_limit,
+            exchange
         }
     }
 
@@ -144,21 +146,21 @@ impl Bot {
         return (q, rl_list);
     }
 
-    async fn order_change_response(&mut self, order_info: Order, exchange: &Exchange){
+    async fn order_change_response(&mut self, order_info: Order){
         if order_info.status.eq("filled") && self.order_info_map.contains_key(&*order_info.id){
             let order: &OrderInfo = self.order_info_map.get(&*order_info.id).unwrap();
             // 1.获取账户信息
-            let balance_info = exchange.get_okx_account(&self.symbol).await.unwrap();
+            let balance_info = self.exchange.get_okx_account(&self.symbol).await.unwrap();
             // 防止有手续费的账户导致的余额小于实际下单数额
             let order_amount = f64::min(balance_info.stocks, order_info.amount);
-            let id = exchange.place_okx_order(&self.symbol, &"sell".to_string(), &"limit".to_string(), &order.sell_price.to_string(), &order_amount.to_string()).await.unwrap();
+            let id = self.exchange.place_okx_order(&self.symbol, &"sell".to_string(), &"limit".to_string(), &order.sell_price.to_string(), &order_amount.to_string()).await.unwrap();
             // 清除已处理订单数据
             self.order_info_map.remove(&*order_info.id);
             eprintln!("卖单id: {}", id);
         }
     }
 
-    async fn order_list_deal(&mut self, exchange: &Exchange) -> Result<i8, io::Error> {
+    async fn order_list_deal(&mut self) -> Result<i8, io::Error> {
         if self.order_info_map.len() == 0{
             return Ok(1_i8);
         }
@@ -167,11 +169,11 @@ impl Bot {
 
         // 超300s 需取消的订单
         for (key, order) in &self.order_info_map {
-            let order_info: Order = exchange.get_okx_order(&self.symbol, &order.id).await.unwrap();
+            let order_info: Order = self.exchange.get_okx_order(&self.symbol, &order.id).await.unwrap();
             eprintln!("order_info: {:?}", order_info);
             // 未成交 && 超时
             if !order_info.status.eq("filled") && order.time_num + self.cancel_time_limit < now_time {
-                let is_success = match exchange. cancel_okx_order(&self.symbol, &order.id).await {
+                let is_success = match self.exchange.cancel_okx_order(&self.symbol, &order.id).await {
                     Ok(m) => m,
                     Err(error) => {
                         return Err(error);
@@ -214,7 +216,7 @@ impl Bot {
         };
         println!("info: {:?}", balance_info);
         // 取消超时订单
-        self.order_list_deal(exchange).await.expect("订单信息处理异常!");
+        self.order_list_deal().await.expect("订单信息处理异常!");
 
         // 2.获取最新k线
         let k_line_data = match  exchange.get_binance_klines(&self.symbol.to_string(), &self.short_interval, &200).await {
@@ -308,7 +310,7 @@ async fn main() {
     let okx_passphrase= env::var("okx_passphrase").unwrap();
     let exchange:Exchange = Exchange::new(okx_access_key, okx_secret_key, okx_passphrase);
 
-    let bot = Bot::new(spread_list, symbol.clone(), limit, short_interval, rl_start, rl_end, quantity_max, amount_decimal_places, order_info_map, last_buy_time, buy_time_limit, cancel_time_limit, price_decimal_places, spread_list_limit);
+    let bot = Bot::new(spread_list, symbol.clone(), limit, short_interval, rl_start, rl_end, quantity_max, amount_decimal_places, order_info_map, last_buy_time, buy_time_limit, cancel_time_limit, price_decimal_places, spread_list_limit, exchange);
     let bot_arc = Arc::new(Mutex::new(bot));
     let bot_binance_depth_arc = Arc::clone(&bot_arc);
 
@@ -345,7 +347,11 @@ async fn main() {
 
     // 各种订阅信息,辅助线程
     let subscribe_binance_depth_thread = tokio::spawn(async move {
-        exchange.get_binance_depth(&symbol, 10, bot_binance_depth_arc).await;
+        let okx_access_key_x= env::var("okx_access_key").unwrap();
+        let okx_secret_key_x= env::var("okx_secret_key").unwrap();
+        let okx_passphrase_x= env::var("okx_passphrase").unwrap();
+        let exchange_x:Exchange = Exchange::new(okx_access_key_x, okx_secret_key_x, okx_passphrase_x);
+        exchange_x.get_binance_depth(&symbol, 10, bot_binance_depth_arc).await;
         // loop {
         //     {
         //         let bot = bot_binance_depth_arc.lock().await;