Browse Source

bitget完善

JiahengHe 2 years ago
parent
commit
bacc298891
2 changed files with 87 additions and 2 deletions
  1. 7 0
      strategy/src/model.rs
  2. 80 2
      strategy/src/quant.rs

+ 7 - 0
strategy/src/model.rs

@@ -4,6 +4,13 @@ use rust_decimal_macros::dec;
 use serde_derive::{Deserialize, Serialize};
 use global::trace_stack::TraceStack;
 
+pub struct TokenParam {
+    // 平台币名称
+    pub token: String,
+    // 最低持有价值(u)
+    pub limit_value: Decimal
+}
+
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub struct LocalPosition {
     // 做多仓位

+ 80 - 2
strategy/src/quant.rs

@@ -22,7 +22,7 @@ use standard::{Account, Market, Order, OrderCommand, Platform, Position, Positio
 use standard::exchange::{Exchange};
 use standard::exchange::ExchangeEnum::{BinanceSpot, BinanceSwap, GateSpot, GateSwap, KucoinSwap};
 
-use crate::model::{LocalPosition, OrderInfo, TraderMsg};
+use crate::model::{LocalPosition, OrderInfo, TokenParam, TraderMsg};
 use crate::predictor::Predictor;
 use crate::strategy::Strategy;
 use crate::utils::clip;
@@ -971,6 +971,64 @@ impl Quant {
         }
     }
 
+    pub async fn buy_token(&mut self){
+        // 买入平台币
+        // 获取U数量,平台币数量
+        // 更新账户
+        let mut cash = Decimal::ZERO;
+        let mut token = Decimal::ZERO;
+        let token_param = get_exchange_token(&self.exchange);
+        if token_param.token=="***"{
+            error!("购买平台币失败,未找到交易所的平台币!");
+            return;
+        }
+        match self.platform_rest.get_spot_account().await {
+            Ok(val) =>{
+                for account in val{
+                    if account.coin == "USDT".to_string() {
+                        cash += account.balance;
+                    }
+                    if token_param.token == account.coin {
+                        token += account.balance;
+                    }
+                }
+            },Err(err)=>{
+                error!("购买{}-获取账户失败 {}", token_param.token, err);
+            }
+        }
+        info!("持u {} , 持有平台币 {}", cash, token);
+        match self.platform_rest.get_ticker_symbol(format!("{}_USDT", token_param.token)).await {
+            Ok(val)=>{
+                let mp = (val.buy + val.sell)/Decimal::TWO;
+                let token_value = token * mp;
+                if token_value < token_param.limit_value {
+                    info!("{} 数量过少!", token_param.token);
+                    if cash > Decimal::TWO*Decimal::ONE_HUNDRED{
+                        info!("准备买入{}", token_param.token);
+                        match self.platform_rest.take_order_symbol(token_param.token, Decimal::ONE, "t-888", "kd", mp * Decimal::from_str("1.001").unwrap(), Decimal::from_str("50").unwrap()/mp).await {
+                            Ok(value) => {
+                                info!("买入平台币下单成功: {:?}", value);
+                            },
+                            Err(error) => {
+                                error!("买入平台币下单失败: {}", error)
+                            }
+                        }
+
+                    } else {
+                        info!("现金不足 无法买入{}!", token_param.token);
+                    }
+                } else {
+                    info!("{}数量充足!", token_param.token);
+                }
+            },
+            Err(err)=>{
+                error!("购买平台币-获取平台币行情失败 {}", err);
+            }
+        }
+
+
+    }
+
     pub async fn check_position(&mut self){
         info!("清空挂单!");
         match self.platform_rest.cancel_orders_all().await {
@@ -1375,13 +1433,33 @@ pub fn on_timer(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()> {
     });
 }
 
+// 是不是不用调整仓位的币
 pub fn check_coin(exchanges :&String, coin_name: &String) -> bool{
     let mut result = false;
     match exchanges.as_str() {
         "bitget_usdt_spot" => {
-            result = ["GT", "USDT", "POINT"].contains(&coin_name.as_str());
+            result = ["BGB", "USDT"].contains(&coin_name.as_str());
         }
         _ => {}
     }
     result
+}
+
+//获取平台币
+pub fn get_exchange_token(exchanges :&String) -> TokenParam{
+    return match exchanges.as_str() {
+        "bitget_usdt_spot" => {
+            TokenParam{
+                token: "BGB".to_string(),
+                // 30u
+                limit_value: Decimal::TEN*(Decimal::ONE + Decimal::TWO)
+            }
+        }
+        _ => {
+            TokenParam{
+                token: "***".to_string(),
+                limit_value: Decimal::ZERO
+            }
+        }
+    }
 }