Sfoglia il codice sorgente

1. 移除一些日志
2. 换弱一点的均线,方便平仓
3. 把带量突破考虑进去了

skyffire 10 mesi fa
parent
commit
e5b4a44fda

+ 4 - 4
strategy/src/bybit_usdt_swap.rs

@@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicBool};
 use rust_decimal::Decimal;
 use tokio::{spawn};
 use tokio::sync::Mutex;
-use tracing::{error, info};
+use tracing::{error};
 use tokio_tungstenite::tungstenite::Message;
 use exchanges::bybit_swap_ws::{BybitSwapLogin, BybitSwapSubscribeType, BybitSwapWs, BybitSwapWsType};
 use exchanges::response_base::ResponseData;
@@ -229,9 +229,9 @@ async fn on_private_data(core_arc_clone: Arc<Mutex<Core>>, ct_val: &Decimal, run
                     continue;
                 }
 
-                if order.deal_amount != Decimal::ZERO {
-                    info!("bybit order 消息原文:{:?}", response);
-                }
+                // if order.deal_amount != Decimal::ZERO {
+                //     info!("bybit order 消息原文:{:?}", response);
+                // }
 
                 let order_info = OrderInfo::parse_order_to_order_info(&mut order);
                 order_infos.push(order_info);

+ 8 - 7
strategy/src/core.rs

