Trading bot grain (#33)
* Trading bot Grain * Fix a bit more of the trading bot * Advance on the tradingbot grain * Fix build * Fix db script * Fix user login * Fix a bit backtest * Fix cooldown and backtest * start fixing bot start * Fix startup * Setup local db * Fix build and update candles and scenario * Add bot registry * Add reminder * Updateing the grains * fix bootstraping * Save stats on tick * Save bot data every tick * Fix serialization * fix save bot stats * Fix get candles * use dict instead of list for position * Switch hashset to dict * Fix a bit * Fix bot launch and bot view * add migrations * Remove the tolist * Add agent grain * Save agent summary * clean * Add save bot * Update get bots * Add get bots * Fix stop/restart * fix Update config * Update scanner table on new backtest saved * Fix backtestRowDetails.tsx * Fix agentIndex * Update agentIndex * Fix more things * Update user cache * Fix * Fix account load/start/restart/run
This commit is contained in:
@@ -8,6 +8,7 @@ using Managing.Domain.Bots;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Risk;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Users;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -624,9 +625,9 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
return clone;
|
||||
}
|
||||
|
||||
public List<GeneticIndicator> GetSelectedIndicators()
|
||||
public List<LightIndicator> GetSelectedIndicators()
|
||||
{
|
||||
var selected = new List<GeneticIndicator>();
|
||||
var selected = new List<LightIndicator>();
|
||||
var genes = GetGenes();
|
||||
|
||||
// Check all indicator selection slots (genes 5 to 5+N-1 where N is number of eligible indicators)
|
||||
@@ -634,7 +635,7 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
{
|
||||
if (genes[5 + i].Value.ToString() == "1")
|
||||
{
|
||||
var indicator = new GeneticIndicator
|
||||
var indicator = new LightIndicator(_eligibleIndicators[i].ToString(), _eligibleIndicators[i])
|
||||
{
|
||||
Type = _eligibleIndicators[i]
|
||||
};
|
||||
@@ -713,39 +714,24 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
// Enforce proper risk-reward constraints
|
||||
var minStopLoss = 0.2; // Minimum 0.2% to cover fees
|
||||
var maxStopLoss = takeProfit / 1.1; // Ensure risk-reward ratio is at least 1.1:1
|
||||
|
||||
|
||||
// Generate a random stop loss between min and max
|
||||
var randomStopLoss = GetRandomInRange((minStopLoss, maxStopLoss));
|
||||
|
||||
|
||||
// Use the random value instead of clamping the original
|
||||
stopLoss = randomStopLoss;
|
||||
|
||||
|
||||
// Log the generated values (for debugging)
|
||||
Console.WriteLine($"Generated: TP={takeProfit:F2}%, SL={stopLoss:F2}% (RR={takeProfit/stopLoss:F2}:1)");
|
||||
Console.WriteLine($"Generated: TP={takeProfit:F2}%, SL={stopLoss:F2}% (RR={takeProfit / stopLoss:F2}:1)");
|
||||
|
||||
// Get loopback period from gene 4
|
||||
var loopbackPeriod = Convert.ToInt32(genes[4].Value);
|
||||
|
||||
// Build scenario using selected indicators
|
||||
var scenario = new Scenario($"Genetic_{request.RequestId}_Scenario", loopbackPeriod);
|
||||
|
||||
foreach (var geneticIndicator in selectedIndicators)
|
||||
var scenario = new LightScenario($"Genetic_{request.RequestId}_Scenario", loopbackPeriod)
|
||||
{
|
||||
var indicator = ScenarioHelpers.BuildIndicator(
|
||||
type: geneticIndicator.Type,
|
||||
name: $"Genetic_{geneticIndicator.Type}_{Guid.NewGuid():N}",
|
||||
period: geneticIndicator.Period,
|
||||
fastPeriods: geneticIndicator.FastPeriods,
|
||||
slowPeriods: geneticIndicator.SlowPeriods,
|
||||
signalPeriods: geneticIndicator.SignalPeriods,
|
||||
multiplier: geneticIndicator.Multiplier,
|
||||
stochPeriods: geneticIndicator.StochPeriods,
|
||||
smoothPeriods: geneticIndicator.SmoothPeriods,
|
||||
cyclePeriods: geneticIndicator.CyclePeriods
|
||||
);
|
||||
|
||||
scenario.AddIndicator(indicator);
|
||||
}
|
||||
Indicators = selectedIndicators
|
||||
};
|
||||
|
||||
var mm = new MoneyManagement
|
||||
{
|
||||
@@ -776,7 +762,7 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
UseForPositionSizing = false,
|
||||
UseForSignalFiltering = false,
|
||||
UseForDynamicStopLoss = false,
|
||||
Scenario = LightScenario.FromScenario(scenario),
|
||||
Scenario = scenario,
|
||||
MoneyManagement = mm,
|
||||
RiskManagement = new RiskManagement
|
||||
{
|
||||
@@ -853,7 +839,7 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
ReplaceGene(1, new Gene(stopLoss));
|
||||
|
||||
// Log the initial values (for debugging)
|
||||
Console.WriteLine($"Initialized: TP={takeProfit:F2}%, SL={stopLoss:F2}% (RR={takeProfit/stopLoss:F2}:1)");
|
||||
Console.WriteLine($"Initialized: TP={takeProfit:F2}%, SL={stopLoss:F2}% (RR={takeProfit / stopLoss:F2}:1)");
|
||||
|
||||
// Initialize remaining genes normally
|
||||
for (int i = 2; i < Length; i++)
|
||||
@@ -863,22 +849,6 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Genetic indicator with parameters
|
||||
/// </summary>
|
||||
public class GeneticIndicator
|
||||
{
|
||||
public IndicatorType Type { get; set; }
|
||||
public int? Period { get; set; }
|
||||
public int? FastPeriods { get; set; }
|
||||
public int? SlowPeriods { get; set; }
|
||||
public int? SignalPeriods { get; set; }
|
||||
public double? Multiplier { get; set; }
|
||||
public int? StochPeriods { get; set; }
|
||||
public int? SmoothPeriods { get; set; }
|
||||
public int? CyclePeriods { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multi-objective fitness function for trading bot optimization
|
||||
/// </summary>
|
||||
@@ -889,7 +859,8 @@ public class TradingBotFitness : IFitness
|
||||
private GeneticAlgorithm _geneticAlgorithm;
|
||||
private readonly ILogger<GeneticService> _logger;
|
||||
|
||||
public TradingBotFitness(IServiceScopeFactory serviceScopeFactory, GeneticRequest request, ILogger<GeneticService> logger)
|
||||
public TradingBotFitness(IServiceScopeFactory serviceScopeFactory, GeneticRequest request,
|
||||
ILogger<GeneticService> logger)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_request = request;
|
||||
|
||||
Reference in New Issue
Block a user