Browse Source

一些数据调整。

skyffire 1 year ago
parent
commit
fbcb6efed8

+ 2 - 2
standard/src/exchange_struct_handler.rs

@@ -231,10 +231,10 @@ impl ExchangeStructHandler {
         }
     }
     // 处理position信息
-    pub fn position_handle(exchange: ExchangeEnum, res_data: &ResponseData, ct_val: &Decimal) -> Vec<Position> {
+    pub fn position_handle(exchange: ExchangeEnum, res_data: &ResponseData, mul: &Decimal) -> Vec<Position> {
         match exchange {
             ExchangeEnum::GateSwap => {
-                gate_swap_handle::handle_position(res_data, ct_val)
+                gate_swap_handle::handle_position(res_data, mul)
             }
             // ExchangeEnum::KucoinSwap => {
             //     kucoin_swap_handle::handle_position(res_data, ct_val)

+ 2 - 2
standard/src/gate_swap_handle.rs

@@ -39,9 +39,9 @@ pub fn format_account_info(data: &Vec<Value>, symbol: &String) -> Account {
 }
 
 // 处理position信息
-pub fn handle_position(res_data: &ResponseData, ct_val: &Decimal) -> Vec<Position> {
+pub fn handle_position(res_data: &ResponseData, mul: &Decimal) -> Vec<Position> {
     let res_data_json = res_data.data.as_array().unwrap();
-    res_data_json.iter().map(|item| { format_position_item(item, ct_val) }).collect()
+    res_data_json.iter().map(|item| { format_position_item(item, mul) }).collect()
 }
 
 pub fn format_position_item(position: &Value, ct_val: &Decimal) -> Position {

+ 1 - 0
strategy/src/core.rs

@@ -729,6 +729,7 @@ impl Core {
             }
             entry_price.rescale(8);
 
+            self.avellaneda_stoikov.inventory = pos;
             let mut cci = self.cci_arc.lock().await;
             cci.pos = pos;
             cci.entry_price = entry_price;

+ 5 - 5
strategy/src/gate_usdt_swap.rs

@@ -140,11 +140,11 @@ async fn on_data(core_arc: Arc<Mutex<Core>>,
         //         core.update_order(order_infos, trace_stack).await;
         //     }
         // }
-        // "futures.positions" => {
-        //     let positions = standard::handle_info::HandleSwapInfo::handle_position(GateSwap, &response, multiplier);
-        //     let mut core = core_arc_clone.lock().await;
-        //     core.update_position(positions).await;
-        // }
+        "futures.positions" => {
+            let positions = ExchangeStructHandler::position_handle(ExchangeEnum::GateSwap, &response, multiplier);
+            let mut core = core_arc.lock().await;
+            core.update_position(positions).await;
+        }
         _ => {
             error!("未知推送类型");
             error!(?response);

+ 38 - 31
strategy/src/predictor.rs

@@ -1,24 +1,31 @@
-use std::cmp::{max, min};
 use std::collections::{BTreeMap, VecDeque};
 use rust_decimal::prelude::*;
 use tracing::info;
 use standard::{Depth, Ticker, Trade};
 
+const MAX_DATA_LENGTH: usize = 1000;
+
 #[derive(Debug)]
 pub struct AvellanedaStoikov {
     pub depth_vec: VecDeque<Depth>,                                             // 深度队列
     pub trade_vec: VecDeque<Trade>,                                             // 交易队列
     pub spread_vec: VecDeque<Decimal>,                                          // 市场冲击队列
 
-    pub optimal_ask_price: Decimal,
-    pub optimal_bid_price: Decimal,
     pub mid_price: Decimal,
     pub ask_price: Decimal,
     pub bid_price: Decimal,
-    pub ref_price: Decimal,
     pub spread: Decimal,
     pub spread_max: Decimal,
     pub spread_min: Decimal,
+    pub optimal_ask_price: Decimal,
+    pub optimal_bid_price: Decimal,
+
+    pub inventory: Decimal,                                                     // 库存,也就是q
+    pub gamma: Decimal,                                                         // γ,库存风险厌恶参数
+    pub sigma: Decimal,                                                         // σ,波动性
+    pub delta_plus: Decimal,                                                    // δa+δb,买卖挂单间距
+    pub kappa: Decimal,                                                         // κ 订单簿 流动性 参数
+    pub ref_price: Decimal,                                                     // 预定价格
 }
 
 /*
@@ -27,48 +34,48 @@ pub struct AvellanedaStoikov {
 */
 impl AvellanedaStoikov {
     pub fn new() -> Self {
-        let mut avellaneda_stoikov = Self {
-            // 分别给与10000的长度
-            depth_vec: VecDeque::with_capacity(10000),
-            trade_vec: VecDeque::with_capacity(10000),
-            spread_vec: VecDeque::with_capacity(10000),
+        let avellaneda_stoikov = Self {
+            // 分别给与MAX_DATA_LENGTH的长度
+            depth_vec: VecDeque::with_capacity(MAX_DATA_LENGTH),
+            trade_vec: VecDeque::with_capacity(MAX_DATA_LENGTH),
+            spread_vec: VecDeque::with_capacity(MAX_DATA_LENGTH),
 
-            optimal_ask_price: Default::default(),
-            optimal_bid_price: Default::default(),
             mid_price: Default::default(),
             ask_price: Default::default(),
             bid_price: Default::default(),
-            ref_price: Default::default(),
             spread: Default::default(),
             spread_max: Default::default(),
             spread_min: Default::default(),
-        };
+            optimal_ask_price: Default::default(),
+            optimal_bid_price: Default::default(),
 
-        // 初始化市场冲击相关参数
-        avellaneda_stoikov.init_spread();
+            inventory: Default::default(),
+            gamma: Default::default(),
+            sigma: Default::default(),
+            delta_plus: Default::default(),
+            kappa: Default::default(),
+            ref_price: Default::default(),
+        };
 
         avellaneda_stoikov
     }
 
-    // 初始化市场冲击参数,初始化时机等后面再研究吧
-    pub fn init_spread(&mut self) {
-        // -1就是未赋值
-        self.spread_max = Decimal::NEGATIVE_ONE;
-        self.spread_min = Decimal::NEGATIVE_ONE;
-    }
-
     // 更新最大市场冲击
     pub fn update_spread_max(&mut self) {
-        self.spread_max = max(self.spread_max, self.spread);
+        self.spread_max = if let Some(&max_value) = self.spread_vec.iter().max() {
+            max_value
+        } else {
+            Decimal::NEGATIVE_ONE
+        }
     }
 
     // 更新最小市场冲击
     pub fn update_spread_min(&mut self) {
-        self.spread_min = if self.spread_min == Decimal::NEGATIVE_ONE {
-            self.spread
+        self.spread_min = if let Some(&min_value) = self.spread_vec.iter().min() {
+            min_value
         } else {
-            min(self.spread_min, self.spread)
-        };
+            Decimal::NEGATIVE_ONE
+        }
     }
 
     pub fn update_spread(&mut self, mid_price_now: &Decimal) {
@@ -80,7 +87,7 @@ impl AvellanedaStoikov {
             self.spread_vec.push_back(self.spread);
 
             // 简易的长度控制
-            if self.spread_vec.len() > 10000 {
+            if self.spread_vec.len() > MAX_DATA_LENGTH {
                 self.spread_vec.pop_front();
             }
         }
@@ -100,10 +107,10 @@ impl AvellanedaStoikov {
 
         self.update_ref_price();
 
-        info!(?self.mid_price, ?self.spread, ?self.spread_max, ?self.spread_min);
+        info!(?self.mid_price, ?self.spread, ?self.spread_max, ?self.spread_min, ?self.inventory);
 
         // 简易的长度控制
-        if self.depth_vec.len() > 10000 {
+        if self.depth_vec.len() > MAX_DATA_LENGTH {
             self.depth_vec.pop_front();
         }
     }
@@ -113,7 +120,7 @@ impl AvellanedaStoikov {
         self.processor();
 
         // 简易的长度控制
-        if self.trade_vec.len() > 10000 {
+        if self.trade_vec.len() > MAX_DATA_LENGTH {
             self.trade_vec.pop_front();
         }
     }