Update closing trade date on SL or TP
This commit is contained in:
@@ -443,20 +443,21 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates a daily snapshot from positions for a specific date
|
||||
/// Calculates a CUMULATIVE daily snapshot from positions up to a specific date
|
||||
/// </summary>
|
||||
/// <param name="positions">All positions to analyze</param>
|
||||
/// <param name="targetDate">The date to calculate the snapshot for</param>
|
||||
/// <returns>A daily snapshot for the specified date</returns>
|
||||
/// <param name="targetDate">The date to calculate the snapshot up to</param>
|
||||
/// <returns>A cumulative daily snapshot for the specified date</returns>
|
||||
private async Task<DailySnapshot> CalculateDailySnapshotFromPositionsAsync(List<Position> positions, DateTime targetDate)
|
||||
{
|
||||
var dayStart = targetDate;
|
||||
var dayEnd = targetDate.AddDays(1);
|
||||
|
||||
// For daily snapshots, we need to consider ALL positions to calculate:
|
||||
// 1. Volume from trades that occurred on this specific day
|
||||
// For CUMULATIVE daily snapshots, we need to consider ALL positions to calculate:
|
||||
// 1. TOTAL volume from all trades that occurred on or before this day
|
||||
// 2. Open interest from positions that were active during this day
|
||||
// So we'll process all positions and filter the relevant data
|
||||
// 3. Cumulative position count up to this date
|
||||
// So we'll process all positions and include relevant data cumulatively
|
||||
|
||||
// Calculate metrics for this specific day
|
||||
var totalVolume = 0m;
|
||||
@@ -499,84 +500,60 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
// Calculate volume for trades that occurred on this specific day
|
||||
var dayVolume = 0m;
|
||||
|
||||
// Calculate CUMULATIVE volume up to this point in time
|
||||
// Include all positions that were opened on or before the target date
|
||||
_logger.LogDebug("Checking position {PositionId}: Position.Date={PositionDate}, TargetDate={TargetDate}, Position.Date.Date={PositionDateOnly}",
|
||||
position.Identifier, position.Date, targetDate, position.Date.Date);
|
||||
|
||||
// Add opening volume if position was opened on this day
|
||||
// Add opening volume if position was opened on or before this day
|
||||
// Use more flexible date comparison to handle timezone differences
|
||||
if (position.Date.Date == targetDate ||
|
||||
(position.Date >= targetDate && position.Date < targetDate.AddDays(1)))
|
||||
if (position.Date.Date <= targetDate)
|
||||
{
|
||||
var openingVolume = position.Open.Price * position.Open.Quantity * position.Open.Leverage;
|
||||
dayVolume += openingVolume;
|
||||
_logger.LogDebug("Position {PositionId} opened on {TargetDate}: Opening volume = {OpeningVolume}",
|
||||
totalVolume += openingVolume;
|
||||
_logger.LogDebug("Position {PositionId} opened on/before {TargetDate}: Opening volume = {OpeningVolume}",
|
||||
position.Identifier, targetDate, openingVolume);
|
||||
}
|
||||
|
||||
// Add closing volume if position was closed on this day
|
||||
// Add closing volume if position was closed on or before this day
|
||||
if (position.IsFinished())
|
||||
{
|
||||
if (position.StopLoss.Status == TradeStatus.Filled &&
|
||||
(position.StopLoss.Date.Date == targetDate ||
|
||||
(position.StopLoss.Date >= targetDate && position.StopLoss.Date < targetDate.AddDays(1))))
|
||||
if (position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date <= targetDate)
|
||||
{
|
||||
var closingVolume = position.StopLoss.Price * position.StopLoss.Quantity * position.StopLoss.Leverage;
|
||||
dayVolume += closingVolume;
|
||||
_logger.LogDebug("Position {PositionId} closed on {TargetDate} via StopLoss: Closing volume = {ClosingVolume}",
|
||||
totalVolume += closingVolume;
|
||||
_logger.LogDebug("Position {PositionId} closed on/before {TargetDate} via StopLoss: Closing volume = {ClosingVolume}",
|
||||
position.Identifier, targetDate, closingVolume);
|
||||
}
|
||||
if (position.TakeProfit1.Status == TradeStatus.Filled &&
|
||||
(position.TakeProfit1.Date.Date == targetDate ||
|
||||
(position.TakeProfit1.Date >= targetDate && position.TakeProfit1.Date < targetDate.AddDays(1))))
|
||||
if (position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date.Date <= targetDate)
|
||||
{
|
||||
var closingVolume = position.TakeProfit1.Price * position.TakeProfit1.Quantity * position.TakeProfit1.Leverage;
|
||||
dayVolume += closingVolume;
|
||||
_logger.LogDebug("Position {PositionId} closed on {TargetDate} via TakeProfit1: Closing volume = {ClosingVolume}",
|
||||
totalVolume += closingVolume;
|
||||
_logger.LogDebug("Position {PositionId} closed on/before {TargetDate} via TakeProfit1: Closing volume = {ClosingVolume}",
|
||||
position.Identifier, targetDate, closingVolume);
|
||||
}
|
||||
if (position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled &&
|
||||
(position.TakeProfit2.Date.Date == targetDate ||
|
||||
(position.TakeProfit2.Date >= targetDate && position.TakeProfit2.Date < targetDate.AddDays(1))))
|
||||
if (position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled && position.TakeProfit2.Date.Date <= targetDate)
|
||||
{
|
||||
var closingVolume = position.TakeProfit2.Price * position.TakeProfit2.Quantity * position.TakeProfit2.Leverage;
|
||||
dayVolume += closingVolume;
|
||||
_logger.LogDebug("Position {PositionId} closed on {TargetDate} via TakeProfit2: Closing volume = {ClosingVolume}",
|
||||
position.Identifier, targetDate, closingVolume);
|
||||
totalVolume += closingVolume;
|
||||
}
|
||||
}
|
||||
|
||||
if (dayVolume > 0)
|
||||
{
|
||||
_logger.LogDebug("Position {PositionId} contributed {DayVolume} to {TargetDate} total volume",
|
||||
position.Identifier, dayVolume, targetDate);
|
||||
}
|
||||
|
||||
totalVolume += dayVolume;
|
||||
|
||||
// Calculate fees and PnL for positions closed on this day
|
||||
var wasClosedOnThisDay = position.IsFinished() && (
|
||||
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date == targetDate) ||
|
||||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date.Date == targetDate) ||
|
||||
(position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled && position.TakeProfit2.Date.Date == targetDate)
|
||||
// Calculate CUMULATIVE fees and PnL for positions closed on or before this day
|
||||
var wasClosedOnOrBeforeThisDay = position.IsFinished() && (
|
||||
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date <= targetDate) ||
|
||||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date.Date <= targetDate) ||
|
||||
(position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled && position.TakeProfit2.Date.Date <= targetDate)
|
||||
);
|
||||
|
||||
if (wasClosedOnThisDay)
|
||||
if (wasClosedOnOrBeforeThisDay)
|
||||
{
|
||||
totalFees += position.CalculateTotalFees();
|
||||
totalPnL += position.ProfitAndLoss?.Realized ?? 0;
|
||||
}
|
||||
|
||||
// Count positions that were active on this day (opened on or before, closed on or after)
|
||||
var wasActiveOnThisDay = position.Date.Date <= targetDate &&
|
||||
(!position.IsFinished() ||
|
||||
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date >= targetDate) ||
|
||||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date.Date >= targetDate) ||
|
||||
(position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled && position.TakeProfit2.Date.Date >= targetDate));
|
||||
|
||||
if (wasActiveOnThisDay)
|
||||
// Count positions that were created on or before this day (CUMULATIVE position count)
|
||||
if (position.Date.Date <= targetDate)
|
||||
{
|
||||
totalPositionCount++;
|
||||
}
|
||||
@@ -586,7 +563,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
var totalAgents = await _agentService.GetTotalAgentCount();
|
||||
var totalStrategies = _state.State.TotalActiveStrategies;
|
||||
|
||||
_logger.LogInformation("Calculated daily snapshot for {TargetDate}: TotalVolume={TotalVolume}, MaxOpenInterest={MaxOpenInterest}, TotalPositionCount={TotalPositionCount}",
|
||||
_logger.LogInformation("Calculated CUMULATIVE daily snapshot for {TargetDate}: CumVolume={TotalVolume}, MaxOpenInterest={MaxOpenInterest}, CumPositionCount={TotalPositionCount}",
|
||||
targetDate, totalVolume, maxOpenInterest, totalPositionCount);
|
||||
|
||||
return new DailySnapshot
|
||||
|
||||
Reference in New Issue
Block a user