Refact task
This commit is contained in:
@@ -13,7 +13,7 @@ public interface IBotService
|
||||
void AddTradingBotToCache(ITradingBot bot);
|
||||
List<ITradingBot> GetActiveBots();
|
||||
IEnumerable<BotBackup> GetSavedBots();
|
||||
void StartBot(BotBackup backupBot);
|
||||
void StartBotFromBackup(BotBackup backupBot);
|
||||
|
||||
ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name, Enums.Ticker ticker,
|
||||
string scenario, Enums.Timeframe interval, bool isForWatchingOnly);
|
||||
|
||||
@@ -744,5 +744,5 @@ public class TradingBotBackup
|
||||
public string AccountName { get; set; }
|
||||
public bool IsForWatchingOnly { get; set; }
|
||||
public Dictionary<DateTime, decimal> WalletBalances { get; set; }
|
||||
public MoneyManagement MoneyManagement { get; internal set; }
|
||||
public MoneyManagement MoneyManagement { get; set; }
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Bots;
|
||||
using Managing.Common;
|
||||
using Managing.Core;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Workflows;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Managing.Application.ManageBot
|
||||
@@ -11,14 +15,24 @@ namespace Managing.Application.ManageBot
|
||||
{
|
||||
private readonly IBotRepository _botRepository;
|
||||
private readonly ITaskCache _taskCache;
|
||||
private readonly IBotFactory _botFactory;
|
||||
|
||||
private readonly IExchangeService _exchangeService;
|
||||
private readonly IMessengerService _messengerService;
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly ILogger<TradingBot> _tradingBotLogger;
|
||||
private readonly ITradingService _tradingService;
|
||||
|
||||
public BotService(IBotRepository botRepository, ITaskCache taskCache, IBotFactory botFactory)
|
||||
|
||||
public BotService(IBotRepository botRepository, ITaskCache taskCache, IExchangeService exchangeService,
|
||||
IMessengerService messengerService, IAccountService accountService, ILogger<TradingBot> tradingBotLogger,
|
||||
ITradingService tradingService)
|
||||
{
|
||||
_botRepository = botRepository;
|
||||
_taskCache = taskCache;
|
||||
_botFactory = botFactory;
|
||||
_exchangeService = exchangeService;
|
||||
_messengerService = messengerService;
|
||||
_accountService = accountService;
|
||||
_tradingBotLogger = tradingBotLogger;
|
||||
_tradingService = tradingService;
|
||||
}
|
||||
|
||||
public async void SaveBotBackup(BotBackup botBackup)
|
||||
@@ -73,20 +87,20 @@ namespace Managing.Application.ManageBot
|
||||
return _botRepository.GetBots();
|
||||
}
|
||||
|
||||
public void StartBot(BotBackup backupBot)
|
||||
public void StartBotFromBackup(BotBackup backupBot)
|
||||
{
|
||||
switch (backupBot.BotType)
|
||||
{
|
||||
case Enums.BotType.SimpleBot:
|
||||
Func<Task<IBot>> simpleBot = () =>
|
||||
Task.FromResult(_botFactory.CreateSimpleBot(backupBot.Name, null));
|
||||
Task.FromResult(CreateSimpleBot(backupBot.Name, null));
|
||||
var bot1 = _taskCache.AddOrGetExisting(backupBot.Name, simpleBot).Result;
|
||||
bot1.LoadBackup(backupBot);
|
||||
bot1.Start();
|
||||
break;
|
||||
case Enums.BotType.ScalpingBot:
|
||||
var data = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||
Func<Task<ITradingBot>> scalpingBot = () => Task.FromResult(_botFactory.CreateScalpingBot(
|
||||
Func<Task<ITradingBot>> scalpingBot = () => Task.FromResult(CreateScalpingBot(
|
||||
data.AccountName,
|
||||
data.MoneyManagement,
|
||||
data.Name,
|
||||
@@ -100,7 +114,7 @@ namespace Managing.Application.ManageBot
|
||||
break;
|
||||
case Enums.BotType.FlippingBot:
|
||||
var dataFlippingBot = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||
Func<Task<ITradingBot>> flippingBot = () => Task.FromResult(_botFactory.CreateFlippingBot(
|
||||
Func<Task<ITradingBot>> flippingBot = () => Task.FromResult(CreateFlippingBot(
|
||||
dataFlippingBot.AccountName,
|
||||
dataFlippingBot.MoneyManagement,
|
||||
dataFlippingBot.Name,
|
||||
@@ -116,5 +130,88 @@ namespace Managing.Application.ManageBot
|
||||
|
||||
;
|
||||
}
|
||||
|
||||
public IBot CreateSimpleBot(string botName, Workflow workflow)
|
||||
{
|
||||
return new SimpleBot(botName, _tradingBotLogger, workflow, this);
|
||||
}
|
||||
|
||||
public ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly)
|
||||
{
|
||||
return new ScalpingBot(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
name,
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
isForWatchingOnly: isForWatchingOnly);
|
||||
}
|
||||
|
||||
public ITradingBot CreateBacktestScalpingBot(string accountName, MoneyManagement moneyManagement,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly)
|
||||
{
|
||||
return new ScalpingBot(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
"BacktestBot",
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
true,
|
||||
isForWatchingOnly);
|
||||
}
|
||||
|
||||
public ITradingBot CreateFlippingBot(string accountName, MoneyManagement moneyManagement, string name,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly)
|
||||
{
|
||||
return new FlippingBot(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
name,
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
isForWatchingOnly: isForWatchingOnly);
|
||||
}
|
||||
|
||||
public ITradingBot CreateBacktestFlippingBot(string accountName, MoneyManagement moneyManagement,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly)
|
||||
{
|
||||
return new FlippingBot(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
"BacktestBot",
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
true,
|
||||
isForWatchingOnly);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
|
||||
if (simpleBot == null)
|
||||
{
|
||||
_logger.LogInformation($"Starting backup bot {backupBot.Name}");
|
||||
_botService.StartBot(backupBot);
|
||||
_botService.StartBotFromBackup(backupBot);
|
||||
result.Add(simpleBot.GetName(), BotStatus.Backup);
|
||||
}
|
||||
else
|
||||
@@ -57,7 +57,10 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
|
||||
if (scalpingBot == null)
|
||||
{
|
||||
_logger.LogInformation($"Starting backup bot {backupBot.Name}");
|
||||
_botService.StartBot(backupBot);
|
||||
_botService.StartBotFromBackup(backupBot);
|
||||
var bots = _botService.GetActiveBots();
|
||||
scalpingBot = bots.FirstOrDefault(b => b.GetName() == backupBot.Name);
|
||||
result.Add(scalpingBot.GetName(), BotStatus.Backup);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -8,11 +8,14 @@ namespace Managing.Infrastructure.Storage
|
||||
public class TaskCache : ITaskCache
|
||||
{
|
||||
private MemoryCache _cache { get; } = MemoryCache.Default;
|
||||
private CacheItemPolicy _defaultPolicy { get; } = new CacheItemPolicy();
|
||||
|
||||
private CacheItemPolicy _defaultPolicy { get; } = new CacheItemPolicy()
|
||||
{
|
||||
SlidingExpiration = TimeSpan.FromMinutes(15),
|
||||
};
|
||||
|
||||
public async Task<T> AddOrGetExisting<T>(string key, Func<Task<T>> valueFactory)
|
||||
{
|
||||
|
||||
var asyncLazyValue = new AsyncLazy<T>(valueFactory);
|
||||
var existingValue = (AsyncLazy<T>)_cache.AddOrGetExisting(key, asyncLazyValue, _defaultPolicy);
|
||||
|
||||
@@ -33,6 +36,7 @@ namespace Managing.Infrastructure.Storage
|
||||
// Get the most recent value with a recursive call.
|
||||
return await AddOrGetExisting(key, valueFactory);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -71,7 +75,7 @@ namespace Managing.Infrastructure.Storage
|
||||
public virtual List<T> GetCache<T>()
|
||||
{
|
||||
List<T> list = new List<T>();
|
||||
|
||||
|
||||
foreach (var item in _cache)
|
||||
{
|
||||
list.Add((T)item.Value);
|
||||
@@ -80,4 +84,4 @@ namespace Managing.Infrastructure.Storage
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user