Ver código fonte

浮动余额与余额分开。

skyffire 8 meses atrás
pai
commit
8e92907a4b
1 arquivos alterados com 12 adições e 5 exclusões
  1. 12 5
      strategy/src/predictor.rs

+ 12 - 5
strategy/src/predictor.rs

@@ -46,7 +46,8 @@ pub struct Predictor {
     pub inventory: Decimal,                                                     // 库存,也就是q
     pub pos_amount: Decimal,                                                    // 原始持仓量
     pub pos_avg_price: Decimal,                                                 // 原始持仓价格
-    pub balance: Decimal,                                                       // 初始余额
+    pub balance: Decimal,                                                       // 余额
+    pub float_balance: Decimal,                                                 // 浮动余额
     pub one_grid_order_value: Decimal,                                          // 每一网格下单价值
     pub profit: Decimal,
     pub profit_high: Decimal,                                                   // 总体持仓利润的最大值
@@ -196,6 +197,7 @@ impl Predictor {
             pos_avg_price: Default::default(),
             pos_amount: Default::default(),
             balance: Default::default(),
+            float_balance: Default::default(),
 
             one_grid_order_value: Default::default(),
             profit: Default::default(),
@@ -253,11 +255,16 @@ impl Predictor {
             // 计算利润(预估)
             if !self.inventory.is_zero() {
                 if !self.pos_avg_price.is_zero() {
+                    let hold_value = self.pos_avg_price * self.pos_amount;
+
                     self.profit = if self.inventory > Decimal::ZERO {
-                        (self.mid_price - self.pos_avg_price) / self.pos_avg_price
+                        hold_value * (self.mid_price - self.pos_avg_price) / self.pos_avg_price
                     } else {
-                        (self.pos_avg_price - self.mid_price) / self.pos_avg_price
+                        hold_value * (self.pos_avg_price - self.mid_price) / self.pos_avg_price
                     };
+
+                    self.float_balance = self.balance + self.profit;
+                    self.float_balance.rescale(8);
                 }
                 self.profit.rescale(6);
                 if self.profit_high < self.profit {
@@ -501,8 +508,8 @@ impl Predictor {
     pub async fn on_ticker(&mut self, _ticker: &Ticker) {}
 
     pub async fn on_balance(&mut self, balance: Decimal) {
-        self.balance = balance;
         if self.inventory.is_zero() {
+            self.balance = balance;
             self.one_grid_order_value = (self.params.lever_rate * self.balance) / self.params.grid;
         }
     }
@@ -771,7 +778,7 @@ impl Predictor {
 
         let sigma_square = self.trend;
         let gamma = self.balance;
-        let kappa = now - data_time;
+        let kappa = self.float_balance;
 
         let flow_ratio = Decimal::ZERO;