Fix front and backtest

This commit is contained in:
2025-07-04 13:14:12 +07:00
parent c9959f7550
commit 2d295fc860
18 changed files with 676 additions and 182 deletions

View File

@@ -76,6 +76,16 @@ public class TradingBot : Bot, ITradingBot
WalletBalances = new Dictionary<DateTime, decimal>();
IndicatorsValues = new Dictionary<IndicatorType, IndicatorsResultBase>();
// Load indicators if scenario is provided in config
if (Config.Scenario != null)
{
LoadIndicators(Config.Scenario);
}
else
{
throw new ArgumentException("Scenario object must be provided in TradingBotConfig. ScenarioName alone is not sufficient.");
}
if (!Config.IsForBacktest)
{
Interval = CandleExtensions.GetIntervalFromTimeframe(Config.Timeframe);
@@ -91,7 +101,13 @@ public class TradingBot : Bot, ITradingBot
if (!Config.IsForBacktest)
{
LoadScenario(Config.ScenarioName);
// Scenario and indicators should already be loaded in constructor
// This is just a safety check
if (Config.Scenario == null || !Indicators.Any())
{
throw new InvalidOperationException("Scenario or indicators not loaded properly in constructor. This indicates a configuration error.");
}
PreloadCandles().GetAwaiter().GetResult();
CancelAllOrders().GetAwaiter().GetResult();
@@ -127,33 +143,32 @@ public class TradingBot : Bot, ITradingBot
}
}
public void LoadScenario(string scenarioName)
{
if (Config.Scenario != null)
return;
var scenario = TradingService.GetScenarioByName(scenarioName);
if (scenario == null)
{
Logger.LogWarning("No scenario found for this scenario name");
Stop();
}
else
{
LoadIndicators(ScenarioHelpers.GetIndicatorsFromScenario(scenario));
}
}
public void LoadScenario(Scenario scenario)
{
if (scenario == null)
{
Logger.LogWarning("Null scenario provided");
Stop();
var errorMessage = "Null scenario provided";
Logger.LogWarning(errorMessage);
// If called during construction, throw exception instead of Stop()
if (Status == BotStatus.Down)
{
throw new ArgumentException(errorMessage);
}
else
{
Stop();
}
}
else
{
// Store the scenario in config and load indicators
Config.Scenario = scenario;
LoadIndicators(ScenarioHelpers.GetIndicatorsFromScenario(scenario));
Logger.LogInformation($"Loaded scenario '{scenario.Name}' with {Indicators.Count} indicators");
}
}
@@ -164,10 +179,15 @@ public class TradingBot : Bot, ITradingBot
public void LoadIndicators(IEnumerable<IIndicator> indicators)
{
foreach (var strategy in indicators)
// Clear existing indicators to prevent duplicates
Indicators.Clear();
foreach (var indicator in indicators)
{
Indicators.Add(strategy);
Indicators.Add(indicator);
}
Logger.LogInformation($"Loaded {Indicators.Count} indicators for bot '{Name}'");
}
public async Task Run()
@@ -1444,9 +1464,9 @@ public class TradingBot : Bot, ITradingBot
throw new ArgumentException("Account name cannot be null or empty");
}
if (string.IsNullOrEmpty(newConfig.ScenarioName))
if (newConfig.Scenario == null)
{
throw new ArgumentException("Scenario name cannot be null or empty");
throw new ArgumentException("Scenario object must be provided in configuration");
}
// Protect critical properties that shouldn't change for running bots
@@ -1487,9 +1507,17 @@ public class TradingBot : Bot, ITradingBot
// If scenario changed, reload it
var currentScenario = Config.Scenario?.Name;
if (Config.ScenarioName != currentScenario)
var newScenario = newConfig.Scenario?.Name;
if (newScenario != currentScenario)
{
LoadScenario(Config.ScenarioName);
if (newConfig.Scenario != null)
{
LoadScenario(newConfig.Scenario);
}
else
{
throw new ArgumentException("New scenario object must be provided when updating configuration.");
}
}
await LogInformation("✅ **Configuration Applied**\n" +