@@ -293,10 +293,10 @@ impl Core {
 
     // #[instrument(skip(self, data, trace_stack), level="TRACE")]
     pub async fn update_local_order(&mut self, data: OrderInfo, trace_stack: TraceStack) {
-        if data.filled != Decimal::ZERO {
-            info!("\n\n");
-            info!("接收到订单信息①:{:?}", data);
-        }
+        // if data.filled != Decimal::ZERO {
+        //     // info!("\n\n");
+        //     // info!("接收到订单信息①:{:?}", data);
+        // }
         /*
          更新订单
             首先直接复写本地订单
@@ -362,7 +362,7 @@ impl Core {
                         let mut filled_order = data.clone();
                         filled_order.side = side.clone();
 
-                        info!("移除本地订单:{:?}, local_by_orders: {:?}", filled_order, self.local_position_by_orders);
+                        // info!("移除本地订单:{:?}, local_by_orders: {:?}", filled_order, self.local_position_by_orders);
                         // 如果是现货交易 还需要修改equity
                         if self.exchange.contains("spot") {
                             // 现货必须考虑fee 买入fee单位为币 卖出fee单位为u
@@ -703,8 +703,9 @@ impl Core {
         }
         // 更新仓位信息
         if position != self.local_position {
-            info!("收到新的仓位推送, position: {:?}", data);
-            info!("更新本地仓位:{:?}", position);
+            info!("收到新的仓位推送: {:?}", data);
+            info!("更新本地仓位: {:?}", position);
+            info!("\n\n");
             self.local_position = position;
         }
 

+ 40 - 44
strategy/src/predictor.rs

@@ -27,10 +27,10 @@ pub struct Predictor {
     pub ask_price: Decimal,                                                     // 卖一价
     pub bid_price: Decimal,                                                     // 买一价
     pub last_price: Decimal,                                                    // 最后成交价
+    pub trades_volume_10s: Decimal,                                             // 过去10秒的成交量总和
+    pub trades_volume_10s_ema: Decimal,                                         // 过去10秒的成交量总和的ema
     pub spread: Decimal,                                                        // 当前价差
-    pub spread_sma: Decimal,                                                    // 价差的sma,默认是sma5000
-    pub spread_sma_2000: Decimal,                                               // 价差的sma,2000级别
-    pub spread_sma_1000: Decimal,                                               // 价差的sma,1000级别
+    pub spread_ema_1000: Decimal,                                               // 价差的ema,1000级别
 
     pub optimal_ask_price: Decimal,                                             // 卖出挂单价
     pub optimal_bid_price: Decimal,                                             // 买入挂单价
@@ -91,7 +91,7 @@ impl Predictor {
     const TRADE_SHORT_RANGE_MICROS: i64 = 10_000_000;
     // const ONE_MILLION: Decimal = dec!(1_000_000);
     // const TWENTY_THOUSAND: Decimal = dec!(20_000);
-    const UN_VIEW: Decimal = dec!(14142135623730951);
+    const DONT_VIEW: Decimal = dec!(14142135623730951);
 
     pub fn new(_cci_arc: Arc<Mutex<CentralControlInfo>>, params: Params) -> Self {
         // 创建数据通道
@@ -107,7 +107,7 @@ impl Predictor {
             while let Some(value) = rx.next().await {
                 // 数据填充到对应位置
                 for i in 0..len {
-                    if value[i] == Self::UN_VIEW {
+                    if value[i] == Self::DONT_VIEW {
                         debugs[i].push_back(None);
                     } else {
                         debugs[i].push_back(Some(value[i]));
@@ -153,10 +153,10 @@ impl Predictor {
             ask_price: Default::default(),
             bid_price: Default::default(),
             last_price: Default::default(),
+            trades_volume_10s: Default::default(),
+            trades_volume_10s_ema: Default::default(),
             spread: Default::default(),
-            spread_sma: Default::default(),
-            spread_sma_2000: Default::default(),
-            spread_sma_1000: Default::default(),
+            spread_ema_1000: Default::default(),
             optimal_ask_price: Default::default(),
             optimal_bid_price: Default::default(),
 
@@ -241,6 +241,7 @@ impl Predictor {
         self.error_rate.rescale(4);
 
         self.last_price = trade.price;
+
         // self.processor().await;
     }
 
@@ -326,7 +327,7 @@ impl Predictor {
             self.fair_price_ema_long = if self.fair_price_ema_long.is_zero() {
                 self.fair_price
             } else {
-                self.fair_price_ema_long * dec!(0.999967) + self.fair_price * dec!(0.000033)
+                self.fair_price_ema_long * dec!(0.9999) + self.fair_price * dec!(0.0001)
             };
             self.fair_price_ema_short = if self.fair_price_ema_short.is_zero() {
                 self.fair_price
@@ -345,13 +346,11 @@ impl Predictor {
 
             // 重置开仓焦点,条件1
             if !self.fair_rate_focus_open.is_zero() {
-                // 向上涨,并且mid下穿fair,视为观测阶段结束
-                if self.fair_rate_focus_open > Decimal::ZERO && self.spread_sma_1000 < Decimal::ZERO {
+                if self.fair_rate_focus_open > Decimal::ZERO && self.spread_ema_1000 < Decimal::ZERO {
                     self.fair_rate_focus_open = Decimal::ZERO;
                 }
 
-                // 向下跌,并且mid上穿fair,视为观测阶段结束
-                if self.fair_rate_focus_open < Decimal::ZERO && self.spread_sma_1000 > Decimal::ZERO {
+                if self.fair_rate_focus_open < Decimal::ZERO && self.spread_ema_1000 > Decimal::ZERO {
                     self.fair_rate_focus_open = Decimal::ZERO;
                 }
             }
@@ -371,10 +370,14 @@ impl Predictor {
                     self.fair_rate_focus_open = Decimal::ZERO;
                 }
             }
+            // 重置开仓焦点,条件4
+            if !self.fair_price_focus_open.is_zero() && self.trades_volume_10s < self.trades_volume_10s_ema {
+                self.fair_rate_focus_open = Decimal::ZERO;
+            }
             // 更新程序关注的开仓焦点
             if self.fair_rate_focus_open.is_zero() && self.inventory.is_zero() {
-                // 只有有强度的rate才有资格被称为针
-                if rate.abs() > self.params.open_activate {
+                // 只有有强度的rate才有资格被称为针,而且要带量
+                if rate.abs() > self.params.open_activate && self.trades_volume_10s > self.trades_volume_10s_ema {
                     // 向上涨,并且fair下穿mid,视为观测阶段开始
                     if rate > Decimal::ZERO {
                         self.fair_rate_focus_open = rate;
@@ -467,26 +470,12 @@ impl Predictor {
         // self.spread.rescale(8);
         self.spread_vec.push(self.spread);
 
-        self.spread_sma = if self.spread_sma.is_zero() {
-            self.spread
-        } else {
-            self.spread_sma * dec!(0.9998) + self.spread * dec!(0.0002)
-        };
-        // self.spread_sma.rescale(8);
-
-        self.spread_sma_2000 = if self.spread_sma_2000.is_zero() {
-            self.spread
-        } else {
-            self.spread_sma_2000 * dec!(0.9995) + self.spread * dec!(0.0005)
-        };
-        // self.spread_sma_2000.rescale(8);
-
-        self.spread_sma_1000 = if self.spread_sma_1000.is_zero() {
+        self.spread_ema_1000 = if self.spread_ema_1000.is_zero() {
             self.spread
         } else {
-            self.spread_sma_1000 * dec!(0.999) + self.spread * dec!(0.001)
+            self.spread_ema_1000 * dec!(0.999) + self.spread * dec!(0.001)
         };
-        self.spread_sma_1000_time_vec.push_back(self.spread_sma_1000);
+        self.spread_sma_1000_time_vec.push_back(self.spread_ema_1000);
         // self.spread_sma_1000.rescale(8);
 
         while self.spread_vec.len() > 1_000 {
@@ -550,7 +539,7 @@ impl Predictor {
         self.optimal_ask_price = if self.ask_delta == dec!(-1) {
             self.bid_price
         } else if self.ask_delta == dec!(-2) {
-            Self::UN_VIEW
+            Self::DONT_VIEW
         } else {
             max(self.ask_price + self.ask_delta, self.bid_price)
         };
@@ -558,7 +547,7 @@ impl Predictor {
         self.optimal_bid_price = if self.bid_delta == dec!(-1) {
             self.ask_price
         } else if self.bid_delta == dec!(-2) {
-            Self::UN_VIEW
+            Self::DONT_VIEW
         } else {
             min(self.bid_price - self.bid_delta, self.ask_price)
         };
@@ -607,15 +596,22 @@ impl Predictor {
 
     // #[instrument(skip(self), level="TRACE")]
     async fn processor(&mut self) {
-        self.update_t_diff();
-        self.update_delta();
-        self.update_optimal_ask_and_bid();
-
         self.check_ready();
         if !self.is_ready {
             return;
         }
 
+        self.trades_volume_10s = self.trade_short_vec.deque.iter().map(|item| item.value).sum();
+        self.trades_volume_10s_ema = if self.trades_volume_10s_ema.is_zero() {
+            self.trades_volume_10s
+        } else {
+            self.trades_volume_10s_ema * dec!(0.9995) + self.trades_volume_10s * dec!(0.0005)
+        };
+
+        self.update_t_diff();
+        self.update_delta();
+        self.update_optimal_ask_and_bid();
+
         // let mut smm = Decimal::ZERO;
         // if !self.depth_vec[1].time.is_zero() {
         //     let sma = self.depth_vec[1].asks[0].price;
@@ -630,10 +626,11 @@ impl Predictor {
         let ask_price = self.ask_price;
         let bid_price = self.bid_price;
         let last_price = self.last_price;
+        let fair_price = self.fair_price_ema_long;
 
-        let spread = self.fair_price;
-        let spread_max = self.fair_price_ema_short;
-        let spread_min = self.fair_price_ema_long;
+        let spread = self.trades_volume_10s;
+        let spread_max = self.trades_volume_10s_ema;
+        let spread_min = Self::DONT_VIEW;
         // let spread = self.price_times_avg;
         // let spread_max = self.fair_price_vec[1] / self.fair_price_vec[0];
         // let spread_min = self.fair_price / self.mid_price;
@@ -656,10 +653,9 @@ impl Predictor {
 
         let gamma = self.fair_rate_focus_open;
 
-        let kappa = self.trade_short_vec.deque.iter().map(|item| item.value).sum();
+        let kappa = Decimal::ZERO;
 
         let flow_ratio = Decimal::ZERO;
-        let ref_price = self.fair_price;
 
         let need_append = now - self.prev_insert_time > Decimal::ONE_HUNDRED;
         if !need_append {
@@ -682,7 +678,7 @@ impl Predictor {
             gamma,
             kappa,
             flow_ratio,
-            ref_price
+            fair_price
         ]).unwrap();
 
         self.prev_insert_time = Decimal::from(Utc::now().timestamp_millis())

+ 8 - 33
strategy/src/strategy.rs

@@ -353,40 +353,15 @@ impl Strategy {
         self.trade_open_dist.rescale(6);
         self.trade_close_dist.rescale(6);
         self.predict.rescale(5);
-        // 挂单列表长度
-        let o_num = self.local_orders.len();
 
-        let mut msg = String::new();
-        msg.push_str("当前状态: ");
-        msg.push_str(format!("[品种 {}, 现价 {:?}, 定价 {:?}, 偏差 {:?}%, 杠杆 {:?}, 动态{:?}, 预估24H成交额 {:?}万], ",
-                             self.params.pair, self.mp, self.ref_price, price_bias, self.lever_rate,
-                             self.adjust_lever_rate, self.trade_vol_24h_w).as_str());
-        msg.push_str(format!("[净值 {:?}, Cash {:?}, Coin(价值) {:?}, 日化 {:?}%], ", self.equity, self.cash, value, self.daily_return).as_str());
-        msg.push_str(format!("[推算利润 {:?}, 盈亏 {:?}%, 做多杠杆 {:?}%, 做多浮盈 {:?}%, 做空杠杆 {:?}%, 做空浮盈 {:?}%], ",
-                             self.local_profit, self.profit, long_pos_leverage, self.long_pos_bias, short_pos_leverage, self.short_pos_bias).as_str());
-        msg.push_str(format!("[请求 {:?}, 上限{:?}次/10秒], ", self._req_num_per_window, self.limit_order_requests_num).as_str());
-        msg.push_str(format!("[当前参数, 开仓 {:?}, 平仓 {:?}, 参考 {:?}, 模式 {:?}], ",
-                             self.trade_open_dist, self.trade_close_dist, self.ref_name[self.ref_index], self.maker_mode).as_str());
-        msg.push_str(format!("[挂单列表,共{:?}单, ", o_num).as_str());
-        for (_, order) in &self.local_orders {
-            let mut order_value = order.amount * self.mp;
-            let mut order_lever_rate = if self.equity.gt(&Decimal::ZERO) {
-                order.amount * self.mp / self.equity
-            } else {
-                Decimal::ZERO
-            };
-            let mut order_bias = Decimal::ONE_HUNDRED * (order.price - self.mp) / self.mp;
-
-            order_value.rescale(2);
-            order_lever_rate.rescale(3);
-            order_bias.rescale(3);
-
-            msg.push_str(format!("[{:?} {:?} {:?}, 杠杆{:?}X 价值{:?}U 价格{:?} 偏离{:?}%]",
-                                 order.symbol, order.client_id, order.side, order_lever_rate, order_value, order.price, order_bias).as_str());
-        }
-
-        msg.push_str("]");
-        info!("{}", msg);
+        info!("------------------当前状态-------------------");
+        info!("[品种 {}, 现价 {:?}, 定价 {:?}, 偏差 {:?}%, 杠杆 {:?}, 动态{:?}, 预估24H成交额 {:?}万], ",
+            self.params.pair, self.mp, self.ref_price, price_bias, self.lever_rate,
+            self.adjust_lever_rate, self.trade_vol_24h_w);
+        info!("[净值 {:?}, Cash {:?}, Coin(价值) {:?}, 日化 {:?}%], ",
+            self.equity, self.cash, value, self.daily_return);
+        info!("[推算利润 {:?}, 盈亏 {:?}%, 做多杠杆 {:?}%, 做多浮盈 {:?}%, 做空杠杆 {:?}%, 做空浮盈 {:?}%], ",
+            self.local_profit, self.profit, long_pos_leverage, self.long_pos_bias, short_pos_leverage, self.short_pos_bias);
     }
 
     // 取消目标方向订单,原文是_cancel_targit_side_orders