Fix bots + positions managements
This commit is contained in:
@@ -330,7 +330,29 @@ public class TradingBot : Bot, ITradingBot
|
||||
? positionForSignal
|
||||
: TradingService.GetPositionByIdentifier(positionForSignal.Identifier);
|
||||
|
||||
if (position.Status == (PositionStatus.Finished | PositionStatus.Flipped))
|
||||
var positionsExchange = IsForBacktest
|
||||
? new List<Position>{position}
|
||||
: await TradingService.GetBrokerPositions(Account);
|
||||
|
||||
if (!IsForBacktest)
|
||||
{
|
||||
position = positionsExchange.FirstOrDefault(p => p.Ticker == Ticker);
|
||||
}
|
||||
|
||||
if (position.Status == PositionStatus.New)
|
||||
{
|
||||
var orders = await ExchangeService.GetOpenOrders(Account, Ticker);
|
||||
if (orders.Any())
|
||||
{
|
||||
await LogInformation($"Cannot update Position. Position is still waiting for opening. There is {orders.Count()} open orders.");
|
||||
}
|
||||
else
|
||||
{
|
||||
await LogWarning($"Cannot update Position. No position on exchange and no orders. Position {signal.Identifier} might be closed already.");
|
||||
await HandleClosedPosition(positionForSignal);
|
||||
}
|
||||
}
|
||||
else if (position.Status == (PositionStatus.Finished | PositionStatus.Flipped))
|
||||
{
|
||||
await HandleClosedPosition(positionForSignal);
|
||||
}
|
||||
@@ -424,11 +446,12 @@ public class TradingBot : Bot, ITradingBot
|
||||
Logger.LogInformation($"Try to re-open position");
|
||||
await OpenPosition(signal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, ex.Message);
|
||||
SentrySdk.CaptureException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -654,13 +677,23 @@ public class TradingBot : Bot, ITradingBot
|
||||
var openOrders = await ExchangeService.GetOpenOrders(Account, Ticker);
|
||||
if (openOrders.Any())
|
||||
{
|
||||
// TODO: Check if position is open, do not cancel orders if position still open
|
||||
Logger.LogInformation($"Canceling all orders for {Ticker}");
|
||||
await ExchangeService.CancelOrder(Account, Ticker);
|
||||
var closePendingOrderStatus = await ExchangeService.CancelOrder(Account, Ticker);
|
||||
Logger.LogInformation($"Closing all {Ticker} orders status : {closePendingOrderStatus}");
|
||||
}
|
||||
var openPositions = (await ExchangeService.GetBrokerPositions(Account))
|
||||
.Where(p => p.Ticker == Ticker);
|
||||
var cancelClose = openPositions.Any();
|
||||
|
||||
if (cancelClose)
|
||||
{
|
||||
Logger.LogInformation($"Position still open, cancel close orders&");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogInformation($"Canceling all orders for {Ticker}");
|
||||
await ExchangeService.CancelOrder(Account, Ticker);
|
||||
var closePendingOrderStatus = await ExchangeService.CancelOrder(Account, Ticker);
|
||||
Logger.LogInformation($"Closing all {Ticker} orders status : {closePendingOrderStatus}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogInformation($"No need to cancel orders for {Ticker}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user