|
|
@@ -53,8 +53,10 @@ pub struct Core {
|
|
|
pub handled_orders_cid: Vec<String>,
|
|
|
// 本地利润值
|
|
|
pub local_profit: Decimal,
|
|
|
- // 本地利润值(包含未实现盈亏)
|
|
|
- pub local_profit_including_unrealized: Decimal,
|
|
|
+ // 本地U值(包含未实现盈亏)
|
|
|
+ pub local_cash_including_unrealized: Decimal,
|
|
|
+ // 本地U值(包含未实现盈亏)
|
|
|
+ pub max_local_cash_including_unrealized: Decimal,
|
|
|
// 本地U保证金
|
|
|
pub local_cash: Decimal,
|
|
|
// 本地币保证金
|
|
|
@@ -143,6 +145,8 @@ impl Core {
|
|
|
local_orders_backup_cid: Default::default(),
|
|
|
handled_orders_cid: Default::default(),
|
|
|
local_profit: Default::default(),
|
|
|
+ local_cash_including_unrealized: Default::default(),
|
|
|
+ max_local_cash_including_unrealized: Default::default(),
|
|
|
local_cash: Default::default(),
|
|
|
local_coin: Default::default(),
|
|
|
local_position: LocalPosition {
|
|
|
@@ -944,6 +948,17 @@ impl Core {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ // 更新本地值(市场价)
|
|
|
+ pub async fn update_equity_profit_including_unrealized(&mut self, profit:Decimal){
|
|
|
+ self.local_cash_including_unrealized = self.local_cash + profit;
|
|
|
+ if self.local_cash_including_unrealized > self.max_local_cash_including_unrealized{
|
|
|
+ self.max_local_cash_including_unrealized = self.local_cash_including_unrealized.clone();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pub async fn check_market_price_risk(&mut self){
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
// #[instrument(skip(self), level="TRACE")]
|
|
|
pub async fn check_risk(&mut self) {
|
|
|
@@ -960,6 +975,27 @@ impl Core {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 不是现货执行的回撤风控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 {
|
|
|
+ let exit_msg = format!("{} 总资金吊灯回撤(市价风控) {}。当前值:{}, 最高净值{},触发止损,准备停机。",
|
|
|
+ self.params.account_name, draw_back, self.local_cash_including_unrealized, self.max_local_cash_including_unrealized);
|
|
|
+ warn!(exit_msg);
|
|
|
+ self.exit_msg = exit_msg;
|
|
|
+ self.stop().await;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // // 市场价盈亏风控
|
|
|
+ let draw_back = self.local_cash_including_unrealized / self.strategy.start_equity;
|
|
|
+ if draw_back < -self.stop_loss {
|
|
|
+ let exit_msg = format!("{} 交易亏损,触发止损,准备停机(市价风控)。", self.params.account_name);
|
|
|
+ warn!(exit_msg);
|
|
|
+ self.exit_msg = exit_msg;
|
|
|
+ self.stop().await;
|
|
|
+ }
|
|
|
+
|
|
|
// 不是现货执行的回撤风控1
|
|
|
if !self.exchange.contains("spot") {
|
|
|
let draw_back = Decimal::ONE - self.strategy.equity / self.strategy.max_equity;
|
|
|
@@ -1536,7 +1572,7 @@ impl Core {
|
|
|
let short_one_hand_value: Decimal;
|
|
|
let long_one_hand_amount: Decimal = (long_one_hand_value / mp / &self.strategy.step_size).floor() * self.strategy.step_size;
|
|
|
let short_one_hand_amount: Decimal;
|
|
|
-
|
|
|
+ info!("mp {}", mp);
|
|
|
if self.exchange.contains("spot") {
|
|
|
short_one_hand_value = start_coin * mp * self.params.lever_rate / grid;
|
|
|
short_one_hand_amount = (short_one_hand_value / mp / self.strategy.step_size).floor() * self.strategy.step_size;
|
|
|
@@ -1556,6 +1592,8 @@ impl Core {
|
|
|
}
|
|
|
// 初始化调度器
|
|
|
self.local_cash = start_cash;
|
|
|
+ self.local_cash_including_unrealized = start_cash;
|
|
|
+ self.max_local_cash_including_unrealized = start_cash;
|
|
|
self.local_coin = start_coin;
|
|
|
|
|
|
// 买入平台币
|