Open position for bots

This commit is contained in:
2025-04-23 10:02:04 +02:00
parent 8d191e8d14
commit ad5996ca61
10 changed files with 410 additions and 208 deletions

View File

@@ -744,6 +744,7 @@ public class TradingBot : Bot, ITradingBot
private async Task LogWarning(string message)
{
Logger.LogWarning(message);
SentrySdk.CaptureException(new Exception(message));
await SendTradeMessage(message, true);
}
@@ -787,6 +788,50 @@ public class TradingBot : Bot, ITradingBot
AccountName = data.AccountName;
IsForWatchingOnly = data.IsForWatchingOnly;
}
/// <summary>
/// Manually opens a position using the bot's settings and a generated signal.
/// Relies on the bot's MoneyManagement for Stop Loss and Take Profit placement.
/// </summary>
/// <param name="direction">The direction of the trade (Long/Short).</param>
/// <returns>The created Position object.</returns>
/// <exception cref="Exception">Throws if no candles are available or position opening fails.</exception>
public async Task<Position> OpenPositionManually(TradeDirection direction)
{
var lastCandle = OptimizedCandles.LastOrDefault();
if (lastCandle == null)
{
throw new Exception("No candles available to open position");
}
// Create a fake signal for manual position opening
var signal = new Signal(Ticker, direction, Confidence.Low, lastCandle, lastCandle.Date, TradingExchanges.GmxV2,
StrategyType.Stc, SignalType.Signal);
signal.Status = SignalStatus.WaitingForPosition; // Ensure status is correct
signal.User = Account.User; // Assign user
// Add the signal to our collection
await AddSignal(signal);
// Open the position using the generated signal (SL/TP handled by MoneyManagement)
await OpenPosition(signal);
// Get the opened position
var position = Positions.FirstOrDefault(p => p.SignalIdentifier == signal.Identifier);
if (position == null)
{
// Clean up the signal if position creation failed
SetSignalStatus(signal.Identifier, SignalStatus.Expired);
throw new Exception("Failed to open position");
}
// Removed manual setting of SL/TP, as MoneyManagement should handle it
// position.StopLoss.Price = stopLossPrice;
// position.TakeProfit1.Price = takeProfitPrice;
Logger.LogInformation($"Manually opened position {position.Identifier} for signal {signal.Identifier}");
return position;
}
}
public class TradingBotBackup