|
|
@@ -1060,7 +1060,7 @@ impl Quant {
|
|
|
|
|
|
}
|
|
|
|
|
|
- pub async fn check_position(&mut self){
|
|
|
+ pub async fn check_position(&mut self, target_hold_coin: Decimal){
|
|
|
info!("清空挂单!");
|
|
|
match self.platform_rest.cancel_orders_all().await {
|
|
|
Ok(val)=>{
|
|
|
@@ -1079,14 +1079,14 @@ impl Quant {
|
|
|
}
|
|
|
}
|
|
|
if self.exchange.contains("spot") { // 现货
|
|
|
- self.check_position_spot().await;
|
|
|
+ self.check_position_spot(target_hold_coin).await;
|
|
|
} else { // 合约
|
|
|
self.check_position_swap().await;
|
|
|
}
|
|
|
info!("遗留仓位检测完毕");
|
|
|
}
|
|
|
|
|
|
- pub async fn check_position_spot(&mut self){
|
|
|
+ pub async fn check_position_spot(&mut self, target_hold_coin: Decimal) {
|
|
|
info!("检查遗漏仓位!");
|
|
|
match self.platform_rest.get_spot_account().await {
|
|
|
Ok(mut val) => {
|
|
|
@@ -1105,8 +1105,9 @@ impl Quant {
|
|
|
}
|
|
|
let symbol = format!("{}_USDT", coin_name);
|
|
|
let mut _hold_coin = Decimal::ZERO;
|
|
|
- if coin_name.eq(self.base.as_str()){
|
|
|
- _hold_coin = self.hold_coin;
|
|
|
+ // 如果是交易币,则设定仓位目标
|
|
|
+ if coin_name.eq(self.base.to_uppercase().as_str()){
|
|
|
+ _hold_coin = target_hold_coin;
|
|
|
}
|
|
|
let ap;
|
|
|
let bp;
|
|
|
@@ -1123,11 +1124,15 @@ impl Quant {
|
|
|
}
|
|
|
}
|
|
|
let coin_value = account.balance * mp;
|
|
|
- let diff = (_hold_coin - coin_value)* Decimal::from_str("0.99").unwrap();
|
|
|
+ let diff = (_hold_coin - coin_value) * dec!(0.99);
|
|
|
let side;
|
|
|
let price= Decimal::ZERO;
|
|
|
let amount;
|
|
|
- if diff > Decimal::from(20) {
|
|
|
+
|
|
|
+ if diff != Decimal::ZERO {
|
|
|
+ info!("{}, 需要调整现货仓位 {} usdt", symbol, diff);
|
|
|
+ }
|
|
|
+ if diff > Decimal::from(10) {
|
|
|
side = "kd";
|
|
|
// price = mp*1.001;
|
|
|
amount = diff/mp;
|
|
|
@@ -1138,14 +1143,13 @@ impl Quant {
|
|
|
} else {
|
|
|
continue;
|
|
|
}
|
|
|
- info!("{}, 需要调整现货仓位 {} usdt", symbol, diff);
|
|
|
// 价格0,市价单
|
|
|
match self.platform_rest.take_order_symbol(symbol.clone(), Decimal::ONE, "t-123", side, price, amount).await{
|
|
|
Ok(v)=>{
|
|
|
info!("side: {}, {} 下单,{:?}", side, symbol, v);
|
|
|
// 执行完当前币对 结束循环
|
|
|
continue;
|
|
|
- },Err(ex)=>{
|
|
|
+ }, Err(ex)=>{
|
|
|
error!("side: {}, {} {}", side, symbol, ex);
|
|
|
// 执行完当前币对 结束循环
|
|
|
continue;
|
|
|
@@ -1255,18 +1259,19 @@ impl Quant {
|
|
|
}
|
|
|
|
|
|
pub async fn exit(&mut self, delay: i8){
|
|
|
+ info!("--------------------------------------------------");
|
|
|
info!("预约退出操作 delay:{}", delay);
|
|
|
if delay > 0i8 {
|
|
|
sleep(Duration::from_secs(delay as u64)).await;
|
|
|
}
|
|
|
info!("开始退出操作");
|
|
|
info!("为避免api失效导致遗漏仓位 建议人工复查");
|
|
|
- self.check_position().await;
|
|
|
+ self.check_position(Decimal::ZERO).await;
|
|
|
// 开启停机信号
|
|
|
|
|
|
- sleep(Duration::from_secs(3)).await;
|
|
|
+ sleep(Duration::from_secs(2)).await;
|
|
|
info!("双重检查遗漏仓位");
|
|
|
- self.check_position().await;
|
|
|
+ self.check_position(Decimal::ZERO).await;
|
|
|
info!("停机退出 停机原因: {}", self.exit_msg);
|
|
|
// 发送交易状态 await self._post_params()
|
|
|
// TODO: 向中控发送信号
|
|
|
@@ -1365,7 +1370,7 @@ impl Quant {
|
|
|
}
|
|
|
|
|
|
// 清空挂单和仓位
|
|
|
- self.check_position().await;
|
|
|
+ self.check_position(self.hold_coin).await;
|
|
|
/*
|
|
|
###### 交易前准备就绪 可以开始交易 ######
|
|
|
self.loop.create_task(self.rest.go())
|