Fix volume over time
This commit is contained in:
@@ -621,11 +621,6 @@ public class BacktestController : BaseController
|
|||||||
// Calculate total number of backtests
|
// Calculate total number of backtests
|
||||||
var totalBacktests = request.DateTimeRanges.Count * request.MoneyManagementVariants.Count * request.TickerVariants.Count;
|
var totalBacktests = request.DateTimeRanges.Count * request.MoneyManagementVariants.Count * request.TickerVariants.Count;
|
||||||
|
|
||||||
if (totalBacktests > 100)
|
|
||||||
{
|
|
||||||
return BadRequest("Maximum of 100 backtests allowed per bundle request");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await GetUser();
|
var user = await GetUser();
|
||||||
|
|||||||
@@ -122,8 +122,21 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
|
|
||||||
var positions = await _tradingService.GetAllDatabasePositionsAsync();
|
var positions = await _tradingService.GetAllDatabasePositionsAsync();
|
||||||
|
|
||||||
|
// Get the last daily snapshot date to calculate cumulative volume
|
||||||
|
var lastSnapshotDate = _state.State.DailySnapshots.Any()
|
||||||
|
? _state.State.DailySnapshots.Max(s => s.Date)
|
||||||
|
: DateTime.MinValue;
|
||||||
|
|
||||||
|
// Start with the cumulative volume from the last snapshot
|
||||||
|
var cumulativeVolume = _state.State.DailySnapshots.Any()
|
||||||
|
? _state.State.DailySnapshots.OrderByDescending(s => s.Date).First().TotalVolume
|
||||||
|
: 0m;
|
||||||
|
|
||||||
|
_logger.LogInformation("Calculating cumulative volume from last snapshot date: {LastSnapshotDate}, Base volume: {CumulativeVolume}",
|
||||||
|
lastSnapshotDate, cumulativeVolume);
|
||||||
|
|
||||||
// Calculate all metrics from positions in a single loop
|
// Calculate all metrics from positions in a single loop
|
||||||
var totalVolume = 0m;
|
var newVolume = 0m; // Volume from positions after last snapshot
|
||||||
var totalFees = 0m;
|
var totalFees = 0m;
|
||||||
var totalPnL = 0m;
|
var totalPnL = 0m;
|
||||||
var totalOpenInterest = 0m;
|
var totalOpenInterest = 0m;
|
||||||
@@ -140,16 +153,17 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
|
|
||||||
// Calculate volume using the dedicated method
|
// Calculate volume using the dedicated method
|
||||||
var positionVolume = TradingHelpers.GetVolumeForPosition(position);
|
var positionVolume = TradingHelpers.GetVolumeForPosition(position);
|
||||||
totalVolume += positionVolume;
|
|
||||||
|
|
||||||
// Calculate fees and PnL for all positions
|
// For cumulative volume: only add volume from positions created AFTER last snapshot
|
||||||
totalFees += position.CalculateTotalFees();
|
// For volume breakdown: include all positions
|
||||||
totalPnL += position.ProfitAndLoss?.Realized ?? 0;
|
if (position.Date.Date > lastSnapshotDate)
|
||||||
|
{
|
||||||
|
newVolume += positionVolume;
|
||||||
|
_logger.LogDebug("Position {PositionId} created after last snapshot ({PositionDate} > {LastSnapshotDate}), adding volume: {Volume}",
|
||||||
|
position.Identifier, position.Date.Date, lastSnapshotDate, positionVolume);
|
||||||
|
}
|
||||||
|
|
||||||
// Count all positions
|
// Calculate breakdown metrics from ALL positions (for current state)
|
||||||
totalPositionCount++;
|
|
||||||
|
|
||||||
// Calculate breakdown metrics and update state directly
|
|
||||||
var ticker = position.Ticker;
|
var ticker = position.Ticker;
|
||||||
var direction = position.OriginDirection;
|
var direction = position.OriginDirection;
|
||||||
|
|
||||||
@@ -158,7 +172,6 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
{
|
{
|
||||||
_state.State.VolumeByAsset[ticker] = 0;
|
_state.State.VolumeByAsset[ticker] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state.State.VolumeByAsset[ticker] += positionVolume;
|
_state.State.VolumeByAsset[ticker] += positionVolume;
|
||||||
|
|
||||||
// Position count breakdown by asset - update state directly
|
// Position count breakdown by asset - update state directly
|
||||||
@@ -166,10 +179,16 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
{
|
{
|
||||||
_state.State.PositionCountByAsset[ticker] = 0;
|
_state.State.PositionCountByAsset[ticker] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state.State.PositionCountByAsset[ticker]++;
|
_state.State.PositionCountByAsset[ticker]++;
|
||||||
|
|
||||||
// Position count breakdown by direction - only count finished positions
|
// Calculate fees and PnL for all positions
|
||||||
|
totalFees += position.CalculateTotalFees();
|
||||||
|
totalPnL += position.ProfitAndLoss?.Realized ?? 0;
|
||||||
|
|
||||||
|
// Count all positions
|
||||||
|
totalPositionCount++;
|
||||||
|
|
||||||
|
// Position count breakdown by direction - only count open positions
|
||||||
if (position.IsOpen())
|
if (position.IsOpen())
|
||||||
{
|
{
|
||||||
var openingVolume = position.Open.Price * position.Open.Quantity * position.Open.Leverage;
|
var openingVolume = position.Open.Price * position.Open.Quantity * position.Open.Leverage;
|
||||||
@@ -184,7 +203,21 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_state.State.TotalPlatformVolume = totalVolume;
|
// CUMULATIVE volume: baseline + new volume since last snapshot
|
||||||
|
var updatedCumulativeVolume = cumulativeVolume + newVolume;
|
||||||
|
|
||||||
|
_logger.LogInformation("Volume calculation: Base={BaseVolume}, New={NewVolume}, Total={TotalVolume}",
|
||||||
|
cumulativeVolume, newVolume, updatedCumulativeVolume);
|
||||||
|
|
||||||
|
// Ensure volume never decreases
|
||||||
|
if (updatedCumulativeVolume < _state.State.TotalPlatformVolume)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Calculated cumulative volume ({Calculated}) is less than current volume ({Current}). Keeping current value.",
|
||||||
|
updatedCumulativeVolume, _state.State.TotalPlatformVolume);
|
||||||
|
updatedCumulativeVolume = _state.State.TotalPlatformVolume;
|
||||||
|
}
|
||||||
|
|
||||||
|
_state.State.TotalPlatformVolume = updatedCumulativeVolume;
|
||||||
_state.State.TotalPlatformFees = totalFees;
|
_state.State.TotalPlatformFees = totalFees;
|
||||||
_state.State.TotalPlatformPnL = totalPnL;
|
_state.State.TotalPlatformPnL = totalPnL;
|
||||||
_state.State.NetPnL = totalPnL - totalFees; // Calculate NetPnL
|
_state.State.NetPnL = totalPnL - totalFees; // Calculate NetPnL
|
||||||
@@ -202,7 +235,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
await RefreshAgentCountAsync();
|
await RefreshAgentCountAsync();
|
||||||
await _state.WriteStateAsync();
|
await _state.WriteStateAsync();
|
||||||
|
|
||||||
_logger.LogInformation("Platform summary data refreshed successfully");
|
_logger.LogInformation("Platform summary data refreshed successfully - Cumulative volume: {Volume}", updatedCumulativeVolume);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user