Update composite And update scenario

This commit is contained in:
2025-06-02 21:57:34 +07:00
parent 7fce1fa59e
commit 71bcaea76d
7 changed files with 244 additions and 80 deletions

View File

@@ -581,12 +581,28 @@ public class TradingBot : Bot, ITradingBot
// ==> Flip the position
if (Config.FlipPosition)
{
await LogInformation("Try to flip the position because of an opposite direction signal");
await CloseTrade(previousSignal, openedPosition, openedPosition.Open, lastPrice, true);
await SetPositionStatus(previousSignal.Identifier, PositionStatus.Flipped);
await OpenPosition(signal);
await LogInformation(
$"Position {previousSignal.Identifier} flipped by {signal.Identifier} at {lastPrice}$");
// Check if current position is in profit before flipping
var isPositionInProfit = await IsPositionInProfit(openedPosition, lastPrice);
if (isPositionInProfit)
{
await LogInformation("Try to flip the position because of an opposite direction signal and current position is in profit");
await CloseTrade(previousSignal, openedPosition, openedPosition.Open, lastPrice, true);
await SetPositionStatus(previousSignal.Identifier, PositionStatus.Flipped);
await OpenPosition(signal);
await LogInformation(
$"Position {previousSignal.Identifier} flipped by {signal.Identifier} at {lastPrice}$");
}
else
{
await LogInformation(
$"Position {previousSignal.Identifier} is not in profit (entry: {openedPosition.Open.Price}, current: {lastPrice}). " +
$"Signal {signal.Identifier} will wait for position to become profitable before flipping.");
// Keep signal in waiting status to check again on next execution
SetSignalStatus(signal.Identifier, SignalStatus.WaitingForPosition);
return;
}
}
else
{
@@ -1167,6 +1183,28 @@ public class TradingBot : Bot, ITradingBot
Logger.LogInformation($"Manually opened position {position.Identifier} for signal {signal.Identifier}");
return position;
}
/// <summary>
/// Checks if a position is currently in profit based on current market price
/// </summary>
/// <param name="position">The position to check</param>
/// <param name="currentPrice">The current market price</param>
/// <returns>True if position is in profit, false otherwise</returns>
private async Task<bool> IsPositionInProfit(Position position, decimal currentPrice)
{
if (position.OriginDirection == TradeDirection.Long)
{
return currentPrice >= position.Open.Price;
}
else if (position.OriginDirection == TradeDirection.Short)
{
return currentPrice <= position.Open.Price;
}
else
{
throw new ArgumentException("Invalid position direction");
}
}
}
public class TradingBotBackup