Enhance TradingBot logic to handle multiple open orders: added checks for order count and elapsed time before canceling unfilled positions.

This commit is contained in:
2025-06-07 23:56:17 +07:00
parent 1e50703da3
commit e737b4f2ce
5 changed files with 38 additions and 2 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
src/.DS_Store vendored

Binary file not shown.

View File

@@ -440,8 +440,44 @@ public class TradingBot : Bot, ITradingBot
var orders = await ExchangeService.GetOpenOrders(Account, Config.Ticker);
if (orders.Any())
{
await LogInformation(
$"Cannot update Position. Position is still waiting for opening. There is {orders.Count()} open orders.");
// If there are 3 or more orders and position is still not filled, check if enough time has passed
if (orders.Count() >= 3)
{
var currentTime = Config.IsForBacktest ? OptimizedCandles.Last().Date : DateTime.UtcNow;
var timeSinceRequest = currentTime - positionForSignal.Open.Date;
var waitTimeMinutes = 10;
if (timeSinceRequest.TotalMinutes >= waitTimeMinutes)
{
await LogWarning(
$"Too many open orders ({orders.Count()}) for unfilled position and {waitTimeMinutes} minutes have passed. Canceling all orders and marking position as canceled.");
try
{
await ExchangeService.CancelOrder(Account, Config.Ticker);
await LogInformation($"Successfully canceled all orders for {Config.Ticker}");
}
catch (Exception ex)
{
await LogWarning($"Failed to cancel orders: {ex.Message}");
}
await SetPositionStatus(signal.Identifier, PositionStatus.Canceled);
SetSignalStatus(signal.Identifier, SignalStatus.Expired);
return;
}
else
{
var remainingMinutes = waitTimeMinutes - timeSinceRequest.TotalMinutes;
await LogInformation(
$"Position has {orders.Count()} open orders but only {timeSinceRequest.TotalMinutes:F1} minutes have passed. Waiting {remainingMinutes:F1} more minutes before canceling.");
}
}
else
{
await LogInformation(
$"Cannot update Position. Position is still waiting for opening. There is {orders.Count()} open orders.");
}
}
else
{

Binary file not shown.

Binary file not shown.