Fix get Gas fees + position direction list

This commit is contained in:
2025-09-26 12:02:07 +07:00
parent bcfeb693ce
commit 1e19e29cec
7 changed files with 213 additions and 50 deletions

View File

@@ -409,7 +409,7 @@ public class TradingBotBase : ITradingBot
{
internalPosition.Open.SetStatus(TradeStatus.Filled);
}
// Also update the position in the bot's positions dictionary
if (positionForSignal.Open != null)
{
@@ -436,7 +436,7 @@ public class TradingBotBase : ITradingBot
{
internalPosition.Open.SetStatus(TradeStatus.Filled);
}
// Also update the position in the bot's positions dictionary
if (positionForSignal.Open != null)
{
@@ -515,19 +515,19 @@ public class TradingBotBase : ITradingBot
// Calculate net PnL after fees for broker position
var brokerNetPnL = brokerPosition.GetNetPnL();
UpdatePositionPnl(positionForSignal.Identifier, brokerNetPnL);
// Update Open trade status when position is found on broker with 2 orders
if (internalPosition.Open != null)
{
internalPosition.Open.SetStatus(TradeStatus.Filled);
}
// Also update the position in the bot's positions dictionary
if (positionForSignal.Open != null)
{
positionForSignal.Open.SetStatus(TradeStatus.Filled);
}
await SetPositionStatus(signal.Identifier, PositionStatus.Filled);
// Notify platform summary about the executed trade
@@ -608,17 +608,18 @@ public class TradingBotBase : ITradingBot
positionForSignal.StopLoss.SetPrice(executionPrice, 2);
positionForSignal.StopLoss.SetDate(lastCandle.Date);
positionForSignal.StopLoss.SetStatus(TradeStatus.Filled);
// Cancel TP trades when SL is hit
if (positionForSignal.TakeProfit1 != null)
{
positionForSignal.TakeProfit1.SetStatus(TradeStatus.Cancelled);
}
if (positionForSignal.TakeProfit2 != null)
{
positionForSignal.TakeProfit2.SetStatus(TradeStatus.Cancelled);
}
await LogInformation(
$"🛑 **Stop Loss Hit**\nClosing LONG position\nPrice: `${executionPrice:F2}` (was `${positionForSignal.StopLoss.Price:F2}`)");
await CloseTrade(signal, positionForSignal, positionForSignal.StopLoss,
@@ -632,13 +633,13 @@ public class TradingBotBase : ITradingBot
positionForSignal.TakeProfit1.SetPrice(executionPrice, 2);
positionForSignal.TakeProfit1.SetDate(lastCandle.Date);
positionForSignal.TakeProfit1.SetStatus(TradeStatus.Filled);
// Cancel SL trade when TP is hit
if (positionForSignal.StopLoss != null)
{
positionForSignal.StopLoss.SetStatus(TradeStatus.Cancelled);
}
await LogInformation(
$"🎯 **Take Profit 1 Hit**\nClosing LONG position\nPrice: `${executionPrice:F2}` (was `${positionForSignal.TakeProfit1.Price:F2}`)");
await CloseTrade(signal, positionForSignal, positionForSignal.TakeProfit1,
@@ -651,13 +652,13 @@ public class TradingBotBase : ITradingBot
positionForSignal.TakeProfit2.SetPrice(executionPrice, 2);
positionForSignal.TakeProfit2.SetDate(lastCandle.Date);
positionForSignal.TakeProfit2.SetStatus(TradeStatus.Filled);
// Cancel SL trade when TP is hit
if (positionForSignal.StopLoss != null)
{
positionForSignal.StopLoss.SetStatus(TradeStatus.Cancelled);
}
await LogInformation(
$"🎯 **Take Profit 2 Hit**\nClosing LONG position\nPrice: `${executionPrice:F2}` (was `${positionForSignal.TakeProfit2.Price:F2}`)");
await CloseTrade(signal, positionForSignal, positionForSignal.TakeProfit2,
@@ -673,17 +674,18 @@ public class TradingBotBase : ITradingBot
positionForSignal.StopLoss.SetPrice(executionPrice, 2);
positionForSignal.StopLoss.SetDate(lastCandle.Date);
positionForSignal.StopLoss.SetStatus(TradeStatus.Filled);
// Cancel TP trades when SL is hit
if (positionForSignal.TakeProfit1 != null)
{
positionForSignal.TakeProfit1.SetStatus(TradeStatus.Cancelled);
}
if (positionForSignal.TakeProfit2 != null)
{
positionForSignal.TakeProfit2.SetStatus(TradeStatus.Cancelled);
}
await LogInformation(
$"🛑 **Stop Loss Hit**\nClosing SHORT position\nPrice: `${executionPrice:F2}` (was `${positionForSignal.StopLoss.Price:F2}`)");
await CloseTrade(signal, positionForSignal, positionForSignal.StopLoss,
@@ -697,13 +699,13 @@ public class TradingBotBase : ITradingBot
positionForSignal.TakeProfit1.SetPrice(executionPrice, 2);
positionForSignal.TakeProfit1.SetDate(lastCandle.Date);
positionForSignal.TakeProfit1.SetStatus(TradeStatus.Filled);
// Cancel SL trade when TP is hit
if (positionForSignal.StopLoss != null)
{
positionForSignal.StopLoss.SetStatus(TradeStatus.Cancelled);
}
await LogInformation(
$"🎯 **Take Profit 1 Hit**\nClosing SHORT position\nPrice: `${executionPrice:F2}` (was `${positionForSignal.TakeProfit1.Price:F2}`)");
await CloseTrade(signal, positionForSignal, positionForSignal.TakeProfit1,
@@ -716,13 +718,13 @@ public class TradingBotBase : ITradingBot
positionForSignal.TakeProfit2.SetPrice(executionPrice, 2);
positionForSignal.TakeProfit2.SetDate(lastCandle.Date);
positionForSignal.TakeProfit2.SetStatus(TradeStatus.Filled);
// Cancel SL trade when TP is hit
if (positionForSignal.StopLoss != null)
{
positionForSignal.StopLoss.SetStatus(TradeStatus.Cancelled);
}
await LogInformation(
$"🎯 **Take Profit 2 Hit**\nClosing SHORT position\nPrice: `${executionPrice:F2}` (was `${positionForSignal.TakeProfit2.Price:F2}`)");
await CloseTrade(signal, positionForSignal, positionForSignal.TakeProfit2,
@@ -795,7 +797,7 @@ public class TradingBotBase : ITradingBot
{
internalPosition.Open.SetStatus(TradeStatus.Filled);
}
// Also update the position in the bot's positions dictionary
if (positionForSignal.Open != null)
{
@@ -1138,7 +1140,8 @@ public class TradingBotBase : ITradingBot
}
else
{
var command = new ClosePositionCommand(position, position.AccountId, lastPrice, isForBacktest: Config.IsForBacktest);
var command = new ClosePositionCommand(position, position.AccountId, lastPrice,
isForBacktest: Config.IsForBacktest);
try
{
Position closedPosition = null;
@@ -1264,19 +1267,20 @@ public class TradingBotBase : ITradingBot
if (wasStopLossHit)
{
// Use actual execution price based on direction
closingPrice = position.OriginDirection == TradeDirection.Long
? minPriceRecent // For LONG, SL hits at the low
closingPrice = position.OriginDirection == TradeDirection.Long
? minPriceRecent // For LONG, SL hits at the low
: maxPriceRecent; // For SHORT, SL hits at the high
position.StopLoss.SetPrice(closingPrice, 2);
position.StopLoss.SetDate(currentCandle.Date);
position.StopLoss.SetStatus(TradeStatus.Filled);
// Cancel TP trades when SL is hit
if (position.TakeProfit1 != null)
{
position.TakeProfit1.SetStatus(TradeStatus.Cancelled);
}
if (position.TakeProfit2 != null)
{
position.TakeProfit2.SetStatus(TradeStatus.Cancelled);
@@ -1291,14 +1295,14 @@ public class TradingBotBase : ITradingBot
else if (wasTakeProfitHit)
{
// Use actual execution price based on direction
closingPrice = position.OriginDirection == TradeDirection.Long
? maxPriceRecent // For LONG, TP hits at the high
closingPrice = position.OriginDirection == TradeDirection.Long
? maxPriceRecent // For LONG, TP hits at the high
: minPriceRecent; // For SHORT, TP hits at the low
position.TakeProfit1.SetPrice(closingPrice, 2);
position.TakeProfit1.SetDate(currentCandle.Date);
position.TakeProfit1.SetStatus(TradeStatus.Filled);
// Cancel SL trade when TP is hit
if (position.StopLoss != null)
{
@@ -1331,7 +1335,7 @@ public class TradingBotBase : ITradingBot
position.TakeProfit1.SetPrice(closingPrice, 2);
position.TakeProfit1.SetDate(currentCandle.Date);
position.TakeProfit1.SetStatus(TradeStatus.Filled);
// Cancel SL trade when TP is used for manual close
if (position.StopLoss != null)
{
@@ -1343,12 +1347,13 @@ public class TradingBotBase : ITradingBot
position.StopLoss.SetPrice(closingPrice, 2);
position.StopLoss.SetDate(currentCandle.Date);
position.StopLoss.SetStatus(TradeStatus.Filled);
// Cancel TP trades when SL is used for manual close
if (position.TakeProfit1 != null)
{
position.TakeProfit1.SetStatus(TradeStatus.Cancelled);
}
if (position.TakeProfit2 != null)
{
position.TakeProfit2.SetStatus(TradeStatus.Cancelled);
@@ -2163,7 +2168,8 @@ public class TradingBotBase : ITradingBot
PositionIdentifier = position.Identifier,
Ticker = position.Ticker,
Volume = position.Open.Price * position.Open.Quantity * position.Open.Leverage,
Fee = position.Open.Fee
Fee = position.GasFees + position.UiFees,
Direction = position.OriginDirection
};
await platformGrain.OnPositionOpenAsync(positionOpenEvent);
break;