Update cooldown
This commit is contained in:
@@ -859,23 +859,67 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var positionSignal = Signals.FirstOrDefault(s => s.Identifier == lastPosition.SignalIdentifier);
|
// Get the actual closing date of the position instead of signal date
|
||||||
if (positionSignal == null)
|
var positionClosingDate = GetPositionClosingDate(lastPosition);
|
||||||
|
if (positionClosingDate == null)
|
||||||
{
|
{
|
||||||
await LogWarning($"Cannot find signal for last position {lastPosition.Identifier}");
|
await LogWarning($"Cannot determine closing date for last position {lastPosition.Identifier}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var canOpenPosition = positionSignal.Date < cooldownCandle.Date;
|
var canOpenPosition = positionClosingDate < cooldownCandle.Date;
|
||||||
if (!canOpenPosition)
|
if (!canOpenPosition)
|
||||||
{
|
{
|
||||||
await LogInformation(
|
await LogInformation(
|
||||||
$"⏳ **Cooldown Active**\nPosition blocked by cooldown period\n📅 Last Position: `{positionSignal.Date:MM/dd HH:mm}`\n🕒 Cooldown Until: `{cooldownCandle.Date:MM/dd HH:mm}`");
|
$"⏳ **Cooldown Active**\nPosition blocked by cooldown period\n📅 Last Position Closed: `{positionClosingDate:MM/dd HH:mm}`\n🕒 Cooldown Until: `{cooldownCandle.Date:MM/dd HH:mm}`");
|
||||||
}
|
}
|
||||||
|
|
||||||
return canOpenPosition;
|
return canOpenPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the actual closing date of a position by checking which trade (Stop Loss or Take Profit) was executed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">The finished position</param>
|
||||||
|
/// <returns>The date when the position was closed, or null if cannot be determined</returns>
|
||||||
|
private DateTime? GetPositionClosingDate(Position position)
|
||||||
|
{
|
||||||
|
if (!position.IsFinished())
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check which trade actually closed the position
|
||||||
|
if (position.StopLoss?.Status == TradeStatus.Filled && position.StopLoss.Date != default)
|
||||||
|
{
|
||||||
|
return position.StopLoss.Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position.TakeProfit1?.Status == TradeStatus.Filled && position.TakeProfit1.Date != default)
|
||||||
|
{
|
||||||
|
return position.TakeProfit1.Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position.TakeProfit2?.Status == TradeStatus.Filled && position.TakeProfit2.Date != default)
|
||||||
|
{
|
||||||
|
return position.TakeProfit2.Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: if we can't determine the exact closing trade, use the latest date available
|
||||||
|
var availableDates = new List<DateTime>();
|
||||||
|
|
||||||
|
if (position.StopLoss?.Date != default)
|
||||||
|
availableDates.Add(position.StopLoss.Date);
|
||||||
|
|
||||||
|
if (position.TakeProfit1?.Date != default)
|
||||||
|
availableDates.Add(position.TakeProfit1.Date);
|
||||||
|
|
||||||
|
if (position.TakeProfit2?.Date != default)
|
||||||
|
availableDates.Add(position.TakeProfit2.Date);
|
||||||
|
|
||||||
|
return availableDates.Any() ? availableDates.Max() : position.Open.Date;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task CloseTrade(Signal signal, Position position, Trade tradeToClose, decimal lastPrice,
|
public async Task CloseTrade(Signal signal, Position position, Trade tradeToClose, decimal lastPrice,
|
||||||
bool tradeClosingPosition = false)
|
bool tradeClosingPosition = false)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user