Balance for bot (#20)

* Add bot balance

* Update amount to trade

* fix initial trading balance

* Update MM modal

* fix backtest

* stop bot if no more balance

* Add constant for minimum trading

* Add constant
This commit is contained in:
Oda
2025-04-28 16:52:42 +02:00
committed by GitHub
parent 967e8410dc
commit 204bd87e6a
39 changed files with 600 additions and 546 deletions

View File

@@ -93,17 +93,6 @@ public class BacktestController : BaseController
return Ok(_backtester.DeleteBacktestByUser(user, id));
}
/// <summary>
/// Deletes all backtests.
/// </summary>
/// <returns>An ActionResult indicating the outcome of the operation.</returns>
[HttpDelete]
[Route("deleteAll")]
public ActionResult DeleteBacktests()
{
return Ok(_backtester.DeleteBacktests());
}
/// <summary>
/// Runs a backtest with the specified parameters.
/// </summary>

View File

@@ -3,6 +3,7 @@ using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.Hubs;
using Managing.Application.ManageBot.Commands;
using Managing.Common;
using Managing.Domain.Trades;
using MediatR;
using Microsoft.AspNetCore.Authorization;
@@ -123,9 +124,16 @@ public class BotController : BaseController
return BadRequest("Money management not found");
}
// Validate initialTradingBalance
if (request.InitialTradingBalance <= Constants.GMX.Config.MinimumPositionAmount)
{
return BadRequest(
$"Initial trading balance must be greater than {Constants.GMX.Config.MinimumPositionAmount}");
}
var result = await _mediator.Send(new StartBotCommand(request.BotType, request.BotName, request.Ticker,
request.Scenario, request.Timeframe, request.AccountName, request.MoneyManagementName, user,
request.IsForWatchOnly));
request.IsForWatchOnly, request.InitialTradingBalance));
await NotifyBotSubscriberAsync();
return Ok(result);
@@ -430,7 +438,7 @@ public class BotController : BaseController
var activeBots = _botService.GetActiveBots();
var bot = activeBots.FirstOrDefault(b => b.Name == request.BotName) as ApplicationTradingBot;
if (bot == null)
{
return NotFound($"Bot {request.BotName} not found or is not a trading bot");
@@ -474,7 +482,7 @@ public class BotController : BaseController
var activeBots = _botService.GetActiveBots();
var bot = activeBots.FirstOrDefault(b => b.Name == request.BotName) as ApplicationTradingBot;
if (bot == null)
{
return NotFound($"Bot {request.BotName} not found or is not a trading bot");
@@ -508,7 +516,7 @@ public class BotController : BaseController
// Close the position at market price
await bot.CloseTrade(signal, position, position.Open, lastCandle.Close, true);
await NotifyBotSubscriberAsync();
return Ok(position);
}
@@ -529,7 +537,7 @@ public class ClosePositionRequest
/// The name of the bot
/// </summary>
public string BotName { get; set; }
/// <summary>
/// The ID of the position to close
/// </summary>

View File

@@ -150,6 +150,7 @@ public class TradingController : BaseController
PositionInitiator.User,
DateTime.UtcNow,
user,
100m, // Default trading balance for user-initiated trades
isForPaperTrading: isForPaperTrading,
price: openPrice);
var result = await _openTradeCommandHandler.Handle(command);