Add bot worker

This commit is contained in:
2024-06-28 17:26:28 +07:00
parent 7a6d656335
commit d96b2a4c3b
24 changed files with 120 additions and 113 deletions

View File

@@ -16,6 +16,7 @@ namespace Managing.Application.Bots.Base
private readonly IAccountService _accountService;
private readonly ILogger<TradingBot> _tradingBotLogger;
private readonly ITradingService _tradingService;
private readonly IBotService _botService;
public BotFactory(
IExchangeService exchangeService,
@@ -23,7 +24,8 @@ namespace Managing.Application.Bots.Base
IMoneyManagementService moneyManagementService,
IMessengerService messengerService,
IAccountService accountService,
ITradingService tradingService)
ITradingService tradingService,
IBotService botService)
{
_tradingBotLogger = tradingBotLogger;
_exchangeService = exchangeService;
@@ -31,11 +33,12 @@ namespace Managing.Application.Bots.Base
_messengerService = messengerService;
_accountService = accountService;
_tradingService = tradingService;
_botService = botService;
}
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)
@@ -52,6 +55,7 @@ namespace Managing.Application.Bots.Base
interval,
_accountService,
_messengerService,
_botService,
isForWatchingOnly: isForWatchingOnly);
}
@@ -69,6 +73,7 @@ namespace Managing.Application.Bots.Base
interval,
_accountService,
_messengerService,
_botService,
true,
isForWatchingOnly);
}
@@ -87,6 +92,7 @@ namespace Managing.Application.Bots.Base
interval,
_accountService,
_messengerService,
_botService,
isForWatchingOnly: isForWatchingOnly);
}
@@ -104,6 +110,7 @@ namespace Managing.Application.Bots.Base
interval,
_accountService,
_messengerService,
_botService,
true,
isForWatchingOnly);
}

View File

@@ -18,6 +18,7 @@ namespace Managing.Application.Bots
Timeframe timeframe,
IAccountService accountService,
IMessengerService messengerService,
IBotService botService,
bool isForBacktest = false,
bool isForWatchingOnly = false)
: base(accountName,
@@ -31,6 +32,7 @@ namespace Managing.Application.Bots
timeframe,
accountService,
messengerService,
botService,
isForBacktest,
isForWatchingOnly,
flipPosition: true)

View File

@@ -18,6 +18,7 @@ namespace Managing.Application.Bots
Timeframe timeframe,
IAccountService accountService,
IMessengerService messengerService,
IBotService botService,
bool isForBacktest = false,
bool isForWatchingOnly = false)
: base(accountName,
@@ -31,6 +32,7 @@ namespace Managing.Application.Bots
timeframe,
accountService,
messengerService,
botService,
isForBacktest,
isForWatchingOnly)
{

View File

@@ -2,17 +2,20 @@
using Managing.Domain.Workflows;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using static Managing.Common.Enums;
namespace Managing.Application.Bots
{
public class SimpleBot : Bot
{
public readonly ILogger<TradingBot> Logger;
private readonly IBotService _botService;
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;
_botService = botService;
_workflow = workflow;
Interval = 100;
Start();
@@ -33,13 +36,15 @@ namespace Managing.Application.Bots
Logger.LogInformation(Identifier);
Logger.LogInformation(DateTime.Now.ToString());
await _workflow.Execute();
SaveBackup();
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)

View File

@@ -23,6 +23,7 @@ public class TradingBot : Bot, ITradingBot
public readonly IMessengerService MessengerService;
public readonly IAccountService AccountService;
private readonly ITradingService TradingService;
private readonly IBotService BotService;
public Account Account { get; set; }
public HashSet<IStrategy> Strategies { get; set; }
@@ -55,6 +56,7 @@ public class TradingBot : Bot, ITradingBot
Timeframe timeframe,
IAccountService accountService,
IMessengerService messengerService,
IBotService botService,
bool isForBacktest = false,
bool isForWatchingOnly = false,
bool flipPosition = false)
@@ -64,7 +66,7 @@ public class TradingBot : Bot, ITradingBot
AccountService = accountService;
MessengerService = messengerService;
TradingService = tradingService;
BotService = botService;
IsForWatchingOnly = isForWatchingOnly;
FlipPosition = flipPosition;
@@ -168,6 +170,9 @@ public class TradingBot : Bot, ITradingBot
if (!IsForWatchingOnly)
await ManagePositions();
if (!IsForBacktest)
SaveBackup();
await UpdateWalletBalances();
Logger.LogInformation($"Candles : {Candles.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,
BotType = BotType,
@@ -675,7 +680,8 @@ public class TradingBot : Bot, ITradingBot
IsForWatchingOnly = IsForWatchingOnly,
WalletBalances = WalletBalances,
MoneyManagement = MoneyManagement
});
};
BotService.SaveBotBackup(Name, BotType, JsonConvert.SerializeObject(data));
}
public override void LoadBackup(BotBackup backup)