Postgres (#30)

* Add postgres

* Migrate users

* Migrate geneticRequest

* Try to fix Concurrent call

* Fix asyncawait

* Fix async and concurrent

* Migrate backtests

* Add cache for user by address

* Fix backtest migration

* Fix not open connection

* Fix backtest command error

* Fix concurrent

* Fix all concurrency

* Migrate TradingRepo

* Fix scenarios

* Migrate statistic repo

* Save botbackup

* Add settings et moneymanagement

* Add bot postgres

* fix a bit more backups

* Fix bot model

* Fix loading backup

* Remove cache market for read positions

* Add workers to postgre

* Fix workers api

* Reduce get Accounts for workers

* Migrate synth to postgre

* Fix backtest saved

* Remove mongodb

* botservice decorrelation

* Fix tradingbot scope call

* fix tradingbot

* fix concurrent

* Fix scope for genetics

* Fix account over requesting

* Fix bundle backtest worker

* fix a lot of things

* fix tab backtest

* Remove optimized moneymanagement

* Add light signal to not use User and too much property

* Make money management lighter

* insert indicators to awaitable

* Migrate add strategies to await

* Refactor scenario and indicator retrieval to use asynchronous methods throughout the application

* add more async await

* Add services

* Fix and clean

* Fix bot a bit

* Fix bot and add message for cooldown

* Remove fees

* Add script to deploy db

* Update dfeeploy script

* fix script

* Add idempotent script and backup

* finish script migration

* Fix did user and agent name on start bot
This commit is contained in:
Oda
2025-07-27 15:42:17 +02:00
committed by GitHub
parent 361bfbf6e8
commit 422fecea7b
294 changed files with 23953 additions and 7272 deletions

View File

@@ -4,7 +4,6 @@ using Managing.Domain.Scenarios;
using Managing.Domain.Strategies;
using Managing.Domain.Users;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using static Managing.Common.Enums;
namespace Managing.Application.Scenarios
@@ -20,20 +19,20 @@ namespace Managing.Application.Scenarios
_tradingService = tradingService;
}
public Scenario CreateScenario(string name, List<string> strategies, int? loopbackPeriod = 1)
public async Task<Scenario> CreateScenario(string name, List<string> strategies, int? loopbackPeriod = 1)
{
var scenario = new Scenario(name, loopbackPeriod);
foreach (var strategy in strategies)
{
scenario.AddIndicator(_tradingService.GetStrategyByName(strategy));
scenario.AddIndicator(await _tradingService.GetStrategyByNameAsync(strategy));
}
try
{
_tradingService.InsertScenario(scenario);
await _tradingService.InsertScenarioAsync(scenario);
}
catch (MongoCommandException ex)
catch (Exception ex)
{
_logger.LogError(ex.Message);
throw new Exception("Cannot create scenario");
@@ -42,7 +41,7 @@ namespace Managing.Application.Scenarios
return scenario;
}
public Indicator CreateStrategy(
public async Task<Indicator> CreateStrategy(
IndicatorType type,
string name,
int? period = null,
@@ -65,30 +64,25 @@ namespace Managing.Application.Scenarios
stochPeriods,
smoothPeriods,
cyclePeriods);
_tradingService.InsertStrategy(strategy);
await _tradingService.InsertStrategyAsync(strategy);
return strategy;
}
public IEnumerable<Scenario> GetScenarios()
public async Task<IEnumerable<Scenario>> GetScenariosAsync()
{
return _tradingService.GetScenarios();
return await _tradingService.GetScenariosAsync();
}
public Scenario GetScenario(string name)
public async Task<IEnumerable<Indicator>> GetIndicatorsAsync()
{
return _tradingService.GetScenarioByName(name);
return await _tradingService.GetStrategiesAsync();
}
public IEnumerable<Indicator> GetIndicators()
{
return _tradingService.GetStrategies();
}
public bool DeleteScenario(string name)
public async Task<bool> DeleteScenarioAsync(string name)
{
try
{
_tradingService.DeleteScenario(name);
await _tradingService.DeleteScenarioAsync(name);
return true;
}
catch (Exception ex)
@@ -98,61 +92,19 @@ namespace Managing.Application.Scenarios
}
}
public bool DeleteStrategy(string name)
public async Task<bool> UpdateScenario(string name, List<string> strategies, int? loopbackPeriod)
{
try
{
_tradingService.DeleteStrategy(name);
return true;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return false;
}
}
public bool DeleteStrategies()
{
try
{
_tradingService.DeleteStrategies();
}
catch (Exception)
{
return false;
}
return true;
}
public bool DeleteScenarios()
{
try
{
_tradingService.DeleteScenarios();
}
catch (Exception)
{
return false;
}
return true;
}
public bool UpdateScenario(string name, List<string> strategies, int? loopbackPeriod)
{
try
{
var scenario = _tradingService.GetScenarioByName(name);
var scenario = await _tradingService.GetScenarioByNameAsync(name);
scenario.Indicators.Clear();
foreach (var strategy in strategies)
{
scenario.AddIndicator(_tradingService.GetStrategyByName(strategy));
scenario.AddIndicator(await _tradingService.GetStrategyByNameAsync(strategy));
}
scenario.LoopbackPeriod = loopbackPeriod ?? 1;
_tradingService.UpdateScenario(scenario);
await _tradingService.UpdateScenarioAsync(scenario);
return true;
}
catch (Exception e)
@@ -162,13 +114,13 @@ namespace Managing.Application.Scenarios
}
}
public bool UpdateStrategy(IndicatorType indicatorType, string name, int? period, int? fastPeriods,
public async Task<bool> UpdateStrategy(IndicatorType indicatorType, string name, int? period, int? fastPeriods,
int? slowPeriods,
int? signalPeriods, double? multiplier, int? stochPeriods, int? smoothPeriods, int? cyclePeriods)
{
try
{
var strategy = _tradingService.GetStrategyByName(name);
var strategy = await _tradingService.GetStrategyByNameAsync(name);
strategy.Type = indicatorType;
strategy.Period = period;
strategy.FastPeriods = fastPeriods;
@@ -178,7 +130,7 @@ namespace Managing.Application.Scenarios
strategy.StochPeriods = stochPeriods;
strategy.SmoothPeriods = smoothPeriods;
strategy.CyclePeriods = cyclePeriods;
_tradingService.UpdateStrategy(strategy);
await _tradingService.UpdateStrategyAsync(strategy);
return true;
}
catch (Exception e)
@@ -188,15 +140,13 @@ namespace Managing.Application.Scenarios
}
}
// User-specific methods implementation
public IEnumerable<Scenario> GetScenariosByUser(User user)
public async Task<IEnumerable<Scenario>> GetScenariosByUserAsync(User user)
{
var scenarios = _tradingService.GetScenarios();
var scenarios = await _tradingService.GetScenariosAsync();
return scenarios.Where(s => s.User?.Name == user.Name);
}
public Scenario CreateScenarioForUser(User user, string name, List<string> strategies, int? loopbackPeriod = 1)
public async Task<Scenario> CreateScenarioForUser(User user, string name, List<string> strategies, int? loopbackPeriod = 1)
{
var scenario = new Scenario(name, loopbackPeriod ?? 1)
{
@@ -205,82 +155,104 @@ namespace Managing.Application.Scenarios
foreach (var strategyName in strategies)
{
var strategy = _tradingService.GetStrategyByName(strategyName);
var strategy = await _tradingService.GetStrategyByNameAsync(strategyName);
if (strategy != null && strategy.User?.Name == user.Name)
{
scenario.AddIndicator(strategy);
}
}
_tradingService.InsertScenario(scenario);
await _tradingService.InsertScenarioAsync(scenario);
return scenario;
}
public IEnumerable<Indicator> GetIndicatorsByUser(User user)
public async Task<IEnumerable<Indicator>> GetIndicatorsByUserAsync(User user)
{
var strategies = _tradingService.GetStrategies();
return strategies.Where(s => s.User?.Name == user.Name);
var indicators = await GetIndicatorsAsync();
return indicators.Where(s => s.User?.Name == user.Name);
}
public bool DeleteIndicatorByUser(User user, string name)
public async Task<bool> DeleteIndicatorByUser(User user, string name)
{
var strategy = _tradingService.GetStrategyByName(name);
var strategy = await _tradingService.GetStrategyByNameAsync(name);
if (strategy != null && strategy.User?.Name == user.Name)
{
_tradingService.DeleteStrategy(strategy.Name);
await _tradingService.DeleteStrategyAsync(strategy.Name);
return true;
}
return false;
}
public bool DeleteScenarioByUser(User user, string name)
public async Task<bool> DeleteScenarioByUser(User user, string name)
{
var scenario = _tradingService.GetScenarioByName(name);
if (scenario != null && scenario.User?.Name == user.Name)
try
{
_tradingService.DeleteScenario(scenario.Name);
var scenarios = await GetScenariosByUserAsync(user);
foreach (var scenario in scenarios.Where(s => s.Name == name))
{
await _tradingService.DeleteScenarioAsync(scenario.Name);
}
return true;
}
return false;
catch (Exception ex)
{
_logger.LogError(ex.Message);
return false;
}
}
public Scenario GetScenarioByUser(User user, string name)
public async Task<bool> DeleteScenariosByUser(User user)
{
var scenario = _tradingService.GetScenarioByName(name);
try
{
var scenarios = await GetScenariosByUserAsync(user);
foreach (var scenario in scenarios)
{
await _tradingService.DeleteScenarioAsync(scenario.Name);
}
return true;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return false;
}
}
public async Task<Scenario> GetScenarioByUser(User user, string name)
{
var scenario = await _tradingService.GetScenarioByNameAsync(name);
return scenario != null && scenario.User?.Name == user.Name ? scenario : null;
}
public Indicator CreateIndicatorForUser(User user, IndicatorType type, string name, int? period = null,
public async Task<Indicator> CreateIndicatorForUser(User user, IndicatorType type, string name, int? period = null,
int? fastPeriods = null, int? slowPeriods = null, int? signalPeriods = null,
double? multiplier = null, int? stochPeriods = null, int? smoothPeriods = null,
int? cyclePeriods = null)
{
// Create a new strategy using the existing implementation
var strategy = CreateStrategy(type, name, period, fastPeriods, slowPeriods, signalPeriods,
var strategy = await CreateStrategy(type, name, period, fastPeriods, slowPeriods, signalPeriods,
multiplier, stochPeriods, smoothPeriods, cyclePeriods);
// Set the user
strategy.User = user;
// Update the strategy to save the user property
_tradingService.UpdateStrategy(strategy);
await _tradingService.UpdateStrategyAsync(strategy);
return strategy;
}
public bool DeleteStrategiesByUser(User user)
public async Task<bool> DeleteStrategiesByUser(User user)
{
try
{
var strategies = GetIndicatorsByUser(user);
var strategies = await GetIndicatorsByUserAsync(user);
foreach (var strategy in strategies)
{
_tradingService.DeleteStrategy(strategy.Name);
await _tradingService.DeleteStrategyAsync(strategy.Name);
}
return true;
}
catch (Exception ex)
@@ -290,29 +262,9 @@ namespace Managing.Application.Scenarios
}
}
public bool DeleteScenariosByUser(User user)
public async Task<bool> UpdateScenarioByUser(User user, string name, List<string> strategies, int? loopbackPeriod)
{
try
{
var scenarios = GetScenariosByUser(user);
foreach (var scenario in scenarios)
{
_tradingService.DeleteScenario(scenario.Name);
}
return true;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return false;
}
}
public bool UpdateScenarioByUser(User user, string name, List<string> strategies, int? loopbackPeriod)
{
var scenario = _tradingService.GetScenarioByName(name);
var scenario = await _tradingService.GetScenarioByNameAsync(name);
if (scenario == null || scenario.User?.Name != user.Name)
{
return false;
@@ -323,29 +275,29 @@ namespace Managing.Application.Scenarios
foreach (var strategyName in strategies)
{
var strategy = _tradingService.GetStrategyByName(strategyName);
var strategy = await _tradingService.GetStrategyByNameAsync(strategyName);
if (strategy != null && strategy.User?.Name == user.Name)
{
scenario.AddIndicator(strategy);
}
}
_tradingService.UpdateScenario(scenario);
await _tradingService.UpdateScenarioAsync(scenario);
return true;
}
public bool UpdateIndicatorByUser(User user, IndicatorType indicatorType, string name, int? period,
public async Task<bool> UpdateIndicatorByUser(User user, IndicatorType indicatorType, string name, int? period,
int? fastPeriods, int? slowPeriods, int? signalPeriods, double? multiplier,
int? stochPeriods, int? smoothPeriods, int? cyclePeriods)
{
var strategy = _tradingService.GetStrategyByName(name);
var strategy = await _tradingService.GetStrategyByNameAsync(name);
if (strategy == null || strategy.User?.Name != user.Name)
{
return false;
}
// Use the existing update strategy logic
var result = UpdateStrategy(indicatorType, name, period, fastPeriods, slowPeriods,
var result = await UpdateStrategy(indicatorType, name, period, fastPeriods, slowPeriods,
signalPeriods, multiplier, stochPeriods, smoothPeriods, cyclePeriods);
return result;