joining tasks

This commit is contained in:
Giulio De Pasquale 2021-01-24 14:01:42 +00:00
parent 9b92d38318
commit 193feac230

View File

@ -427,16 +427,32 @@ impl OrderManager {
) -> Result<(), BoxError> { ) -> Result<(), BoxError> {
info!("Closing position #{}", position.id()); info!("Closing position #{}", position.id());
debug!("Retrieving open orders..."); debug!("Retrieving open orders and current prices...");
let open_orders = self.client.active_orders(&self.pair).await?; let (open_orders, order_book) = tokio::join!(
self.client.active_orders(&self.pair),
self.client.order_book(&self.pair)
);
let open_orders = match open_orders {
Ok(open_orders) => open_orders,
Err(e) => {
error!("Could not retrieve open orders: {}", e);
return Err(e);
}
};
let order_book = match order_book {
Ok(order_book) => order_book,
Err(e) => {
error!("Could not retrieve order book: {}", e);
return Err(e);
}
};
let opt_position_order = open_orders let opt_position_order = open_orders
.iter() .iter()
.find(|x| x.current_form.amount().neg() == position.amount()); .find(|x| x.current_form.amount().neg() == position.amount());
debug!("Getting current prices...");
let order_book = self.client.order_book(&self.pair).await?;
// checking if the position has an open order. // checking if the position has an open order.
// If so, the strategy method is called, otherwise we open // If so, the strategy method is called, otherwise we open
// an undercut limit order at the best current price. // an undercut limit order at the best current price.
@ -560,9 +576,15 @@ impl PairManager {
} }
pub async fn update_managers(&mut self, tick: u64) -> Result<(), BoxError> { pub async fn update_managers(&mut self, tick: u64) -> Result<(), BoxError> {
let (opt_price_events, opt_price_messages) = self.price_manager.update(tick).await?; let (price_results, pos_results, order_results) = tokio::join!(
let (opt_pos_events, opt_pos_messages) = self.position_manager.update(tick).await?; self.price_manager.update(tick),
let (opt_order_events, opt_order_messages) = self.order_manager.update(tick).await?; self.position_manager.update(tick),
self.order_manager.update(tick)
);
let (opt_price_events, opt_price_messages) = price_results?;
let (opt_pos_events, opt_pos_messages) = pos_results?;
let (opt_order_events, opt_order_messages) = order_results?;
// TODO: to move into Handler? // TODO: to move into Handler?