Fix backtest

This commit is contained in:
2025-04-29 01:17:58 +07:00
parent 204bd87e6a
commit f91b12fbcc
14 changed files with 113 additions and 92 deletions

View File

@@ -45,7 +45,7 @@ namespace Managing.Application.Bots
public override void SaveBackup()
{
var data = JsonConvert.SerializeObject(_workflow);
_botService.SaveOrUpdateBotBackup(Name, BotType.SimpleBot, data);
_botService.SaveOrUpdateBotBackup(User, Identifier, BotType.SimpleBot, data);
}
public override void LoadBackup(BotBackup backup)

View File

@@ -146,7 +146,7 @@ public class TradingBot : Bot, ITradingBot
public async Task LoadAccount()
{
var account = await AccountService.GetAccount(AccountName, false, true);
var account = await AccountService.GetAccount(AccountName, false, false);
if (account == null)
{
Logger.LogWarning($"No account found for this {AccountName}");
@@ -192,7 +192,7 @@ public class TradingBot : Bot, ITradingBot
if (balance < Constants.GMX.Config.MinimumPositionAmount)
{
await LogWarning(
$"Balance on broker is below {Constants.GMX.Config.MinimumPositionAmount} USD (actual: {balance}). Stopping bot and saving backup.");
$"Balance on broker is below {Constants.GMX.Config.MinimumPositionAmount} USD (actual: {balance}). Stopping bot {Identifier} and saving backup.");
SaveBackup();
Stop();
return;
@@ -587,8 +587,8 @@ public class TradingBot : Bot, ITradingBot
lastPrice,
signalIdentifier: signal.Identifier);
var position = await new OpenPositionCommandHandler(ExchangeService, AccountService, TradingService)
.Handle(command);
var position = (new OpenPositionCommandHandler(ExchangeService, AccountService, TradingService)
.Handle(command)).Result;
if (position != null)
{
@@ -665,12 +665,11 @@ public class TradingBot : Bot, ITradingBot
}
else
{
var command = new ClosePositionCommand(position, lastPrice);
var command = new ClosePositionCommand(position, lastPrice, isForBacktest: IsForBacktest);
try
{
var closedPosition =
(new ClosePositionCommandHandler(ExchangeService, AccountService, TradingService)
.Handle(command)).Result;
var closedPosition = (new ClosePositionCommandHandler(ExchangeService, AccountService, TradingService)
.Handle(command)).Result;
if (closedPosition.Status == (PositionStatus.Finished | PositionStatus.Flipped))
{
@@ -909,7 +908,7 @@ public class TradingBot : Bot, ITradingBot
BotTradingBalance = BotTradingBalance,
StartupTime = StartupTime,
};
BotService.SaveOrUpdateBotBackup(Name, BotType, JsonConvert.SerializeObject(data));
BotService.SaveOrUpdateBotBackup(User, Identifier, BotType, JsonConvert.SerializeObject(data));
}
public override void LoadBackup(BotBackup backup)
@@ -925,6 +924,8 @@ public class TradingBot : Bot, ITradingBot
AccountName = data.AccountName;
IsForWatchingOnly = data.IsForWatchingOnly;
BotTradingBalance = data.BotTradingBalance;
Identifier = backup.Identifier;
User = backup.User;
// Restore the startup time if it was previously saved
if (data.StartupTime != DateTime.MinValue)