docker files fixes from liaqat

This commit is contained in:
alirehmani
2024-05-03 16:39:25 +05:00
commit 464a8730e8
587 changed files with 44288 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
namespace Managing.Infrastructure.MongoDb.Attributes
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class BsonCollectionAttribute : Attribute
{
public string CollectionName { get; }
public BsonCollectionAttribute(string collectionName)
{
CollectionName = collectionName;
}
}
}

View File

@@ -0,0 +1,14 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.MongoDb.Collections;
[BsonCollection("Accounts")]
public class AccountDto : Document
{
public string Name { get; set; }
public Exchanges Exchanges { get; set; }
public string Key { get; set; }
public string Secret { get; set; }
}

View File

@@ -0,0 +1,24 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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 string 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; }
}
}

View File

@@ -0,0 +1,28 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using MongoDB.Bson.Serialization.Attributes;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.MongoDb.Collections
{
[BsonCollection("Candles")]
public class CandleDto : Document
{
public Exchanges 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; }
}
}

View File

@@ -0,0 +1,18 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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 QuantityTakeProfit { get; set; }
public decimal Leverage { get; set; }
}
}

View File

@@ -0,0 +1,24 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using MongoDB.Bson.Serialization.Attributes;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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 TradeStatus Status { get; set; }
public string SignalIdentifier { get; set; }
public string AccountName { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
namespace Managing.Infrastructure.MongoDb.Collections
{
[BsonCollection("Scenarios")]
public class ScenarioDto : Document
{
public string Name { get; set; }
public List<StrategyDto> Strategies { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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 string Ticker { get; set; }
public SignalStatus Status { get; set; }
public Timeframe Timeframe { get; set; }
public StrategyType Type { get; set; }
}
}

View File

@@ -0,0 +1,21 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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; }
}
}

View File

@@ -0,0 +1,15 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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 Exchanges Exchange { get; set; }
}

View File

@@ -0,0 +1,24 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using MongoDB.Bson.Serialization.Attributes;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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 string 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; }
}
}

View File

@@ -0,0 +1,15 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.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; }
}

View File

@@ -0,0 +1,13 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Managing.Infrastructure.MongoDb.Configurations
{
public abstract class Document : IDocument
{
[BsonId]
public ObjectId Id { get; set; }
[BsonDateTimeOptions]
public DateTime CreatedAt => Id.CreationTime;
}
}

View File

@@ -0,0 +1,14 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Managing.Infrastructure.MongoDb.Configurations
{
public interface IDocument
{
[BsonId]
[BsonRepresentation(BsonType.String)]
ObjectId Id { get; set; }
DateTime CreatedAt { get; }
}
}

View File

@@ -0,0 +1,8 @@
namespace Managing.Infrastructure.MongoDb
{
public interface IManagingDatabaseSettings
{
string ConnectionString { get; set; }
string DatabaseName { get; set; }
}
}

View File

@@ -0,0 +1,7 @@
namespace Managing.Infrastructure.MongoDb;
public class ManagingDatabaseSettings : IManagingDatabaseSettings
{
public string ConnectionString { get; set; }
public string DatabaseName { get; set; }
}

View File

@@ -0,0 +1,55 @@
using Managing.Infrastructure.MongoDb.Configurations;
using System.Linq.Expressions;
namespace Managing.Infrastructure.MongoDb
{
public interface IMongoRepository<TDocument> where TDocument : IDocument
{
IQueryable<TDocument> AsQueryable();
IEnumerable<TDocument> FilterBy(
Expression<Func<TDocument, bool>> filterExpression);
IEnumerable<TDocument> FindAll();
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();
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Managing.Common\Managing.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,20 @@
using MongoDB.Bson;
using MongoDB.Driver;
namespace Managing.Infrastructure.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);
}
}
}

View File

@@ -0,0 +1,173 @@
using Managing.Infrastructure.MongoDb.Attributes;
using Managing.Infrastructure.MongoDb.Configurations;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Linq.Expressions;
namespace Managing.Infrastructure.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> 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 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 void DeleteOne(Expression<Func<TDocument, bool>> filterExpression)
{
_collection.FindOneAndDelete(filterExpression);
}
public Task DeleteOneAsync(Expression<Func<TDocument, bool>> filterExpression)
{
return Task.Run(() => _collection.FindOneAndDeleteAsync(filterExpression));
}
public void DeleteById(string id)
{
var objectId = new ObjectId(id);
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId);
_collection.FindOneAndDelete(filter);
}
public 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 void DeleteMany(Expression<Func<TDocument, bool>> filterExpression)
{
_collection.DeleteMany(filterExpression);
}
public 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);
}
}
}