|
|
@@ -1,12 +1,14 @@
|
|
|
use std::cmp::max;
|
|
|
+use std::sync::Arc;
|
|
|
+use std::time::Duration;
|
|
|
use chrono::Utc;
|
|
|
use rust_decimal::Decimal;
|
|
|
use rust_decimal_macros::dec;
|
|
|
-use tracing::warn;
|
|
|
+use tokio::sync::Mutex;
|
|
|
+use tokio::task::JoinHandle;
|
|
|
+use tracing::{info, warn};
|
|
|
use crate::quant::Quant;
|
|
|
|
|
|
-impl Quant {}
|
|
|
-
|
|
|
pub fn check_risk(mut _self: Quant) {
|
|
|
// 参数检查的风控
|
|
|
if _self.strategy.start_cash == Decimal::ZERO {
|
|
|
@@ -44,8 +46,8 @@ pub fn check_risk(mut _self: Quant) {
|
|
|
// _self.stop()
|
|
|
}
|
|
|
// 报单延迟风控,平均延迟允许上限5000ms
|
|
|
- // TODO quant.rest不存在
|
|
|
- // if _self.rest.avg_delay > 5000 {
|
|
|
+ // TODO quant.platform_rest.avg_delay不存在
|
|
|
+ // if _self.platform_rest.avg_delay > 5000 {
|
|
|
// let exit_msg = format!("{} 延迟爆表 触发风控 准备停机。", _self.params.account_name);
|
|
|
// warn!(exit_msg);
|
|
|
// _self.exit_msg = exit_msg;
|
|
|
@@ -169,3 +171,43 @@ pub fn check_risk(mut _self: Quant) {
|
|
|
// _self.stop()
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// 定期触发的系统逻辑
|
|
|
+pub fn on_timer(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()> {
|
|
|
+ let quant_arc_clone = quant_arc.clone();
|
|
|
+
|
|
|
+ return tokio::spawn(async move {
|
|
|
+ tokio::time::sleep(Duration::from_secs(20)).await;
|
|
|
+
|
|
|
+ loop {
|
|
|
+ tokio::time::sleep(Duration::from_secs(10)).await;
|
|
|
+
|
|
|
+ let mut quant = quant_arc_clone.lock().await;
|
|
|
+ {
|
|
|
+ // 检查风控
|
|
|
+ // quant.check_risk();
|
|
|
+
|
|
|
+ // 线程停止信号
|
|
|
+ if quant.mode_signal == 1 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算预估成交额
|
|
|
+ let total_trade_value = quant.local_buy_value + quant.local_sell_value;
|
|
|
+ let time_diff = Decimal::from(Utc::now().timestamp_millis() - quant.start_time);
|
|
|
+ let trade_vol_24h = ((total_trade_value / time_diff) * dec!(86400));
|
|
|
+ quant.strategy.trade_vol_24h_w = (trade_vol_24h / dec!(10000));
|
|
|
+ quant.strategy.trade_vol_24h_w.rescale(2);
|
|
|
+
|
|
|
+ // 打印各类信息
|
|
|
+ quant.strategy._print_summary();
|
|
|
+ // TODO quant没有rest
|
|
|
+ // info!("Rest报单平均延迟{}ms", quant.rest.avg_delay);
|
|
|
+ // info!("Rest报单最高延迟{}ms", quant.rest.max_delay);
|
|
|
+ for (name, interval) in &quant.market_update_interval {
|
|
|
+ info!("WS盘口{}行情平均更新间隔{}ms。", name, interval);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|