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

@@ -16,25 +16,28 @@ namespace Managing.Application.Trading.Commands
PositionInitiator initiator,
DateTime date,
User user,
decimal amountToTrade,
bool isForPaperTrading = false,
decimal? price = null,
decimal? balance = 1000,
decimal? fee = null,
bool? ignoreSLTP = false,
string signalIdentifier = null)
{
AccountName = accountName;
MoneyManagement = moneyManagement;
Direction = direction;
Ticker = ticker;
Initiator = initiator;
Date = date;
User = user;
if (amountToTrade <= 10)
{
throw new ArgumentException("Bot trading balance must be greater than zero", nameof(amountToTrade));
}
AmountToTrade = amountToTrade;
IsForPaperTrading = isForPaperTrading;
Price = price;
Date = date;
Balance = balance;
Initiator = initiator;
Fee = fee;
IgnoreSLTP = ignoreSLTP;
User = user;
SignalIdentifier = signalIdentifier;
}
@@ -45,11 +48,9 @@ namespace Managing.Application.Trading.Commands
public Ticker Ticker { get; }
public bool IsForPaperTrading { get; }
public decimal? Price { get; }
public decimal? Fee { get; }
public bool? IgnoreSLTP { get; }
public decimal? Balance { get; }
public DateTime Date { get; set; }
public PositionInitiator Initiator { get; internal set; }
public User User { get; internal set; }
public decimal AmountToTrade { get; }
public DateTime Date { get; }
public PositionInitiator Initiator { get; }
public User User { get; }
}
}

View File

@@ -1,6 +1,7 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.Trading.Commands;
using Managing.Common;
using Managing.Domain.Shared.Helpers;
using Managing.Domain.Trades;
using static Managing.Common.Enums;
@@ -36,22 +37,20 @@ namespace Managing.Application.Trading
position.SignalIdentifier = request.SignalIdentifier;
}
var balance = request.IsForPaperTrading
? request.Balance.GetValueOrDefault()
: exchangeService.GetBalance(account, request.IsForPaperTrading).Result;
var balanceAtRisk = RiskHelpers.GetBalanceAtRisk(balance, request.MoneyManagement);
// Always use BotTradingBalance directly as the balance to risk
decimal balanceToRisk = request.AmountToTrade;
if (balanceAtRisk < 7)
// Minimum check
if (balanceToRisk < Constants.GMX.Config.MinimumPositionAmount)
{
throw new Exception($"Try to risk {balanceAtRisk} $ but inferior to minimum to trade");
throw new Exception(
$"Bot trading balance of {balanceToRisk} USD is less than the minimum {Constants.GMX.Config.MinimumPositionAmount} USD required to trade");
}
var price = request.IsForPaperTrading && request.Price.HasValue
? request.Price.Value
: exchangeService.GetPrice(account, request.Ticker, DateTime.Now);
var quantity = balanceAtRisk / price;
// var expectedStatus = GetExpectedStatus(request);
// position.Open = TradingPolicies.OpenPosition(expectedStatus).Execute(async () => { });
var quantity = balanceToRisk / price;
var openPrice = request.IsForPaperTrading || request.Price.HasValue
? request.Price.Value
@@ -71,20 +70,19 @@ namespace Managing.Application.Trading
TradeType.Limit,
isForPaperTrading: request.IsForPaperTrading,
currentDate: request.Date,
stopLossPrice: stopLossPrice, // Pass determined SL price
takeProfitPrice: takeProfitPrice); // Pass determined TP price
stopLossPrice: stopLossPrice,
takeProfitPrice: takeProfitPrice);
//trade.Fee = TradingHelpers.GetFeeAmount(fee, openPrice * quantity, account.Exchange);
position.Open = trade;
var closeDirection = request.Direction == TradeDirection.Long
? TradeDirection.Short
: TradeDirection.Long;
// Stop loss - Use the determined price
// Stop loss
position.StopLoss = exchangeService.BuildEmptyTrade(
request.Ticker,
stopLossPrice, // Use determined SL price
stopLossPrice,
position.Open.Quantity,
closeDirection,
request.MoneyManagement.Leverage,
@@ -92,13 +90,10 @@ namespace Managing.Application.Trading
request.Date,
TradeStatus.Requested);
// position.StopLoss.Fee = TradingHelpers.GetFeeAmount(fee,
// position.StopLoss.Price * position.StopLoss.Quantity, account.Exchange);
// Take profit - Use the determined price
// Take profit
position.TakeProfit1 = exchangeService.BuildEmptyTrade(
request.Ticker,
takeProfitPrice, // Use determined TP price
takeProfitPrice,
quantity,
closeDirection,
request.MoneyManagement.Leverage,