Push merge conflict

This commit is contained in:
2025-07-04 11:02:53 +07:00
parent 88f195c0ca
commit 59c5de7df7
27 changed files with 1133 additions and 1118 deletions

View File

@@ -6,25 +6,19 @@ namespace Managing.Application.Abstractions
public interface IBotFactory
{
IBot CreateSimpleBot(string botName, Workflow workflow);
/// <summary>
/// Creates a trading bot using the unified TradingBot class
/// </summary>
/// <param name="config">The trading bot configuration</param>
/// <returns>ITradingBot instance</returns>
ITradingBot CreateTradingBot(TradingBotConfig config);
/// <summary>
/// Creates a trading bot for backtesting using the unified TradingBot class
/// </summary>
/// <param name="config">The trading bot configuration</param>
/// <returns>ITradingBot instance configured for backtesting</returns>
ITradingBot CreateBacktestTradingBot(TradingBotConfig config);
// Legacy methods - these will use TradingBot internally but maintain backward compatibility
ITradingBot CreateScalpingBot(TradingBotConfig config);
ITradingBot CreateBacktestScalpingBot(TradingBotConfig config);
ITradingBot CreateFlippingBot(TradingBotConfig config);
ITradingBot CreateBacktestFlippingBot(TradingBotConfig config);
}
}
}

View File

@@ -114,7 +114,7 @@ namespace Managing.Application.Backtesting
User user = null)
{
// Set FlipPosition based on BotType
config.FlipPosition = config.BotType == BotType.FlippingBot;
config.FlipPosition = config.FlipPosition;
var tradingBot = _botFactory.CreateBacktestTradingBot(config);
@@ -164,11 +164,11 @@ namespace Managing.Application.Backtesting
private List<Candle> GetCandles(Account account, Ticker ticker, Timeframe timeframe,
DateTime startDate, DateTime endDate)
{
var candles = _exchangeService.GetCandlesInflux(account.Exchange, ticker,
var candles = _exchangeService.GetCandlesInflux(TradingExchanges.Evm, ticker,
startDate, timeframe, endDate).Result;
if (candles == null || candles.Count == 0)
throw new Exception($"No candles for {ticker} on {account.Exchange}");
throw new Exception($"No candles for {ticker} on {timeframe} timeframe");
return candles;
}

View File

@@ -3,7 +3,6 @@ using Managing.Application.Abstractions.Services;
using Managing.Domain.Bots;
using Managing.Domain.Workflows;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
namespace Managing.Application.Bots.Base
{
@@ -61,64 +60,5 @@ namespace Managing.Application.Bots.Base
_botService,
config);
}
// Legacy methods for backward compatibility - will be deprecated
ITradingBot IBotFactory.CreateScalpingBot(TradingBotConfig config)
{
config.BotType = BotType.ScalpingBot;
config.FlipPosition = false;
return new TradingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
_botService,
config);
}
ITradingBot IBotFactory.CreateBacktestScalpingBot(TradingBotConfig config)
{
config.BotType = BotType.ScalpingBot;
config.IsForBacktest = true;
config.FlipPosition = false;
return new TradingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
_botService,
config);
}
public ITradingBot CreateFlippingBot(TradingBotConfig config)
{
config.BotType = BotType.FlippingBot;
config.FlipPosition = true;
return new TradingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
_botService,
config);
}
public ITradingBot CreateBacktestFlippingBot(TradingBotConfig config)
{
config.BotType = BotType.FlippingBot;
config.IsForBacktest = true;
config.FlipPosition = true;
return new TradingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
_botService,
config);
}
}
}
}

View File

