Fix .First Position update + add more details when position rejected

This commit is contained in:
2025-10-11 12:27:54 +07:00
parent 3b3e383781
commit 04df72a6bd
4 changed files with 13 additions and 19 deletions

View File

@@ -369,10 +369,8 @@ public class TradingBotBase : ITradingBot
// No existing position found, proceed to open a new one // No existing position found, proceed to open a new one
var newlyCreatedPosition = await OpenPosition(signal); var newlyCreatedPosition = await OpenPosition(signal);
if (newlyCreatedPosition != null) // Position is now added to Positions collection inside OpenPosition method
{ // No need to add it here again
Positions[newlyCreatedPosition.Identifier] = newlyCreatedPosition;
}
} }
} }
@@ -996,6 +994,9 @@ public class TradingBotBase : ITradingBot
if (position != null) if (position != null)
{ {
// Add position to internal collection before any status updates
Positions[position.Identifier] = position;
if (position.Open.Status != TradeStatus.Cancelled && position.Status != PositionStatus.Rejected) if (position.Open.Status != TradeStatus.Cancelled && position.Status != PositionStatus.Rejected)
{ {
SetSignalStatus(signal.Identifier, SignalStatus.PositionOpen); SetSignalStatus(signal.Identifier, SignalStatus.PositionOpen);
@@ -1011,6 +1012,7 @@ public class TradingBotBase : ITradingBot
} }
else else
{ {
SentrySdk.CaptureMessage("Position rejected", SentryLevel.Error);
await SetPositionStatus(signal.Identifier, PositionStatus.Rejected); await SetPositionStatus(signal.Identifier, PositionStatus.Rejected);
position.Status = PositionStatus.Rejected; position.Status = PositionStatus.Rejected;
await UpdatePositionDatabase(position); await UpdatePositionDatabase(position);
@@ -1768,7 +1770,7 @@ public class TradingBotBase : ITradingBot
{ {
var position = Positions.Values.First(p => p.SignalIdentifier == signalIdentifier); var position = Positions.Values.First(p => p.SignalIdentifier == signalIdentifier);
if (positionStatus.Equals(PositionStatus.Canceled)) if (positionStatus.Equals(PositionStatus.Canceled | PositionStatus.Rejected))
{ {
var stackTrace = new StackTrace(true); var stackTrace = new StackTrace(true);
var callingMethod = stackTrace.GetFrame(1)?.GetMethod(); var callingMethod = stackTrace.GetFrame(1)?.GetMethod();

View File

@@ -125,6 +125,11 @@ namespace Managing.Application.Trading.Handlers
? position.Status ? position.Status
: PositionStatus.Rejected; : PositionStatus.Rejected;
if (position.Status == PositionStatus.Rejected)
{
SentrySdk.CaptureException(new Exception($"Position {position.Identifier} for {request.SignalIdentifier} rejected"));
}
if (!request.IsForPaperTrading) if (!request.IsForPaperTrading)
{ {
await tradingService.InsertPositionAsync(position); await tradingService.InsertPositionAsync(position);

View File

@@ -51,20 +51,6 @@ namespace Managing.Infrastructure.Exchanges
reduceOnly ? TradeStatus.PendingOpen : TradeStatus.Filled); reduceOnly ? TradeStatus.PendingOpen : TradeStatus.Filled);
} }
// Check gas fees for EVM exchanges before opening position
if (IsEvmExchange(account))
{
var gasFeeUsd = await GetFee(account);
if (gasFeeUsd > 0.5m)
{
_logger.LogWarning(
$"Gas fee too high for position opening: {gasFeeUsd:F2} USD (threshold: 0.5 USD). Cancelling position opening.");
// Return a cancelled trade
return BuildEmptyTrade(ticker, price, quantity, direction, leverage, tradeType, currentDate.Value,
TradeStatus.Cancelled);
}
}
var processor = GetProcessor(account); var processor = GetProcessor(account);
return await processor.OpenTrade(account, ticker, direction, price, quantity, leverage, tradeType, return await processor.OpenTrade(account, ticker, direction, price, quantity, leverage, tradeType,

View File

@@ -688,6 +688,7 @@ public class EvmManager : IEvmManager
} }
catch (Exception ex) catch (Exception ex)
{ {
SentrySdk.CaptureException(ex);
throw; throw;
} }