Refact task
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user