@@ -187,7 +187,7 @@ public class TradingBot : Bot, ITradingBot
Logger.LogInformation($"____________________{Name}____________________");
Logger.LogInformation(
$"Time : {DateTime.Now} - Server time {DateTime.Now.ToUniversalTime()} - Last candle : {OptimizedCandles.Last().Date} - Bot : {Name} - Type {Config.BotType} - Ticker : {Config.Ticker}");
$"Time : {DateTime.Now} - Server time {DateTime.Now.ToUniversalTime()} - Last candle : {OptimizedCandles.Last().Date} - Bot : {Name} - Ticker : {Config.Ticker}");
}
var previousLastCandle = OptimizedCandles.LastOrDefault();
@@ -1450,7 +1450,6 @@ public class TradingBot : Bot, ITradingBot
}
// Protect critical properties that shouldn't change for running bots
var protectedBotType = Config.BotType;
var protectedIsForBacktest = Config.IsForBacktest;
var protectedName = allowNameChange ? newConfig.Name : Config.Name;
@@ -1470,7 +1469,6 @@ public class TradingBot : Bot, ITradingBot
Config = newConfig;
// Restore protected properties
Config.BotType = protectedBotType;
Config.IsForBacktest = protectedIsForBacktest;
Config.Name = protectedName;
@@ -1532,7 +1530,6 @@ public class TradingBot : Bot, ITradingBot
Timeframe = Config.Timeframe,
IsForWatchingOnly = Config.IsForWatchingOnly,
BotTradingBalance = Config.BotTradingBalance,
BotType = Config.BotType,
IsForBacktest = Config.IsForBacktest,
CooldownPeriod = Config.CooldownPeriod,
MaxLossStreak = Config.MaxLossStreak,

View File

@@ -1,17 +1,14 @@
using MediatR;
using static Managing.Common.Enums;
namespace Managing.Application.ManageBot.Commands
{
public class StopBotCommand : IRequest<string>
{
public string Name { get; }
public BotType BotType { get; }
public string Identifier { get; }
public StopBotCommand(BotType botType, string name)
public StopBotCommand(string identifier)
{
BotType = botType;
Name = name;
Identifier = identifier;
}
}
}
}

View File

@@ -70,37 +70,25 @@ namespace Managing.Application.ManageBot
Timeframe = request.Config.Timeframe,
IsForWatchingOnly = request.Config.IsForWatchingOnly,
BotTradingBalance = request.Config.BotTradingBalance,
BotType = request.Config.BotType,
IsForBacktest = request.Config.IsForBacktest,
CooldownPeriod =
request.Config.CooldownPeriod > 0 ? request.Config.CooldownPeriod : 1, // Default to 1 if not set
MaxLossStreak = request.Config.MaxLossStreak,
MaxPositionTimeHours = request.Config.MaxPositionTimeHours, // Properly handle nullable value
FlipOnlyWhenInProfit = request.Config.FlipOnlyWhenInProfit,
FlipPosition = request.Config.BotType == BotType.FlippingBot, // Set FlipPosition based on BotType
FlipPosition = request.Config.FlipPosition, // Set FlipPosition
Name = request.Config.Name ?? request.Name,
CloseEarlyWhenProfitable = request.Config.CloseEarlyWhenProfitable
};
switch (configToUse.BotType)
{
case BotType.SimpleBot:
var bot = _botFactory.CreateSimpleBot(request.Name, null);
bot.User = request.User;
_botService.AddSimpleBotToCache(bot);
return bot.GetStatus();
var tradingBot = _botFactory.CreateTradingBot(configToUse);
tradingBot.User = request.User;
case BotType.ScalpingBot:
case BotType.FlippingBot:
var tradingBot = _botFactory.CreateTradingBot(configToUse);
tradingBot.User = request.User;
// Log the configuration being used
await LogBotConfigurationAsync(tradingBot, $"{configToUse.Name} created");
// Log the configuration being used
await LogBotConfigurationAsync(tradingBot, $"{configToUse.BotType} created");
_botService.AddTradingBotToCache(tradingBot);
return tradingBot.GetStatus();
}
_botService.AddTradingBotToCache(tradingBot);
return tradingBot.GetStatus();
return botStatus.ToString();
}
@@ -116,7 +104,6 @@ namespace Managing.Application.ManageBot
{
var config = bot.GetConfiguration();
var logMessage = $"{context} - Bot: {config.Name}, " +
$"Type: {config.BotType}, " +
$"Account: {config.AccountName}, " +
$"Ticker: {config.Ticker}, " +
$"Balance: {config.BotTradingBalance}, " +

View File

@@ -15,7 +15,7 @@ namespace Managing.Application.ManageBot
public Task<string> Handle(StopBotCommand request, CancellationToken cancellationToken)
{
return _botService.StopBot(request.Name);
return _botService.StopBot(request.Identifier);
}
}
}