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:
@@ -0,0 +1,444 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Infrastructure.Databases.PostgreSql.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.PostgreSql;
|
||||
|
||||
public class PostgreSqlTradingRepository : ITradingRepository
|
||||
{
|
||||
private readonly ManagingDbContext _context;
|
||||
|
||||
public PostgreSqlTradingRepository(ManagingDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
#region Scenario Methods
|
||||
|
||||
public async Task DeleteScenarioAsync(string name)
|
||||
{
|
||||
var scenario = await _context.Scenarios.FirstOrDefaultAsync(s => s.Name == name);
|
||||
if (scenario != null)
|
||||
{
|
||||
_context.Scenarios.Remove(scenario);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public Scenario GetScenarioByName(string name)
|
||||
{
|
||||
return GetScenarioByNameAsync(name).Result;
|
||||
}
|
||||
|
||||
public async Task<Scenario> GetScenarioByNameAsync(string name)
|
||||
{
|
||||
var scenario = await _context.Scenarios
|
||||
.AsNoTracking()
|
||||
.Include(s => s.ScenarioIndicators)
|
||||
.ThenInclude(si => si.Indicator)
|
||||
.FirstOrDefaultAsync(s => s.Name == name)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (scenario == null) return null;
|
||||
|
||||
var mappedScenario = PostgreSqlMappers.Map(scenario);
|
||||
// Map indicators from junction table
|
||||
mappedScenario.Indicators = scenario.ScenarioIndicators
|
||||
.Select(si => PostgreSqlMappers.Map(si.Indicator))
|
||||
.ToList();
|
||||
|
||||
return mappedScenario;
|
||||
}
|
||||
|
||||
public IEnumerable<Scenario> GetScenarios()
|
||||
{
|
||||
return GetScenariosAsync().Result;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Scenario>> GetScenariosAsync()
|
||||
{
|
||||
var scenarios = await _context.Scenarios
|
||||
.AsNoTracking()
|
||||
.Include(s => s.ScenarioIndicators)
|
||||
.ThenInclude(si => si.Indicator)
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return scenarios.Select(scenario =>
|
||||
{
|
||||
var mappedScenario = PostgreSqlMappers.Map(scenario);
|
||||
mappedScenario.Indicators = scenario.ScenarioIndicators
|
||||
.Select(si => PostgreSqlMappers.Map(si.Indicator))
|
||||
.ToList();
|
||||
return mappedScenario;
|
||||
});
|
||||
}
|
||||
|
||||
public async Task InsertScenarioAsync(Scenario scenario)
|
||||
{
|
||||
// Check if scenario already exists for the same user
|
||||
var existingScenario = await _context.Scenarios
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(s => s.Name == scenario.Name &&
|
||||
((scenario.User == null && s.UserName == null) ||
|
||||
(scenario.User != null && s.UserName == scenario.User.Name)));
|
||||
|
||||
if (existingScenario != null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Scenario with name '{scenario.Name}' already exists for user '{scenario.User?.Name}'");
|
||||
}
|
||||
|
||||
var scenarioEntity = PostgreSqlMappers.Map(scenario);
|
||||
_context.Scenarios.Add(scenarioEntity);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Handle scenario-indicator relationships
|
||||
if (scenario.Indicators != null && scenario.Indicators.Any())
|
||||
{
|
||||
foreach (var indicator in scenario.Indicators)
|
||||
{
|
||||
var indicatorEntity = await _context.Indicators
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(i => i.Name == indicator.Name &&
|
||||
((indicator.User == null && i.UserName == null) ||
|
||||
(indicator.User != null && i.UserName == indicator.User.Name)));
|
||||
|
||||
if (indicatorEntity != null)
|
||||
{
|
||||
var junction = new ScenarioIndicatorEntity
|
||||
{
|
||||
ScenarioId = scenarioEntity.Id,
|
||||
IndicatorId = indicatorEntity.Id
|
||||
};
|
||||
_context.ScenarioIndicators.Add(junction);
|
||||
}
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateScenarioAsync(Scenario scenario)
|
||||
{
|
||||
var entity = _context.Scenarios
|
||||
.AsTracking()
|
||||
.FirstOrDefault(s => s.Name == scenario.Name);
|
||||
|
||||
if (entity != null)
|
||||
{
|
||||
entity.LoopbackPeriod = scenario.LoopbackPeriod ?? 1;
|
||||
entity.UserName = scenario.User?.Name;
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indicator Methods
|
||||
|
||||
public async Task DeleteIndicatorAsync(string name)
|
||||
{
|
||||
var indicator = _context.Indicators
|
||||
.AsTracking()
|
||||
.FirstOrDefault(i => i.Name == name);
|
||||
|
||||
if (indicator != null)
|
||||
{
|
||||
_context.Indicators.Remove(indicator);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteIndicatorsAsync()
|
||||
{
|
||||
var indicators = _context.Indicators.AsTracking().ToList();
|
||||
_context.Indicators.RemoveRange(indicators);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Indicator>> GetIndicatorsAsync()
|
||||
{
|
||||
var indicators = await _context.Indicators
|
||||
.AsNoTracking()
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(indicators);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Indicator>> GetStrategiesAsync()
|
||||
{
|
||||
var indicators = await _context.Indicators
|
||||
.AsNoTracking()
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
return PostgreSqlMappers.Map(indicators);
|
||||
}
|
||||
|
||||
public async Task<Indicator> GetStrategyByNameAsync(string name)
|
||||
{
|
||||
var indicator = await _context.Indicators
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(i => i.Name == name)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(indicator);
|
||||
}
|
||||
|
||||
public async Task InsertStrategyAsync(Indicator indicator)
|
||||
{
|
||||
// Check if indicator already exists for the same user
|
||||
var existingIndicator = await _context.Indicators
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(i => i.Name == indicator.Name &&
|
||||
((indicator.User == null && i.UserName == null) ||
|
||||
(indicator.User != null && i.UserName == indicator.User.Name)));
|
||||
|
||||
if (existingIndicator != null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Indicator with name '{indicator.Name}' already exists for user '{indicator.User?.Name}'");
|
||||
}
|
||||
|
||||
var entity = PostgreSqlMappers.Map(indicator);
|
||||
_context.Indicators.Add(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateStrategyAsync(Indicator indicator)
|
||||
{
|
||||
var entity = _context.Indicators
|
||||
.AsTracking()
|
||||
.FirstOrDefault(i => i.Name == indicator.Name);
|
||||
|
||||
if (entity != null)
|
||||
{
|
||||
entity.Type = indicator.Type;
|
||||
entity.SignalType = indicator.SignalType;
|
||||
entity.MinimumHistory = indicator.MinimumHistory;
|
||||
entity.Period = indicator.Period;
|
||||
entity.FastPeriods = indicator.FastPeriods;
|
||||
entity.SlowPeriods = indicator.SlowPeriods;
|
||||
entity.SignalPeriods = indicator.SignalPeriods;
|
||||
entity.Multiplier = indicator.Multiplier;
|
||||
entity.SmoothPeriods = indicator.SmoothPeriods;
|
||||
entity.StochPeriods = indicator.StochPeriods;
|
||||
entity.CyclePeriods = indicator.CyclePeriods;
|
||||
entity.UserName = indicator.User?.Name;
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region Position Methods
|
||||
|
||||
public Position GetPositionByIdentifier(string identifier)
|
||||
{
|
||||
return GetPositionByIdentifierAsync(identifier).Result;
|
||||
}
|
||||
|
||||
public async Task<Position> GetPositionByIdentifierAsync(string identifier)
|
||||
{
|
||||
var position = await _context.Positions
|
||||
.AsNoTracking()
|
||||
.Include(p => p.OpenTrade)
|
||||
.Include(p => p.StopLossTrade)
|
||||
.Include(p => p.TakeProfit1Trade)
|
||||
.Include(p => p.TakeProfit2Trade)
|
||||
.FirstOrDefaultAsync(p => p.Identifier == identifier)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(position);
|
||||
}
|
||||
|
||||
public IEnumerable<Position> GetPositions(PositionInitiator positionInitiator)
|
||||
{
|
||||
return GetPositionsAsync(positionInitiator).Result;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Position>> GetPositionsAsync(PositionInitiator positionInitiator)
|
||||
{
|
||||
var positions = await _context.Positions
|
||||
.AsNoTracking()
|
||||
.Include(p => p.OpenTrade)
|
||||
.Include(p => p.StopLossTrade)
|
||||
.Include(p => p.TakeProfit1Trade)
|
||||
.Include(p => p.TakeProfit2Trade)
|
||||
.Where(p => p.Initiator == positionInitiator)
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(positions);
|
||||
}
|
||||
|
||||
public IEnumerable<Position> GetPositionsByStatus(PositionStatus positionStatus)
|
||||
{
|
||||
return GetPositionsByStatusAsync(positionStatus).Result;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Position>> GetPositionsByStatusAsync(PositionStatus positionStatus)
|
||||
{
|
||||
var positions = await _context.Positions
|
||||
.AsNoTracking()
|
||||
.Include(p => p.OpenTrade)
|
||||
.Include(p => p.StopLossTrade)
|
||||
.Include(p => p.TakeProfit1Trade)
|
||||
.Include(p => p.TakeProfit2Trade)
|
||||
.Where(p => p.Status == positionStatus)
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(positions);
|
||||
}
|
||||
|
||||
public async Task InsertPositionAsync(Position position)
|
||||
{
|
||||
// Check if position already exists for the same user
|
||||
var existingPosition = await _context.Positions
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(p => p.Identifier == position.Identifier &&
|
||||
((position.User == null && p.UserName == null) ||
|
||||
(position.User != null && p.UserName == position.User.Name)));
|
||||
|
||||
if (existingPosition != null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Position with identifier '{position.Identifier}' already exists for user '{position.User?.Name}'");
|
||||
}
|
||||
|
||||
var entity = PostgreSqlMappers.Map(position);
|
||||
|
||||
// Handle related trades
|
||||
if (position.Open != null)
|
||||
{
|
||||
var openTrade = PostgreSqlMappers.Map(position.Open);
|
||||
_context.Trades.Add(openTrade);
|
||||
await _context.SaveChangesAsync();
|
||||
entity.OpenTradeId = openTrade.Id;
|
||||
}
|
||||
|
||||
if (position.StopLoss != null)
|
||||
{
|
||||
var stopLossTrade = PostgreSqlMappers.Map(position.StopLoss);
|
||||
_context.Trades.Add(stopLossTrade);
|
||||
await _context.SaveChangesAsync();
|
||||
entity.StopLossTradeId = stopLossTrade.Id;
|
||||
}
|
||||
|
||||
if (position.TakeProfit1 != null)
|
||||
{
|
||||
var takeProfit1Trade = PostgreSqlMappers.Map(position.TakeProfit1);
|
||||
_context.Trades.Add(takeProfit1Trade);
|
||||
await _context.SaveChangesAsync();
|
||||
entity.TakeProfit1TradeId = takeProfit1Trade.Id;
|
||||
}
|
||||
|
||||
if (position.TakeProfit2 != null)
|
||||
{
|
||||
var takeProfit2Trade = PostgreSqlMappers.Map(position.TakeProfit2);
|
||||
_context.Trades.Add(takeProfit2Trade);
|
||||
await _context.SaveChangesAsync();
|
||||
entity.TakeProfit2TradeId = takeProfit2Trade.Id;
|
||||
}
|
||||
|
||||
_context.Positions.Add(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdatePositionAsync(Position position)
|
||||
{
|
||||
var entity = _context.Positions
|
||||
.AsTracking()
|
||||
.FirstOrDefault(p => p.Identifier == position.Identifier);
|
||||
|
||||
if (entity != null)
|
||||
{
|
||||
entity.Date = position.Date;
|
||||
entity.ProfitAndLoss = position.ProfitAndLoss?.Realized ?? 0;
|
||||
entity.Status = position.Status;
|
||||
entity.SignalIdentifier = position.SignalIdentifier;
|
||||
entity.MoneyManagementJson = position.MoneyManagement != null
|
||||
? JsonConvert.SerializeObject(position.MoneyManagement)
|
||||
: null;
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Signal Methods
|
||||
|
||||
public IEnumerable<Signal> GetSignalsByUser(User user)
|
||||
{
|
||||
return GetSignalsByUserAsync(user).Result;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Signal>> GetSignalsByUserAsync(User user)
|
||||
{
|
||||
var signals = await _context.Signals
|
||||
.AsNoTracking()
|
||||
.Where(s => (user == null && s.UserName == null) ||
|
||||
(user != null && s.UserName == user.Name))
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(signals);
|
||||
}
|
||||
|
||||
public Signal GetSignalByIdentifier(string identifier, User user = null)
|
||||
{
|
||||
return GetSignalByIdentifierAsync(identifier, user).Result;
|
||||
}
|
||||
|
||||
public async Task<Signal> GetSignalByIdentifierAsync(string identifier, User user = null)
|
||||
{
|
||||
var signal = await _context.Signals
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(s => s.Identifier == identifier &&
|
||||
((user == null && s.UserName == null) ||
|
||||
(user != null && s.UserName == user.Name)))
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(signal);
|
||||
}
|
||||
|
||||
public async Task InsertSignalAsync(Signal signal)
|
||||
{
|
||||
// Check if signal already exists with the same identifier, date, and user
|
||||
var existingSignal = _context.Signals
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(s => s.Identifier == signal.Identifier &&
|
||||
s.Date == signal.Date &&
|
||||
((s.UserName == null && signal.User == null) ||
|
||||
(s.UserName != null && signal.User != null && s.UserName == signal.User.Name)));
|
||||
|
||||
if (existingSignal != null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Signal with identifier '{signal.Identifier}' and date '{signal.Date}' already exists for this user");
|
||||
}
|
||||
|
||||
var entity = PostgreSqlMappers.Map(signal);
|
||||
_context.Signals.Add(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user