|
|
@@ -375,7 +375,7 @@ impl Core {
|
|
|
if side == "kd" { // buy 开多
|
|
|
self.local_buy_amount += filled - fee;
|
|
|
self.local_buy_value += (filled - fee) * filled_price;
|
|
|
- let new_long_pos = self.local_position_by_orders.long_pos + filled - fee;
|
|
|
+ let new_long_pos = self.local_position_by_orders.long_pos + filled - fee;
|
|
|
if new_long_pos == Decimal::ZERO {
|
|
|
self.local_position_by_orders.long_avg = Decimal::ZERO;
|
|
|
self.local_position_by_orders.long_pos = Decimal::ZERO;
|
|
|
@@ -494,14 +494,14 @@ impl Core {
|
|
|
// 更新策略时间
|
|
|
self.strategy.local_time = Utc::now().timestamp_millis();
|
|
|
// trace_stack.on_before_strategy();
|
|
|
- let order = self.strategy.on_tick(&self.local_orders,
|
|
|
- &self.local_position_by_orders,
|
|
|
- &self.agg_market,
|
|
|
- &self.local_cash,
|
|
|
- &self.local_coin,
|
|
|
- &self.ref_price,
|
|
|
- &self.predict,
|
|
|
- &trace_stack.ins);
|
|
|
+ let mut order = self.strategy.on_tick(&self.local_orders,
|
|
|
+ &self.local_position_by_orders,
|
|
|
+ &self.agg_market,
|
|
|
+ &self.local_cash,
|
|
|
+ &self.local_coin,
|
|
|
+ &self.ref_price,
|
|
|
+ &self.predict,
|
|
|
+ &trace_stack.ins);
|
|
|
// trace_stack.on_after_strategy();
|
|
|
// 记录指令触发信息
|
|
|
if order.is_not_empty() {
|
|
|
@@ -515,7 +515,7 @@ impl Core {
|
|
|
// ts.on_before_send();
|
|
|
// info!("update_order 订单指令:{:?}", order);
|
|
|
spawn(async move {
|
|
|
- platform_rest_fb.command_order(order, ts.clone()).await;
|
|
|
+ platform_rest_fb.command_order(&mut order, &mut ts).await;
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
@@ -648,14 +648,14 @@ impl Core {
|
|
|
self.strategy.local_time = Utc::now().timestamp_millis();
|
|
|
|
|
|
// 产生交易信号
|
|
|
- let orders = self.strategy.on_tick(&self.local_orders,
|
|
|
- &self.local_position_by_orders,
|
|
|
- &self.agg_market,
|
|
|
- &self.local_cash,
|
|
|
- &self.local_coin,
|
|
|
- &self.ref_price,
|
|
|
- &self.predict,
|
|
|
- &trace_stack.ins);
|
|
|
+ let mut orders = self.strategy.on_tick(&self.local_orders,
|
|
|
+ &self.local_position_by_orders,
|
|
|
+ &self.agg_market,
|
|
|
+ &self.local_cash,
|
|
|
+ &self.local_coin,
|
|
|
+ &self.ref_price,
|
|
|
+ &self.predict,
|
|
|
+ &trace_stack.ins);
|
|
|
|
|
|
if orders.is_not_empty() {
|
|
|
// debug!("触发onTick");
|
|
|
@@ -664,11 +664,10 @@ impl Core {
|
|
|
let mut platform_rest_fb = self.platform_rest.clone_box();
|
|
|
// info!("订单指令:{:?}", orders);
|
|
|
self.update_trade_msg();
|
|
|
- TraceStack::show_delay(&trace_stack.ins);
|
|
|
- let mut ts = trace_stack.clone();
|
|
|
- ts.set_order_command(orders.to_string());
|
|
|
+ let ts = trace_stack.clone();
|
|
|
spawn(async move {
|
|
|
- platform_rest_fb.command_order(orders, ts.clone()).await;
|
|
|
+ TraceStack::show_delay(&ts.ins);
|
|
|
+ platform_rest_fb.command_order(&mut orders, &ts).await;
|
|
|
});
|
|
|
|
|
|
// 更新中控账户相关信息
|
|
|
@@ -684,7 +683,6 @@ impl Core {
|
|
|
}
|
|
|
|
|
|
{
|
|
|
-
|
|
|
let mut unrealized_pn_l = self.local_profit;
|
|
|
unrealized_pn_l.rescale(4);
|
|
|
|
|
|
@@ -906,7 +904,7 @@ impl Core {
|
|
|
Ok(val) => {
|
|
|
// info!("bybit_swap:定时获取的仓位信息");
|
|
|
self.update_position(val).await;
|
|
|
- },
|
|
|
+ }
|
|
|
Err(err) => {
|
|
|
error!("bybit_swap:定时获取仓位信息错误!\nget_position:res_data={:?}", err);
|
|
|
}
|
|
|
@@ -1350,65 +1348,65 @@ impl Core {
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
- // match self.platform_rest.get_ticker_symbol(position.symbol.clone()).await {
|
|
|
- // Ok(ticker) => {
|
|
|
- // let ap = ticker.sell;
|
|
|
- // let bp = ticker.buy;
|
|
|
- // let mp = (ap + bp) / Decimal::TWO;
|
|
|
- // let price;
|
|
|
- // let side;
|
|
|
- // let market_info;
|
|
|
- // // 获取market
|
|
|
- // match self.platform_rest.get_market_symbol(position.symbol.clone()).await {
|
|
|
- // Ok(market) => {
|
|
|
- // market_info = market;
|
|
|
- // }
|
|
|
- // Err(err) => {
|
|
|
- // error!(" {} 获取当前market异常: {}", position.symbol.clone(), err);
|
|
|
- // continue;
|
|
|
- // }
|
|
|
- // }
|
|
|
- // info!(?position);
|
|
|
- // match position.position_mode {
|
|
|
- // PositionModeEnum::Long => {
|
|
|
- // // pd
|
|
|
- // price = (mp * dec!(0.9985) / market_info.tick_size).floor() * market_info.tick_size;
|
|
|
- // side = "pd";
|
|
|
- // }
|
|
|
- // PositionModeEnum::Short => {
|
|
|
- // // pk
|
|
|
- // price = (mp * dec!(1.0015) / market_info.tick_size).floor() * market_info.tick_size;
|
|
|
- // side = "pk";
|
|
|
- // }
|
|
|
- // _ => {
|
|
|
- // error!(" 仓位position_mode匹配失败,不做操作!");
|
|
|
- // // 执行完当前币对 结束循环
|
|
|
- // continue;
|
|
|
- // }
|
|
|
- // }
|
|
|
- // // 发起清仓订单
|
|
|
- // info!(?ticker);
|
|
|
- // let mut ts = TraceStack::new(0, Instant::now());
|
|
|
- // ts.on_before_send();
|
|
|
- // match self.platform_rest.take_order_symbol(position.symbol.clone(), Decimal::ONE, utils::generate_client_id(None).as_str(), side, price, position.amount.abs()).await {
|
|
|
- // Ok(order) => {
|
|
|
- // ts.on_after_send();
|
|
|
- // info!(" {}仓位清除下单成功 {:?}, {}", position.symbol.clone(), order, ts.to_string());
|
|
|
- // // 执行完当前币对 结束循环
|
|
|
- // continue;
|
|
|
- // }
|
|
|
- // Err(error) => {
|
|
|
- // ts.on_after_send();
|
|
|
- // error!(" {}仓位清除下单异常 {}, {}", position.symbol.clone(), error, ts.to_string());
|
|
|
- // // 执行完当前币对 结束循环
|
|
|
- // continue;
|
|
|
- // }
|
|
|
- // };
|
|
|
- // }
|
|
|
- // Err(err) => {
|
|
|
- // error!(" {} 获取当前ticker异常: {}", position.symbol.clone(), err)
|
|
|
- // }
|
|
|
- // }
|
|
|
+ // match self.platform_rest.get_ticker_symbol(position.symbol.clone()).await {
|
|
|
+ // Ok(ticker) => {
|
|
|
+ // let ap = ticker.sell;
|
|
|
+ // let bp = ticker.buy;
|
|
|
+ // let mp = (ap + bp) / Decimal::TWO;
|
|
|
+ // let price;
|
|
|
+ // let side;
|
|
|
+ // let market_info;
|
|
|
+ // // 获取market
|
|
|
+ // match self.platform_rest.get_market_symbol(position.symbol.clone()).await {
|
|
|
+ // Ok(market) => {
|
|
|
+ // market_info = market;
|
|
|
+ // }
|
|
|
+ // Err(err) => {
|
|
|
+ // error!(" {} 获取当前market异常: {}", position.symbol.clone(), err);
|
|
|
+ // continue;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // info!(?position);
|
|
|
+ // match position.position_mode {
|
|
|
+ // PositionModeEnum::Long => {
|
|
|
+ // // pd
|
|
|
+ // price = (mp * dec!(0.9985) / market_info.tick_size).floor() * market_info.tick_size;
|
|
|
+ // side = "pd";
|
|
|
+ // }
|
|
|
+ // PositionModeEnum::Short => {
|
|
|
+ // // pk
|
|
|
+ // price = (mp * dec!(1.0015) / market_info.tick_size).floor() * market_info.tick_size;
|
|
|
+ // side = "pk";
|
|
|
+ // }
|
|
|
+ // _ => {
|
|
|
+ // error!(" 仓位position_mode匹配失败,不做操作!");
|
|
|
+ // // 执行完当前币对 结束循环
|
|
|
+ // continue;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // // 发起清仓订单
|
|
|
+ // info!(?ticker);
|
|
|
+ // let mut ts = TraceStack::new(0, Instant::now());
|
|
|
+ // ts.on_before_send();
|
|
|
+ // match self.platform_rest.take_order_symbol(position.symbol.clone(), Decimal::ONE, utils::generate_client_id(None).as_str(), side, price, position.amount.abs()).await {
|
|
|
+ // Ok(order) => {
|
|
|
+ // ts.on_after_send();
|
|
|
+ // info!(" {}仓位清除下单成功 {:?}, {}", position.symbol.clone(), order, ts.to_string());
|
|
|
+ // // 执行完当前币对 结束循环
|
|
|
+ // continue;
|
|
|
+ // }
|
|
|
+ // Err(error) => {
|
|
|
+ // ts.on_after_send();
|
|
|
+ // error!(" {}仓位清除下单异常 {}, {}", position.symbol.clone(), error, ts.to_string());
|
|
|
+ // // 执行完当前币对 结束循环
|
|
|
+ // continue;
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+ // }
|
|
|
+ // Err(err) => {
|
|
|
+ // error!(" {} 获取当前ticker异常: {}", position.symbol.clone(), err)
|
|
|
+ // }
|
|
|
+ // }
|
|
|
// }
|
|
|
}
|
|
|
Err(error) => {
|
|
|
@@ -1418,7 +1416,7 @@ impl Core {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return length
|
|
|
+ return length;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -1616,19 +1614,20 @@ pub fn run_strategy(core_arc: Arc<Mutex<Core>>) -> JoinHandle<()> {
|
|
|
let ref_price = core.ref_price.clone();
|
|
|
let predict = core.predict.clone();
|
|
|
|
|
|
- let orders = core.strategy.on_exit(&local_orders,
|
|
|
- &position,
|
|
|
- &agg_market,
|
|
|
- &local_cash,
|
|
|
- &local_coin,
|
|
|
- &ref_price,
|
|
|
- &predict);
|
|
|
+ let mut orders = core.strategy.on_exit(&local_orders,
|
|
|
+ &position,
|
|
|
+ &agg_market,
|
|
|
+ &local_cash,
|
|
|
+ &local_coin,
|
|
|
+ &ref_price,
|
|
|
+ &predict);
|
|
|
if orders.is_not_empty() {
|
|
|
info!("触发onExit");
|
|
|
info!(?orders);
|
|
|
core._update_local_orders(&orders);
|
|
|
spawn(async move {
|
|
|
- platform_rest_fb.command_order(orders, TraceStack::new(0, Instant::now())).await;
|
|
|
+ let mut ts = TraceStack::new(0, Instant::now());
|
|
|
+ platform_rest_fb.command_order(&mut orders, &mut ts).await;
|
|
|
});
|
|
|
}
|
|
|
} else {
|
|
|
@@ -1641,20 +1640,21 @@ pub fn run_strategy(core_arc: Arc<Mutex<Core>>) -> JoinHandle<()> {
|
|
|
let ref_price = core.ref_price.clone();
|
|
|
let predict = core.predict.clone();
|
|
|
|
|
|
- let orders = core.strategy.on_sleep(&local_orders,
|
|
|
- &position,
|
|
|
- &agg_market,
|
|
|
- &local_cash,
|
|
|
- &local_coin,
|
|
|
- &ref_price,
|
|
|
- &predict);
|
|
|
+ let mut orders = core.strategy.on_sleep(&local_orders,
|
|
|
+ &position,
|
|
|
+ &agg_market,
|
|
|
+ &local_cash,
|
|
|
+ &local_coin,
|
|
|
+ &ref_price,
|
|
|
+ &predict);
|
|
|
// 记录指令触发信息
|
|
|
if orders.is_not_empty() {
|
|
|
info!("触发onSleep");
|
|
|
info!(?orders);
|
|
|
core._update_local_orders(&orders);
|
|
|
spawn(async move {
|
|
|
- platform_rest_fb.command_order(orders, TraceStack::new(0, Instant::now())).await;
|
|
|
+ let mut ts = TraceStack::new(0, Instant::now());
|
|
|
+ platform_rest_fb.command_order(&mut orders, &mut ts).await;
|
|
|
});
|
|
|
}
|
|
|
}
|