瀏覽代碼

增加浮动盈亏止损字段

JiahengHe 1 年之前
父節點
當前提交
b54db7c4ac
共有 2 個文件被更改,包括 10 次插入4 次删除
  1. 3 0
      global/src/params.rs
  2. 7 4
      strategy/src/core.rs

+ 3 - 0
global/src/params.rs

@@ -40,6 +40,8 @@ pub struct Params {
     pub used_pct: Decimal,
     // 止损比例 默认0.02  0.02 = 2%
     pub stop_loss: Decimal,
+    // 浮动盈亏止损比例
+    pub fluctuation_stop_loss: Decimal,
     // 平滑系数 默认0.999
     pub gamma: Decimal,
     // 分批建仓功能 小资金建议1 大资金建议3 默认 1
@@ -96,6 +98,7 @@ impl Params {
             gamma: dec!(0.999),
             log_level: "info".to_string(),
             port: call_port,
+            fluctuation_stop_loss: Decimal::try_from(json_value["fluctuation_stop_loss"].as_f64().unwrap_or_default()).unwrap(),
         };
         Ok(params)
     }

+ 7 - 4
strategy/src/core.rs

@@ -78,6 +78,8 @@ pub struct Core {
     pub position_check_series: Vec<i8>,
     // 止损大小
     pub stop_loss: Decimal,
+    // 浮动盈亏止损比例
+    pub fluctuation_stop_loss: Decimal,
     // 资金使用率
     pub used_pct: Decimal,
     // 启停信号 0 表示运行 大于1开始倒计时 1时停机
@@ -171,6 +173,7 @@ impl Core {
             exit_msg: "正常退出".to_string(),
             position_check_series: Default::default(),
             stop_loss: params.stop_loss,
+            fluctuation_stop_loss: params.fluctuation_stop_loss,
             used_pct: dec!(0.95),
             mode_signal: 0,
             trade_order_update_time: Utc::now().timestamp_millis(),
@@ -975,11 +978,11 @@ impl Core {
             return;
         }
 
-        // 不是现货执行的回撤风控0
+        // 不是现货执行的回撤风控0(市价盈亏)
         if !self.exchange.contains("spot") {  // 市场价盈亏风控
             let draw_back = Decimal::ONE - self.local_cash_including_unrealized / self.max_local_cash_including_unrealized;
 
-            if draw_back > self.stop_loss {
+            if draw_back > self.fluctuation_stop_loss {
                 let exit_msg = format!("{} 总资金吊灯回撤(市价风控) {}。当前值:{}, 最高净值{},触发止损,准备停机。",
                                        self.params.account_name, draw_back, self.local_cash_including_unrealized, self.max_local_cash_including_unrealized);
                 warn!(exit_msg);
@@ -987,9 +990,9 @@ impl Core {
                 self.stop().await;
             }
         }
-        // // 市场价盈亏风控
+        // // 市场价盈亏风控(市价盈亏)
         let draw_back = self.local_cash_including_unrealized / self.strategy.start_equity;
-        if draw_back < -self.stop_loss {
+        if draw_back < -self.fluctuation_stop_loss {
             let exit_msg = format!("{} 交易亏损,触发止损,准备停机(市价风控)。", self.params.account_name);
             warn!(exit_msg);
             self.exit_msg = exit_msg;