Fix backtest
This commit is contained in:
@@ -27,14 +27,14 @@ public class ClosePositionCommandHandler(
|
||||
return request.Position;
|
||||
}
|
||||
|
||||
var isForPaperTrading = request.Position.Initiator == PositionInitiator.PaperTrading;
|
||||
var isForPaperTrading = request.IsForBacktest;
|
||||
|
||||
var lastPrice = request.Position.Initiator == PositionInitiator.PaperTrading
|
||||
? request.ExecutionPrice.GetValueOrDefault()
|
||||
: exchangeService.GetPrice(account, request.Position.Ticker, DateTime.UtcNow);
|
||||
|
||||
// Check if position still open
|
||||
if (!isForPaperTrading)
|
||||
if (!request.IsForBacktest)
|
||||
{
|
||||
var p = (await exchangeService.GetBrokerPositions(account))
|
||||
.FirstOrDefault(x => x.Ticker == request.Position.Ticker);
|
||||
@@ -65,17 +65,19 @@ public class ClosePositionCommandHandler(
|
||||
request.Position.ProfitAndLoss =
|
||||
TradingBox.GetProfitAndLoss(request.Position, closedPosition.Quantity, lastPrice,
|
||||
request.Position.Open.Leverage);
|
||||
tradingService.UpdatePosition(request.Position);
|
||||
|
||||
if (!request.IsForBacktest)
|
||||
tradingService.UpdatePosition(request.Position);
|
||||
}
|
||||
|
||||
return request.Position;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log the error - regardless of the error type
|
||||
logger?.LogError(ex, "Error closing position: {Message}", ex.Message);
|
||||
logger?.LogError(ex, "Error closing position: {Message} \n Stacktrace : {StackTrace}", ex.Message,
|
||||
ex.StackTrace);
|
||||
|
||||
throw new Exception(ex.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,15 @@ namespace Managing.Application.Trading.Commands
|
||||
{
|
||||
public class ClosePositionCommand : IRequest<Position>
|
||||
{
|
||||
public ClosePositionCommand(Position position, decimal? executionPrice = null)
|
||||
public ClosePositionCommand(Position position, decimal? executionPrice = null, bool isForBacktest = false)
|
||||
{
|
||||
Position = position;
|
||||
ExecutionPrice = executionPrice;
|
||||
IsForBacktest = isForBacktest;
|
||||
}
|
||||
|
||||
public Position Position { get; }
|
||||
public Position Position { get; }
|
||||
public decimal? ExecutionPrice { get; set; }
|
||||
public bool IsForBacktest { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user