|
@@ -15,6 +15,7 @@ use tokio::sync::{Mutex};
|
|
|
use tokio::task::JoinHandle;
|
|
use tokio::task::JoinHandle;
|
|
|
use tokio::time::sleep;
|
|
use tokio::time::sleep;
|
|
|
use tracing::{error, info, warn, instrument};
|
|
use tracing::{error, info, warn, instrument};
|
|
|
|
|
+use global::cci::CentralControlInfo;
|
|
|
use global::params::Params;
|
|
use global::params::Params;
|
|
|
use global::public_params::{ASK_PRICE_INDEX, BID_PRICE_INDEX, LENGTH};
|
|
use global::public_params::{ASK_PRICE_INDEX, BID_PRICE_INDEX, LENGTH};
|
|
|
use global::trace_stack::TraceStack;
|
|
use global::trace_stack::TraceStack;
|
|
@@ -114,10 +115,19 @@ pub struct Quant {
|
|
|
pub recall_max_count: usize, // 最大回溯条数
|
|
pub recall_max_count: usize, // 最大回溯条数
|
|
|
pub short_volume_rate: Decimal, // 主动性跌比率(0.01代表1%)
|
|
pub short_volume_rate: Decimal, // 主动性跌比率(0.01代表1%)
|
|
|
pub long_volume_rate: Decimal, // 主动性涨比率(0.01代表1%)
|
|
pub long_volume_rate: Decimal, // 主动性涨比率(0.01代表1%)
|
|
|
|
|
+
|
|
|
|
|
+ // 中控
|
|
|
|
|
+ pub cci_arc: Arc<Mutex<CentralControlInfo>>, // 中控信息汇集
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
impl Quant {
|
|
impl Quant {
|
|
|
- pub async fn new(exchange: String, params: Params, exchange_params: BTreeMap<String, String>, order_sender: Sender<Order>, error_sender: Sender<Error>, running: Arc<AtomicBool>) -> Quant {
|
|
|
|
|
|
|
+ pub async fn new(exchange: String,
|
|
|
|
|
+ params: Params,
|
|
|
|
|
+ exchange_params: BTreeMap<String, String>,
|
|
|
|
|
+ order_sender: Sender<Order>,
|
|
|
|
|
+ error_sender: Sender<Error>,
|
|
|
|
|
+ running: Arc<AtomicBool>,
|
|
|
|
|
+ cci_arc: Arc<Mutex<CentralControlInfo>>) -> Quant {
|
|
|
let symbol = params.pair.clone();
|
|
let symbol = params.pair.clone();
|
|
|
let pairs: Vec<&str> = params.pair.split('_').collect();
|
|
let pairs: Vec<&str> = params.pair.split('_').collect();
|
|
|
let mut quant_obj = Quant {
|
|
let mut quant_obj = Quant {
|
|
@@ -238,6 +248,7 @@ impl Quant {
|
|
|
recall_max_count: 5000.to_usize().unwrap(),
|
|
recall_max_count: 5000.to_usize().unwrap(),
|
|
|
short_volume_rate: dec!(0.618),
|
|
short_volume_rate: dec!(0.618),
|
|
|
long_volume_rate: dec!(0.618),
|
|
long_volume_rate: dec!(0.618),
|
|
|
|
|
+ cci_arc,
|
|
|
};
|
|
};
|
|
|
for i in 0..=params.ref_exchange.len() - 1 {
|
|
for i in 0..=params.ref_exchange.len() - 1 {
|
|
|
// 拼接不会消耗原字符串
|
|
// 拼接不会消耗原字符串
|
|
@@ -597,7 +608,7 @@ impl Quant {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self, depth, name, trace_stack), level="TRACE")]
|
|
#[instrument(skip(self, depth, name, trace_stack), level="TRACE")]
|
|
|
- pub fn _update_depth(&mut self, depth: Vec<Decimal>, name: String, mut trace_stack: TraceStack) {
|
|
|
|
|
|
|
+ pub async fn _update_depth(&mut self, depth: Vec<Decimal>, name: String, mut trace_stack: TraceStack) {
|
|
|
// info!(?depth, ?name);
|
|
// info!(?depth, ?name);
|
|
|
trace_stack.on_depth();
|
|
trace_stack.on_depth();
|
|
|
|
|
|
|
@@ -673,13 +684,36 @@ impl Quant {
|
|
|
ts.on_before_send();
|
|
ts.on_before_send();
|
|
|
platform_rest_fb.command_order(orders, ts.clone()).await;
|
|
platform_rest_fb.command_order(orders, ts.clone()).await;
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ // 更新中控账户相关信息
|
|
|
|
|
+ {
|
|
|
|
|
+ let mut now_balance = self.strategy.equity / self.used_pct;
|
|
|
|
|
+ now_balance.rescale(4);
|
|
|
|
|
+
|
|
|
|
|
+ let mut cci = self.cci_arc.lock().await;
|
|
|
|
|
+ cci.now_balance = now_balance;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ let mut unrealized_pn_l = self.local_profit;
|
|
|
|
|
+ unrealized_pn_l.rescale(4);
|
|
|
|
|
+
|
|
|
|
|
+ let mut now_price = self.strategy.mp;
|
|
|
|
|
+ now_price.rescale(8);
|
|
|
|
|
+
|
|
|
|
|
+ let mut cci = self.cci_arc.lock().await;
|
|
|
|
|
+ cci.unrealized_pn_l = unrealized_pn_l;
|
|
|
|
|
+ cci.now_price = now_price;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self, data), level="TRACE")]
|
|
#[instrument(skip(self, data), level="TRACE")]
|
|
|
- pub fn update_position(&mut self, data: Vec<Position>) {
|
|
|
|
|
|
|
+ pub async fn update_position(&mut self, data: Vec<Position>) {
|
|
|
if data.is_empty() {
|
|
if data.is_empty() {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -699,6 +733,27 @@ impl Quant {
|
|
|
info!("更新本地仓位:{:?}", position);
|
|
info!("更新本地仓位:{:?}", position);
|
|
|
self.local_position = position;
|
|
self.local_position = position;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 更新中控持仓相关的信息
|
|
|
|
|
+ {
|
|
|
|
|
+ let mut pos = self.local_position_by_orders.long_pos - self.local_position_by_orders.short_pos;
|
|
|
|
|
+ if !self.exchange.contains("spot") {
|
|
|
|
|
+ pos = self.local_position.long_pos - self.local_position.short_pos;
|
|
|
|
|
+ }
|
|
|
|
|
+ pos.rescale(8);
|
|
|
|
|
+
|
|
|
|
|
+ let mut entry_price;
|
|
|
|
|
+ if pos.gt(&Decimal::ZERO) {
|
|
|
|
|
+ entry_price = self.local_position_by_orders.long_avg;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ entry_price = self.local_position_by_orders.short_avg;
|
|
|
|
|
+ }
|
|
|
|
|
+ entry_price.rescale(8);
|
|
|
|
|
+
|
|
|
|
|
+ let mut cci = self.cci_arc.lock().await;
|
|
|
|
|
+ cci.pos = pos;
|
|
|
|
|
+ cci.entry_price = entry_price;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self, data, name), level="TRACE")]
|
|
#[instrument(skip(self, data, name), level="TRACE")]
|
|
@@ -842,7 +897,7 @@ impl Quant {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self, data), level="TRACE")]
|
|
#[instrument(skip(self, data), level="TRACE")]
|
|
|
- pub fn update_equity(&mut self, data: Account) {
|
|
|
|
|
|
|
+ pub async fn update_equity(&mut self, data: Account) {
|
|
|
/*
|
|
/*
|
|
|
更新保证金信息
|
|
更新保证金信息
|
|
|
合约一直更新
|
|
合约一直更新
|
|
@@ -851,7 +906,7 @@ impl Quant {
|
|
|
if self.exchange.contains("spot") {
|
|
if self.exchange.contains("spot") {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- self.local_cash = data.balance * self.used_pct
|
|
|
|
|
|
|
+ self.local_cash = data.balance * self.used_pct;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self), level="TRACE")]
|
|
#[instrument(skip(self), level="TRACE")]
|
|
@@ -1396,6 +1451,14 @@ impl Quant {
|
|
|
} else {
|
|
} else {
|
|
|
self.update_equity_rest_swap().await;
|
|
self.update_equity_rest_swap().await;
|
|
|
}
|
|
}
|
|
|
|
|
+ // 更新中控账户相关信息
|
|
|
|
|
+ {
|
|
|
|
|
+ let mut now_balance = self.local_cash / self.used_pct;
|
|
|
|
|
+ now_balance.rescale(4);
|
|
|
|
|
+
|
|
|
|
|
+ let mut cci = self.cci_arc.lock().await;
|
|
|
|
|
+ cci.now_balance = now_balance;
|
|
|
|
|
+ }
|
|
|
// 初始资金
|
|
// 初始资金
|
|
|
let start_cash = self.local_cash.clone();
|
|
let start_cash = self.local_cash.clone();
|
|
|
let start_coin = self.local_coin.clone();
|
|
let start_coin = self.local_coin.clone();
|