From 4c76c040cbadd36c8e2d82e1743e1614044d5725 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Mon, 7 Jul 2025 10:32:27 +0700 Subject: [PATCH] fix fee calculation --- src/Managing.Application/Bots/TradingBot.cs | 30 +++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Managing.Application/Bots/TradingBot.cs b/src/Managing.Application/Bots/TradingBot.cs index 81c98cf..c684202 100644 --- a/src/Managing.Application/Bots/TradingBot.cs +++ b/src/Managing.Application/Bots/TradingBot.cs @@ -1304,14 +1304,34 @@ public class TradingBot : Bot, ITradingBot decimal fees = 0; // Calculate position size in USD (leverage is already included in quantity calculation) - var positionSizeUsd = position.Open.Price * position.Open.Quantity; + var positionSizeUsd = (position.Open.Price * position.Open.Quantity) * position.Open.Leverage; - // UI Fee: 0.1% of position size paid BOTH on opening AND closing + // UI Fee: 0.1% of position size paid on opening var uiFeeRate = 0.001m; // 0.1% var uiFeeOpen = positionSizeUsd * uiFeeRate; // Fee paid on opening - var uiFeeClose = positionSizeUsd * uiFeeRate; // Fee paid on closing - var totalUiFees = uiFeeOpen + uiFeeClose; // Total: 0.2% of position size - fees += totalUiFees; + fees += uiFeeOpen; + + // UI Fee: 0.1% of position size paid on closing - only if position was actually closed + // Check which closing trade was executed (StopLoss, TakeProfit1, or TakeProfit2) + // Calculate closing fee based on the actual executed trade's price and quantity + if (position.StopLoss?.Status == TradeStatus.Filled) + { + var stopLossPositionSizeUsd = (position.StopLoss.Price * position.StopLoss.Quantity) * position.StopLoss.Leverage; + var uiFeeClose = stopLossPositionSizeUsd * uiFeeRate; // Fee paid on closing via StopLoss + fees += uiFeeClose; + } + else if (position.TakeProfit1?.Status == TradeStatus.Filled) + { + var takeProfit1PositionSizeUsd = (position.TakeProfit1.Price * position.TakeProfit1.Quantity) * position.TakeProfit1.Leverage; + var uiFeeClose = takeProfit1PositionSizeUsd * uiFeeRate; // Fee paid on closing via TakeProfit1 + fees += uiFeeClose; + } + else if (position.TakeProfit2?.Status == TradeStatus.Filled) + { + var takeProfit2PositionSizeUsd = (position.TakeProfit2.Price * position.TakeProfit2.Quantity) * position.TakeProfit2.Leverage; + var uiFeeClose = takeProfit2PositionSizeUsd * uiFeeRate; // Fee paid on closing via TakeProfit2 + fees += uiFeeClose; + } // Network Fee: $0.50 for opening position only // Closing is handled by oracle, so no network fee for closing