Add volumes and fix missing signal

This commit is contained in:
2025-06-01 19:04:34 +07:00
parent 8f8bdf8d69
commit ea4458e21a
2 changed files with 110 additions and 2 deletions

View File

@@ -287,6 +287,53 @@ public class TradingBot : Bot, ITradingBot
}
}
private async Task<Signal> RecreateSignalFromPosition(Position position)
{
try
{
// Get the candle that corresponds to the position opening time
var positionCandle = OptimizedCandles.FirstOrDefault(c => c.Date <= position.Open.Date)
?? OptimizedCandles.LastOrDefault();
if (positionCandle == null)
{
await LogWarning($"Cannot find candle for position {position.Identifier} opened at {position.Open.Date}");
return null;
}
// Create a new signal based on position information
var recreatedSignal = new Signal(
ticker: Config.Ticker,
direction: position.OriginDirection,
confidence: Confidence.Medium, // Default confidence for recreated signals
candle: positionCandle,
date: position.Open.Date,
exchange: Account.Exchange,
strategyType: StrategyType.Stc, // Use a valid strategy type for recreated signals
signalType: SignalType.Signal
);
// Since Signal identifier is auto-generated, we need to update our position
// to use the new signal identifier, or find another approach
// For now, let's update the position's SignalIdentifier to match the recreated signal
position.SignalIdentifier = recreatedSignal.Identifier;
recreatedSignal.Status = SignalStatus.PositionOpen;
recreatedSignal.User = Account.User;
// Add the recreated signal to our collection
Signals.Add(recreatedSignal);
await LogInformation($"Successfully recreated signal {recreatedSignal.Identifier} for position {position.Identifier}");
return recreatedSignal;
}
catch (Exception ex)
{
await LogWarning($"Error recreating signal for position {position.Identifier}: {ex.Message}");
return null;
}
}
private async Task ManagePositions()
{
// Update positions - iterate through positions instead of signals for better synchronization
@@ -295,8 +342,16 @@ public class TradingBot : Bot, ITradingBot
var signalForPosition = Signals.FirstOrDefault(s => s.Identifier == position.SignalIdentifier);
if (signalForPosition == null)
{
await LogWarning($"Cannot find signal for position {position.Identifier}");
continue;
await LogInformation($"Signal not found for position {position.Identifier}. Recreating signal...");
// Recreate the signal based on position information
signalForPosition = await RecreateSignalFromPosition(position);
if (signalForPosition == null)
{
await LogWarning($"Failed to recreate signal for position {position.Identifier}");
continue;
}
}
// Ensure signal status is correctly set to PositionOpen if position is not finished