docker files fixes from liaqat
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using MediatR;
|
||||
|
||||
namespace Managing.Application.Shared.Behaviours
|
||||
{
|
||||
public interface IUnhandledExceptionBehaviour<TRequest, TResponse>
|
||||
{
|
||||
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Managing.Application.Shared.Behaviours
|
||||
{
|
||||
public class UnhandledExceptionBehaviour<TRequest, TResponse> : IUnhandledExceptionBehaviour<TRequest, TResponse>
|
||||
{
|
||||
private readonly ILogger<TRequest> logger;
|
||||
|
||||
public UnhandledExceptionBehaviour(ILogger<TRequest> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await next();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var requestName = typeof(TRequest).Name;
|
||||
|
||||
logger.LogError(ex, $"Unhandled Exception for Request {requestName} {request}");
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Managing.Application.Shared.Behaviours
|
||||
{
|
||||
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
private readonly IEnumerable<IValidator<TRequest>> validators;
|
||||
|
||||
public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators)
|
||||
{
|
||||
this.validators = validators;
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
|
||||
{
|
||||
if (validators.Any())
|
||||
{
|
||||
var context = new ValidationContext<TRequest>(request);
|
||||
var validationResults = await Task.WhenAll(validators.Select(v => v.ValidateAsync(context, cancellationToken)));
|
||||
var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList();
|
||||
if (failures.Count != 0) throw new ValidationException(failures);
|
||||
}
|
||||
|
||||
return await next();
|
||||
}
|
||||
}
|
||||
}
|
||||
66
src/Managing.Application/Shared/MessengerService.cs
Normal file
66
src/Managing.Application/Shared/MessengerService.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Common;
|
||||
using Managing.Domain.Statistics;
|
||||
using Managing.Domain.Trades;
|
||||
|
||||
namespace Managing.Application.Shared;
|
||||
|
||||
public class MessengerService : IMessengerService
|
||||
{
|
||||
private readonly IDiscordService _discordService;
|
||||
|
||||
public MessengerService(IDiscordService discordService)
|
||||
{
|
||||
_discordService = discordService;
|
||||
}
|
||||
|
||||
public async Task SendClosedPosition(string address, Trade oldTrade)
|
||||
{
|
||||
await _discordService.SendClosedPosition(address, oldTrade);
|
||||
}
|
||||
|
||||
public async Task SendClosingPosition(Position position)
|
||||
{
|
||||
await _discordService.SendClosingPosition(position);
|
||||
}
|
||||
|
||||
public async Task SendIncreasePosition(string address, Trade trade, string copyAccountName, Trade? oldTrade = null)
|
||||
{
|
||||
await _discordService.SendIncreasePosition(address, trade, copyAccountName, oldTrade);
|
||||
}
|
||||
|
||||
public async Task SendDecreasePosition(string address, Trade newTrade, decimal decreaseAmount)
|
||||
{
|
||||
await _discordService.SendDecreasePosition(address, newTrade, decreaseAmount);
|
||||
}
|
||||
|
||||
public async Task SendMessage(string message)
|
||||
{
|
||||
await _discordService.SendMessage(message);
|
||||
}
|
||||
|
||||
public async Task SendPosition(Position position)
|
||||
{
|
||||
await _discordService.SendPosition(position);
|
||||
}
|
||||
|
||||
public async Task SendSignal(string message, Enums.TradingExchanges exchange, Enums.Ticker ticker, Enums.TradeDirection direction, Enums.Timeframe timeframe)
|
||||
{
|
||||
await _discordService.SendSignal(message, exchange, ticker, direction, timeframe);
|
||||
}
|
||||
|
||||
public async Task SendTradeMessage(string message, bool isBadBehavior = false)
|
||||
{
|
||||
await _discordService.SendTradeMessage(message, isBadBehavior);
|
||||
}
|
||||
|
||||
public async Task SendBestTraders(List<Trader> traders)
|
||||
{
|
||||
await _discordService.SendBestTraders(traders);
|
||||
}
|
||||
|
||||
public async Task SendBadTraders(List<Trader> traders)
|
||||
{
|
||||
await _discordService.SendBadTraders(traders);
|
||||
}
|
||||
}
|
||||
199
src/Managing.Application/Shared/SettingsService.cs
Normal file
199
src/Managing.Application/Shared/SettingsService.cs
Normal file
@@ -0,0 +1,199 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Shared;
|
||||
|
||||
public class SettingsService : ISettingsService
|
||||
{
|
||||
private readonly IMoneyManagementService _moneyManagementService;
|
||||
private readonly IScenarioService _scenarioService;
|
||||
private readonly IBacktester _backtester;
|
||||
private readonly ILogger<SettingsService> _logger;
|
||||
|
||||
public SettingsService(IMoneyManagementService moneyManagementService,
|
||||
IScenarioService scenarioService,
|
||||
IBacktester backtester,
|
||||
ILogger<SettingsService> logger)
|
||||
{
|
||||
_moneyManagementService = moneyManagementService;
|
||||
_scenarioService = scenarioService;
|
||||
_backtester = backtester;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> ResetSettings()
|
||||
{
|
||||
if (!_backtester.DeleteBacktests())
|
||||
{
|
||||
throw new Exception("Cannot delete all backtests");
|
||||
}
|
||||
|
||||
if (!_scenarioService.DeleteScenarios())
|
||||
{
|
||||
throw new Exception("Cannot delete scenarios");
|
||||
}
|
||||
|
||||
if (!_scenarioService.DeleteStrategies())
|
||||
{
|
||||
throw new Exception("Cannot delete all strategies");
|
||||
}
|
||||
|
||||
if (!_moneyManagementService.DeleteMoneyManagements())
|
||||
{
|
||||
throw new Exception("Cannot delete all money management settings");
|
||||
}
|
||||
|
||||
if (!SetupSettings())
|
||||
{
|
||||
throw new Exception("Cannot setup settings");
|
||||
}
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
public bool SetupSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
SetupMoneyManagementsSeed(Timeframe.FiveMinutes);
|
||||
SetupMoneyManagementsSeed(Timeframe.FifteenMinutes);
|
||||
SetupMoneyManagementsSeed(Timeframe.OneHour);
|
||||
SetupMoneyManagementsSeed(Timeframe.OneDay);
|
||||
SetupScenariosSeed(Timeframe.FifteenMinutes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private async void SetupMoneyManagementsSeed(Timeframe timeframe)
|
||||
{
|
||||
var moneyManagement = new MoneyManagement()
|
||||
{
|
||||
Timeframe = timeframe,
|
||||
BalanceAtRisk = 0.05m,
|
||||
Leverage = 1,
|
||||
StopLoss = 0.021m,
|
||||
TakeProfit = 0.042m,
|
||||
Name = $"{timeframe} Money Management"
|
||||
};
|
||||
|
||||
await _moneyManagementService.CreateOrUpdateMoneyManagement(moneyManagement);
|
||||
}
|
||||
|
||||
private void SetupScenariosSeed(Timeframe timeframe)
|
||||
{
|
||||
SetupMacd(timeframe);
|
||||
SetupRsiDiv(timeframe);
|
||||
SetupRsiDivConfirm(timeframe);
|
||||
SetupSuperTrend(timeframe);
|
||||
SetupChandelierExit(timeframe);
|
||||
SetupStochRsiTrend(timeframe);
|
||||
SetupStochSTCTrend(timeframe);
|
||||
SetupEmaTrend(timeframe);
|
||||
SetupEmaCross(timeframe);
|
||||
}
|
||||
|
||||
private void SetupStochSTCTrend(Timeframe timeframe)
|
||||
{
|
||||
var name = "STCTrend";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.Stc,
|
||||
timeframe,
|
||||
name,
|
||||
fastPeriods: 23,
|
||||
slowPeriods: 50,
|
||||
cyclePeriods: 10);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
|
||||
private void SetupMacd(Timeframe timeframe)
|
||||
{
|
||||
var name = "MacdCross";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.MacdCross,
|
||||
timeframe,
|
||||
name,
|
||||
fastPeriods: 12,
|
||||
slowPeriods: 26,
|
||||
signalPeriods: 9);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
|
||||
private void SetupRsiDiv(Timeframe timeframe)
|
||||
{
|
||||
var name = "RsiDiv6";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.RsiDivergence,
|
||||
timeframe,
|
||||
name,
|
||||
period: 6);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
|
||||
private void SetupRsiDivConfirm(Timeframe timeframe)
|
||||
{
|
||||
var name = "RsiDivConfirm6";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.RsiDivergenceConfirm,
|
||||
timeframe,
|
||||
name,
|
||||
period: 6);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
|
||||
private void SetupSuperTrend(Timeframe timeframe)
|
||||
{
|
||||
var name = "SuperTrend";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.SuperTrend,
|
||||
timeframe,
|
||||
name,
|
||||
period: 10,
|
||||
multiplier: 3);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
|
||||
private void SetupChandelierExit(Timeframe timeframe)
|
||||
{
|
||||
var name = "ChandelierExit";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.ChandelierExit,
|
||||
timeframe,
|
||||
name,
|
||||
period: 22,
|
||||
multiplier: 3);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
private void SetupStochRsiTrend(Timeframe timeframe)
|
||||
{
|
||||
var name = "StochRsiTrend";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.StochRsiTrend,
|
||||
timeframe,
|
||||
name,
|
||||
period: 14,
|
||||
stochPeriods: 14,
|
||||
signalPeriods: 3,
|
||||
smoothPeriods: 1);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
|
||||
private void SetupEmaTrend(Timeframe timeframe)
|
||||
{
|
||||
var name = "Ema200Trend";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.EmaTrend,
|
||||
timeframe,
|
||||
name,
|
||||
period: 200);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
|
||||
private void SetupEmaCross(Timeframe timeframe)
|
||||
{
|
||||
var name = "Ema200Cross";
|
||||
var strategy = _scenarioService.CreateStrategy(StrategyType.EmaCross,
|
||||
timeframe,
|
||||
name,
|
||||
period: 200);
|
||||
_scenarioService.CreateScenario(name, new List<string> { strategy.Name });
|
||||
}
|
||||
}
|
||||
30
src/Managing.Application/Shared/StreamService.cs
Normal file
30
src/Managing.Application/Shared/StreamService.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Managing.Application.Shared;
|
||||
|
||||
public class StreamService : IStreamService
|
||||
{
|
||||
private readonly IExchangeStream _exchangeStream;
|
||||
private readonly IHubContext<CandleHub> _hubContext;
|
||||
|
||||
|
||||
public StreamService(IExchangeStream exchangeStream, IHubContext<CandleHub> hubContext)
|
||||
{
|
||||
_exchangeStream = exchangeStream;
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
|
||||
public async Task SubscribeCandle()
|
||||
{
|
||||
await _exchangeStream.StartBinanceWorker(Common.Enums.Ticker.BTC, async (candle) => {
|
||||
await _hubContext.Clients.All.SendAsync(candle.Ticker, candle);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task UnSubscribeCandle()
|
||||
{
|
||||
await _exchangeStream.StopBinanceWorker();
|
||||
}
|
||||
}
|
||||
21
src/Managing.Application/Shared/TickerService.cs
Normal file
21
src/Managing.Application/Shared/TickerService.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Shared;
|
||||
|
||||
public class TickerService : ITickerService
|
||||
{
|
||||
private readonly IEvmManager _evmManager;
|
||||
|
||||
public TickerService(IEvmManager evmManager)
|
||||
{
|
||||
_evmManager = evmManager;
|
||||
}
|
||||
|
||||
public async Task<List<Ticker>> GetAvailableTicker()
|
||||
{
|
||||
var tickers = await _evmManager.GetAvailableTicker();
|
||||
return tickers;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user