Update workers setup
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using HealthChecks.UI.Client;
|
||||
using Managing.Api.Workers.Filters;
|
||||
using Managing.Api.Workers.Workers;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Bootstrap;
|
||||
using Managing.Common;
|
||||
@@ -152,18 +151,6 @@ builder.Services.AddSwaggerGen(options =>
|
||||
});
|
||||
|
||||
builder.WebHost.SetupDiscordBot();
|
||||
// builder.Services.AddHostedService<FeeWorker>();
|
||||
// builder.Services.AddHostedService<PositionManagerWorker>();
|
||||
// builder.Services.AddHostedService<PositionFetcher>();
|
||||
// builder.Services.AddHostedService<PricesFiveMinutesWorker>();
|
||||
builder.Services.AddHostedService<PricesFifteenMinutesWorker>();
|
||||
builder.Services.AddHostedService<PricesOneHourWorker>();
|
||||
builder.Services.AddHostedService<PricesFourHoursWorker>();
|
||||
builder.Services.AddHostedService<PricesOneDayWorker>();
|
||||
// builder.Services.AddHostedService<SpotlightWorker>();
|
||||
// builder.Services.AddHostedService<TraderWatcher>();
|
||||
// builder.Services.AddHostedService<LeaderboardWorker>();
|
||||
// builder.Services.AddHostedService<FundingRatesWatcher>();
|
||||
|
||||
// App
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class FeeWorker : BaseWorker<FeeWorker>
|
||||
{
|
||||
private readonly ITradingService _tradingService;
|
||||
private static readonly WorkerType _workerType = WorkerType.Fee;
|
||||
|
||||
public FeeWorker(
|
||||
ILogger<FeeWorker> logger,
|
||||
ITradingService tradingService,
|
||||
IWorkerService workerService) : base(
|
||||
_workerType,
|
||||
logger,
|
||||
TimeSpan.FromHours(12),
|
||||
workerService
|
||||
)
|
||||
{
|
||||
_tradingService = tradingService;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
_tradingService.UpdateFee(TradingExchanges.Evm);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Common;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class FundingRatesWatcher : BaseWorker<FundingRatesWatcher>
|
||||
{
|
||||
private readonly IStatisticService _statisticService;
|
||||
|
||||
public FundingRatesWatcher(
|
||||
ILogger<FundingRatesWatcher> logger,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
Enums.WorkerType.FundingRatesWatcher,
|
||||
logger,
|
||||
TimeSpan.FromMinutes(30),
|
||||
workerService
|
||||
)
|
||||
{
|
||||
_statisticService = statisticService;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await _statisticService.UpdateFundingRates();
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class LeaderboardWorker : BaseWorker<FeeWorker>
|
||||
{
|
||||
private readonly IStatisticService _statisticService;
|
||||
private static readonly WorkerType _workerType = WorkerType.LeaderboardWorker;
|
||||
|
||||
public LeaderboardWorker(
|
||||
ILogger<FeeWorker> logger,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
_workerType,
|
||||
logger,
|
||||
TimeSpan.FromHours(24),
|
||||
workerService
|
||||
)
|
||||
{
|
||||
_statisticService = statisticService;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await _statisticService.UpdateLeaderboard();
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class NoobiesboardWorker : BaseWorker<FeeWorker>
|
||||
{
|
||||
private readonly IStatisticService _statisticService;
|
||||
private static readonly WorkerType _workerType = WorkerType.Noobiesboard;
|
||||
|
||||
public NoobiesboardWorker(
|
||||
ILogger<FeeWorker> logger,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
_workerType,
|
||||
logger,
|
||||
TimeSpan.FromHours(24),
|
||||
workerService
|
||||
)
|
||||
{
|
||||
_statisticService = statisticService;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await _statisticService.UpdateNoobiesboard();
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class PositionFetcher : BaseWorker<PositionFetcher>
|
||||
{
|
||||
private static readonly WorkerType _workerType = WorkerType.PositionFetcher;
|
||||
private readonly ITradingService _tradingService;
|
||||
private readonly IHubContext<PositionHub> _hubContext;
|
||||
private readonly ILogger<PositionFetcher> _logger;
|
||||
|
||||
public PositionFetcher(
|
||||
ILogger<PositionFetcher> logger,
|
||||
IWorkerService workerService,
|
||||
ITradingService tradingService,
|
||||
|
||||
IHubContext<PositionHub> hubContext) : base(
|
||||
_workerType,
|
||||
logger,
|
||||
TimeSpan.FromSeconds(10),
|
||||
workerService)
|
||||
{
|
||||
_logger = logger;
|
||||
_tradingService = tradingService;
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
var positions = _tradingService.GetPositions().Where(p => p.Initiator != PositionInitiator.PaperTrading);
|
||||
await _hubContext.Clients.All.SendAsync("Positions", positions);
|
||||
}
|
||||
}
|
||||
@@ -1,377 +0,0 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Shared.Helpers;
|
||||
using Managing.Domain.Trades;
|
||||
using Newtonsoft.Json;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class PositionManagerWorker : BaseWorker<PositionManagerWorker>
|
||||
{
|
||||
private static readonly WorkerType _workerType = WorkerType.PositionManager;
|
||||
private readonly ITradingService _tradingService;
|
||||
private readonly IExchangeService _exchangeService;
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly ILogger<PositionManagerWorker> _logger;
|
||||
private readonly ICacheService _cacheService;
|
||||
|
||||
public PositionManagerWorker(
|
||||
ILogger<PositionManagerWorker> logger,
|
||||
IWorkerService workerService,
|
||||
ITradingService tradingService,
|
||||
IExchangeService exchangeService,
|
||||
IAccountService accountService, ICacheService cacheService) : base(
|
||||
_workerType,
|
||||
logger,
|
||||
TimeSpan.FromMinutes(1),
|
||||
workerService)
|
||||
{
|
||||
_logger = logger;
|
||||
_tradingService = tradingService;
|
||||
_exchangeService = exchangeService;
|
||||
_accountService = accountService;
|
||||
_cacheService = cacheService;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await ManageNewPositions();
|
||||
await ManagePartiallyFilledPositions();
|
||||
await ManageFilledPositions();
|
||||
}
|
||||
|
||||
private async Task ManagePartiallyFilledPositions()
|
||||
{
|
||||
var positions = GetPositions(PositionStatus.PartiallyFilled);
|
||||
_logger.LogInformation("Processing {PartiallyFilledCount} partially filled positions", positions.Count());
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
using (_logger.BeginScope("Position {PositionId} ({Ticker})", position.Identifier, position.Ticker))
|
||||
{
|
||||
try
|
||||
{
|
||||
// Lock position for processing
|
||||
position.Status = PositionStatus.Updating;
|
||||
_tradingService.UpdatePosition(position);
|
||||
|
||||
_logger.LogDebug("Verifying position on exchange for {Direction} position opened at {OpenDate}",
|
||||
position.OriginDirection, position.Date.ToString("o"));
|
||||
|
||||
var account = await _accountService.GetAccount(position.AccountName, false, false);
|
||||
|
||||
// Get positions directly from broker
|
||||
var brokerPositions = await _exchangeService.GetBrokerPositions(account);
|
||||
var exchangePosition = brokerPositions.FirstOrDefault(p =>
|
||||
p.Ticker == position.Ticker &&
|
||||
p.OriginDirection == position.OriginDirection);
|
||||
|
||||
if (exchangePosition == null)
|
||||
{
|
||||
_logger.LogWarning("Position not found on exchange - marking as canceled");
|
||||
position.Status = PositionStatus.Canceled;
|
||||
_tradingService.UpdatePosition(position);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update with exchange data if available
|
||||
if (exchangePosition.StopLoss != null)
|
||||
{
|
||||
_logger.LogInformation("Stop Loss found on exchange - ID: {OrderId}", exchangePosition.StopLoss.ExchangeOrderId);
|
||||
position.StopLoss = exchangePosition.StopLoss;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Stop Loss not found on exchange - creating new SL order");
|
||||
var updatedSl = await _exchangeService.OpenStopLoss(account, position.Ticker, position.OriginDirection,
|
||||
position.StopLoss.Price, position.StopLoss.Quantity, false, DateTime.UtcNow);
|
||||
|
||||
if (updatedSl != null)
|
||||
{
|
||||
position.StopLoss = updatedSl;
|
||||
}
|
||||
}
|
||||
|
||||
if (exchangePosition.TakeProfit1 != null)
|
||||
{
|
||||
_logger.LogInformation("Take Profit found on exchange - ID: {OrderId}", exchangePosition.TakeProfit1.ExchangeOrderId);
|
||||
position.TakeProfit1 = exchangePosition.TakeProfit1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Take Profit not found on exchange - creating new TP order");
|
||||
var updatedTp1 = await _exchangeService.OpenTakeProfit(account, position.Ticker, position.OriginDirection,
|
||||
position.TakeProfit1.Price, position.TakeProfit1.Quantity, false, DateTime.UtcNow);
|
||||
|
||||
if (updatedTp1 != null)
|
||||
{
|
||||
position.TakeProfit1 = updatedTp1;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle TP2 if it exists
|
||||
if (position.TakeProfit2 != null)
|
||||
{
|
||||
if (exchangePosition.TakeProfit2 != null)
|
||||
{
|
||||
_logger.LogInformation("Take Profit 2 found on exchange - ID: {OrderId}", exchangePosition.TakeProfit2.ExchangeOrderId);
|
||||
position.TakeProfit2 = exchangePosition.TakeProfit2;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Take Profit 2 not found on exchange - creating new TP2 order");
|
||||
var updatedTp2 = await _exchangeService.OpenTakeProfit(account, position.Ticker, position.OriginDirection,
|
||||
position.TakeProfit2.Price, position.TakeProfit2.Quantity, false, DateTime.UtcNow);
|
||||
|
||||
if (updatedTp2 != null)
|
||||
{
|
||||
position.TakeProfit2 = updatedTp2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update position status based on verification results
|
||||
var success = AllTradesActive(position);
|
||||
position.Status = success ? PositionStatus.Filled : PositionStatus.PartiallyFilled;
|
||||
|
||||
_logger.LogInformation("Final position status: {Status}", position.Status);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Position processing failed");
|
||||
position.Status = PositionStatus.PartiallyFilled;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_tradingService.UpdatePosition(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool AllTradesActive(Position position)
|
||||
{
|
||||
return position.StopLoss.Status.IsActive() &&
|
||||
position.TakeProfit1.Status.IsActive() &&
|
||||
(position.TakeProfit2?.Status.IsActive() ?? true);
|
||||
}
|
||||
|
||||
private async Task ManageFilledPositions()
|
||||
{
|
||||
var positions = GetPositions(PositionStatus.Filled);
|
||||
_logger.LogInformation("Monitoring {FilledPositionCount} filled positions", positions.Count());
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
using (_logger.BeginScope("Position {PositionId} ({Ticker})", position.Identifier, position.Ticker))
|
||||
{
|
||||
try
|
||||
{
|
||||
// Acquire processing lock
|
||||
_logger.LogDebug("Acquiring position lock");
|
||||
position.Status = PositionStatus.Updating;
|
||||
_tradingService.UpdatePosition(position);
|
||||
|
||||
_logger.LogInformation("Checking position state on exchange - Direction: {Direction}, Open Since: {OpenDate}",
|
||||
position.OriginDirection, position.Date.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
var account = await GetAccount(position.AccountName);
|
||||
|
||||
// Check if position still exists on broker
|
||||
var brokerPositions = await _exchangeService.GetBrokerPositions(account);
|
||||
var exchangePosition = brokerPositions.FirstOrDefault(p =>
|
||||
p.Ticker == position.Ticker &&
|
||||
p.OriginDirection == position.OriginDirection);
|
||||
|
||||
if (exchangePosition == null)
|
||||
{
|
||||
// Position no longer on exchange - it has been closed
|
||||
_logger.LogInformation("Position no longer on exchange - marking as finished");
|
||||
position.Status = PositionStatus.Finished;
|
||||
|
||||
// Determine if SL or TP was hit by checking which one is missing
|
||||
if (exchangePosition?.StopLoss == null && position.StopLoss.Status != TradeStatus.Filled)
|
||||
{
|
||||
_logger.LogInformation("Stop loss appears to have been hit");
|
||||
position.StopLoss.SetStatus(TradeStatus.Filled);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(
|
||||
position,
|
||||
position.StopLoss.Quantity,
|
||||
position.StopLoss.Price,
|
||||
position.Open.Leverage);
|
||||
}
|
||||
else if (exchangePosition?.TakeProfit1 == null && position.TakeProfit1.Status != TradeStatus.Filled)
|
||||
{
|
||||
_logger.LogInformation("Take profit 1 appears to have been hit");
|
||||
position.TakeProfit1.SetStatus(TradeStatus.Filled);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(
|
||||
position,
|
||||
position.TakeProfit1.Quantity,
|
||||
position.TakeProfit1.Price,
|
||||
position.Open.Leverage);
|
||||
}
|
||||
else if (exchangePosition?.TakeProfit2 == null && position.TakeProfit2?.Status != TradeStatus.Filled)
|
||||
{
|
||||
_logger.LogInformation("Take profit 2 appears to have been hit");
|
||||
position.TakeProfit2.SetStatus(TradeStatus.Filled);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(
|
||||
position,
|
||||
position.TakeProfit2.Quantity,
|
||||
position.TakeProfit2.Price,
|
||||
position.Open.Leverage);
|
||||
}
|
||||
|
||||
// Cancel any remaining orders
|
||||
await _exchangeService.CancelOrder(account, position.Ticker);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Position still exists - update with exchange data
|
||||
_logger.LogInformation("Position still active on exchange with quantity {Quantity}",
|
||||
exchangePosition.Open?.Quantity ?? 0);
|
||||
|
||||
// Update our position with broker data
|
||||
if (exchangePosition.Open != null)
|
||||
position.Open = exchangePosition.Open;
|
||||
|
||||
if (exchangePosition.StopLoss != null)
|
||||
position.StopLoss = exchangePosition.StopLoss;
|
||||
|
||||
if (exchangePosition.TakeProfit1 != null)
|
||||
position.TakeProfit1 = exchangePosition.TakeProfit1;
|
||||
|
||||
if (exchangePosition.TakeProfit2 != null)
|
||||
position.TakeProfit2 = exchangePosition.TakeProfit2;
|
||||
|
||||
if (exchangePosition.ProfitAndLoss != null)
|
||||
position.ProfitAndLoss = exchangePosition.ProfitAndLoss;
|
||||
else
|
||||
{
|
||||
// Calculate PNL if not provided
|
||||
var lastPrice = _exchangeService.GetPrice(account, position.Ticker, DateTime.UtcNow);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(
|
||||
position,
|
||||
position.Open.Quantity,
|
||||
lastPrice,
|
||||
position.Open.Leverage);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Updated position from exchange - PNL: {PNL}",
|
||||
position.ProfitAndLoss?.Net ?? 0);
|
||||
|
||||
// Keep status as Filled
|
||||
position.Status = PositionStatus.Filled;
|
||||
}
|
||||
|
||||
_tradingService.UpdatePosition(position);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to manage position - {ErrorMessage}", ex.Message);
|
||||
|
||||
// Reset status for retry
|
||||
position.Status = PositionStatus.Filled;
|
||||
_tradingService.UpdatePosition(position);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Ensure lock is always released
|
||||
if (position.Status == PositionStatus.Updating)
|
||||
{
|
||||
position.Status = PositionStatus.Filled;
|
||||
_tradingService.UpdatePosition(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Position> GetPositions(PositionStatus positionStatus)
|
||||
{
|
||||
return _tradingService.GetPositionsByStatus(positionStatus)
|
||||
.Where(p => p.Initiator != PositionInitiator.PaperTrading);
|
||||
}
|
||||
|
||||
private async Task ManageNewPositions()
|
||||
{
|
||||
var positions = GetPositions(PositionStatus.New);
|
||||
|
||||
_logger.LogInformation("Processing {NewPositionCount} new positions", positions.Count());
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
using (_logger.BeginScope("Position {Identifier}", position.Identifier))
|
||||
{
|
||||
try
|
||||
{
|
||||
// Immediate status update for concurrency protection
|
||||
_logger.LogDebug("[{Identifier}] Acquiring position lock via status update", position.Identifier);
|
||||
position.Status = PositionStatus.Updating;
|
||||
_tradingService.UpdatePosition(position);
|
||||
|
||||
var account = await GetAccount(position.AccountName);
|
||||
|
||||
// Check if position exists on broker
|
||||
var brokerPositions = await _exchangeService.GetBrokerPositions(account);
|
||||
var exchangePosition = brokerPositions.FirstOrDefault(p =>
|
||||
p.Ticker == position.Ticker &&
|
||||
p.OriginDirection == position.OriginDirection);
|
||||
|
||||
if (exchangePosition != null)
|
||||
{
|
||||
// Position is confirmed on exchange
|
||||
position.Status = PositionStatus.PartiallyFilled;
|
||||
position.Open = exchangePosition.Open; // Use the exchange data
|
||||
_tradingService.UpdatePosition(position);
|
||||
|
||||
_logger.LogInformation("[{Identifier}] Position found on exchange - moving to partially filled status",
|
||||
position.Identifier);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Position not found on exchange - check for staleness
|
||||
if (position.Date < DateTime.UtcNow.AddDays(-1))
|
||||
{
|
||||
position.Status = PositionStatus.Canceled;
|
||||
_tradingService.UpdatePosition(position);
|
||||
_logger.LogWarning("[{Identifier}] Position canceled - stale since {PositionAge} days",
|
||||
position.Identifier,
|
||||
(DateTime.UtcNow - position.Date).TotalDays);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Reset status to try again
|
||||
position.Status = PositionStatus.New;
|
||||
_tradingService.UpdatePosition(position);
|
||||
_logger.LogInformation("[{Identifier}] Position not yet found on exchange - awaiting fill",
|
||||
position.Identifier);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error processing position {Identifier}", position.Identifier);
|
||||
// Reset to New status for retry
|
||||
position.Status = PositionStatus.New;
|
||||
_tradingService.UpdatePosition(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Account> GetAccount(string accountName)
|
||||
{
|
||||
var account = _cacheService.GetValue<Account>(accountName);
|
||||
|
||||
if (account == null)
|
||||
{
|
||||
account = await _accountService.GetAccount(accountName, false, false);
|
||||
_cacheService.SaveValue(accountName, JsonConvert.SerializeObject(account));
|
||||
}
|
||||
|
||||
return account;
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public abstract class PricesBaseWorker<T> : BaseWorker<T> where T : class
|
||||
{
|
||||
private readonly IPricesService _pricesService;
|
||||
private readonly IStatisticService _statisticService;
|
||||
private readonly Timeframe _timeframe;
|
||||
|
||||
public PricesBaseWorker(
|
||||
ILogger<T> logger,
|
||||
IPricesService pricesService,
|
||||
IWorkerService workerService,
|
||||
IStatisticService statisticService,
|
||||
TimeSpan delay,
|
||||
WorkerType workerType,
|
||||
Timeframe timeframe) : base(
|
||||
workerType,
|
||||
logger,
|
||||
delay,
|
||||
workerService
|
||||
)
|
||||
{
|
||||
_pricesService = pricesService;
|
||||
_statisticService = statisticService;
|
||||
_timeframe = timeframe;
|
||||
}
|
||||
|
||||
private List<Ticker> _eligibleTickers = new List<Ticker>
|
||||
{
|
||||
Ticker.BTC,
|
||||
Ticker.ETH,
|
||||
Ticker.BNB,
|
||||
Ticker.DOGE,
|
||||
Ticker.ADA,
|
||||
Ticker.SOL,
|
||||
Ticker.XRP,
|
||||
Ticker.LINK,
|
||||
Ticker.RENDER,
|
||||
Ticker.SUI,
|
||||
Ticker.GMX,
|
||||
Ticker.ARB,
|
||||
Ticker.PEPE,
|
||||
Ticker.PENDLE,
|
||||
Ticker.AAVE,
|
||||
Ticker.HYPE
|
||||
};
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
var tickers = await _statisticService.GetTickers();
|
||||
|
||||
var filteredTickers = tickers
|
||||
.Where(t => _eligibleTickers.Contains(t))
|
||||
.ToList();
|
||||
|
||||
// Filter with the eligible tickers
|
||||
foreach (var ticker in filteredTickers)
|
||||
{
|
||||
await _pricesService.UpdatePrice(TradingExchanges.Evm, ticker, _timeframe);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class PricesFifteenMinutesWorker : PricesBaseWorker<PricesFifteenMinutesWorker>
|
||||
{
|
||||
public PricesFifteenMinutesWorker(
|
||||
ILogger<PricesFifteenMinutesWorker> logger,
|
||||
IPricesService pricesService,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
logger,
|
||||
pricesService,
|
||||
workerService,
|
||||
statisticService,
|
||||
TimeSpan.FromMinutes(1),
|
||||
WorkerType.PriceFifteenMinutes,
|
||||
Timeframe.FifteenMinutes
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class PricesFiveMinutesWorker : PricesBaseWorker<PricesFiveMinutesWorker>
|
||||
{
|
||||
public PricesFiveMinutesWorker(
|
||||
ILogger<PricesFiveMinutesWorker> logger,
|
||||
IPricesService pricesService,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
logger,
|
||||
pricesService,
|
||||
workerService,
|
||||
statisticService,
|
||||
TimeSpan.FromMinutes(1),
|
||||
WorkerType.PriceFiveMinutes,
|
||||
Timeframe.FiveMinutes
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class PricesFourHoursWorker : PricesBaseWorker<PricesFourHoursWorker>
|
||||
{
|
||||
public PricesFourHoursWorker(
|
||||
ILogger<PricesFourHoursWorker> logger,
|
||||
IPricesService pricesService,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
logger,
|
||||
pricesService,
|
||||
workerService,
|
||||
statisticService,
|
||||
TimeSpan.FromHours(2),
|
||||
WorkerType.PriceFourHour,
|
||||
Timeframe.FourHour
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class PricesOneDayWorker : PricesBaseWorker<PricesOneDayWorker>
|
||||
{
|
||||
public PricesOneDayWorker(
|
||||
ILogger<PricesOneDayWorker> logger,
|
||||
IPricesService pricesService,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
logger,
|
||||
pricesService,
|
||||
workerService,
|
||||
statisticService,
|
||||
TimeSpan.FromHours(12),
|
||||
WorkerType.PriceOneDay,
|
||||
Timeframe.OneDay
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class PricesOneHourWorker : PricesBaseWorker<PricesOneHourWorker>
|
||||
{
|
||||
public PricesOneHourWorker(
|
||||
ILogger<PricesOneHourWorker> logger,
|
||||
IPricesService pricesService,
|
||||
IStatisticService statisticService,
|
||||
IWorkerService workerService) : base(
|
||||
logger,
|
||||
pricesService,
|
||||
workerService,
|
||||
statisticService,
|
||||
TimeSpan.FromMinutes(30),
|
||||
WorkerType.PriceOneHour,
|
||||
Timeframe.OneHour)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Common;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class SpotlightWorker : BaseWorker<SpotlightWorker>
|
||||
{
|
||||
private readonly IStatisticService _statisticService;
|
||||
|
||||
public SpotlightWorker(
|
||||
ILogger<SpotlightWorker> logger,
|
||||
IWorkerService workerService,
|
||||
IStatisticService statisticService) : base(
|
||||
Enums.WorkerType.Spotlight,
|
||||
logger,
|
||||
TimeSpan.FromMinutes(5),
|
||||
workerService)
|
||||
{
|
||||
_statisticService = statisticService;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _statisticService.UpdateSpotlight();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Enable to update spotlight", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
public class TopVolumeTickerWorker : BaseWorker<TopVolumeTickerWorker>
|
||||
{
|
||||
private readonly IStatisticService _statisticService;
|
||||
private static readonly WorkerType _workerType = WorkerType.TopVolumeTicker;
|
||||
|
||||
public TopVolumeTickerWorker(
|
||||
ILogger<TopVolumeTickerWorker> logger,
|
||||
IWorkerService workerService,
|
||||
IStatisticService statisticService) : base(
|
||||
_workerType,
|
||||
logger,
|
||||
TimeSpan.FromHours(12),
|
||||
workerService
|
||||
)
|
||||
{
|
||||
_statisticService = statisticService;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await _statisticService.UpdateTopVolumeTicker(TradingExchanges.Evm, 10);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Workers.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a worker that watches traders and performs actions based on trading activities.
|
||||
/// Inherits from <see cref="BaseWorker{TWorker}"/> where TWorker is <see cref="FeeWorker"/>.
|
||||
/// </summary>
|
||||
public class TraderWatcher : BaseWorker<FeeWorker>
|
||||
{
|
||||
private readonly ITradingService _tradingService;
|
||||
private static readonly WorkerType _workerType = WorkerType.TraderWatcher;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TraderWatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger to be used by the worker.</param>
|
||||
/// <param name="tradingService">The trading service to monitor trading activities.</param>
|
||||
/// <param name="workerService">The worker service to manage worker lifecycle.</param>
|
||||
public TraderWatcher(
|
||||
ILogger<FeeWorker> logger,
|
||||
ITradingService tradingService,
|
||||
IWorkerService workerService) : base(
|
||||
_workerType,
|
||||
logger,
|
||||
TimeSpan.FromSeconds(120),
|
||||
workerService
|
||||
)
|
||||
{
|
||||
_tradingService = tradingService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the worker's task to watch traders. This method is called periodically based on the worker's configured interval.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await _tradingService.WatchTrader();
|
||||
}
|
||||
}
|
||||
@@ -24,5 +24,17 @@
|
||||
"ElasticConfiguration": {
|
||||
"Uri": "http://localhost:9200"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"WorkerPricesFifteenMinutes": true,
|
||||
"WorkerPricesOneHour": true,
|
||||
"WorkerPricesFourHours": true,
|
||||
"WorkerPricesOneDay": true,
|
||||
"WorkerPricesFiveMinutes": false,
|
||||
"WorkerFee": false,
|
||||
"WorkerPositionManager": false,
|
||||
"WorkerPositionFetcher": false,
|
||||
"WorkerSpotlight": false,
|
||||
"WorkerTraderWatcher": false,
|
||||
"WorkerLeaderboard": false,
|
||||
"WorkerFundingRatesWatcher": false
|
||||
}
|
||||
Reference in New Issue
Block a user