Fix close position

This commit is contained in:
2025-12-11 12:23:01 +07:00
parent 8ff9437400
commit 292a48d108
3 changed files with 21 additions and 22 deletions

View File

@@ -1,4 +1,3 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Services;
using Managing.Application.Trading.Commands;
@@ -18,7 +17,7 @@ using static Managing.Common.Enums;
namespace Managing.Application.Bots;
public class SpotBot : TradingBotBase, ITradingBot
public class SpotBot : TradingBotBase
{
public SpotBot(
ILogger<TradingBotBase> logger,
@@ -288,16 +287,9 @@ public class SpotBot : TradingBotBase, ITradingBot
}
// Calculate and update PnL based on current price
var currentPrice = LastCandle?.Close ?? 0;
if (currentPrice == 0)
{
currentPrice = await ServiceScopeHelpers.WithScopedService<IExchangeService, decimal>(
_scopeFactory,
async exchangeService =>
{
return await exchangeService.GetCurrentPrice(Account, Config.Ticker);
});
}
var currentPrice = await ServiceScopeHelpers.WithScopedService<IExchangeService, decimal>(
_scopeFactory,
async exchangeService => { return await exchangeService.GetCurrentPrice(Account, Config.Ticker); });
if (currentPrice > 0)
{
@@ -728,14 +720,11 @@ public class SpotBot : TradingBotBase, ITradingBot
var command = new CloseSpotPositionCommand(position, position.AccountId, lastPrice);
try
{
Position closedPosition = null;
await ServiceScopeHelpers.WithScopedServices<IExchangeService, IAccountService, ITradingService>(
_scopeFactory, async (exchangeService, accountService, tradingService) =>
{
closedPosition =
Position closedPosition = await ServiceScopeHelpers
.WithScopedServices<IExchangeService, IAccountService, ITradingService, Position>(
_scopeFactory, async (exchangeService, accountService, tradingService) =>
await new CloseSpotPositionCommandHandler(exchangeService, accountService, tradingService)
.Handle(command);
});
.Handle(command));
if (closedPosition.Status == PositionStatus.Finished || closedPosition.Status == PositionStatus.Flipped)
{