skyffire vor 9 Monaten
Ursprung
Commit
b112a2c713
2 geänderte Dateien mit 25 neuen und 16 gelöschten Zeilen
  1. 11 4
      strategy/src/core.rs
  2. 14 12
      strategy/src/predictor.rs

+ 11 - 4
strategy/src/core.rs

@@ -489,7 +489,7 @@ impl Core {
                         // info!("成交单耗时数据:{}", time_record.to_string());
                         info!("更新推算仓位 {:?}", self.local_position_by_orders);
                         // 本地计算利润
-                        self._print_local_trades_summary();
+                        self._print_local_trades_summary().await;
                         // 打印各类信息
                         self.strategy.local_orders = self.local_orders.clone();
                         self.strategy._print_summary();
@@ -544,7 +544,7 @@ impl Core {
     }
 
     // #[instrument(skip(self), level="TRACE")]
-    pub fn _print_local_trades_summary(&mut self) {
+    pub async fn _print_local_trades_summary(&mut self) {
         // 计算本地累计利润
         let local_buy_amount = self.local_buy_amount.round_dp(5);
         let local_buy_value = self.local_buy_value.round_dp(5);
@@ -557,6 +557,12 @@ impl Core {
             let local_profit = (unrealized + realized).round_dp(5);
             self.strategy.local_profit = local_profit;
             info!("买量 {},卖量 {},买额{},卖额{}", local_buy_amount, local_sell_amount, local_buy_value, local_sell_value);
+
+            // 更新账户余额
+            let mut now_balance = (self.local_cash + self.local_coin * self.predictor.mid_price) / self.used_pct;
+            now_balance.rescale(4);
+
+            self.predictor.on_balance(now_balance).await;
         }
     }
 
@@ -657,11 +663,11 @@ impl Core {
                     platform_rest_fb.command_order(&mut orders, &mut ts).await;
                 });
 
-                // 更新账户余额
+                // 更新账户余额到中控
                 {
+                    // 更新账户余额
                     let mut now_balance = (self.local_cash + self.local_coin * self.predictor.mid_price) / self.used_pct;
                     now_balance.rescale(4);
-
                     let mut cci = self.cci_arc.lock().await;
                     cci.now_balance = now_balance;
                 }
@@ -1555,6 +1561,7 @@ impl Core {
         // 初始化调度器
         self.local_cash = start_cash;
         self.local_coin = start_coin;
+        self.predictor.on_balance(self.local_cash).await;
 
         // 买入平台币
         if self.exchange.contains("spot") { // 现货

+ 14 - 12
strategy/src/predictor.rs

@@ -30,6 +30,7 @@ pub struct Predictor {
     pub inventory: Decimal,                                                     // 库存,也就是q
     pub pos_amount: Decimal,                                                    // 原始持仓量
     pub pos_avg_price: Decimal,                                                 // 原始持仓价格
+    pub balance: Decimal,                                                       // 初始余额
 
     pub signal: Decimal,                                                        // 大于0代表此时是正向信号,小于0则相反
 
@@ -134,6 +135,7 @@ impl Predictor {
             inventory: Default::default(),
             pos_avg_price: Default::default(),
             pos_amount: Default::default(),
+            balance: Default::default(),
 
             signal: Default::default(),
 
@@ -202,6 +204,10 @@ impl Predictor {
         self.processor().await;
     }
 
+    pub async fn on_balance(&mut self, balance: Decimal) {
+        self.balance = balance;
+    }
+
     pub fn get_real_rate(price_vec: &FixedTimeRangeDeque<Decimal>) -> Decimal {
         let last_fair_price = price_vec.deque.iter().last().unwrap();
         let min_price = price_vec.deque.iter().min().unwrap();
@@ -313,7 +319,7 @@ impl Predictor {
             return;
         }
 
-        if self.mid_price == Decimal::ZERO {
+        if self.mid_price.is_zero() {
            return;
         }
 
@@ -323,11 +329,15 @@ impl Predictor {
             }
         }
 
-        if self.ask_price == Decimal::ZERO {
+        if self.ask_price.is_zero() {
+            return;
+        }
+
+        if self.bid_price.is_zero() {
             return;
         }
 
-        if self.bid_price == Decimal::ZERO {
+        if self.balance.is_zero() {
             return;
         }
 
@@ -344,14 +354,6 @@ impl Predictor {
 
         self.update_delta();
 
-        // let mut smm = Decimal::ZERO;
-        // if !self.depth_vec[1].time.is_zero() {
-        //     let sma = self.depth_vec[1].asks[0].price;
-        //     let smb = self.depth_vec[1].bids[0].price;
-        //     smm = (sma + smb) / Decimal::TWO;
-        // }
-
-
         // let cci_arc = self.cci_arc.clone();
         let now = Decimal::from_i64(Utc::now().timestamp_millis()).unwrap();
         let mid_price = self.mid_price;
@@ -373,7 +375,7 @@ impl Predictor {
 
         let sigma_square = self.signal;
 
-        let gamma = Decimal::ZERO;
+        let gamma = self.balance;
         let kappa = Decimal::ZERO;
 
         let flow_ratio = Decimal::ZERO;