Fix backtest consistency
This commit is contained in:
@@ -230,9 +230,6 @@ public class BacktestExecutor
|
||||
var fixedCandlesHashSet = new HashSet<Candle>(rollingWindowSize); // Reuse HashSet to avoid allocations
|
||||
var candlesProcessed = 0;
|
||||
|
||||
// Pre-allocate reusable collections to minimize allocations during processing
|
||||
var tempCandlesList = new List<Candle>(rollingWindowSize);
|
||||
|
||||
// Signal caching optimization - reduce signal update frequency for better performance
|
||||
var signalUpdateSkipCount = 0;
|
||||
|
||||
@@ -256,39 +253,26 @@ public class BacktestExecutor
|
||||
// Process all candles with optimized rolling window approach
|
||||
_logger.LogInformation("🎯 Starting to process {Count} candles in loop", orderedCandles.Count);
|
||||
Console.WriteLine("CONSOLE: About to start candle processing loop");
|
||||
|
||||
// Optimize: Pre-populate rolling window with initial candles to avoid repeated checks
|
||||
var initialWindowSize = Math.Min(rollingWindowSize, orderedCandles.Count);
|
||||
for (int i = 0; i < initialWindowSize; i++)
|
||||
{
|
||||
var candle = orderedCandles[i];
|
||||
rollingCandles.Add(candle);
|
||||
fixedCandlesHashSet.Add(candle);
|
||||
}
|
||||
|
||||
foreach (var candle in orderedCandles)
|
||||
{
|
||||
// Optimized rolling window maintenance - only modify when window is full
|
||||
if (rollingCandles.Count >= rollingWindowSize)
|
||||
// Maintain rolling window efficiently using List
|
||||
rollingCandles.Add(candle);
|
||||
|
||||
if (rollingCandles.Count > rollingWindowSize)
|
||||
{
|
||||
// Remove oldest candle from both structures efficiently
|
||||
// Remove oldest candle from both structures
|
||||
var removedCandle = rollingCandles[0];
|
||||
rollingCandles.RemoveAt(0);
|
||||
fixedCandlesHashSet.Remove(removedCandle);
|
||||
}
|
||||
|
||||
// Add new candle to rolling window (skip if already in initial population)
|
||||
if (!fixedCandlesHashSet.Contains(candle))
|
||||
{
|
||||
rollingCandles.Add(candle);
|
||||
fixedCandlesHashSet.Add(candle);
|
||||
}
|
||||
|
||||
// Add to HashSet for reuse
|
||||
fixedCandlesHashSet.Add(candle);
|
||||
tradingBot.LastCandle = candle;
|
||||
|
||||
// Smart signal caching - reduce signal update frequency for performance
|
||||
// RSI and similar indicators don't need updates every candle for 15-minute data
|
||||
var shouldSkipSignalUpdate = ShouldSkipSignalUpdate(currentCandle, totalCandles, config);
|
||||
var shouldSkipSignalUpdate = ShouldSkipSignalUpdate(currentCandle, totalCandles);
|
||||
if (currentCandle <= 5) // Debug first few candles
|
||||
{
|
||||
_logger.LogInformation("🔍 Candle {CurrentCandle}: shouldSkip={ShouldSkip}, totalCandles={Total}",
|
||||
@@ -549,70 +533,24 @@ public class BacktestExecutor
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Advanced signal caching based on indicator update frequency and timeframe
|
||||
/// Dynamically adjusts update frequency based on timeframe and indicator characteristics
|
||||
/// Advanced signal caching based on indicator update frequency
|
||||
/// Instead of hashing candles, we cache signals based on how often indicators need updates
|
||||
/// </summary>
|
||||
private bool ShouldSkipSignalUpdate(int currentCandleIndex, int totalCandles, TradingBotConfig config)
|
||||
private bool ShouldSkipSignalUpdate(int currentCandleIndex, int totalCandles)
|
||||
{
|
||||
// RSI and similar indicators don't need to be recalculated every candle
|
||||
// For 15-minute candles, we can update signals every 3-5 candles without significant accuracy loss
|
||||
const int signalUpdateFrequency = 3; // Update signals every N candles
|
||||
|
||||
// Always update signals for the first few candles to establish baseline
|
||||
if (currentCandleIndex < 20)
|
||||
if (currentCandleIndex < 10)
|
||||
return false;
|
||||
|
||||
// Always update signals near the end to ensure final trades are calculated
|
||||
if (currentCandleIndex > totalCandles - 20)
|
||||
if (currentCandleIndex > totalCandles - 10)
|
||||
return false;
|
||||
|
||||
// Adaptive update frequency based on timeframe
|
||||
// Shorter timeframes can skip more updates as they're more volatile
|
||||
int signalUpdateFrequency;
|
||||
switch (config.Timeframe)
|
||||
{
|
||||
case Timeframe.OneMinute:
|
||||
case Timeframe.FiveMinutes:
|
||||
signalUpdateFrequency = 10; // Update every 10 candles for fast timeframes
|
||||
break;
|
||||
case Timeframe.FifteenMinutes:
|
||||
case Timeframe.ThirtyMinutes:
|
||||
signalUpdateFrequency = 5; // Update every 5 candles for medium timeframes
|
||||
break;
|
||||
case Timeframe.OneHour:
|
||||
case Timeframe.FourHour:
|
||||
signalUpdateFrequency = 3; // Update every 3 candles for slower timeframes
|
||||
break;
|
||||
case Timeframe.OneDay:
|
||||
signalUpdateFrequency = 1; // Update every candle for daily (already slow)
|
||||
break;
|
||||
default:
|
||||
signalUpdateFrequency = 5; // Default fallback
|
||||
break;
|
||||
}
|
||||
|
||||
// Further optimize based on indicator types in the scenario
|
||||
if (config.Scenario?.Indicators != null)
|
||||
{
|
||||
var hasFastIndicators = config.Scenario.Indicators.Any(ind =>
|
||||
ind.Type == IndicatorType.RsiDivergence ||
|
||||
ind.Type == IndicatorType.StochRsiTrend ||
|
||||
ind.Type == IndicatorType.MacdCross);
|
||||
|
||||
var hasSlowIndicators = config.Scenario.Indicators.Any(ind =>
|
||||
ind.Type == IndicatorType.EmaCross ||
|
||||
ind.Type == IndicatorType.EmaTrend ||
|
||||
ind.Type == IndicatorType.SuperTrend);
|
||||
|
||||
// If we have mostly slow indicators, we can update less frequently
|
||||
if (!hasFastIndicators && hasSlowIndicators)
|
||||
{
|
||||
signalUpdateFrequency = Math.Max(signalUpdateFrequency, 8);
|
||||
}
|
||||
// If we have fast indicators, we need more frequent updates
|
||||
else if (hasFastIndicators && !hasSlowIndicators)
|
||||
{
|
||||
signalUpdateFrequency = Math.Min(signalUpdateFrequency, 3);
|
||||
}
|
||||
}
|
||||
|
||||
// Skip signal updates based on calculated frequency
|
||||
// Skip signal updates based on frequency
|
||||
return (currentCandleIndex % signalUpdateFrequency) != 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,19 +51,6 @@ public class TradingBotBase : ITradingBot
|
||||
/// </summary>
|
||||
public Dictionary<IndicatorType, IndicatorsResultBase> PreCalculatedIndicatorValues { get; set; }
|
||||
|
||||
// Cached properties for performance optimization
|
||||
private bool? _isForBacktest;
|
||||
private bool? _isForWatchingOnly;
|
||||
private int? _maxLossStreak;
|
||||
private int? _cooldownPeriod;
|
||||
private bool? _flipPosition;
|
||||
|
||||
private bool IsForBacktest => _isForBacktest ??= Config.IsForBacktest;
|
||||
private bool IsForWatchingOnly => _isForWatchingOnly ??= Config.IsForWatchingOnly;
|
||||
private int MaxLossStreak => _maxLossStreak ??= Config.MaxLossStreak;
|
||||
private int CooldownPeriod => _cooldownPeriod ??= Config.CooldownPeriod;
|
||||
private bool FlipPosition => _flipPosition ??= Config.FlipPosition;
|
||||
|
||||
|
||||
public TradingBotBase(
|
||||
ILogger<TradingBotBase> logger,
|
||||
@@ -83,7 +70,7 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
public async Task Start(BotStatus previousStatus)
|
||||
{
|
||||
if (!IsForBacktest)
|
||||
if (!Config.IsForBacktest)
|
||||
{
|
||||
// Start async initialization in the background without blocking
|
||||
try
|
||||
@@ -107,8 +94,17 @@ public class TradingBotBase : ITradingBot
|
||||
switch (previousStatus)
|
||||
{
|
||||
case BotStatus.Saved:
|
||||
var indicatorNames = Config.Scenario.Indicators.Select(i => i.Type.ToString());
|
||||
var startupMessage = $"🚀 Bot Started Successfully\n\n📊 Trading Setup:\n🎯 Ticker: `{Config.Ticker}`\n⏰ Timeframe: `{Config.Timeframe}`\n🎮 Scenario: `{Config.Scenario?.Name ?? "Unknown"}`\n💰 Balance: `${Config.BotTradingBalance:F2}`\n👀 Mode: `{(Config.IsForWatchingOnly ? "Watch Only" : "Live Trading")}`\n\n📈 Active Indicators: `{string.Join(", ", indicatorNames)}`\n\n✅ Ready to monitor signals and execute trades\n📢 Notifications will be sent when positions are triggered";
|
||||
var indicatorNames = Config.Scenario.Indicators.Select(i => i.Type.ToString()).ToList();
|
||||
var startupMessage = $"🚀 Bot Started Successfully\n\n" +
|
||||
$"📊 Trading Setup:\n" +
|
||||
$"🎯 Ticker: `{Config.Ticker}`\n" +
|
||||
$"⏰ Timeframe: `{Config.Timeframe}`\n" +
|
||||
$"🎮 Scenario: `{Config.Scenario?.Name ?? "Unknown"}`\n" +
|
||||
$"💰 Balance: `${Config.BotTradingBalance:F2}`\n" +
|
||||
$"👀 Mode: `{(Config.IsForWatchingOnly ? "Watch Only" : "Live Trading")}`\n\n" +
|
||||
$"📈 Active Indicators: `{string.Join(", ", indicatorNames)}`\n\n" +
|
||||
$"✅ Ready to monitor signals and execute trades\n" +
|
||||
$"📢 Notifications will be sent when positions are triggered";
|
||||
|
||||
await LogInformation(startupMessage);
|
||||
break;
|
||||
@@ -176,7 +172,7 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
public async Task LoadAccount()
|
||||
{
|
||||
if (IsForBacktest) return;
|
||||
if (Config.IsForBacktest) return;
|
||||
await ServiceScopeHelpers.WithScopedService<IAccountService>(_scopeFactory, async accountService =>
|
||||
{
|
||||
var account = await accountService.GetAccountByAccountName(Config.AccountName, false, false);
|
||||
@@ -190,7 +186,7 @@ public class TradingBotBase : ITradingBot
|
||||
/// </summary>
|
||||
public async Task VerifyAndUpdateBalance()
|
||||
{
|
||||
if (IsForBacktest) return;
|
||||
if (Config.IsForBacktest) return;
|
||||
if (Account == null)
|
||||
{
|
||||
Logger.LogWarning("Cannot verify balance: Account is null");
|
||||
@@ -237,85 +233,40 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
// Fast path for backtests - skip live trading operations
|
||||
if (IsForBacktest)
|
||||
// Update signals for live trading only
|
||||
if (!Config.IsForBacktest)
|
||||
{
|
||||
if (!IsForWatchingOnly)
|
||||
await ManagePositions();
|
||||
|
||||
UpdateWalletBalances();
|
||||
return;
|
||||
await UpdateSignals();
|
||||
await LoadLastCandle();
|
||||
}
|
||||
|
||||
// Live trading path
|
||||
await UpdateSignals();
|
||||
await LoadLastCandle();
|
||||
|
||||
if (!IsForWatchingOnly)
|
||||
if (!Config.IsForWatchingOnly)
|
||||
await ManagePositions();
|
||||
|
||||
UpdateWalletBalances();
|
||||
|
||||
ExecutionCount++;
|
||||
|
||||
// Optimized logging - cache frequently used values
|
||||
var serverDate = DateTime.UtcNow;
|
||||
var lastCandleDate = LastCandle?.Date;
|
||||
var signalCount = Signals.Count;
|
||||
var positionCount = Positions.Count;
|
||||
|
||||
Logger.LogInformation(
|
||||
"Bot Status {Name} - ServerDate: {ServerDate}, LastCandleDate: {LastCandleDate}, Signals: {SignalCount}, Executions: {ExecutionCount}, Positions: {PositionCount}",
|
||||
Config.Name, serverDate, lastCandleDate, signalCount, ExecutionCount, positionCount);
|
||||
|
||||
// Optimize position logging - build string efficiently
|
||||
if (positionCount > 0)
|
||||
if (!Config.IsForBacktest)
|
||||
{
|
||||
var positionStrings = new string[positionCount];
|
||||
var index = 0;
|
||||
foreach (var position in Positions.Values)
|
||||
{
|
||||
positionStrings[index++] = $"{position.SignalIdentifier} - Status: {position.Status}";
|
||||
}
|
||||
ExecutionCount++;
|
||||
|
||||
Logger.LogInformation(
|
||||
"Bot Status {Name} - ServerDate: {ServerDate}, LastCandleDate: {LastCandleDate}, Signals: {SignalCount}, Executions: {ExecutionCount}, Positions: {PositionCount}",
|
||||
Config.Name, DateTime.UtcNow, LastCandle?.Date, Signals.Count, ExecutionCount, Positions.Count);
|
||||
|
||||
Logger.LogInformation("[{Name}] Internal Positions : {Position}", Config.Name,
|
||||
string.Join(", ", positionStrings));
|
||||
string.Join(", ",
|
||||
Positions.Values.Select(p => $"{p.SignalIdentifier} - Status: {p.Status}")));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateSignals(HashSet<Candle> candles = null)
|
||||
{
|
||||
// Fast path for backtests - skip live trading checks
|
||||
if (IsForBacktest && candles != null)
|
||||
{
|
||||
var backtestSignal =
|
||||
TradingBox.GetSignal(candles, Config.Scenario, Signals, Config.Scenario.LoopbackPeriod,
|
||||
PreCalculatedIndicatorValues);
|
||||
if (backtestSignal == null) return;
|
||||
await AddSignal(backtestSignal);
|
||||
return;
|
||||
}
|
||||
|
||||
// Live trading path with checks
|
||||
// Skip indicator checking if flipping is disabled and there's an open position
|
||||
// This prevents unnecessary indicator calculations when we can't act on signals anyway
|
||||
if (!FlipPosition)
|
||||
if (!Config.FlipPosition && Positions.Any(p => p.Value.IsOpen()))
|
||||
{
|
||||
var hasOpenPosition = false;
|
||||
foreach (var position in Positions.Values)
|
||||
{
|
||||
if (position.IsOpen())
|
||||
{
|
||||
hasOpenPosition = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasOpenPosition)
|
||||
{
|
||||
Logger.LogDebug(
|
||||
$"Skipping signal update: Position open and flip disabled. Open positions: {Positions.Count(p => p.Value.IsOpen())}");
|
||||
return;
|
||||
}
|
||||
Logger.LogDebug(
|
||||
$"Skipping signal update: Position open and flip disabled. Open positions: {Positions.Count(p => p.Value.IsOpen())}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if we're in cooldown period for any direction
|
||||
@@ -325,13 +276,24 @@ public class TradingBotBase : ITradingBot
|
||||
return;
|
||||
}
|
||||
|
||||
await ServiceScopeHelpers.WithScopedService<IGrainFactory>(_scopeFactory, async grainFactory =>
|
||||
if (Config.IsForBacktest && candles != null)
|
||||
{
|
||||
var scenarioRunnerGrain = grainFactory.GetGrain<IScenarioRunnerGrain>(Guid.NewGuid());
|
||||
var signal = await scenarioRunnerGrain.GetSignals(Config, Signals, Account.Exchange, LastCandle);
|
||||
if (signal == null) return;
|
||||
await AddSignal(signal);
|
||||
});
|
||||
var backtestSignal =
|
||||
TradingBox.GetSignal(candles, Config.Scenario, Signals, Config.Scenario.LoopbackPeriod,
|
||||
PreCalculatedIndicatorValues);
|
||||
if (backtestSignal == null) return;
|
||||
await AddSignal(backtestSignal);
|
||||
}
|
||||
else
|
||||
{
|
||||
await ServiceScopeHelpers.WithScopedService<IGrainFactory>(_scopeFactory, async grainFactory =>
|
||||
{
|
||||
var scenarioRunnerGrain = grainFactory.GetGrain<IScenarioRunnerGrain>(Guid.NewGuid());
|
||||
var signal = await scenarioRunnerGrain.GetSignals(Config, Signals, Account.Exchange, LastCandle);
|
||||
if (signal == null) return;
|
||||
await AddSignal(signal);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<LightSignal> RecreateSignalFromPosition(Position position)
|
||||
@@ -390,40 +352,17 @@ public class TradingBotBase : ITradingBot
|
||||
private async Task ManagePositions()
|
||||
{
|
||||
// Early exit optimization - skip if no positions to manage
|
||||
var hasOpenPositions = false;
|
||||
var hasWaitingSignals = false;
|
||||
|
||||
// Optimize: Use foreach instead of LINQ for better performance
|
||||
foreach (var position in Positions.Values)
|
||||
{
|
||||
if (!position.IsFinished())
|
||||
{
|
||||
hasOpenPositions = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasOpenPositions)
|
||||
{
|
||||
foreach (var signal in Signals.Values)
|
||||
{
|
||||
if (signal.Status == SignalStatus.WaitingForPosition)
|
||||
{
|
||||
hasWaitingSignals = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var hasOpenPositions = Positions.Values.Any(p => !p.IsFinished());
|
||||
var hasWaitingSignals = Signals.Values.Any(s => s.Status == SignalStatus.WaitingForPosition);
|
||||
|
||||
if (!hasOpenPositions && !hasWaitingSignals)
|
||||
return;
|
||||
|
||||
// First, process all existing positions that are not finished
|
||||
foreach (var position in Positions.Values)
|
||||
foreach (var position in Positions.Values.Where(p => !p.IsFinished()))
|
||||
{
|
||||
if (position.IsFinished()) continue;
|
||||
|
||||
if (!Signals.TryGetValue(position.SignalIdentifier, out var signalForPosition))
|
||||
var signalForPosition = Signals[position.SignalIdentifier];
|
||||
if (signalForPosition == null)
|
||||
{
|
||||
await LogInformation(
|
||||
$"🔍 Signal Recovery\nSignal not found for position `{position.Identifier}`\nRecreating signal from position data...");
|
||||
@@ -450,9 +389,11 @@ public class TradingBotBase : ITradingBot
|
||||
}
|
||||
|
||||
// Then, open positions for signals waiting for a position open
|
||||
foreach (var signal in Signals.Values)
|
||||
// But first, check if we already have a position for any of these signals
|
||||
var signalsWaitingForPosition = Signals.Values.Where(s => s.Status == SignalStatus.WaitingForPosition);
|
||||
|
||||
foreach (var signal in signalsWaitingForPosition)
|
||||
{
|
||||
if (signal.Status != SignalStatus.WaitingForPosition) continue;
|
||||
if (LastCandle != null && signal.Date < LastCandle.Date)
|
||||
{
|
||||
await LogWarning(
|
||||
@@ -491,33 +432,23 @@ public class TradingBotBase : ITradingBot
|
||||
return;
|
||||
}
|
||||
|
||||
// Optimize: Use TryGetValue instead of ContainsKey + First()
|
||||
if (!WalletBalances.TryGetValue(date, out _))
|
||||
if (!WalletBalances.ContainsKey(date))
|
||||
{
|
||||
// Cache the calculation to avoid repeated computation
|
||||
var profitAndLoss = GetProfitAndLoss();
|
||||
var previousBalance = WalletBalances.Count > 0 ? WalletBalances.First().Value : Config.BotTradingBalance;
|
||||
WalletBalances[date] = previousBalance + profitAndLoss;
|
||||
var previousBalance = WalletBalances.First().Value;
|
||||
WalletBalances[date] = previousBalance + GetProfitAndLoss();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdatePosition(LightSignal signal, Position positionForSignal)
|
||||
{
|
||||
// Skip processing if position is already canceled or rejected (never filled)
|
||||
if (positionForSignal.Status == PositionStatus.Canceled ||
|
||||
positionForSignal.Status == PositionStatus.Rejected)
|
||||
{
|
||||
await LogDebug(
|
||||
$"Skipping update for position {positionForSignal.Identifier} - status is {positionForSignal.Status} (never filled)");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Fast path for backtests - simplified position handling
|
||||
if (IsForBacktest)
|
||||
// Skip processing if position is already canceled or rejected (never filled)
|
||||
if (positionForSignal.Status == PositionStatus.Canceled ||
|
||||
positionForSignal.Status == PositionStatus.Rejected)
|
||||
{
|
||||
await UpdatePositionForBacktest(signal, positionForSignal);
|
||||
await LogDebug(
|
||||
$"Skipping update for position {positionForSignal.Identifier} - status is {positionForSignal.Status} (never filled)");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -525,14 +456,23 @@ public class TradingBotBase : ITradingBot
|
||||
var brokerPositions = await ServiceScopeHelpers.WithScopedService<ITradingService, List<Position>>(
|
||||
_scopeFactory, async tradingService =>
|
||||
{
|
||||
internalPosition = await tradingService.GetPositionByIdentifierAsync(positionForSignal.Identifier);
|
||||
internalPosition = Config.IsForBacktest
|
||||
? positionForSignal
|
||||
: await tradingService.GetPositionByIdentifierAsync(positionForSignal.Identifier);
|
||||
|
||||
return await ServiceScopeHelpers.WithScopedService<IExchangeService, List<Position>>(
|
||||
_scopeFactory,
|
||||
async exchangeService =>
|
||||
{
|
||||
return [.. await exchangeService.GetBrokerPositions(Account)];
|
||||
});
|
||||
if (Config.IsForBacktest)
|
||||
{
|
||||
return new List<Position> { internalPosition };
|
||||
}
|
||||
else
|
||||
{
|
||||
return await ServiceScopeHelpers.WithScopedService<IExchangeService, List<Position>>(
|
||||
_scopeFactory,
|
||||
async exchangeService =>
|
||||
{
|
||||
return [.. await exchangeService.GetBrokerPositions(Account)];
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (!Config.IsForBacktest)
|
||||
@@ -1023,30 +963,6 @@ public class TradingBotBase : ITradingBot
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Optimized position update method for backtests - skips live trading overhead
|
||||
/// </summary>
|
||||
private async Task UpdatePositionForBacktest(LightSignal signal, Position positionForSignal)
|
||||
{
|
||||
// For backtests, positions are filled immediately
|
||||
if (positionForSignal.Status == PositionStatus.New)
|
||||
{
|
||||
positionForSignal.Status = PositionStatus.Filled;
|
||||
await SetPositionStatus(signal.Identifier, PositionStatus.Filled);
|
||||
SetSignalStatus(signal.Identifier, SignalStatus.PositionOpen);
|
||||
}
|
||||
else if (positionForSignal.Status == PositionStatus.Filled)
|
||||
{
|
||||
// Handle position closing logic for backtests
|
||||
await HandleClosedPosition(positionForSignal);
|
||||
}
|
||||
else if (positionForSignal.Status == PositionStatus.Finished ||
|
||||
positionForSignal.Status == PositionStatus.Flipped)
|
||||
{
|
||||
await HandleClosedPosition(positionForSignal);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdatePositionDatabase(Position position)
|
||||
{
|
||||
await ServiceScopeHelpers.WithScopedService<ITradingService>(_scopeFactory,
|
||||
@@ -1215,20 +1131,20 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
private async Task<bool> CanOpenPosition(LightSignal signal)
|
||||
{
|
||||
// Fast path for backtests - skip live trading checks
|
||||
if (IsForBacktest)
|
||||
{
|
||||
return !await IsInCooldownPeriodAsync() && await CheckLossStreak(signal);
|
||||
}
|
||||
|
||||
// Live trading path
|
||||
// Early return if we haven't executed yet
|
||||
if (ExecutionCount == 0)
|
||||
// Early return if we're in backtest mode and haven't executed yet
|
||||
// TODO : check if its a startup cycle
|
||||
if (!Config.IsForBacktest && ExecutionCount == 0)
|
||||
{
|
||||
await LogInformation("⏳ Bot Not Ready\nCannot open position\nBot hasn't executed first cycle yet");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if we're in backtest mode
|
||||
if (Config.IsForBacktest)
|
||||
{
|
||||
return !await IsInCooldownPeriodAsync() && await CheckLossStreak(signal);
|
||||
}
|
||||
|
||||
// Check broker positions for live trading
|
||||
var canOpenPosition = await CheckBrokerPositions();
|
||||
if (!canOpenPosition)
|
||||
@@ -1242,15 +1158,18 @@ public class TradingBotBase : ITradingBot
|
||||
decimal currentPrice = 0;
|
||||
await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService =>
|
||||
{
|
||||
currentPrice = await exchangeService.GetCurrentPrice(Account, Config.Ticker);
|
||||
currentPrice = Config.IsForBacktest
|
||||
? LastCandle?.Close ?? 0
|
||||
: await exchangeService.GetCurrentPrice(Account, Config.Ticker);
|
||||
});
|
||||
|
||||
|
||||
bool synthRisk = false;
|
||||
await ServiceScopeHelpers.WithScopedService<ITradingService>(_scopeFactory, async tradingService =>
|
||||
{
|
||||
synthRisk = await tradingService.AssessSynthPositionRiskAsync(Config.Ticker, signal.Direction,
|
||||
currentPrice,
|
||||
Config, false);
|
||||
Config, Config.IsForBacktest);
|
||||
});
|
||||
if (!synthRisk)
|
||||
{
|
||||
@@ -1265,69 +1184,38 @@ public class TradingBotBase : ITradingBot
|
||||
private async Task<bool> CheckLossStreak(LightSignal signal)
|
||||
{
|
||||
// If MaxLossStreak is 0, there's no limit
|
||||
if (MaxLossStreak <= 0)
|
||||
if (Config.MaxLossStreak <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Optimize: Pre-allocate array and use manual sorting for better performance
|
||||
var maxStreak = MaxLossStreak;
|
||||
var recentPositions = new Position[maxStreak];
|
||||
var count = 0;
|
||||
|
||||
// Collect recent finished positions manually for better performance
|
||||
foreach (var position in Positions.Values)
|
||||
{
|
||||
if (!position.IsFinished()) continue;
|
||||
|
||||
// Simple insertion sort by date (descending)
|
||||
var insertIndex = 0;
|
||||
while (insertIndex < count && recentPositions[insertIndex].Open.Date > position.Open.Date)
|
||||
{
|
||||
insertIndex++;
|
||||
}
|
||||
|
||||
if (insertIndex < maxStreak)
|
||||
{
|
||||
// Shift elements
|
||||
for (var i = Math.Min(count, maxStreak - 1); i > insertIndex; i--)
|
||||
{
|
||||
recentPositions[i] = recentPositions[i - 1];
|
||||
}
|
||||
|
||||
recentPositions[insertIndex] = position;
|
||||
if (count < maxStreak) count++;
|
||||
}
|
||||
}
|
||||
// Get the last N finished positions regardless of direction
|
||||
var recentPositions = Positions
|
||||
.Values
|
||||
.Where(p => p.IsFinished())
|
||||
.OrderByDescending(p => p.Open.Date)
|
||||
.Take(Config.MaxLossStreak)
|
||||
.ToList();
|
||||
|
||||
// If we don't have enough positions to form a streak, we can open
|
||||
if (count < maxStreak)
|
||||
if (recentPositions.Count < Config.MaxLossStreak)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if all recent positions were losses
|
||||
var allLosses = true;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
if (recentPositions[i].ProfitAndLoss?.Realized >= 0)
|
||||
{
|
||||
allLosses = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var allLosses = recentPositions.All(p => p.ProfitAndLoss?.Realized < 0);
|
||||
if (!allLosses)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// If we have a loss streak, check if the last position was in the same direction as the signal
|
||||
var lastPosition = recentPositions[0]; // First element is most recent due to descending sort
|
||||
var lastPosition = recentPositions.First();
|
||||
if (lastPosition.OriginDirection == signal.Direction)
|
||||
{
|
||||
await LogWarning(
|
||||
$"🔥 Loss Streak Limit\nCannot open position\nMax loss streak: `{maxStreak}` reached\n📉 Last `{count}` trades were losses\n🎯 Last position: `{lastPosition.OriginDirection}`\nWaiting for opposite direction signal");
|
||||
$"🔥 Loss Streak Limit\nCannot open position\nMax loss streak: `{Config.MaxLossStreak}` reached\n📉 Last `{recentPositions.Count}` trades were losses\n🎯 Last position: `{lastPosition.OriginDirection}`\nWaiting for opposite direction signal");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2142,21 +2030,8 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
public int GetWinRate()
|
||||
{
|
||||
// Optimize: Single pass through positions
|
||||
var succeededPositions = 0;
|
||||
var total = 0;
|
||||
|
||||
foreach (var position in Positions.Values)
|
||||
{
|
||||
if (position.IsValidForMetrics())
|
||||
{
|
||||
total++;
|
||||
if (position.IsInProfit())
|
||||
{
|
||||
succeededPositions++;
|
||||
}
|
||||
}
|
||||
}
|
||||
var succeededPositions = Positions.Values.Where(p => p.IsValidForMetrics()).Count(p => p.IsInProfit());
|
||||
var total = Positions.Values.Where(p => p.IsValidForMetrics()).Count();
|
||||
|
||||
if (total == 0)
|
||||
return 0;
|
||||
@@ -2166,15 +2041,9 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
public decimal GetProfitAndLoss()
|
||||
{
|
||||
// Optimize: Manual loop instead of LINQ for better performance
|
||||
var netPnl = 0m;
|
||||
foreach (var position in Positions.Values)
|
||||
{
|
||||
if (position.IsValidForMetrics() && position.ProfitAndLoss != null)
|
||||
{
|
||||
netPnl += position.GetPnLBeforeFees();
|
||||
}
|
||||
}
|
||||
// Calculate net PnL after deducting fees for each position
|
||||
var netPnl = Positions.Values.Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null)
|
||||
.Sum(p => p.GetPnLBeforeFees());
|
||||
return netPnl;
|
||||
}
|
||||
|
||||
@@ -2186,15 +2055,11 @@ public class TradingBotBase : ITradingBot
|
||||
/// <returns>Returns the total fees paid as a decimal value.</returns>
|
||||
public decimal GetTotalFees()
|
||||
{
|
||||
// Optimize: Manual loop instead of LINQ
|
||||
var totalFees = 0m;
|
||||
decimal totalFees = 0;
|
||||
|
||||
foreach (var position in Positions.Values)
|
||||
foreach (var position in Positions.Values.Where(p => p.IsValidForMetrics()))
|
||||
{
|
||||
if (position.IsValidForMetrics())
|
||||
{
|
||||
totalFees += TradingHelpers.CalculatePositionFees(position);
|
||||
}
|
||||
totalFees += TradingHelpers.CalculatePositionFees(position);
|
||||
}
|
||||
|
||||
return totalFees;
|
||||
@@ -2715,8 +2580,8 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
// Calculate cooldown end time based on last position closing time
|
||||
var baseIntervalSeconds = CandleHelpers.GetBaseIntervalInSeconds(Config.Timeframe);
|
||||
var cooldownEndTime = LastPositionClosingTime.Value.AddSeconds(baseIntervalSeconds * CooldownPeriod);
|
||||
var isInCooldown = (IsForBacktest ? LastCandle.Date : DateTime.UtcNow) < cooldownEndTime;
|
||||
var cooldownEndTime = LastPositionClosingTime.Value.AddSeconds(baseIntervalSeconds * Config.CooldownPeriod);
|
||||
var isInCooldown = (Config.IsForBacktest ? LastCandle.Date : DateTime.UtcNow) < cooldownEndTime;
|
||||
|
||||
if (isInCooldown)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Bots;
|
||||
@@ -432,7 +431,6 @@ public class TradingService : ITradingService
|
||||
|
||||
/// <summary>
|
||||
/// Calculates indicators values for a given scenario and candles.
|
||||
/// Uses parallel processing for independent indicator calculations to improve performance.
|
||||
/// </summary>
|
||||
/// <param name="scenario">The scenario containing indicators.</param>
|
||||
/// <param name="candles">The candles to calculate indicators for.</param>
|
||||
@@ -441,7 +439,7 @@ public class TradingService : ITradingService
|
||||
Scenario scenario,
|
||||
HashSet<Candle> candles)
|
||||
{
|
||||
// Offload CPU-bound indicator calculations to thread pool with parallel processing
|
||||
// Offload CPU-bound indicator calculations to thread pool
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var indicatorsValues = new Dictionary<IndicatorType, IndicatorsResultBase>();
|
||||
@@ -451,39 +449,19 @@ public class TradingService : ITradingService
|
||||
return indicatorsValues;
|
||||
}
|
||||
|
||||
// Use parallel processing for independent indicator calculations
|
||||
// Configure parallelism based on indicator count and system capabilities
|
||||
var maxDegreeOfParallelism = Math.Min(scenario.Indicators.Count, Environment.ProcessorCount);
|
||||
|
||||
var options = new ParallelOptions
|
||||
{
|
||||
MaxDegreeOfParallelism = maxDegreeOfParallelism,
|
||||
CancellationToken = CancellationToken.None
|
||||
};
|
||||
|
||||
// Use thread-safe concurrent dictionary for parallel writes
|
||||
var concurrentResults = new ConcurrentDictionary<IndicatorType, IndicatorsResultBase>();
|
||||
|
||||
// Parallel calculation of indicators
|
||||
Parallel.ForEach(scenario.Indicators, options, indicator =>
|
||||
// Build indicators from scenario
|
||||
foreach (var indicator in scenario.Indicators)
|
||||
{
|
||||
try
|
||||
{
|
||||
var buildedIndicator = ScenarioHelpers.BuildIndicator(ScenarioHelpers.BaseToLight(indicator));
|
||||
var result = buildedIndicator.GetIndicatorValues(candles);
|
||||
concurrentResults[indicator.Type] = result;
|
||||
indicatorsValues[indicator.Type] = buildedIndicator.GetIndicatorValues(candles);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error calculating indicator {IndicatorName}: {ErrorMessage}",
|
||||
indicator.Name, ex.Message);
|
||||
}
|
||||
});
|
||||
|
||||
// Convert to regular dictionary for return
|
||||
foreach (var kvp in concurrentResults)
|
||||
{
|
||||
indicatorsValues[kvp.Key] = kvp.Value;
|
||||
}
|
||||
|
||||
return indicatorsValues;
|
||||
|
||||
Reference in New Issue
Block a user