Update account/position and platform summary

This commit is contained in:
2025-09-26 01:18:59 +07:00
parent b2e38811ed
commit bcfeb693ce
32 changed files with 3301 additions and 151 deletions

View File

@@ -5,14 +5,16 @@ namespace Managing.Application.Trading.Commands
{
public class ClosePositionCommand : IRequest<Position>
{
public ClosePositionCommand(Position position, decimal? executionPrice = null, bool isForBacktest = false)
public ClosePositionCommand(Position position, int accountId, decimal? executionPrice = null, bool isForBacktest = false)
{
Position = position;
AccountId = accountId;
ExecutionPrice = executionPrice;
IsForBacktest = isForBacktest;
}
public Position Position { get; }
public int AccountId { get; }
public decimal? ExecutionPrice { get; set; }
public bool IsForBacktest { get; set; }
}

View File

@@ -22,7 +22,7 @@ public class ClosePositionCommandHandler(
try
{
// Get Trade
var account = await accountService.GetAccount(request.Position.AccountName, false, false);
var account = await accountService.GetAccountById(request.AccountId, false, false);
if (request.Position == null)
{
_ = await exchangeService.CancelOrder(account, request.Position.Ticker);
@@ -48,6 +48,12 @@ public class ClosePositionCommandHandler(
request.Position.ProfitAndLoss =
TradingBox.GetProfitAndLoss(request.Position, request.Position.Open.Quantity, lastPrice,
request.Position.Open.Leverage);
// Add UI fees for closing the position (broker closed it)
var closingPositionSizeUsd = (lastPrice * request.Position.Open.Quantity) * request.Position.Open.Leverage;
var closingUiFees = TradingHelpers.CalculateClosingUiFees(closingPositionSizeUsd);
request.Position.AddUiFees(closingUiFees);
await tradingService.UpdatePositionAsync(request.Position);
return request.Position;
}
@@ -68,6 +74,11 @@ public class ClosePositionCommandHandler(
TradingBox.GetProfitAndLoss(request.Position, closedPosition.Quantity, lastPrice,
request.Position.Open.Leverage);
// Add UI fees for closing the position
var closingPositionSizeUsd = (lastPrice * closedPosition.Quantity) * request.Position.Open.Leverage;
var closingUiFees = TradingHelpers.CalculateClosingUiFees(closingPositionSizeUsd);
request.Position.AddUiFees(closingUiFees);
if (!request.IsForBacktest)
await tradingService.UpdatePositionAsync(request.Position);
}

View File

@@ -21,7 +21,7 @@ namespace Managing.Application.Trading.Handlers
var account = await accountService.GetAccount(request.AccountName, hideSecrets: false, getBalance: false);
var initiator = request.IsForPaperTrading ? PositionInitiator.PaperTrading : request.Initiator;
var position = new Position(Guid.NewGuid(), request.AccountName, request.Direction,
var position = new Position(Guid.NewGuid(), account.Id, request.Direction,
request.Ticker,
request.MoneyManagement,
initiator, request.Date, request.User);
@@ -45,9 +45,10 @@ namespace Managing.Application.Trading.Handlers
}
// Gas fee check for EVM exchanges
decimal gasFeeUsd = 0;
if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2)
{
var gasFeeUsd = await exchangeService.GetFee(account);
gasFeeUsd = await exchangeService.GetFee(account);
if (gasFeeUsd > Constants.GMX.Config.MaximumGasFeeUsd)
{
throw new InsufficientFundsException(
@@ -84,6 +85,22 @@ namespace Managing.Application.Trading.Handlers
position.Open = trade;
// Calculate and set fees for the position
var positionSizeUsd = (position.Open.Price * position.Open.Quantity) * position.Open.Leverage;
// Set gas fees (only for EVM exchanges)
if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2)
{
position.GasFees = gasFeeUsd;
}
else
{
position.GasFees = TradingHelpers.CalculateOpeningGasFees();
}
// Set UI fees for opening
position.UiFees = TradingHelpers.CalculateOpeningUiFees(positionSizeUsd);
var closeDirection = request.Direction == TradeDirection.Long
? TradeDirection.Short
: TradeDirection.Long;