docker files fixes from liaqat
This commit is contained in:
46
src/Managing.Infrastructure.Database/AccountRepository.cs
Normal file
46
src/Managing.Infrastructure.Database/AccountRepository.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class AccountRepository : IAccountRepository
|
||||
{
|
||||
private readonly IMongoRepository<AccountDto> _accountRepository;
|
||||
|
||||
public AccountRepository(IMongoRepository<AccountDto> accountRepository)
|
||||
{
|
||||
_accountRepository = accountRepository;
|
||||
}
|
||||
|
||||
public void DeleteAccountByName(string name)
|
||||
{
|
||||
var account = _accountRepository.FindOne(a => a.Name == name);
|
||||
_accountRepository.DeleteById(account.Id.ToString());
|
||||
}
|
||||
|
||||
public async Task<Account> GetAccountByKeyAsync(string key)
|
||||
{
|
||||
var account = await _accountRepository.FindOneAsync(a => a.Key == key);
|
||||
return MongoMappers.Map(account);
|
||||
}
|
||||
|
||||
public async Task<Account> GetAccountByNameAsync(string name)
|
||||
{
|
||||
var account = await _accountRepository.FindOneAsync(a => a.Name == name);
|
||||
return MongoMappers.Map(account);
|
||||
}
|
||||
|
||||
public IEnumerable<Account> GetAccounts()
|
||||
{
|
||||
var accounts = _accountRepository.FindAll();
|
||||
return MongoMappers.Map(accounts);
|
||||
}
|
||||
|
||||
public async Task InsertAccountAsync(Account account)
|
||||
{
|
||||
await _accountRepository.InsertOneAsync(MongoMappers.Map(account));
|
||||
}
|
||||
}
|
||||
38
src/Managing.Infrastructure.Database/BacktestRepository.cs
Normal file
38
src/Managing.Infrastructure.Database/BacktestRepository.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class BacktestRepository : IBacktestRepository
|
||||
{
|
||||
private readonly IMongoRepository<BacktestDto> _backtestRepository;
|
||||
|
||||
public BacktestRepository(IMongoRepository<BacktestDto> backtestRepository)
|
||||
{
|
||||
_backtestRepository = backtestRepository;
|
||||
}
|
||||
|
||||
public void DeleteAllBacktests()
|
||||
{
|
||||
_backtestRepository.DropCollection();
|
||||
}
|
||||
|
||||
public void DeleteBacktestById(string id)
|
||||
{
|
||||
_backtestRepository.DeleteById(id);
|
||||
}
|
||||
|
||||
public IEnumerable<Backtest> GetBacktests()
|
||||
{
|
||||
var backtests = _backtestRepository.FindAll();
|
||||
return backtests.Select(b => MongoMappers.Map(b));
|
||||
}
|
||||
|
||||
public void InsertBacktest(Backtest backtest)
|
||||
{
|
||||
_backtestRepository.InsertOne(MongoMappers.Map(backtest));
|
||||
}
|
||||
}
|
||||
105
src/Managing.Infrastructure.Database/CandleRepository.cs
Normal file
105
src/Managing.Infrastructure.Database/CandleRepository.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using InfluxDB.Client.Writes;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Core;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Infrastructure.Databases.InfluxDb;
|
||||
using Managing.Infrastructure.Databases.InfluxDb.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class CandleRepository : ICandleRepository
|
||||
{
|
||||
private readonly string _priceBucket = "prices-bucket";
|
||||
private readonly IInfluxDbRepository _influxDbRepository;
|
||||
private readonly ILogger<CandleRepository> _logger;
|
||||
|
||||
public CandleRepository(IInfluxDbRepository influxDbRepository, ILogger<CandleRepository> logger)
|
||||
{
|
||||
_influxDbRepository = influxDbRepository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<IList<Candle>> GetCandles(
|
||||
TradingExchanges exchange,
|
||||
Ticker ticker,
|
||||
Timeframe timeframe,
|
||||
DateTime start)
|
||||
{
|
||||
var results = await _influxDbRepository.QueryAsync(async query =>
|
||||
{
|
||||
var flux = $"from(bucket:\"{_priceBucket}\") " +
|
||||
$"|> range(start: {start:s}Z) " +
|
||||
$"|> filter(fn: (r) => r[\"exchange\"] == \"{exchange}\")" +
|
||||
$"|> filter(fn: (r) => r[\"ticker\"] == \"{ticker}\")" +
|
||||
$"|> filter(fn: (r) => r[\"timeframe\"] == \"{timeframe}\")" +
|
||||
$"|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";
|
||||
|
||||
var prices = await query.QueryAsync<PriceDto>(flux, _influxDbRepository.Organization);
|
||||
return prices.Select(price => PriceHelpers.Map(price)).ToList();
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<IList<Ticker>> GetTickersAsync(
|
||||
TradingExchanges exchange,
|
||||
Timeframe timeframe,
|
||||
DateTime start)
|
||||
{
|
||||
var results = await _influxDbRepository.QueryAsync(async query =>
|
||||
{
|
||||
var flux = $"from(bucket:\"{_priceBucket}\") " +
|
||||
$"|> range(start: {start:s}Z, stop: now()) " +
|
||||
$"|> filter(fn: (r) => r[\"_measurement\"] == \"price\")" +
|
||||
$"|> filter(fn: (r) => r[\"exchange\"] == \"{exchange}\")" +
|
||||
$"|> filter(fn: (r) => r[\"timeframe\"] == \"{timeframe}\")" +
|
||||
$"|> keep(columns: [\"ticker\"])" +
|
||||
$"|> distinct()";
|
||||
|
||||
var tickers = new List<Ticker>();
|
||||
var records = await query.QueryAsync(flux, _influxDbRepository.Organization);
|
||||
records.ForEach(table =>
|
||||
{
|
||||
var fluxRecords = table.Records;
|
||||
fluxRecords.ForEach(fluxRecord =>
|
||||
{
|
||||
tickers.AddItem(MiscExtensions.ParseEnum<Ticker>(fluxRecord.GetValueByKey("ticker").ToString()));
|
||||
});
|
||||
});
|
||||
|
||||
return tickers;
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public void InsertCandle(Candle candle)
|
||||
{
|
||||
_influxDbRepository.Write(write =>
|
||||
{
|
||||
PriceDto price = PriceHelpers.Map(candle);
|
||||
write.WriteMeasurement(
|
||||
price,
|
||||
InfluxDB.Client.Api.Domain.WritePrecision.Ns,
|
||||
_priceBucket,
|
||||
_influxDbRepository.Organization);
|
||||
});
|
||||
}
|
||||
|
||||
public void Test(Candle candle)
|
||||
{
|
||||
_influxDbRepository.Write(write =>
|
||||
{
|
||||
var point = PointData.Measurement("");
|
||||
PriceDto price = PriceHelpers.Map(candle);
|
||||
point.Tag("", "");
|
||||
point.Timestamp(price.OpenTime, InfluxDB.Client.Api.Domain.WritePrecision.Ns);
|
||||
write.WritePoint(
|
||||
point,
|
||||
_priceBucket,
|
||||
_influxDbRepository.Organization);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using InfluxDB.Client;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public interface IInfluxDbRepository
|
||||
{
|
||||
string Organization { get; }
|
||||
|
||||
Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action);
|
||||
void Write(Action<WriteApi> action);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Managing.Infrastructure.Databases.Abstractions;
|
||||
|
||||
public interface IInfluxDbSettings
|
||||
{
|
||||
string Url { get; set; }
|
||||
string Token { get; set; }
|
||||
string Organization { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using InfluxDB.Client;
|
||||
using Managing.Infrastructure.Databases.Abstractions;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.InfluxDb;
|
||||
|
||||
public class InfluxDbRepository : IInfluxDbRepository
|
||||
{
|
||||
private readonly string _token;
|
||||
private readonly string _url;
|
||||
public string Organization { get; set; }
|
||||
|
||||
public InfluxDbRepository(IInfluxDbSettings settings)
|
||||
{
|
||||
_token = settings.Token;
|
||||
_url = settings.Url;
|
||||
Organization = settings.Organization;
|
||||
}
|
||||
|
||||
public void Write(Action<WriteApi> action)
|
||||
{
|
||||
using var client = InfluxDBClientFactory.Create(_url, _token);
|
||||
using var write = client.GetWriteApi();
|
||||
action(write);
|
||||
}
|
||||
|
||||
public async Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action)
|
||||
{
|
||||
using var client = InfluxDBClientFactory.Create(_url, _token);
|
||||
var query = client.GetQueryApi();
|
||||
return await action(query);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Managing.Infrastructure.Databases.Abstractions;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.InfluxDb.Models;
|
||||
|
||||
public class InfluxDbSettings : IInfluxDbSettings
|
||||
{
|
||||
public string Url { get ; set; }
|
||||
public string Token { get; set; }
|
||||
public string Organization { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using InfluxDB.Client.Core;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.InfluxDb.Models;
|
||||
|
||||
[Measurement("price")]
|
||||
public class PriceDto
|
||||
{
|
||||
[Column("exchange", IsTag = true)]
|
||||
public string Exchange { get; set; }
|
||||
[Column("ticker", IsTag = true)]
|
||||
public string Ticker { get; set; }
|
||||
[Column("timeframe", IsTag = true)]
|
||||
public string Timeframe { get; set; }
|
||||
[Column("openTime", IsTimestamp = true)]
|
||||
public DateTime OpenTime { get; set; }
|
||||
[Column("closeTime", IsTimestamp = true)]
|
||||
public DateTime CloseTime { get; set; }
|
||||
[Column("open")]
|
||||
public decimal Open { get; set; }
|
||||
[Column("close")]
|
||||
public decimal Close { get; set; }
|
||||
[Column("high")]
|
||||
public decimal High { get; set; }
|
||||
[Column("low")]
|
||||
public decimal Low { get; set; }
|
||||
[Column("baseVolume")]
|
||||
public decimal BaseVolume { get; set; }
|
||||
[Column("quoteVolume")]
|
||||
public decimal QuoteVolume { get; set; }
|
||||
[Column("TradeCount")]
|
||||
public int TradeCount { get; set; }
|
||||
[Column("takerBuyBaseVolume")]
|
||||
public decimal TakerBuyBaseVolume { get; set; }
|
||||
[Column("takerBuyQuoteVolume")]
|
||||
public decimal TakerBuyQuoteVolume { get; set; }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using Managing.Core;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Infrastructure.Databases.InfluxDb.Models;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.InfluxDb;
|
||||
|
||||
public static class PriceHelpers
|
||||
{
|
||||
public static PriceDto Map(Candle candle)
|
||||
{
|
||||
var price = new PriceDto
|
||||
{
|
||||
Exchange = candle.Exchange.ToString(),
|
||||
Ticker = candle.Ticker,
|
||||
OpenTime = candle.OpenTime.ToUniversalTime(),
|
||||
Open = candle.Open,
|
||||
Close = candle.Close,
|
||||
CloseTime = candle.Date.ToUniversalTime(),
|
||||
High = candle.High,
|
||||
Low = candle.Low,
|
||||
BaseVolume = candle.BaseVolume,
|
||||
QuoteVolume = candle.QuoteVolume,
|
||||
TradeCount = candle.TradeCount,
|
||||
TakerBuyBaseVolume = candle.TakerBuyBaseVolume,
|
||||
TakerBuyQuoteVolume = candle.TakerBuyQuoteVolume,
|
||||
Timeframe = candle.Timeframe.ToString()
|
||||
};
|
||||
|
||||
return price;
|
||||
}
|
||||
|
||||
public static Candle Map(PriceDto dto)
|
||||
{
|
||||
return new Candle
|
||||
{
|
||||
Exchange = MiscExtensions.ParseEnum<TradingExchanges>(dto.Exchange),
|
||||
Ticker = dto.Ticker,
|
||||
OpenTime = dto.OpenTime,
|
||||
Open = dto.Open,
|
||||
Close = dto.Close,
|
||||
Date = dto.CloseTime,
|
||||
High = dto.High,
|
||||
Low = dto.Low,
|
||||
BaseVolume = dto.BaseVolume,
|
||||
QuoteVolume = dto.QuoteVolume,
|
||||
TradeCount = dto.TradeCount,
|
||||
TakerBuyBaseVolume = dto.TakerBuyBaseVolume,
|
||||
TakerBuyQuoteVolume = dto.TakerBuyQuoteVolume,
|
||||
Timeframe = MiscExtensions.ParseEnum<Timeframe>(dto.Timeframe)
|
||||
};
|
||||
}
|
||||
|
||||
internal static IEnumerable<PriceDto> Map(IEnumerable<Candle> candles)
|
||||
{
|
||||
return candles.Select(candle => Map(candle));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="InfluxDB.Client" Version="4.13.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||
<PackageReference Include="MongoDB.Bson" Version="2.21.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Managing.Application.Workers\Managing.Application.Workers.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,57 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using MongoDB.Driver;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Abstractions
|
||||
{
|
||||
public interface IMongoRepository<TDocument> where TDocument : IDocument
|
||||
{
|
||||
IQueryable<TDocument> AsQueryable();
|
||||
|
||||
IEnumerable<TDocument> FilterBy(
|
||||
Expression<Func<TDocument, bool>> filterExpression);
|
||||
|
||||
IEnumerable<TDocument> FindAll();
|
||||
IEnumerable<TDocument> FilterBy(FilterDefinition<TDocument> filter);
|
||||
|
||||
IEnumerable<TProjected> FilterBy<TProjected>(
|
||||
Expression<Func<TDocument, bool>> filterExpression,
|
||||
Expression<Func<TDocument, TProjected>> projectionExpression);
|
||||
|
||||
TDocument FindOne(Expression<Func<TDocument, bool>> filterExpression);
|
||||
|
||||
Task<TDocument> FindOneAsync(Expression<Func<TDocument, bool>> filterExpression);
|
||||
|
||||
TDocument FindById(string id);
|
||||
|
||||
Task<TDocument> FindByIdAsync(string id);
|
||||
|
||||
void InsertOne(TDocument document);
|
||||
|
||||
Task InsertOneAsync(TDocument document);
|
||||
|
||||
void InsertMany(ICollection<TDocument> documents);
|
||||
|
||||
Task InsertManyAsync(ICollection<TDocument> documents);
|
||||
|
||||
void ReplaceOne(TDocument document);
|
||||
|
||||
Task ReplaceOneAsync(TDocument document);
|
||||
|
||||
void DeleteOne(Expression<Func<TDocument, bool>> filterExpression);
|
||||
|
||||
Task DeleteOneAsync(Expression<Func<TDocument, bool>> filterExpression);
|
||||
|
||||
void DeleteById(string id);
|
||||
|
||||
Task DeleteByIdAsync(string id);
|
||||
|
||||
void DeleteMany(Expression<Func<TDocument, bool>> filterExpression);
|
||||
|
||||
Task DeleteManyAsync(Expression<Func<TDocument, bool>> filterExpression);
|
||||
|
||||
void Update(TDocument entity);
|
||||
void CreateIndex(string column);
|
||||
void DropCollection();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
public class BsonCollectionAttribute : Attribute
|
||||
{
|
||||
public string CollectionName { get; }
|
||||
|
||||
public BsonCollectionAttribute(string collectionName)
|
||||
{
|
||||
CollectionName = collectionName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("Accounts")]
|
||||
public class AccountDto : Document
|
||||
{
|
||||
public UserDto User { get; set; }
|
||||
public string Name { get; set; }
|
||||
public TradingExchanges Exchanges { get; set; }
|
||||
public AccountType Type { get; set; }
|
||||
public string Key { get; set; }
|
||||
public string Secret { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("Backtests")]
|
||||
public class BacktestDto : Document
|
||||
{
|
||||
public decimal FinalPnl { get; set; }
|
||||
public int WinRate { get; set; }
|
||||
public decimal GrowthPercentage { get; set; }
|
||||
public decimal HodlPercentage { get; set; }
|
||||
public Ticker Ticker { get; set; }
|
||||
public string Scenario { get; set; }
|
||||
public List<PositionDto> Positions { get; set; }
|
||||
public List<SignalDto> Signals { get; set; }
|
||||
public Timeframe Timeframe { get; set; }
|
||||
public RiskLevel RiskLevel { get; set; }
|
||||
public string AccountName { get; set; }
|
||||
public List<CandleDto> Candles { get; set; }
|
||||
public BotType BotType { get; set; }
|
||||
public MoneyManagementDto MoneyManagement { get; internal set; }
|
||||
public MoneyManagementDto OptimizedMoneyManagement { get; internal set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
|
||||
[BsonCollection("BadTrader")]
|
||||
public class BadTraderDto : Document
|
||||
{
|
||||
public string Address { get; set; }
|
||||
public int Winrate { get; set; }
|
||||
public decimal Pnl { get; set; }
|
||||
public int TradeCount { get; set; }
|
||||
public decimal AverageWin { get; set; }
|
||||
public decimal AverageLoss { get; set; }
|
||||
public decimal Roi { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("BestTrader")]
|
||||
public class BestTraderDto : Document
|
||||
{
|
||||
public string Address { get; set; }
|
||||
public int Winrate { get; set; }
|
||||
public decimal Pnl { get; set; }
|
||||
public int TradeCount { get; set; }
|
||||
public decimal AverageWin { get; set; }
|
||||
public decimal AverageLoss { get; set; }
|
||||
public decimal Roi { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("Candles")]
|
||||
public class CandleDto : Document
|
||||
{
|
||||
public TradingExchanges Exchange { get; set; }
|
||||
public Timeframe Timeframe { get; set; }
|
||||
public string Ticker { get; set; }
|
||||
[BsonDateTimeOptions]
|
||||
public DateTime OpenTime { get; set; }
|
||||
[BsonDateTimeOptions]
|
||||
public DateTime CloseTime { get; set; }
|
||||
public decimal Open { get; set; }
|
||||
public decimal Close { get; set; }
|
||||
public decimal High { get; set; }
|
||||
public decimal Low { get; set; }
|
||||
public decimal BaseVolume { get; set; }
|
||||
public decimal QuoteVolume { get; set; }
|
||||
public int TradeCount { get; set; }
|
||||
public decimal TakerBuyBaseVolume { get; set; }
|
||||
public decimal TakerBuyQuoteVolume { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("Fees")]
|
||||
public class FeeDto : Document
|
||||
{
|
||||
public decimal Cost { get; set; }
|
||||
public TradingExchanges Exchange { get; set; }
|
||||
public DateTime LastUpdate { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("MoneyManagement")]
|
||||
public class MoneyManagementDto : Document
|
||||
{
|
||||
public Timeframe Timeframe { get; set; }
|
||||
public RiskLevel RiskLevel { get; set; }
|
||||
public decimal BalanceAtRisk { get; set; }
|
||||
public decimal StopLoss { get; set; }
|
||||
public decimal TakeProfit { get; set; }
|
||||
public decimal Leverage { get; set; }
|
||||
public string Name { get; internal set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("Positions")]
|
||||
public class PositionDto : Document
|
||||
{
|
||||
[BsonDateTimeOptions]
|
||||
public DateTime Date { get; set; }
|
||||
public TradeDto Open { get; set; }
|
||||
public TradeDto StopLoss { get; set; }
|
||||
public TradeDto TakeProfit1 { get; set; }
|
||||
public TradeDto TakeProfit2 { get; set; }
|
||||
public decimal ProfitAndLoss { get; set; }
|
||||
public TradeDirection OriginDirection { get; set; }
|
||||
public string Identifier { get; set; }
|
||||
public PositionStatus Status { get; set; }
|
||||
public Ticker Ticker { get; set; }
|
||||
public string SignalIdentifier { get; set; }
|
||||
public string AccountName { get; set; }
|
||||
public MoneyManagementDto MoneyManagement { get; set; }
|
||||
public PositionInitiator Initiator { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("Scenarios")]
|
||||
public class ScenarioDto : Document
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public List<StrategyDto> Strategies { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("Signals")]
|
||||
public class SignalDto : Document
|
||||
{
|
||||
public TradeDirection Direction { get; set; }
|
||||
public Confidence Confidence { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public CandleDto Candle { get; set; }
|
||||
public string Identifier { get; set; }
|
||||
public Ticker Ticker { get; set; }
|
||||
public SignalStatus Status { get; set; }
|
||||
public Timeframe Timeframe { get; set; }
|
||||
public StrategyType Type { get; set; }
|
||||
public SignalType SignalType { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("SpotlighOverview")]
|
||||
public class SpotlighOverviewDto : Document
|
||||
{
|
||||
public List<SpotlightDto> Spotlights { get; set; }
|
||||
public DateTime DateTime { get; set; }
|
||||
public Guid Identifier { get; set; }
|
||||
public int ScenarioCount { get; set; }
|
||||
}
|
||||
|
||||
public class SpotlightDto
|
||||
{
|
||||
public ScenarioDto Scenario { get; set; }
|
||||
public List<TickerSignalDto> TickerSignals { get; set; }
|
||||
}
|
||||
|
||||
public class TickerSignalDto
|
||||
{
|
||||
public Ticker Ticker { get; set; }
|
||||
public List<SignalDto> FiveMinutes { get; set; }
|
||||
public List<SignalDto> FifteenMinutes { get; set; }
|
||||
public List<SignalDto> OneHour { get; set; }
|
||||
public List<SignalDto> FourHour { get; set; }
|
||||
public List<SignalDto> OneDay { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("Strategies")]
|
||||
public class StrategyDto : Document
|
||||
{
|
||||
public StrategyType Type { get; set; }
|
||||
public Timeframe Timeframe { get; set; }
|
||||
public string Name { 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; }
|
||||
public SignalType SignalType { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("TopVolumeTickers")]
|
||||
public class TopVolumeTickerDto : Document
|
||||
{
|
||||
public Ticker Ticker { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public decimal Volume { get; set; }
|
||||
public int Rank { get; set; }
|
||||
public TradingExchanges Exchange { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections
|
||||
{
|
||||
[BsonCollection("Trades")]
|
||||
public class TradeDto : Document
|
||||
{
|
||||
[BsonDateTimeOptions]
|
||||
public DateTime Date { get; set; }
|
||||
public TradeDirection Direction { get; set; }
|
||||
public TradeStatus Status { get; set; }
|
||||
public TradeType TradeType { get; set; }
|
||||
public Ticker Ticker { get; set; }
|
||||
public decimal Fee { get; set; }
|
||||
public decimal Quantity { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public decimal Leverage { get; set; }
|
||||
public string ExchangeOrderId { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("Users")]
|
||||
public class UserDto : Document
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("Workers")]
|
||||
public class WorkerDto : Document
|
||||
{
|
||||
public WorkerType WorkerType { get; set; }
|
||||
public DateTime StartTime { get; set; }
|
||||
public DateTime? LastRunTime { get; set; }
|
||||
public int ExecutionCount { get; set; }
|
||||
public TimeSpan Delay { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Managing.Domain.Workflows.Synthetics;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("Workflow")]
|
||||
public class WorkflowDto : Document
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public WorkflowUsage Usage { get; set; }
|
||||
public List<SyntheticFlow> Flows { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Configurations
|
||||
{
|
||||
public abstract class Document : IDocument
|
||||
{
|
||||
[BsonId]
|
||||
public ObjectId Id { get; set; }
|
||||
[BsonDateTimeOptions]
|
||||
public DateTime CreatedAt => Id.CreationTime;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Configurations
|
||||
{
|
||||
public interface IDocument
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.String)]
|
||||
ObjectId Id { get; set; }
|
||||
|
||||
DateTime CreatedAt { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Managing.Infrastructure.Databases.MongoDb
|
||||
{
|
||||
public interface IManagingDatabaseSettings
|
||||
{
|
||||
string ConnectionString { get; set; }
|
||||
string DatabaseName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Managing.Infrastructure.Databases.MongoDb;
|
||||
|
||||
public class ManagingDatabaseSettings : IManagingDatabaseSettings
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
public string DatabaseName { get; set; }
|
||||
}
|
||||
20
src/Managing.Infrastructure.Database/MongoDb/MongoHelpers.cs
Normal file
20
src/Managing.Infrastructure.Database/MongoDb/MongoHelpers.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb
|
||||
{
|
||||
public static class MongoHelpers
|
||||
{
|
||||
public static async Task EnsureIndexExists(this IMongoDatabase database, string collectionName, string indexName)
|
||||
{
|
||||
var collection = database.GetCollection<BsonDocument>(collectionName);
|
||||
var index = new BsonDocument
|
||||
{
|
||||
{indexName, 1}
|
||||
};
|
||||
|
||||
var indexModel = new CreateIndexModel<BsonDocument>(index, new CreateIndexOptions { Unique = true });
|
||||
await collection.Indexes.CreateOneAsync(indexModel).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
654
src/Managing.Infrastructure.Database/MongoDb/MongoMappers.cs
Normal file
654
src/Managing.Infrastructure.Database/MongoDb/MongoMappers.cs
Normal file
@@ -0,0 +1,654 @@
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Statistics;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Domain.Workers;
|
||||
using Managing.Domain.Workflows.Synthetics;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb;
|
||||
|
||||
public static class MongoMappers
|
||||
{
|
||||
#region Statistics
|
||||
internal static TopVolumeTickerDto Map(TopVolumeTicker topVolumeTicker)
|
||||
{
|
||||
return new TopVolumeTickerDto
|
||||
{
|
||||
Ticker = topVolumeTicker.Ticker,
|
||||
Date = topVolumeTicker.Date,
|
||||
Volume = topVolumeTicker.Volume,
|
||||
Rank = topVolumeTicker.Rank,
|
||||
Exchange = topVolumeTicker.Exchange
|
||||
};
|
||||
}
|
||||
|
||||
internal static IList<TopVolumeTicker> Map(IEnumerable<TopVolumeTickerDto> top)
|
||||
{
|
||||
return top.Select(topElement => new TopVolumeTicker
|
||||
{
|
||||
Ticker = topElement.Ticker,
|
||||
Date = topElement.Date,
|
||||
Volume = topElement.Volume,
|
||||
Rank = topElement.Rank,
|
||||
Exchange = topElement.Exchange
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accounts
|
||||
internal static AccountDto Map(Account request)
|
||||
{
|
||||
return new AccountDto
|
||||
{
|
||||
Name = request.Name,
|
||||
Exchanges = request.Exchange,
|
||||
Key = request.Key,
|
||||
Secret = request.Secret,
|
||||
Type = request.Type,
|
||||
User = Map(request.User)
|
||||
};
|
||||
}
|
||||
|
||||
internal static IEnumerable<Account> Map(IEnumerable<AccountDto> accounts)
|
||||
{
|
||||
return accounts.Select(account => Map(account));
|
||||
}
|
||||
|
||||
internal static Account Map(AccountDto account, bool hideKeys = false)
|
||||
{
|
||||
if (account == null) return null;
|
||||
|
||||
var a = new Account
|
||||
{
|
||||
Name = account.Name,
|
||||
Exchange = account.Exchanges,
|
||||
};
|
||||
|
||||
if (!hideKeys)
|
||||
{
|
||||
a.Key = account.Key;
|
||||
a.Secret = account.Secret;
|
||||
}
|
||||
|
||||
a.Exchange = account.Exchanges;
|
||||
a.Type = account.Type;
|
||||
a.User = Map(account.User);
|
||||
|
||||
return a;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Workers
|
||||
internal static WorkerDto Map(Worker worker)
|
||||
{
|
||||
return new WorkerDto
|
||||
{
|
||||
WorkerType = worker.WorkerType,
|
||||
StartTime = worker.StartTime,
|
||||
LastRunTime = worker.LastRunTime,
|
||||
ExecutionCount = worker.ExecutionCount,
|
||||
Delay = worker.Delay
|
||||
};
|
||||
}
|
||||
|
||||
internal static Worker Map(WorkerDto worker)
|
||||
{
|
||||
if (worker == null)
|
||||
return null;
|
||||
|
||||
return new Worker
|
||||
{
|
||||
WorkerType = worker.WorkerType,
|
||||
StartTime = worker.StartTime,
|
||||
LastRunTime = worker.LastRunTime,
|
||||
ExecutionCount = worker.ExecutionCount,
|
||||
Delay = worker.Delay,
|
||||
IsActive = worker.IsActive
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Backtests
|
||||
internal static Backtest Map(BacktestDto b)
|
||||
{
|
||||
return new Backtest(
|
||||
ticker: b.Ticker,
|
||||
scenario: b.Scenario,
|
||||
positions: b.Positions.ConvertAll(bPosition => Map(bPosition)),
|
||||
signals: b.Signals != null ? b.Signals.ConvertAll(bSignal => Map(bSignal)) : null,
|
||||
timeframe: b.Timeframe,
|
||||
candles: b.Candles.ConvertAll(bCandle => Map(bCandle)),
|
||||
accountName: b.AccountName,
|
||||
botType: b.BotType)
|
||||
{
|
||||
Id = b.Id.ToString(),
|
||||
FinalPnl = b.FinalPnl,
|
||||
WinRate = b.WinRate,
|
||||
GrowthPercentage = b.GrowthPercentage,
|
||||
HodlPercentage = b.HodlPercentage,
|
||||
MoneyManagement = Map(b.MoneyManagement),
|
||||
OptimizedMoneyManagement = Map(b.OptimizedMoneyManagement)
|
||||
};
|
||||
}
|
||||
|
||||
internal static BacktestDto Map(Backtest result)
|
||||
{
|
||||
var backtest = new BacktestDto
|
||||
{
|
||||
FinalPnl = result.FinalPnl,
|
||||
WinRate = result.WinRate,
|
||||
GrowthPercentage = result.GrowthPercentage,
|
||||
HodlPercentage = result.HodlPercentage,
|
||||
Candles = Map(result.Candles),
|
||||
Positions = Map(result.Positions),
|
||||
AccountName = result.AccountName,
|
||||
BotType = result.BotType,
|
||||
MoneyManagement = Map(result.MoneyManagement),
|
||||
OptimizedMoneyManagement = Map(result.OptimizedMoneyManagement),
|
||||
};
|
||||
|
||||
backtest.Timeframe = result.Timeframe;
|
||||
backtest.Ticker = result.Ticker;
|
||||
backtest.Scenario = result.Scenario;
|
||||
|
||||
return backtest;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Candles
|
||||
public static Candle Map(CandleDto candle)
|
||||
{
|
||||
if (candle == null)
|
||||
return null;
|
||||
|
||||
return new Candle()
|
||||
{
|
||||
Ticker = candle.Ticker,
|
||||
BaseVolume = candle.BaseVolume,
|
||||
Close = candle.Close,
|
||||
Date = candle.OpenTime,
|
||||
Open = candle.Open,
|
||||
OpenTime = candle.OpenTime,
|
||||
High = candle.High,
|
||||
Low = candle.Low,
|
||||
QuoteVolume = candle.QuoteVolume,
|
||||
TakerBuyBaseVolume = candle.TakerBuyBaseVolume,
|
||||
TakerBuyQuoteVolume = candle.TakerBuyQuoteVolume,
|
||||
TradeCount = candle.TradeCount,
|
||||
Exchange = candle.Exchange,
|
||||
};
|
||||
}
|
||||
|
||||
public static CandleDto Map(Candle candle)
|
||||
{
|
||||
return new CandleDto
|
||||
{
|
||||
Exchange = candle.Exchange,
|
||||
Ticker = candle.Ticker,
|
||||
OpenTime = candle.OpenTime,
|
||||
Open = candle.Open,
|
||||
Close = candle.Close,
|
||||
High = candle.High,
|
||||
Low = candle.Low,
|
||||
BaseVolume = candle.BaseVolume,
|
||||
QuoteVolume = candle.QuoteVolume,
|
||||
TradeCount = candle.TradeCount,
|
||||
TakerBuyBaseVolume = candle.TakerBuyBaseVolume,
|
||||
TakerBuyQuoteVolume = candle.TakerBuyQuoteVolume
|
||||
};
|
||||
}
|
||||
|
||||
public static List<CandleDto> Map(List<Candle> candles)
|
||||
{
|
||||
return candles.ConvertAll(candle => Map(candle));
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Positions
|
||||
public static PositionDto Map(Position position)
|
||||
{
|
||||
var p = new PositionDto
|
||||
{
|
||||
Date = position.Date,
|
||||
Open = Map(position.Open),
|
||||
OriginDirection = position.OriginDirection,
|
||||
Identifier = position.Identifier,
|
||||
SignalIdentifier = position.SignalIdentifier,
|
||||
Status = position.Status,
|
||||
AccountName = position.AccountName,
|
||||
MoneyManagement = Map(position.MoneyManagement),
|
||||
Initiator = position.Initiator,
|
||||
Ticker = position.Ticker
|
||||
};
|
||||
|
||||
if (position.StopLoss != null)
|
||||
p.StopLoss = Map(position.StopLoss);
|
||||
|
||||
if (position.TakeProfit1 != null)
|
||||
p.TakeProfit1 = Map(position.TakeProfit1);
|
||||
|
||||
if (position.TakeProfit2 != null)
|
||||
p.TakeProfit2 = Map(position.TakeProfit2);
|
||||
|
||||
if (position.ProfitAndLoss != null)
|
||||
p.ProfitAndLoss = position.ProfitAndLoss.Realized;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
private static TradeDto Map(Trade trade)
|
||||
{
|
||||
return new TradeDto
|
||||
{
|
||||
Date = trade.Date,
|
||||
Direction = trade.Direction,
|
||||
Status = trade.Status,
|
||||
TradeType = trade.TradeType,
|
||||
Ticker = trade.Ticker,
|
||||
Quantity = trade.Quantity,
|
||||
Price = trade.Price,
|
||||
Leverage = trade.Leverage,
|
||||
ExchangeOrderId = trade.ExchangeOrderId,
|
||||
Message = trade.Message
|
||||
};
|
||||
}
|
||||
|
||||
public static Position Map(PositionDto dto)
|
||||
{
|
||||
var position = new Position(dto.AccountName, originDirection: dto.OriginDirection, dto.Ticker, Map(dto.MoneyManagement), dto.Initiator, dto.Date)
|
||||
{
|
||||
Open = new Trade(date: dto.Open.Date, direction: dto.Open.Direction, status: dto.Open.Status, tradeType: dto.Open.TradeType, ticker: dto.Open.Ticker, quantity: dto.Open.Quantity, price: dto.Open.Price, leverage: dto.Open.Leverage, exchangeOrderId: dto.Open.ExchangeOrderId, message: dto.Open.Message),
|
||||
ProfitAndLoss = new ProfitAndLoss { Realized = dto.ProfitAndLoss },
|
||||
Status = dto.Status,
|
||||
SignalIdentifier = dto.SignalIdentifier,
|
||||
Identifier = dto.Identifier
|
||||
};
|
||||
|
||||
if (dto.StopLoss != null)
|
||||
{
|
||||
position.StopLoss = new Trade(date: dto.StopLoss.Date, direction: dto.StopLoss.Direction, status: dto.StopLoss.Status, tradeType: dto.StopLoss.TradeType, ticker: dto.StopLoss.Ticker, quantity: dto.StopLoss.Quantity, price: dto.StopLoss.Price, leverage: dto.StopLoss.Leverage, exchangeOrderId: dto.StopLoss.ExchangeOrderId, message: dto.StopLoss.Message);
|
||||
}
|
||||
|
||||
if (dto.TakeProfit1 != null)
|
||||
{
|
||||
position.TakeProfit1 = new Trade(date: dto.TakeProfit1.Date, direction: dto.TakeProfit1.Direction, status: dto.TakeProfit1.Status, tradeType: dto.TakeProfit1.TradeType, ticker: dto.TakeProfit1.Ticker, quantity: dto.TakeProfit1.Quantity, price: dto.TakeProfit1.Price, leverage: dto.TakeProfit1.Leverage, exchangeOrderId: dto.TakeProfit1.ExchangeOrderId, message: dto.TakeProfit1.Message);
|
||||
}
|
||||
|
||||
if (dto.TakeProfit2 != null)
|
||||
{
|
||||
position.TakeProfit2 = new Trade(date: dto.TakeProfit2.Date, direction: dto.TakeProfit2.Direction, status: dto.TakeProfit2.Status, tradeType: dto.TakeProfit2.TradeType, ticker: dto.TakeProfit2.Ticker, quantity: dto.TakeProfit2.Quantity, price: dto.TakeProfit2.Price, leverage: dto.TakeProfit2.Leverage, exchangeOrderId: dto.TakeProfit2.ExchangeOrderId, message: dto.TakeProfit2.Message);
|
||||
}
|
||||
|
||||
return position;
|
||||
}
|
||||
|
||||
internal static List<PositionDto> Map(List<Position> positions)
|
||||
{
|
||||
return positions.ConvertAll(position => Map(position));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Signals
|
||||
public static SignalDto Map(Signal signal)
|
||||
{
|
||||
return new SignalDto
|
||||
{
|
||||
Identifier = signal.Identifier,
|
||||
Direction = signal.Direction,
|
||||
Candle = Map(signal.Candle),
|
||||
Confidence = signal.Confidence,
|
||||
Date = signal.Date,
|
||||
Ticker = signal.Ticker,
|
||||
Status = signal.Status,
|
||||
Timeframe = signal.Timeframe,
|
||||
Type = signal.StrategyType
|
||||
};
|
||||
}
|
||||
|
||||
internal static Signal Map(SignalDto bSignal)
|
||||
{
|
||||
return new Signal(ticker: bSignal.Ticker, direction: bSignal.Direction, confidence: bSignal.Confidence,
|
||||
candle: Map(bSignal.Candle), date: bSignal.Date, exchange: default, timeframe: bSignal.Timeframe,
|
||||
strategyType: bSignal.Type, signalType: bSignal.SignalType)
|
||||
{
|
||||
Status = bSignal.Status
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Scenarios
|
||||
public static ScenarioDto Map(Scenario scenario)
|
||||
{
|
||||
return new ScenarioDto()
|
||||
{
|
||||
Name = scenario.Name,
|
||||
Strategies = Map(scenario.Strategies),
|
||||
};
|
||||
}
|
||||
|
||||
internal static IEnumerable<Scenario> Map(IEnumerable<ScenarioDto> dtos)
|
||||
{
|
||||
return dtos.Select(d => Map(d));
|
||||
}
|
||||
|
||||
internal static Scenario Map(ScenarioDto d)
|
||||
{
|
||||
return new Scenario(d.Name)
|
||||
{
|
||||
Name = d.Name,
|
||||
Strategies = Map(d.Strategies).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
private static List<StrategyDto> Map(List<Strategy> strategies)
|
||||
{
|
||||
return strategies.ConvertAll(strategy => Map(strategy));
|
||||
}
|
||||
|
||||
internal static Strategy Map(StrategyDto strategyDto)
|
||||
{
|
||||
return new Strategy(name: strategyDto.Name, timeframe: strategyDto.Timeframe, type: strategyDto.Type)
|
||||
{
|
||||
Period = strategyDto.Period,
|
||||
FastPeriods = strategyDto.FastPeriods,
|
||||
SlowPeriods = strategyDto.SlowPeriods,
|
||||
SignalPeriods = strategyDto.SignalPeriods,
|
||||
Multiplier = strategyDto.Multiplier,
|
||||
SmoothPeriods = strategyDto.SmoothPeriods,
|
||||
StochPeriods = strategyDto.StochPeriods,
|
||||
CyclePeriods = strategyDto.CyclePeriods
|
||||
};
|
||||
}
|
||||
internal static StrategyDto Map(Strategy strategy)
|
||||
{
|
||||
var dto = new StrategyDto
|
||||
{
|
||||
Type = strategy.Type,
|
||||
Timeframe = strategy.Timeframe,
|
||||
Name = strategy.Name,
|
||||
SignalType = strategy.SignalType
|
||||
};
|
||||
|
||||
switch (strategy.Type)
|
||||
{
|
||||
case StrategyType.RsiDivergenceConfirm:
|
||||
case StrategyType.RsiDivergence:
|
||||
case StrategyType.EmaCross:
|
||||
case StrategyType.EmaTrend:
|
||||
case StrategyType.StDev:
|
||||
dto.Period = strategy.Period;
|
||||
break;
|
||||
case StrategyType.MacdCross:
|
||||
dto.SlowPeriods = strategy.SlowPeriods;
|
||||
dto.FastPeriods = strategy.FastPeriods;
|
||||
dto.SignalPeriods = strategy.SignalPeriods;
|
||||
break;
|
||||
case StrategyType.ThreeWhiteSoldiers:
|
||||
break;
|
||||
case StrategyType.ChandelierExit:
|
||||
case StrategyType.SuperTrend:
|
||||
dto.Period = strategy.Period;
|
||||
dto.Multiplier = strategy.Multiplier;
|
||||
break;
|
||||
case StrategyType.StochRsiTrend:
|
||||
dto.Period = strategy.Period;
|
||||
dto.StochPeriods = strategy.StochPeriods;
|
||||
dto.SignalPeriods = strategy.SignalPeriods;
|
||||
dto.SmoothPeriods = strategy.SmoothPeriods;
|
||||
break;
|
||||
case StrategyType.Stc:
|
||||
dto.SlowPeriods = strategy.SlowPeriods;
|
||||
dto.FastPeriods = strategy.FastPeriods;
|
||||
dto.CyclePeriods = strategy.CyclePeriods;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
internal static IEnumerable<Strategy> Map(IEnumerable<StrategyDto> strategies)
|
||||
{
|
||||
return strategies.Select(strategy => Map(strategy));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Money Management
|
||||
public static MoneyManagementDto Map(MoneyManagement request)
|
||||
{
|
||||
if (request == null) return null;
|
||||
return new MoneyManagementDto
|
||||
{
|
||||
Timeframe = request.Timeframe,
|
||||
BalanceAtRisk = request.BalanceAtRisk,
|
||||
StopLoss = request.StopLoss,
|
||||
TakeProfit = request.TakeProfit,
|
||||
Leverage = request.Leverage,
|
||||
Name = request.Name
|
||||
};
|
||||
}
|
||||
|
||||
public static MoneyManagement Map(MoneyManagementDto request)
|
||||
{
|
||||
if (request == null)
|
||||
return null;
|
||||
|
||||
return new MoneyManagement
|
||||
{
|
||||
Timeframe = request.Timeframe,
|
||||
BalanceAtRisk = request.BalanceAtRisk,
|
||||
StopLoss = request.StopLoss,
|
||||
TakeProfit = request.TakeProfit,
|
||||
Leverage = request.Leverage,
|
||||
Name = request.Name
|
||||
};
|
||||
}
|
||||
|
||||
internal static User Map(UserDto user)
|
||||
{
|
||||
if (user == null)
|
||||
return null;
|
||||
|
||||
return new User
|
||||
{
|
||||
Name = user.Name,
|
||||
};
|
||||
}
|
||||
|
||||
internal static UserDto Map(User user)
|
||||
{
|
||||
return new UserDto
|
||||
{
|
||||
Name = user.Name
|
||||
};
|
||||
}
|
||||
|
||||
internal static SpotlighOverviewDto Map(SpotlightOverview overview)
|
||||
{
|
||||
return new SpotlighOverviewDto
|
||||
{
|
||||
Spotlights = Map(overview.Spotlights),
|
||||
DateTime = overview.DateTime,
|
||||
Identifier = overview.Identifier,
|
||||
ScenarioCount = overview.ScenarioCount,
|
||||
};
|
||||
}
|
||||
|
||||
private static List<SpotlightDto> Map(List<Spotlight> spotlights)
|
||||
{
|
||||
return spotlights.ConvertAll(spotlight => new SpotlightDto
|
||||
{
|
||||
Scenario = new ScenarioDto
|
||||
{
|
||||
Name = spotlight.Scenario.Name,
|
||||
Strategies = spotlight.Scenario.Strategies.ConvertAll(spotlightScenarioStrategy => Map(spotlightScenarioStrategy))
|
||||
},
|
||||
TickerSignals = spotlight.TickerSignals.ConvertAll(spotlightTickerSignal => new TickerSignalDto
|
||||
{
|
||||
Ticker = spotlightTickerSignal.Ticker,
|
||||
FiveMinutes = spotlightTickerSignal.FiveMinutes?.ConvertAll(spotlightTickerSignalFiveMinute => Map(spotlightTickerSignalFiveMinute)) ?? new List<SignalDto>(),
|
||||
FifteenMinutes = spotlightTickerSignal.FifteenMinutes?.ConvertAll(spotlightTickerSignalFifteenMinute => Map(spotlightTickerSignalFifteenMinute)) ?? new List<SignalDto>(),
|
||||
OneHour = spotlightTickerSignal.OneHour?.ConvertAll(spotlightTickerSignalOneHour => Map(spotlightTickerSignalOneHour)) ?? new List<SignalDto>(),
|
||||
FourHour = spotlightTickerSignal.FourHour?.ConvertAll(spotlightTickerSignalFourHour => Map(spotlightTickerSignalFourHour)) ?? new List<SignalDto>(),
|
||||
OneDay = spotlightTickerSignal.OneDay?.ConvertAll(spotlightTickerSignalOneDay => Map(spotlightTickerSignalOneDay)) ?? new List<SignalDto>()
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
internal static SpotlightOverview Map(SpotlighOverviewDto overview)
|
||||
{
|
||||
return new SpotlightOverview
|
||||
{
|
||||
Spotlights = Map(overview.Spotlights),
|
||||
DateTime = overview.DateTime,
|
||||
Identifier = overview.Identifier,
|
||||
ScenarioCount = overview.ScenarioCount
|
||||
};
|
||||
}
|
||||
|
||||
private static List<Spotlight> Map(List<SpotlightDto> spotlights)
|
||||
{
|
||||
return spotlights.ConvertAll(spotlight => new Spotlight
|
||||
{
|
||||
Scenario = new Scenario(name: spotlight.Scenario.Name)
|
||||
{
|
||||
Strategies = spotlight.Scenario.Strategies.ConvertAll(spotlightScenarioStrategy => Map(spotlightScenarioStrategy))
|
||||
},
|
||||
TickerSignals = spotlight.TickerSignals.ConvertAll(spotlightTickerSignal => new TickerSignal
|
||||
{
|
||||
Ticker = spotlightTickerSignal.Ticker,
|
||||
FiveMinutes = spotlightTickerSignal.FiveMinutes.ConvertAll(spotlightTickerSignalFiveMinute => Map(spotlightTickerSignalFiveMinute)),
|
||||
FifteenMinutes = spotlightTickerSignal.FifteenMinutes.ConvertAll(spotlightTickerSignalFifteenMinute => Map(spotlightTickerSignalFifteenMinute)),
|
||||
OneHour = spotlightTickerSignal.OneHour.ConvertAll(spotlightTickerSignalOneHour => Map(spotlightTickerSignalOneHour)),
|
||||
FourHour = spotlightTickerSignal.FourHour.ConvertAll(spotlightTickerSignalFourHour => Map(spotlightTickerSignalFourHour)),
|
||||
OneDay = spotlightTickerSignal.OneDay.ConvertAll(spotlightTickerSignalOneDay => Map(spotlightTickerSignalOneDay))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
internal static IList<SpotlightOverview> Map(IEnumerable<SpotlighOverviewDto> overviews)
|
||||
{
|
||||
return overviews.Select(Map).ToList();
|
||||
}
|
||||
|
||||
|
||||
internal static FeeDto Map(Fee fee)
|
||||
{
|
||||
return new FeeDto
|
||||
{
|
||||
Cost = fee.Cost,
|
||||
Exchange = fee.Exchange,
|
||||
LastUpdate = fee.LastUpdate
|
||||
};
|
||||
}
|
||||
|
||||
internal static Fee Map(FeeDto fee)
|
||||
{
|
||||
if (fee == null) return null;
|
||||
|
||||
return new Fee
|
||||
{
|
||||
Cost = fee.Cost,
|
||||
Exchange = fee.Exchange,
|
||||
LastUpdate = fee.LastUpdate
|
||||
};
|
||||
}
|
||||
|
||||
internal static List<Trader> Map(IEnumerable<BestTraderDto> enumerable)
|
||||
{
|
||||
return enumerable.Select(enumerableElement => new Trader
|
||||
{
|
||||
Address = enumerableElement.Address,
|
||||
Winrate = enumerableElement.Winrate,
|
||||
Pnl = enumerableElement.Pnl,
|
||||
TradeCount = enumerableElement.TradeCount,
|
||||
AverageWin = enumerableElement.AverageWin,
|
||||
AverageLoss = enumerableElement.AverageLoss,
|
||||
Roi = enumerableElement.Roi
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
internal static BestTraderDto Map(Trader trader)
|
||||
{
|
||||
return new BestTraderDto
|
||||
{
|
||||
Address = trader.Address,
|
||||
Winrate = trader.Winrate,
|
||||
Pnl = trader.Pnl,
|
||||
TradeCount = trader.TradeCount,
|
||||
AverageWin = trader.AverageWin,
|
||||
AverageLoss = trader.AverageLoss,
|
||||
Roi = trader.Roi
|
||||
};
|
||||
}
|
||||
|
||||
internal static List<Trader> Map(IEnumerable<BadTraderDto> enumerable)
|
||||
{
|
||||
return enumerable.Select(enumerableElement => new Trader
|
||||
{
|
||||
Address = enumerableElement.Address,
|
||||
Winrate = enumerableElement.Winrate,
|
||||
Pnl = enumerableElement.Pnl,
|
||||
TradeCount = enumerableElement.TradeCount,
|
||||
AverageWin = enumerableElement.AverageWin,
|
||||
AverageLoss = enumerableElement.AverageLoss,
|
||||
Roi = enumerableElement.Roi
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
internal static BadTraderDto BadTraderMap(Trader trader)
|
||||
{
|
||||
return new BadTraderDto
|
||||
{
|
||||
Address = trader.Address,
|
||||
Winrate = trader.Winrate,
|
||||
Pnl = trader.Pnl,
|
||||
TradeCount = trader.TradeCount,
|
||||
AverageWin = trader.AverageWin,
|
||||
AverageLoss = trader.AverageLoss,
|
||||
Roi = trader.Roi
|
||||
};
|
||||
}
|
||||
|
||||
internal static WorkflowDto Map(SyntheticWorkflow workflow)
|
||||
{
|
||||
return new WorkflowDto
|
||||
{
|
||||
Name = workflow.Name,
|
||||
Description = workflow.Description,
|
||||
Usage = workflow.Usage,
|
||||
Flows = workflow.Flows
|
||||
};
|
||||
}
|
||||
|
||||
internal static SyntheticWorkflow Map(WorkflowDto m)
|
||||
{
|
||||
if (m == null) return null;
|
||||
|
||||
return new SyntheticWorkflow
|
||||
{
|
||||
Name = m.Name,
|
||||
Usage = m.Usage,
|
||||
Description = m.Description,
|
||||
Flows = m.Flows.ToList(),
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
178
src/Managing.Infrastructure.Database/MongoDb/MongoRepository.cs
Normal file
178
src/Managing.Infrastructure.Database/MongoDb/MongoRepository.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb
|
||||
{
|
||||
public class MongoRepository<TDocument> : IMongoRepository<TDocument>
|
||||
where TDocument : IDocument
|
||||
{
|
||||
private readonly IMongoCollection<TDocument> _collection;
|
||||
private readonly IMongoDatabase _database;
|
||||
|
||||
public MongoRepository(IManagingDatabaseSettings settings)
|
||||
{
|
||||
_database = new MongoClient(settings.ConnectionString).GetDatabase(settings.DatabaseName);
|
||||
_collection = _database.GetCollection<TDocument>(GetCollectionName(typeof(TDocument)));
|
||||
}
|
||||
|
||||
private protected string GetCollectionName(Type documentType)
|
||||
{
|
||||
return ((BsonCollectionAttribute)documentType.GetCustomAttributes(
|
||||
typeof(BsonCollectionAttribute),
|
||||
true)
|
||||
.FirstOrDefault())?.CollectionName;
|
||||
}
|
||||
|
||||
public virtual IQueryable<TDocument> AsQueryable()
|
||||
{
|
||||
return _collection.AsQueryable();
|
||||
}
|
||||
|
||||
public virtual IEnumerable<TDocument> FilterBy(
|
||||
Expression<Func<TDocument, bool>> filterExpression)
|
||||
{
|
||||
return _collection.Find(filterExpression).ToEnumerable();
|
||||
}
|
||||
|
||||
public virtual IEnumerable<TDocument> FilterBy(FilterDefinition<TDocument> filter)
|
||||
{
|
||||
return _collection.Find(filter).ToEnumerable();
|
||||
}
|
||||
|
||||
public virtual IEnumerable<TDocument> FindAll()
|
||||
{
|
||||
return _collection.Find(_ => true).ToEnumerable();
|
||||
}
|
||||
|
||||
public virtual IEnumerable<TProjected> FilterBy<TProjected>(
|
||||
Expression<Func<TDocument, bool>> filterExpression,
|
||||
Expression<Func<TDocument, TProjected>> projectionExpression)
|
||||
{
|
||||
return _collection.Find(filterExpression).Project(projectionExpression).ToEnumerable();
|
||||
}
|
||||
|
||||
public virtual TDocument FindOne(Expression<Func<TDocument, bool>> filterExpression)
|
||||
{
|
||||
return _collection.Find(filterExpression).FirstOrDefault();
|
||||
}
|
||||
|
||||
public virtual Task<TDocument> FindOneAsync(Expression<Func<TDocument, bool>> filterExpression)
|
||||
{
|
||||
return Task.Run(() => _collection.Find(filterExpression).FirstOrDefaultAsync());
|
||||
}
|
||||
|
||||
public virtual TDocument FindById(string id)
|
||||
{
|
||||
var objectId = new ObjectId(id);
|
||||
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId);
|
||||
return _collection.Find(filter).SingleOrDefault();
|
||||
}
|
||||
|
||||
public virtual Task<TDocument> FindByIdAsync(string id)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var objectId = new ObjectId(id);
|
||||
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId);
|
||||
return _collection.Find(filter).SingleOrDefaultAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public virtual void InsertOne(TDocument document)
|
||||
{
|
||||
_collection.InsertOne(document);
|
||||
}
|
||||
|
||||
public virtual Task InsertOneAsync(TDocument document)
|
||||
{
|
||||
return Task.Run(() => _collection.InsertOneAsync(document));
|
||||
}
|
||||
|
||||
public void InsertMany(ICollection<TDocument> documents)
|
||||
{
|
||||
_collection.InsertMany(documents);
|
||||
}
|
||||
|
||||
public void DropCollection()
|
||||
{
|
||||
_database.DropCollection(GetCollectionName(typeof(TDocument)));
|
||||
}
|
||||
|
||||
public virtual async Task InsertManyAsync(ICollection<TDocument> documents)
|
||||
{
|
||||
await _collection.InsertManyAsync(documents);
|
||||
}
|
||||
|
||||
public void ReplaceOne(TDocument document)
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, document.Id);
|
||||
_collection.FindOneAndReplace(filter, document);
|
||||
}
|
||||
|
||||
public virtual async Task ReplaceOneAsync(TDocument document)
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, document.Id);
|
||||
await _collection.FindOneAndReplaceAsync(filter, document);
|
||||
}
|
||||
|
||||
public virtual void Update(TDocument entity)
|
||||
{
|
||||
if (entity.Id == ObjectId.Empty)
|
||||
{
|
||||
entity.Id = ObjectId.GenerateNewId();
|
||||
}
|
||||
|
||||
var option = new ReplaceOptions { IsUpsert = true };
|
||||
_collection.ReplaceOne(x => entity != null && x.Id == entity.Id, entity, option);
|
||||
}
|
||||
|
||||
public virtual void DeleteOne(Expression<Func<TDocument, bool>> filterExpression)
|
||||
{
|
||||
_collection.FindOneAndDelete(filterExpression);
|
||||
}
|
||||
|
||||
public virtual Task DeleteOneAsync(Expression<Func<TDocument, bool>> filterExpression)
|
||||
{
|
||||
return Task.Run(() => _collection.FindOneAndDeleteAsync(filterExpression));
|
||||
}
|
||||
|
||||
public virtual void DeleteById(string id)
|
||||
{
|
||||
var objectId = new ObjectId(id);
|
||||
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId);
|
||||
_collection.FindOneAndDelete(filter);
|
||||
}
|
||||
|
||||
public virtual Task DeleteByIdAsync(string id)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var objectId = new ObjectId(id);
|
||||
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId);
|
||||
_collection.FindOneAndDeleteAsync(filter);
|
||||
});
|
||||
}
|
||||
|
||||
public virtual void DeleteMany(Expression<Func<TDocument, bool>> filterExpression)
|
||||
{
|
||||
_collection.DeleteMany(filterExpression);
|
||||
}
|
||||
|
||||
public virtual Task DeleteManyAsync(Expression<Func<TDocument, bool>> filterExpression)
|
||||
{
|
||||
return _collection.DeleteManyAsync(filterExpression);
|
||||
}
|
||||
|
||||
public virtual void CreateIndex(string column)
|
||||
{
|
||||
var keys = Builders<TDocument>.IndexKeys.Ascending(column);
|
||||
var indexOptions = new CreateIndexOptions { Unique = true };
|
||||
var model = new CreateIndexModel<TDocument>(keys, indexOptions);
|
||||
_collection.Indexes.CreateOne(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
53
src/Managing.Infrastructure.Database/SettingsRepository.cs
Normal file
53
src/Managing.Infrastructure.Database/SettingsRepository.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class SettingsRepository : ISettingsRepository
|
||||
{
|
||||
private readonly IMongoRepository<MoneyManagementDto> _moneyManagementRepository;
|
||||
|
||||
public SettingsRepository(IMongoRepository<MoneyManagementDto> moneyManagementRepository)
|
||||
{
|
||||
_moneyManagementRepository = moneyManagementRepository;
|
||||
}
|
||||
|
||||
public void DeleteMoneyManagement(string name)
|
||||
{
|
||||
var moneyManagement = _moneyManagementRepository.FindOne(m => m.Name == name);
|
||||
_moneyManagementRepository.DeleteById(moneyManagement.Id.ToString());
|
||||
}
|
||||
|
||||
public void DeleteMoneyManagements()
|
||||
{
|
||||
_moneyManagementRepository.DropCollection();
|
||||
}
|
||||
|
||||
public async Task<MoneyManagement> GetMoneyManagement(string name)
|
||||
{
|
||||
var moneyManagement = await _moneyManagementRepository.FindOneAsync(m => m.Name == name);
|
||||
return MongoMappers.Map(moneyManagement);
|
||||
}
|
||||
|
||||
public IEnumerable<MoneyManagement> GetMoneyManagements()
|
||||
{
|
||||
var moneyManagements = _moneyManagementRepository.FindAll();
|
||||
return moneyManagements.Select(m => MongoMappers.Map(m));
|
||||
}
|
||||
|
||||
public async Task InsertMoneyManagement(MoneyManagement request)
|
||||
{
|
||||
await _moneyManagementRepository.InsertOneAsync(MongoMappers.Map(request));
|
||||
}
|
||||
|
||||
public void UpdateMoneyManagement(MoneyManagement moneyManagement)
|
||||
{
|
||||
var mm = _moneyManagementRepository.FindOne(m => m.Name == moneyManagement.Name);
|
||||
var dto = MongoMappers.Map(moneyManagement);
|
||||
dto.Id = mm.Id;
|
||||
_moneyManagementRepository.Update(dto);
|
||||
}
|
||||
}
|
||||
103
src/Managing.Infrastructure.Database/StatisticRepository.cs
Normal file
103
src/Managing.Infrastructure.Database/StatisticRepository.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Domain.Statistics;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class StatisticRepository : IStatisticRepository
|
||||
{
|
||||
private readonly IMongoRepository<BestTraderDto> _bestTrader;
|
||||
private readonly IMongoRepository<BadTraderDto> _badTrader;
|
||||
private readonly IMongoRepository<TopVolumeTickerDto> _topRepository;
|
||||
private readonly IMongoRepository<SpotlighOverviewDto> _spotlightRepository;
|
||||
|
||||
public StatisticRepository(
|
||||
IMongoRepository<TopVolumeTickerDto> topRepository,
|
||||
IMongoRepository<SpotlighOverviewDto> spotlightRepository,
|
||||
IMongoRepository<BestTraderDto> bestTrader,
|
||||
IMongoRepository<BadTraderDto> badTrader)
|
||||
{
|
||||
_topRepository = topRepository;
|
||||
_spotlightRepository = spotlightRepository;
|
||||
_bestTrader = bestTrader;
|
||||
_badTrader = badTrader;
|
||||
}
|
||||
|
||||
public async Task InsertTopVolumeTicker(TopVolumeTicker topVolumeTicker)
|
||||
{
|
||||
await _topRepository.InsertOneAsync(MongoMappers.Map(topVolumeTicker));
|
||||
}
|
||||
|
||||
public IList<TopVolumeTicker> GetTopVolumeTickers(DateTime date)
|
||||
{
|
||||
var top = _topRepository.FilterBy(t => date < t.Date);
|
||||
return MongoMappers.Map(top);
|
||||
}
|
||||
|
||||
public async Task SaveSpotligthtOverview(SpotlightOverview overview)
|
||||
{
|
||||
await _spotlightRepository.InsertOneAsync(MongoMappers.Map(overview));
|
||||
}
|
||||
|
||||
public IList<SpotlightOverview> GetSpotlightOverviews(DateTime date)
|
||||
{
|
||||
var overviews = _spotlightRepository.FilterBy(t => date < t.DateTime);
|
||||
return MongoMappers.Map(overviews);
|
||||
}
|
||||
|
||||
public void UpdateSpotlightOverview(SpotlightOverview overview)
|
||||
{
|
||||
var s = _spotlightRepository.FindOne(o => o.Identifier == overview.Identifier);
|
||||
var dto = MongoMappers.Map(overview);
|
||||
dto.Id = s.Id;
|
||||
_spotlightRepository.Update(dto);
|
||||
}
|
||||
|
||||
public List<Trader> GetBestTraders()
|
||||
{
|
||||
return MongoMappers.Map(_bestTrader.FindAll());
|
||||
}
|
||||
|
||||
public void UpdateBestTrader(Trader trader)
|
||||
{
|
||||
var t = _bestTrader.FindOne(t => t.Address == trader.Address);
|
||||
var dto = MongoMappers.Map(trader);
|
||||
dto.Id = t.Id;
|
||||
_bestTrader.Update(dto);
|
||||
}
|
||||
|
||||
public async Task InsertBestTrader(Trader trader)
|
||||
{
|
||||
await _bestTrader.InsertOneAsync(MongoMappers.Map(trader));
|
||||
}
|
||||
|
||||
public async Task RemoveBestTrader(Trader trader)
|
||||
{
|
||||
await _bestTrader.DeleteOneAsync(t => t.Address == trader.Address);
|
||||
}
|
||||
|
||||
public List<Trader> GetBadTraders()
|
||||
{
|
||||
return MongoMappers.Map(_badTrader.FindAll());
|
||||
}
|
||||
|
||||
public void UpdateBadTrader(Trader trader)
|
||||
{
|
||||
var t = _badTrader.FindOne(t => t.Address == trader.Address);
|
||||
var dto = MongoMappers.BadTraderMap(trader);
|
||||
dto.Id = t.Id;
|
||||
_badTrader.Update(dto);
|
||||
}
|
||||
|
||||
public async Task InsertBadTrader(Trader trader)
|
||||
{
|
||||
await _badTrader.InsertOneAsync(MongoMappers.BadTraderMap(trader));
|
||||
}
|
||||
|
||||
public async Task RemoveBadTrader(Trader trader)
|
||||
{
|
||||
await _badTrader.DeleteOneAsync(t => t.Address == trader.Address);
|
||||
}
|
||||
}
|
||||
150
src/Managing.Infrastructure.Database/TradingRepository.cs
Normal file
150
src/Managing.Infrastructure.Database/TradingRepository.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Common;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
using MongoDB.Driver;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class TradingRepository : ITradingRepository
|
||||
{
|
||||
private readonly IMongoRepository<ScenarioDto> _scenarioRepository;
|
||||
private readonly IMongoRepository<SignalDto> _signalRepository;
|
||||
private readonly IMongoRepository<PositionDto> _positionRepository;
|
||||
private readonly IMongoRepository<StrategyDto> _strategyRepository;
|
||||
private readonly IMongoRepository<FeeDto> _feeRepository;
|
||||
|
||||
|
||||
public TradingRepository(
|
||||
IMongoRepository<ScenarioDto> scenarioRepository,
|
||||
IMongoRepository<SignalDto> signalRepository,
|
||||
IMongoRepository<PositionDto> positionRepository,
|
||||
IMongoRepository<StrategyDto> strategyRepository,
|
||||
IMongoRepository<FeeDto> feeRepository)
|
||||
{
|
||||
_scenarioRepository = scenarioRepository;
|
||||
_signalRepository = signalRepository;
|
||||
_positionRepository = positionRepository;
|
||||
_strategyRepository = strategyRepository;
|
||||
_feeRepository = feeRepository;
|
||||
}
|
||||
|
||||
public void DeleteScenario(string name)
|
||||
{
|
||||
var scenario = _scenarioRepository.FindOne(s => s.Name == name);
|
||||
_scenarioRepository.DeleteById(scenario.Id.ToString());
|
||||
}
|
||||
|
||||
public void DeleteScenarios()
|
||||
{
|
||||
_scenarioRepository.DropCollection();
|
||||
}
|
||||
|
||||
public void DeleteStrategies()
|
||||
{
|
||||
_strategyRepository.DropCollection();
|
||||
}
|
||||
|
||||
public void DeleteStrategy(string name)
|
||||
{
|
||||
var strategy = _strategyRepository.FindOne(s => s.Name == name);
|
||||
_strategyRepository.DeleteById(strategy.Id.ToString());
|
||||
}
|
||||
|
||||
public Position GetPositionByIdentifier(string identifier)
|
||||
{
|
||||
var position = _positionRepository.FindOne(p => p.Identifier == identifier);
|
||||
return MongoMappers.Map(position);
|
||||
}
|
||||
|
||||
public IEnumerable<Position> GetPositions(PositionInitiator positionInitiator)
|
||||
{
|
||||
var filter = Builders<PositionDto>.Filter.Eq(p => p.Initiator, positionInitiator);
|
||||
var positions = _positionRepository.FilterBy(filter);
|
||||
return positions.Select(MongoMappers.Map);
|
||||
}
|
||||
|
||||
public IEnumerable<Position> GetPositionsByStatus(Enums.PositionStatus positionStatus)
|
||||
{
|
||||
var filter = Builders<PositionDto>.Filter.Eq(p => p.Status, positionStatus);
|
||||
var positions = _positionRepository.FilterBy(filter);
|
||||
return positions.Select(MongoMappers.Map);
|
||||
}
|
||||
|
||||
public Scenario GetScenarioByName(string name)
|
||||
{
|
||||
var scenario = _scenarioRepository.FindOne(s => s.Name == name);
|
||||
return MongoMappers.Map(scenario);
|
||||
}
|
||||
|
||||
public IEnumerable<Scenario> GetScenarios()
|
||||
{
|
||||
var scenarios = _scenarioRepository.FindAll();
|
||||
return scenarios.Select(s => MongoMappers.Map(s));
|
||||
}
|
||||
|
||||
public IEnumerable<Strategy> GetStrategies()
|
||||
{
|
||||
var strategies = _strategyRepository.FindAll();
|
||||
return strategies.Select(MongoMappers.Map);
|
||||
}
|
||||
|
||||
public Strategy GetStrategyByName(string name)
|
||||
{
|
||||
var strategy = _strategyRepository.FindOne(s => s.Name == name);
|
||||
return MongoMappers.Map(strategy);
|
||||
}
|
||||
|
||||
public void InsertPosition(Position position)
|
||||
{
|
||||
_positionRepository.InsertOne(MongoMappers.Map(position));
|
||||
}
|
||||
|
||||
public void InsertScenario(Scenario scenario)
|
||||
{
|
||||
_scenarioRepository.CreateIndex(nameof(Scenario.Name));
|
||||
_scenarioRepository.InsertOne(MongoMappers.Map(scenario));
|
||||
}
|
||||
|
||||
public void InsertSignal(Signal signal)
|
||||
{
|
||||
_signalRepository.InsertOne(MongoMappers.Map(signal));
|
||||
}
|
||||
|
||||
public void InsertStrategy(Strategy strategy)
|
||||
{
|
||||
_strategyRepository.InsertOne(MongoMappers.Map(strategy));
|
||||
}
|
||||
|
||||
public void UpdatePosition(Position position)
|
||||
{
|
||||
var p = _positionRepository.FindOne(p => p.Open.ExchangeOrderId == position.Open.ExchangeOrderId);
|
||||
var dto = MongoMappers.Map(position);
|
||||
dto.Id = p.Id;
|
||||
_positionRepository.Update(dto);
|
||||
}
|
||||
|
||||
public Fee GetFee(TradingExchanges exchange)
|
||||
{
|
||||
var fee = _feeRepository.FindOne(f => f.Exchange == exchange);
|
||||
return MongoMappers.Map(fee);
|
||||
}
|
||||
|
||||
public void InsertFee(Fee fee)
|
||||
{
|
||||
_feeRepository.InsertOne(MongoMappers.Map(fee));
|
||||
}
|
||||
|
||||
public void UpdateFee(Fee fee)
|
||||
{
|
||||
var f = _feeRepository.FindOne(f => f.Exchange == fee.Exchange);
|
||||
var dto = MongoMappers.Map(fee);
|
||||
dto.Id = f.Id;
|
||||
_feeRepository.Update(dto);
|
||||
}
|
||||
}
|
||||
28
src/Managing.Infrastructure.Database/UserRepository.cs
Normal file
28
src/Managing.Infrastructure.Database/UserRepository.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class UserRepository : IUserRepository
|
||||
{
|
||||
private readonly IMongoRepository<UserDto> _userRepository;
|
||||
|
||||
public UserRepository(IMongoRepository<UserDto> userRepository)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
public async Task<User> GetUserByNameAsync(string name)
|
||||
{
|
||||
var user = await _userRepository.FindOneAsync(u => u.Name == name);
|
||||
return MongoMappers.Map(user);
|
||||
}
|
||||
|
||||
public async Task InsertUserAsync(User user)
|
||||
{
|
||||
await _userRepository.InsertOneAsync(MongoMappers.Map(user));
|
||||
}
|
||||
}
|
||||
60
src/Managing.Infrastructure.Database/WorkerRepository.cs
Normal file
60
src/Managing.Infrastructure.Database/WorkerRepository.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Common;
|
||||
using Managing.Domain.Workers;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class WorkerRepository : IWorkerRepository
|
||||
{
|
||||
private readonly IMongoRepository<WorkerDto> _workerRepository;
|
||||
public WorkerRepository(IMongoRepository<WorkerDto> workerRepository)
|
||||
{
|
||||
_workerRepository = workerRepository;
|
||||
}
|
||||
|
||||
public async Task DisableWorker(Enums.WorkerType workerType)
|
||||
{
|
||||
var worker = await GetWorker(workerType);
|
||||
worker.IsActive = false;
|
||||
_workerRepository.Update(worker);
|
||||
}
|
||||
|
||||
public async Task EnableWorker(Enums.WorkerType workerType)
|
||||
{
|
||||
var worker = await GetWorker(workerType);
|
||||
worker.IsActive = true;
|
||||
_workerRepository.Update(worker);
|
||||
}
|
||||
|
||||
public async Task<Worker> GetWorkerAsync(Enums.WorkerType workerType)
|
||||
{
|
||||
return MongoMappers.Map(await GetWorker(workerType));
|
||||
}
|
||||
|
||||
public async Task InsertWorker(Worker worker)
|
||||
{
|
||||
await _workerRepository.InsertOneAsync(MongoMappers.Map(worker));
|
||||
}
|
||||
|
||||
public async Task UpdateWorker(Enums.WorkerType workerType, int executionCount)
|
||||
{
|
||||
var worker = await GetWorker(workerType);
|
||||
worker.ExecutionCount = executionCount;
|
||||
worker.LastRunTime = DateTime.UtcNow;
|
||||
_workerRepository.Update(worker);
|
||||
}
|
||||
|
||||
public IEnumerable<Worker> GetWorkers()
|
||||
{
|
||||
var workers = _workerRepository.FindAll();
|
||||
return workers.Select(MongoMappers.Map);
|
||||
}
|
||||
|
||||
private async Task<WorkerDto> GetWorker(Enums.WorkerType workerType)
|
||||
{
|
||||
return await _workerRepository.FindOneAsync(w => w.WorkerType == workerType);
|
||||
}
|
||||
}
|
||||
48
src/Managing.Infrastructure.Database/WorkflowRepository.cs
Normal file
48
src/Managing.Infrastructure.Database/WorkflowRepository.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Domain.Workflows.Synthetics;
|
||||
using Managing.Infrastructure.Databases.MongoDb;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class WorkflowRepository : IWorkflowRepository
|
||||
{
|
||||
private readonly IMongoRepository<WorkflowDto> _workflowRepository;
|
||||
|
||||
public WorkflowRepository(IMongoRepository<WorkflowDto> workflowRepository)
|
||||
{
|
||||
_workflowRepository = workflowRepository;
|
||||
}
|
||||
public bool DeleteWorkflow(string name)
|
||||
{
|
||||
var workflow = _workflowRepository.FindOne(m => m.Name == name);
|
||||
_workflowRepository.DeleteById(workflow.Id.ToString());
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task InsertWorkflow(SyntheticWorkflow workflow)
|
||||
{
|
||||
await _workflowRepository.InsertOneAsync(MongoMappers.Map(workflow));
|
||||
}
|
||||
|
||||
public async Task UpdateWorkflow(SyntheticWorkflow workflow)
|
||||
{
|
||||
var w = await _workflowRepository.FindOneAsync(m => m.Name == workflow.Name);
|
||||
var dto = MongoMappers.Map(workflow);
|
||||
dto.Id = w.Id;
|
||||
_workflowRepository.Update(dto);
|
||||
}
|
||||
|
||||
public async Task<SyntheticWorkflow> GetWorkflow(string name)
|
||||
{
|
||||
var workflow = await _workflowRepository.FindOneAsync(m => m.Name == name);
|
||||
return MongoMappers.Map(workflow);
|
||||
}
|
||||
|
||||
public IEnumerable<SyntheticWorkflow> GetWorkflows()
|
||||
{
|
||||
var workflows = _workflowRepository.FindAll();
|
||||
return workflows.Select(m => MongoMappers.Map(m));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user