don't crash if no open positions are found
This commit is contained in:
parent
d445dc137a
commit
7357d48115
@ -415,43 +415,43 @@ impl OrderManager {
|
|||||||
let (open_orders, order_book, open_positions) =
|
let (open_orders, order_book, open_positions) =
|
||||||
(res_open_orders?, res_order_book?, res_open_positions?);
|
(res_open_orders?, res_order_book?, res_open_positions?);
|
||||||
|
|
||||||
let position = open_positions
|
// if there are open positions
|
||||||
.ok_or("No open positions!")?
|
if let Some(open_positions) = open_positions {
|
||||||
.into_iter()
|
// if we find an open position with the ID we are looking for
|
||||||
.find(|x| x.id() == position_id)
|
if let Some(position) = open_positions.into_iter().find(|x| x.id() == position_id) {
|
||||||
.ok_or("Position #{} not found in open positions.")?;
|
let opt_position_order = open_orders
|
||||||
|
.iter()
|
||||||
|
.find(|x| x.current_form.amount().neg() == position.amount());
|
||||||
|
|
||||||
let opt_position_order = open_orders
|
// checking if the position has an open order.
|
||||||
.iter()
|
// If so, don't do anything since the order is taken care of
|
||||||
.find(|x| x.current_form.amount().neg() == position.amount());
|
// in the update phase.
|
||||||
|
// If no order is open, send an undercut limit order at the best current price.
|
||||||
|
if let None = opt_position_order {
|
||||||
|
// No open order, undercutting best price with limit order
|
||||||
|
let closing_price = self.best_closing_price(&position, &order_book);
|
||||||
|
|
||||||
// checking if the position has an open order.
|
// TODO: hardcoded platform to Margin!
|
||||||
// If so, don't do anything since the order is taken care of
|
let order_form = OrderForm::new(
|
||||||
// in the update phase.
|
self.pair.clone(),
|
||||||
// If no order is open, send an undercut limit order at the best current price.
|
OrderKind::Limit {
|
||||||
if let None = opt_position_order {
|
price: closing_price,
|
||||||
// No open order, undercutting best price with limit order
|
amount: position.amount().neg(),
|
||||||
let closing_price = self.best_closing_price(&position, &order_book);
|
},
|
||||||
|
TradingPlatform::Margin,
|
||||||
|
);
|
||||||
|
|
||||||
// TODO: hardcoded platform to Margin!
|
info!("Submitting {} order", order_form.kind());
|
||||||
let order_form = OrderForm::new(
|
if let Err(e) = self.client.submit_order(&order_form).await {
|
||||||
self.pair.clone(),
|
error!(
|
||||||
OrderKind::Limit {
|
"Could not submit {} to close position #{}: {}",
|
||||||
price: closing_price,
|
order_form.kind(),
|
||||||
amount: position.amount().neg(),
|
position.id(),
|
||||||
},
|
e
|
||||||
TradingPlatform::Margin,
|
);
|
||||||
);
|
return Err(e);
|
||||||
|
}
|
||||||
info!("Submitting {} order", order_form.kind());
|
}
|
||||||
if let Err(e) = self.client.submit_order(&order_form).await {
|
|
||||||
error!(
|
|
||||||
"Could not submit {} to close position #{}: {}",
|
|
||||||
order_form.kind(),
|
|
||||||
position.id(),
|
|
||||||
e
|
|
||||||
);
|
|
||||||
return Err(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user