Add bot worker
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using Managing.Application.Abstractions.Services;
|
using Managing.Application.Abstractions.Services;
|
||||||
|
using Managing.Application.Workers;
|
||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers.Abstractions;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers;
|
||||||
|
using Managing.Application.Workers.Abstractions;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
namespace Managing.Api.Workers.Workers;
|
namespace Managing.Api.Workers.Workers;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers;
|
||||||
|
using Managing.Application.Workers.Abstractions;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
namespace Managing.Api.Workers.Workers;
|
namespace Managing.Api.Workers.Workers;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Managing.Application.Abstractions.Services;
|
using Managing.Application.Abstractions.Services;
|
||||||
using Managing.Application.Hubs;
|
using Managing.Application.Hubs;
|
||||||
|
using Managing.Application.Workers;
|
||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers.Abstractions;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Managing.Application.Abstractions.Services;
|
using Managing.Application.Abstractions.Services;
|
||||||
|
using Managing.Application.Workers;
|
||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers.Abstractions;
|
||||||
using Managing.Domain.Trades;
|
using Managing.Domain.Trades;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers;
|
||||||
|
using Managing.Application.Workers.Abstractions;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
namespace Managing.Api.Workers.Workers;
|
namespace Managing.Api.Workers.Workers;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers;
|
||||||
|
using Managing.Application.Workers.Abstractions;
|
||||||
using Managing.Common;
|
using Managing.Common;
|
||||||
|
|
||||||
namespace Managing.Api.Workers.Workers;
|
namespace Managing.Api.Workers.Workers;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Managing.Application.Abstractions.Services;
|
using Managing.Application.Abstractions.Services;
|
||||||
|
using Managing.Application.Workers;
|
||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers.Abstractions;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Text.Json.Serialization;
|
|||||||
using Managing.Api.Authorization;
|
using Managing.Api.Authorization;
|
||||||
using Managing.Api.Exceptions;
|
using Managing.Api.Exceptions;
|
||||||
using Managing.Api.Filters;
|
using Managing.Api.Filters;
|
||||||
|
using Managing.Api.Workers;
|
||||||
using Managing.Application.Hubs;
|
using Managing.Application.Hubs;
|
||||||
using Managing.Bootstrap;
|
using Managing.Bootstrap;
|
||||||
using Managing.Common;
|
using Managing.Common;
|
||||||
@@ -126,6 +127,7 @@ builder.Services.AddSwaggerGen(options =>
|
|||||||
});
|
});
|
||||||
|
|
||||||
builder.WebHost.SetupDiscordBot();
|
builder.WebHost.SetupDiscordBot();
|
||||||
|
builder.Services.AddHostedService<BotManagerWorker>();
|
||||||
|
|
||||||
// App
|
// App
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|||||||
32
src/Managing.Api/Workers/BotManagerWorker.cs
Normal file
32
src/Managing.Api/Workers/BotManagerWorker.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using Managing.Application.ManageBot;
|
||||||
|
using Managing.Application.Workers;
|
||||||
|
using Managing.Application.Workers.Abstractions;
|
||||||
|
using MediatR;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
|
namespace Managing.Api.Workers;
|
||||||
|
|
||||||
|
public class BotManagerWorker : BaseWorker<BotManagerWorker>
|
||||||
|
{
|
||||||
|
private readonly IMediator _mediator;
|
||||||
|
private static readonly WorkerType _workerType = WorkerType.Fee;
|
||||||
|
|
||||||
|
public BotManagerWorker(
|
||||||
|
ILogger<BotManagerWorker> logger,
|
||||||
|
IMediator tradingService,
|
||||||
|
IWorkerService workerService) : base(
|
||||||
|
_workerType,
|
||||||
|
logger,
|
||||||
|
TimeSpan.FromMinutes(1),
|
||||||
|
workerService
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_mediator = tradingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task Run(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var loadBackupBotCommand = new LoadBackupBotCommand();
|
||||||
|
await _mediator.Send(loadBackupBotCommand, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
|
|
||||||
using Managing.Application.Workers.Abstractions;
|
using Managing.Application.Workers.Abstractions;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
namespace Managing.Api.Workers;
|
namespace Managing.Application.Workers;
|
||||||
|
|
||||||
public abstract class BaseWorker<T> : BackgroundService where T : class
|
public abstract class BaseWorker<T> : BackgroundService where T : class
|
||||||
{
|
{
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
|
using Managing.Application.Bots;
|
||||||
|
using Managing.Common;
|
||||||
|
|
||||||
public interface IBotService
|
public interface IBotService
|
||||||
{
|
{
|
||||||
void SaveBotBackup(BotBackup botBackup);
|
void SaveBotBackup(BotBackup botBackup);
|
||||||
|
void SaveBotBackup(string name, Enums.BotType botType, string data);
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,7 @@ namespace Managing.Application.Bots.Base
|
|||||||
private readonly IAccountService _accountService;
|
private readonly IAccountService _accountService;
|
||||||
private readonly ILogger<TradingBot> _tradingBotLogger;
|
private readonly ILogger<TradingBot> _tradingBotLogger;
|
||||||
private readonly ITradingService _tradingService;
|
private readonly ITradingService _tradingService;
|
||||||
|
private readonly IBotService _botService;
|
||||||
|
|
||||||
public BotFactory(
|
public BotFactory(
|
||||||
IExchangeService exchangeService,
|
IExchangeService exchangeService,
|
||||||
@@ -23,7 +24,8 @@ namespace Managing.Application.Bots.Base
|
|||||||
IMoneyManagementService moneyManagementService,
|
IMoneyManagementService moneyManagementService,
|
||||||
IMessengerService messengerService,
|
IMessengerService messengerService,
|
||||||
IAccountService accountService,
|
IAccountService accountService,
|
||||||
ITradingService tradingService)
|
ITradingService tradingService,
|
||||||
|
IBotService botService)
|
||||||
{
|
{
|
||||||
_tradingBotLogger = tradingBotLogger;
|
_tradingBotLogger = tradingBotLogger;
|
||||||
_exchangeService = exchangeService;
|
_exchangeService = exchangeService;
|
||||||
@@ -31,11 +33,12 @@ namespace Managing.Application.Bots.Base
|
|||||||
_messengerService = messengerService;
|
_messengerService = messengerService;
|
||||||
_accountService = accountService;
|
_accountService = accountService;
|
||||||
_tradingService = tradingService;
|
_tradingService = tradingService;
|
||||||
|
_botService = botService;
|
||||||
}
|
}
|
||||||
|
|
||||||
IBot IBotFactory.CreateSimpleBot(string botName, Workflow workflow)
|
IBot IBotFactory.CreateSimpleBot(string botName, Workflow workflow)
|
||||||
{
|
{
|
||||||
return new SimpleBot(botName, _tradingBotLogger, workflow);
|
return new SimpleBot(botName, _tradingBotLogger, workflow, _botService);
|
||||||
}
|
}
|
||||||
|
|
||||||
ITradingBot IBotFactory.CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name, Ticker ticker, string scenario, Timeframe interval, bool isForWatchingOnly)
|
ITradingBot IBotFactory.CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name, Ticker ticker, string scenario, Timeframe interval, bool isForWatchingOnly)
|
||||||
@@ -52,6 +55,7 @@ namespace Managing.Application.Bots.Base
|
|||||||
interval,
|
interval,
|
||||||
_accountService,
|
_accountService,
|
||||||
_messengerService,
|
_messengerService,
|
||||||
|
_botService,
|
||||||
isForWatchingOnly: isForWatchingOnly);
|
isForWatchingOnly: isForWatchingOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +73,7 @@ namespace Managing.Application.Bots.Base
|
|||||||
interval,
|
interval,
|
||||||
_accountService,
|
_accountService,
|
||||||
_messengerService,
|
_messengerService,
|
||||||
|
_botService,
|
||||||
true,
|
true,
|
||||||
isForWatchingOnly);
|
isForWatchingOnly);
|
||||||
}
|
}
|
||||||
@@ -87,6 +92,7 @@ namespace Managing.Application.Bots.Base
|
|||||||
interval,
|
interval,
|
||||||
_accountService,
|
_accountService,
|
||||||
_messengerService,
|
_messengerService,
|
||||||
|
_botService,
|
||||||
isForWatchingOnly: isForWatchingOnly);
|
isForWatchingOnly: isForWatchingOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,6 +110,7 @@ namespace Managing.Application.Bots.Base
|
|||||||
interval,
|
interval,
|
||||||
_accountService,
|
_accountService,
|
||||||
_messengerService,
|
_messengerService,
|
||||||
|
_botService,
|
||||||
true,
|
true,
|
||||||
isForWatchingOnly);
|
isForWatchingOnly);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace Managing.Application.Bots
|
|||||||
Timeframe timeframe,
|
Timeframe timeframe,
|
||||||
IAccountService accountService,
|
IAccountService accountService,
|
||||||
IMessengerService messengerService,
|
IMessengerService messengerService,
|
||||||
|
IBotService botService,
|
||||||
bool isForBacktest = false,
|
bool isForBacktest = false,
|
||||||
bool isForWatchingOnly = false)
|
bool isForWatchingOnly = false)
|
||||||
: base(accountName,
|
: base(accountName,
|
||||||
@@ -31,6 +32,7 @@ namespace Managing.Application.Bots
|
|||||||
timeframe,
|
timeframe,
|
||||||
accountService,
|
accountService,
|
||||||
messengerService,
|
messengerService,
|
||||||
|
botService,
|
||||||
isForBacktest,
|
isForBacktest,
|
||||||
isForWatchingOnly,
|
isForWatchingOnly,
|
||||||
flipPosition: true)
|
flipPosition: true)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace Managing.Application.Bots
|
|||||||
Timeframe timeframe,
|
Timeframe timeframe,
|
||||||
IAccountService accountService,
|
IAccountService accountService,
|
||||||
IMessengerService messengerService,
|
IMessengerService messengerService,
|
||||||
|
IBotService botService,
|
||||||
bool isForBacktest = false,
|
bool isForBacktest = false,
|
||||||
bool isForWatchingOnly = false)
|
bool isForWatchingOnly = false)
|
||||||
: base(accountName,
|
: base(accountName,
|
||||||
@@ -31,6 +32,7 @@ namespace Managing.Application.Bots
|
|||||||
timeframe,
|
timeframe,
|
||||||
accountService,
|
accountService,
|
||||||
messengerService,
|
messengerService,
|
||||||
|
botService,
|
||||||
isForBacktest,
|
isForBacktest,
|
||||||
isForWatchingOnly)
|
isForWatchingOnly)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,17 +2,20 @@
|
|||||||
using Managing.Domain.Workflows;
|
using Managing.Domain.Workflows;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
namespace Managing.Application.Bots
|
namespace Managing.Application.Bots
|
||||||
{
|
{
|
||||||
public class SimpleBot : Bot
|
public class SimpleBot : Bot
|
||||||
{
|
{
|
||||||
public readonly ILogger<TradingBot> Logger;
|
public readonly ILogger<TradingBot> Logger;
|
||||||
|
private readonly IBotService _botService;
|
||||||
private Workflow _workflow;
|
private Workflow _workflow;
|
||||||
|
|
||||||
public SimpleBot(string name, ILogger<TradingBot> logger, Workflow workflow) : base(name)
|
public SimpleBot(string name, ILogger<TradingBot> logger, Workflow workflow, IBotService botService) : base(name)
|
||||||
{
|
{
|
||||||
Logger = logger;
|
Logger = logger;
|
||||||
|
_botService = botService;
|
||||||
_workflow = workflow;
|
_workflow = workflow;
|
||||||
Interval = 100;
|
Interval = 100;
|
||||||
Start();
|
Start();
|
||||||
@@ -33,13 +36,15 @@ namespace Managing.Application.Bots
|
|||||||
Logger.LogInformation(Identifier);
|
Logger.LogInformation(Identifier);
|
||||||
Logger.LogInformation(DateTime.Now.ToString());
|
Logger.LogInformation(DateTime.Now.ToString());
|
||||||
await _workflow.Execute();
|
await _workflow.Execute();
|
||||||
|
SaveBackup();
|
||||||
Logger.LogInformation("__________________________________________________");
|
Logger.LogInformation("__________________________________________________");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string GetBackup()
|
public override void SaveBackup()
|
||||||
{
|
{
|
||||||
return JsonConvert.SerializeObject(_workflow);
|
var data = JsonConvert.SerializeObject(_workflow);
|
||||||
|
_botService.SaveBotBackup(Name, BotType.SimpleBot, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void LoadBackup(BotBackup backup)
|
public override void LoadBackup(BotBackup backup)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
public readonly IMessengerService MessengerService;
|
public readonly IMessengerService MessengerService;
|
||||||
public readonly IAccountService AccountService;
|
public readonly IAccountService AccountService;
|
||||||
private readonly ITradingService TradingService;
|
private readonly ITradingService TradingService;
|
||||||
|
private readonly IBotService BotService;
|
||||||
|
|
||||||
public Account Account { get; set; }
|
public Account Account { get; set; }
|
||||||
public HashSet<IStrategy> Strategies { get; set; }
|
public HashSet<IStrategy> Strategies { get; set; }
|
||||||
@@ -55,6 +56,7 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
Timeframe timeframe,
|
Timeframe timeframe,
|
||||||
IAccountService accountService,
|
IAccountService accountService,
|
||||||
IMessengerService messengerService,
|
IMessengerService messengerService,
|
||||||
|
IBotService botService,
|
||||||
bool isForBacktest = false,
|
bool isForBacktest = false,
|
||||||
bool isForWatchingOnly = false,
|
bool isForWatchingOnly = false,
|
||||||
bool flipPosition = false)
|
bool flipPosition = false)
|
||||||
@@ -64,7 +66,7 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
AccountService = accountService;
|
AccountService = accountService;
|
||||||
MessengerService = messengerService;
|
MessengerService = messengerService;
|
||||||
TradingService = tradingService;
|
TradingService = tradingService;
|
||||||
|
BotService = botService;
|
||||||
|
|
||||||
IsForWatchingOnly = isForWatchingOnly;
|
IsForWatchingOnly = isForWatchingOnly;
|
||||||
FlipPosition = flipPosition;
|
FlipPosition = flipPosition;
|
||||||
@@ -168,6 +170,9 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
if (!IsForWatchingOnly)
|
if (!IsForWatchingOnly)
|
||||||
await ManagePositions();
|
await ManagePositions();
|
||||||
|
|
||||||
|
if (!IsForBacktest)
|
||||||
|
SaveBackup();
|
||||||
|
|
||||||
await UpdateWalletBalances();
|
await UpdateWalletBalances();
|
||||||
Logger.LogInformation($"Candles : {Candles.Count}");
|
Logger.LogInformation($"Candles : {Candles.Count}");
|
||||||
Logger.LogInformation($"Signals : {Signals.Count}");
|
Logger.LogInformation($"Signals : {Signals.Count}");
|
||||||
@@ -659,9 +664,9 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string GetBackup()
|
public override void SaveBackup()
|
||||||
{
|
{
|
||||||
return JsonConvert.SerializeObject(new TradingBotBackup
|
var data = new TradingBotBackup
|
||||||
{
|
{
|
||||||
Name = Name,
|
Name = Name,
|
||||||
BotType = BotType,
|
BotType = BotType,
|
||||||
@@ -675,7 +680,8 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
IsForWatchingOnly = IsForWatchingOnly,
|
IsForWatchingOnly = IsForWatchingOnly,
|
||||||
WalletBalances = WalletBalances,
|
WalletBalances = WalletBalances,
|
||||||
MoneyManagement = MoneyManagement
|
MoneyManagement = MoneyManagement
|
||||||
});
|
};
|
||||||
|
BotService.SaveBotBackup(Name, BotType, JsonConvert.SerializeObject(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void LoadBackup(BotBackup backup)
|
public override void LoadBackup(BotBackup backup)
|
||||||
|
|||||||
@@ -1,63 +0,0 @@
|
|||||||
using Managing.Domain.Bots;
|
|
||||||
using MediatR;
|
|
||||||
using static Managing.Common.Enums;
|
|
||||||
using Managing.Application.Abstractions;
|
|
||||||
|
|
||||||
namespace Managing.Application.ManageBot
|
|
||||||
{
|
|
||||||
public class BackupBotCommandHandler : IRequestHandler<BackupBotCommand, bool>
|
|
||||||
{
|
|
||||||
private readonly ITaskCache _taskCache;
|
|
||||||
private readonly IBotService _botService;
|
|
||||||
|
|
||||||
public BackupBotCommandHandler(ITaskCache taskCache, IBotService botService)
|
|
||||||
{
|
|
||||||
_taskCache = taskCache;
|
|
||||||
_botService = botService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task<bool> Handle(BackupBotCommand request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var botBackup = new BotBackup
|
|
||||||
{
|
|
||||||
Name = request.Name,
|
|
||||||
BotType = request.BotType,
|
|
||||||
Data = ""
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (request.BotType)
|
|
||||||
{
|
|
||||||
case BotType.SimpleBot:
|
|
||||||
var simpleBot = _taskCache.Get<IBot>(request.Name);
|
|
||||||
botBackup.Data = simpleBot.GetBackup();
|
|
||||||
break;
|
|
||||||
case BotType.ScalpingBot:
|
|
||||||
var scalpingBot = _taskCache.Get<ITradingBot>(request.Name);
|
|
||||||
botBackup.Data = scalpingBot.GetBackup();
|
|
||||||
break;
|
|
||||||
case BotType.FlippingBot:
|
|
||||||
var flippingBot = _taskCache.Get<ITradingBot>(request.Name);
|
|
||||||
botBackup.Data = flippingBot.GetBackup();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return Task.FromResult(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
_botService.SaveBotBackup(botBackup);
|
|
||||||
|
|
||||||
return Task.FromResult(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class BackupBotCommand : IRequest<bool>
|
|
||||||
{
|
|
||||||
public string Name { get; }
|
|
||||||
public BotType BotType { get; }
|
|
||||||
|
|
||||||
public BackupBotCommand(BotType botType, string name)
|
|
||||||
{
|
|
||||||
BotType = botType;
|
|
||||||
Name = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +1,33 @@
|
|||||||
using Managing.Application.Abstractions;
|
using Managing.Application.Abstractions;
|
||||||
|
using Managing.Application.Bots;
|
||||||
|
using Managing.Common;
|
||||||
|
|
||||||
namespace Managing.Application.ManageBot
|
namespace Managing.Application.ManageBot
|
||||||
{
|
{
|
||||||
public class BotService : IBotService
|
public class BotService : IBotService
|
||||||
{
|
{
|
||||||
private readonly IBotFactory _botFactory;
|
|
||||||
private readonly IBotRepository _botRepository;
|
private readonly IBotRepository _botRepository;
|
||||||
|
|
||||||
public BotService(IBotFactory botFactory, IBotRepository botRepository)
|
public BotService(IBotRepository botRepository)
|
||||||
{
|
{
|
||||||
_botFactory = botFactory;
|
|
||||||
_botRepository = botRepository;
|
_botRepository = botRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void CreateBot()
|
|
||||||
// {
|
|
||||||
// // Use the factory to create a new bot
|
|
||||||
// return _botFactory.CreateBot();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public void LoadBotBackup(BotBackup botBackup)
|
|
||||||
// {
|
|
||||||
// // Deserialize the JSON into a Bot object
|
|
||||||
// var bot = JsonConvert.DeserializeObject<Bot>(json);
|
|
||||||
|
|
||||||
// return bot;
|
|
||||||
// }
|
|
||||||
|
|
||||||
public async void SaveBotBackup(BotBackup botBackup)
|
public async void SaveBotBackup(BotBackup botBackup)
|
||||||
{
|
{
|
||||||
await _botRepository.InsertBotAsync(botBackup);
|
await _botRepository.InsertBotAsync(botBackup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SaveBotBackup(string name, Enums.BotType botType, string data)
|
||||||
|
{
|
||||||
|
var botBackup = new BotBackup
|
||||||
|
{
|
||||||
|
Name = name,
|
||||||
|
BotType = botType,
|
||||||
|
Data = data
|
||||||
|
};
|
||||||
|
|
||||||
|
_botRepository.InsertBotAsync(botBackup);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,9 @@ using MediatR;
|
|||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
using Managing.Application.Abstractions;
|
using Managing.Application.Abstractions;
|
||||||
using Managing.Core;
|
using Managing.Core;
|
||||||
using Managing.Domain.MoneyManagements;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Managing.Application.Bots;
|
using Managing.Application.Bots;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Managing.Application.ManageBot
|
namespace Managing.Application.ManageBot
|
||||||
{
|
{
|
||||||
@@ -15,20 +15,18 @@ namespace Managing.Application.ManageBot
|
|||||||
private readonly ITaskCache _taskCache;
|
private readonly ITaskCache _taskCache;
|
||||||
private readonly IMoneyManagementService _moneyManagementService;
|
private readonly IMoneyManagementService _moneyManagementService;
|
||||||
private readonly IBotRepository _botRepository;
|
private readonly IBotRepository _botRepository;
|
||||||
private readonly IMediator _mediator;
|
private readonly ILogger<LoadBackupBotCommandHandler> _logger;
|
||||||
|
|
||||||
public LoadBackupBotCommandHandler(
|
public LoadBackupBotCommandHandler(
|
||||||
IBotFactory botFactory,
|
IBotFactory botFactory,
|
||||||
ITaskCache taskCache,
|
ITaskCache taskCache,
|
||||||
IMoneyManagementService moneyManagementService,
|
IMoneyManagementService moneyManagementService,
|
||||||
IBotRepository botRepository,
|
IBotRepository botRepository)
|
||||||
IMediator mediator)
|
|
||||||
{
|
{
|
||||||
_botFactory = botFactory;
|
_botFactory = botFactory;
|
||||||
_taskCache = taskCache;
|
_taskCache = taskCache;
|
||||||
_moneyManagementService = moneyManagementService;
|
_moneyManagementService = moneyManagementService;
|
||||||
_botRepository = botRepository;
|
_botRepository = botRepository;
|
||||||
_mediator = mediator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<string> Handle(LoadBackupBotCommand request, CancellationToken cancellationToken)
|
public Task<string> Handle(LoadBackupBotCommand request, CancellationToken cancellationToken)
|
||||||
@@ -37,6 +35,8 @@ namespace Managing.Application.ManageBot
|
|||||||
var backupBots = _botRepository.GetBots();
|
var backupBots = _botRepository.GetBots();
|
||||||
var result = new Dictionary<string, BotStatus>();
|
var result = new Dictionary<string, BotStatus>();
|
||||||
|
|
||||||
|
_logger.LogInformation($"Loading {backupBots.Count()} backup bots");
|
||||||
|
|
||||||
foreach (var backupBot in backupBots)
|
foreach (var backupBot in backupBots)
|
||||||
{
|
{
|
||||||
// Check if bot is existing in cache
|
// Check if bot is existing in cache
|
||||||
@@ -46,7 +46,8 @@ namespace Managing.Application.ManageBot
|
|||||||
var simpleBot = _taskCache.Get<IBot>(backupBot.Name);
|
var simpleBot = _taskCache.Get<IBot>(backupBot.Name);
|
||||||
if (simpleBot == null)
|
if (simpleBot == null)
|
||||||
{
|
{
|
||||||
StartBot(request, backupBot);
|
_logger.LogInformation($"Starting backup bot {backupBot.Name}");
|
||||||
|
StartBot(backupBot);
|
||||||
simpleBot.LoadBackup(backupBot);
|
simpleBot.LoadBackup(backupBot);
|
||||||
result.Add(simpleBot.GetName(), BotStatus.Backup);
|
result.Add(simpleBot.GetName(), BotStatus.Backup);
|
||||||
}
|
}
|
||||||
@@ -61,7 +62,8 @@ namespace Managing.Application.ManageBot
|
|||||||
var scalpingBot = _taskCache.Get<ITradingBot>(backupBot.Name);
|
var scalpingBot = _taskCache.Get<ITradingBot>(backupBot.Name);
|
||||||
if (scalpingBot == null)
|
if (scalpingBot == null)
|
||||||
{
|
{
|
||||||
StartBot(request, backupBot);
|
_logger.LogInformation($"Starting backup bot {backupBot.Name}");
|
||||||
|
StartBot(backupBot);
|
||||||
scalpingBot.LoadBackup(backupBot);
|
scalpingBot.LoadBackup(backupBot);
|
||||||
result.Add(scalpingBot.GetName(), BotStatus.Backup);
|
result.Add(scalpingBot.GetName(), BotStatus.Backup);
|
||||||
}
|
}
|
||||||
@@ -80,13 +82,13 @@ namespace Managing.Application.ManageBot
|
|||||||
return Task.FromResult(botStatus.ToString());
|
return Task.FromResult(botStatus.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartBot(LoadBackupBotCommand request, BotBackup backupBot)
|
private void StartBot(BotBackup backupBot)
|
||||||
{
|
{
|
||||||
switch (backupBot.BotType)
|
switch (backupBot.BotType)
|
||||||
{
|
{
|
||||||
case BotType.SimpleBot:
|
case BotType.SimpleBot:
|
||||||
Func<Task<IBot>> simpleBot = () => Task.FromResult(_botFactory.CreateSimpleBot(request.Name, null));
|
Func<Task<IBot>> simpleBot = () => Task.FromResult(_botFactory.CreateSimpleBot(backupBot.Name, null));
|
||||||
var bot1 = _taskCache.AddOrGetExisting(request.Name, simpleBot).Result;
|
var bot1 = _taskCache.AddOrGetExisting(backupBot.Name, simpleBot).Result;
|
||||||
bot1.LoadBackup(backupBot);
|
bot1.LoadBackup(backupBot);
|
||||||
break;
|
break;
|
||||||
case BotType.ScalpingBot:
|
case BotType.ScalpingBot:
|
||||||
@@ -99,7 +101,7 @@ namespace Managing.Application.ManageBot
|
|||||||
data.Scenario,
|
data.Scenario,
|
||||||
data.Timeframe,
|
data.Timeframe,
|
||||||
data.IsForWatchingOnly));
|
data.IsForWatchingOnly));
|
||||||
var bot2 = _taskCache.AddOrGetExisting(request.Name, scalpingBot).Result;
|
var bot2 = _taskCache.AddOrGetExisting(backupBot.Name, scalpingBot).Result;
|
||||||
bot2.LoadBackup(backupBot);
|
bot2.LoadBackup(backupBot);
|
||||||
break;
|
break;
|
||||||
case BotType.FlippingBot:
|
case BotType.FlippingBot:
|
||||||
@@ -112,7 +114,7 @@ namespace Managing.Application.ManageBot
|
|||||||
dataFlippingBot.Scenario,
|
dataFlippingBot.Scenario,
|
||||||
dataFlippingBot.Timeframe,
|
dataFlippingBot.Timeframe,
|
||||||
dataFlippingBot.IsForWatchingOnly));
|
dataFlippingBot.IsForWatchingOnly));
|
||||||
var bot3 = _taskCache.AddOrGetExisting(request.Name, flippingBot).Result;
|
var bot3 = _taskCache.AddOrGetExisting(backupBot.Name, flippingBot).Result;
|
||||||
bot3.LoadBackup(backupBot);
|
bot3.LoadBackup(backupBot);
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
@@ -121,7 +123,5 @@ namespace Managing.Application.ManageBot
|
|||||||
|
|
||||||
public class LoadBackupBotCommand : IRequest<string>
|
public class LoadBackupBotCommand : IRequest<string>
|
||||||
{
|
{
|
||||||
public string Name { get; internal set; }
|
|
||||||
public string AccountName { get; internal set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ public static class ApiBootstrap
|
|||||||
services.AddTransient<IStatisticRepository, StatisticRepository>();
|
services.AddTransient<IStatisticRepository, StatisticRepository>();
|
||||||
services.AddTransient<IWorkflowRepository, WorkflowRepository>();
|
services.AddTransient<IWorkflowRepository, WorkflowRepository>();
|
||||||
services.AddTransient<IBotRepository, BotRepository>();
|
services.AddTransient<IBotRepository, BotRepository>();
|
||||||
|
services.AddTransient<IWorkerRepository, WorkerRepository>();
|
||||||
|
|
||||||
// Cache
|
// Cache
|
||||||
services.AddDistributedMemoryCache();
|
services.AddDistributedMemoryCache();
|
||||||
@@ -126,6 +127,7 @@ public static class ApiBootstrap
|
|||||||
services.AddSingleton<IMessengerService, MessengerService>();
|
services.AddSingleton<IMessengerService, MessengerService>();
|
||||||
services.AddSingleton<IDiscordService, DiscordService>();
|
services.AddSingleton<IDiscordService, DiscordService>();
|
||||||
services.AddSingleton<IBotService, BotService>();
|
services.AddSingleton<IBotService, BotService>();
|
||||||
|
services.AddSingleton<IWorkerService, WorkerService>();
|
||||||
|
|
||||||
// Stream
|
// Stream
|
||||||
services.AddSingleton<IBinanceSocketClient, BinanceSocketClient>();
|
services.AddSingleton<IBinanceSocketClient, BinanceSocketClient>();
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ namespace Managing.Domain.Bots
|
|||||||
public int Interval { get; set; }
|
public int Interval { get; set; }
|
||||||
public BotStatus Status { get; set; }
|
public BotStatus Status { get; set; }
|
||||||
private CancellationTokenSource CancellationToken { get; set; }
|
private CancellationTokenSource CancellationToken { get; set; }
|
||||||
public string Data { get; set; }
|
|
||||||
|
|
||||||
public Bot(string name)
|
public Bot(string name)
|
||||||
{
|
{
|
||||||
@@ -74,7 +73,7 @@ namespace Managing.Domain.Bots
|
|||||||
return Name;
|
return Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract string GetBackup();
|
public abstract void SaveBackup();
|
||||||
public abstract void LoadBackup(BotBackup backup);
|
public abstract void LoadBackup(BotBackup backup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
void Restart();
|
void Restart();
|
||||||
string GetStatus();
|
string GetStatus();
|
||||||
string GetName();
|
string GetName();
|
||||||
string GetBackup();
|
void SaveBackup();
|
||||||
void LoadBackup(BotBackup backup);
|
void LoadBackup(BotBackup backup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user