Skip to content

Commit 73104e6

Browse files
konardclaude
andcommitted
Add comprehensive loss detection logging to trader bot
- Enhanced GetMinimumSellPrice() with loss detection warnings when MinimumProfitSteps < 0 - Added profit/loss logging when placing sell orders to detect loss scenarios - Added early sell loss detection with clear warnings when selling at top bid results in loss - Added price change loss detection when sell order prices are adjusted downward - Added realized profit/loss tracking in UpdateCashBalance() for executed sell trades - All loss scenarios now logged with LogWarning level for easy detection and investigation - Profit scenarios logged with LogInformation level for comparison - Each log message includes source price, target price, actual loss/profit amount, and lot quantity This allows careful investigation of loss reasons as requested in issue #200. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 6c37076 commit 73104e6

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

csharp/TraderBot/TradingService.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,29 @@ protected void UpdateCashBalance(OrderTrades orderTrades)
147147
if(orderTrades.Direction == OrderDirection.Buy)
148148
{
149149
SetCashBalance(CashBalanceFree, CashBalanceLocked - cashBalanceDelta);
150+
Logger.LogInformation($"Buy execution: {trade.Quantity} lots at {trade.Price}, total cost: {cashBalanceDelta}");
150151
}
151152
else if (orderTrades.Direction == OrderDirection.Sell)
152153
{
153154
SetCashBalance(CashBalanceFree + cashBalanceDelta, CashBalanceLocked);
155+
156+
// Track realized profit/loss for sell trades
157+
if (ActiveSellOrderSourcePrice.TryGetValue(orderTrades.OrderId, out var sourcePrice))
158+
{
159+
var realizedProfitLoss = (trade.Price - sourcePrice) * trade.Quantity;
160+
if (realizedProfitLoss < 0)
161+
{
162+
Logger.LogWarning($"Loss realized: SELL EXECUTED at LOSS. Source price: {sourcePrice}, execution price: {trade.Price}, lots: {trade.Quantity}, realized loss: {Math.Abs(realizedProfitLoss)}, total revenue: {cashBalanceDelta}");
163+
}
164+
else
165+
{
166+
Logger.LogInformation($"Profit realized: sell executed at profit. Source price: {sourcePrice}, execution price: {trade.Price}, lots: {trade.Quantity}, realized profit: {realizedProfitLoss}, total revenue: {cashBalanceDelta}");
167+
}
168+
}
169+
else
170+
{
171+
Logger.LogInformation($"Sell execution: {trade.Quantity} lots at {trade.Price}, total revenue: {cashBalanceDelta} (source price not tracked)");
172+
}
154173
}
155174
}
156175
}
@@ -461,6 +480,17 @@ await marketDataStream.RequestStream.WriteAsync(new MarketDataRequest
461480
var targetSellPrice = GetTargetSellPrice(minimumSellPrice, bestAsk);
462481
var marketLotsAtTargetPrice = orderBook.Asks.FirstOrDefault(o => o.Price == targetSellPrice)?.Quantity ?? 0;
463482
Logger.LogInformation($"marketLotsAtTargetPrice: {marketLotsAtTargetPrice}");
483+
484+
var actualProfitLoss = targetSellPrice - maxPrice;
485+
if (actualProfitLoss < 0)
486+
{
487+
Logger.LogWarning($"Loss detection: placing sell order at LOSS. Source price: {maxPrice}, target sell price: {targetSellPrice}, actual loss: {Math.Abs(actualProfitLoss)}, lots: {totalAmount}");
488+
}
489+
else
490+
{
491+
Logger.LogInformation($"Profit tracking: placing sell order at profit. Source price: {maxPrice}, target sell price: {targetSellPrice}, profit: {actualProfitLoss}, lots: {totalAmount}");
492+
}
493+
464494
var response = await PlaceSellOrder(totalAmount, targetSellPrice);
465495
ActiveSellOrderSourcePrice[response.OrderId] = maxPrice;
466496
Logger.LogInformation($"sell complete");
@@ -611,6 +641,15 @@ await marketDataStream.RequestStream.WriteAsync(new MarketDataRequest
611641
continue;
612642
}
613643
// Place new order at top bid price
644+
var earlySellProfitLoss = topBid - sourcePrice;
645+
if (earlySellProfitLoss < 0)
646+
{
647+
Logger.LogWarning($"Loss detection: EARLY SELL at LOSS. Source price: {sourcePrice}, top bid price: {topBid}, actual loss: {Math.Abs(earlySellProfitLoss)}, lots: {activeSellOrder.LotsRequested}");
648+
}
649+
else
650+
{
651+
Logger.LogInformation($"Early sell profit tracking: source price: {sourcePrice}, top bid price: {topBid}, profit: {earlySellProfitLoss}, lots: {activeSellOrder.LotsRequested}");
652+
}
614653
var response = await PlaceSellOrder(activeSellOrder.LotsRequested, topBid);
615654
SyncActiveOrders();
616655
Logger.LogInformation($"early sell is complete");
@@ -636,6 +675,17 @@ await marketDataStream.RequestStream.WriteAsync(new MarketDataRequest
636675
var targetSellPrice = GetTargetSellPrice(minimumSellPrice, bestAsk);
637676
var marketLotsAtTargetPrice = orderBook.Asks.FirstOrDefault(o => o.Price == targetSellPrice)?.Quantity ?? 0;
638677
Logger.LogInformation($"marketLotsAtTargetPrice: {marketLotsAtTargetPrice}");
678+
679+
var priceChangeProfitLoss = targetSellPrice - sourcePrice;
680+
if (priceChangeProfitLoss < 0)
681+
{
682+
Logger.LogWarning($"Loss detection: PRICE CHANGE to LOSS position. Source price: {sourcePrice}, new target price: {targetSellPrice}, actual loss: {Math.Abs(priceChangeProfitLoss)}, lots: {activeSellOrder.LotsRequested}");
683+
}
684+
else
685+
{
686+
Logger.LogInformation($"Price change profit tracking: source price: {sourcePrice}, new target price: {targetSellPrice}, profit: {priceChangeProfitLoss}, lots: {activeSellOrder.LotsRequested}");
687+
}
688+
639689
var response = await PlaceSellOrder(activeSellOrder.LotsRequested, targetSellPrice);
640690
ActiveSellOrderSourcePrice[response.OrderId] = sourcePrice;
641691
SyncActiveOrders();
@@ -680,7 +730,17 @@ private void SetCashBalance(decimal free, decimal locked)
680730
private decimal GetMinimumSellPrice(decimal sourcePrice)
681731
{
682732
var minimumSellPrice = sourcePrice + Settings.MinimumProfitSteps * PriceStep;
683-
// Logger.LogInformation($"minimumSellPrice: {minimumSellPrice}");
733+
var profitLoss = minimumSellPrice - sourcePrice;
734+
735+
if (Settings.MinimumProfitSteps < 0)
736+
{
737+
Logger.LogWarning($"Loss detection: selling at loss allowed. Source price: {sourcePrice}, minimum sell price: {minimumSellPrice}, potential loss: {Math.Abs(profitLoss)} ({Settings.MinimumProfitSteps} price steps)");
738+
}
739+
else
740+
{
741+
Logger.LogInformation($"Profit calculation: source price: {sourcePrice}, minimum sell price: {minimumSellPrice}, minimum profit: {profitLoss} ({Settings.MinimumProfitSteps} price steps)");
742+
}
743+
684744
return minimumSellPrice;
685745
}
686746

0 commit comments

Comments
 (0)