using IBKRTrader.Core.Database;
using IBKRTrader.Core.Logging;
namespace IBKRTrader.Core.Trading;
/// Verwaltet core_trade_history – Lesen und Schreiben von Trade-Einträgen.
public class TradeHistoryService
{
private readonly DatabaseService _db;
private readonly LoggingService _logger;
public TradeHistoryService(DatabaseService db, LoggingService logger)
{
_db = db;
_logger = logger;
}
public async Task RecordTradeAsync(
string module, string symbol, string action,
decimal quantity, decimal price, string? ibkrOrderId = null, string? notes = null)
{
await _db.ExecuteAsync(@"
INSERT INTO `core_trade_history`
(module, symbol, action, quantity, price, total_value, traded_at, ibkr_order_id, status, notes)
VALUES
(@module, @symbol, @action, @qty, @price, @total, @now, @orderId, 'Executed', @notes)",
new
{
module,
symbol,
action,
qty = quantity,
price,
total = quantity * price,
now = DateTime.UtcNow,
orderId = ibkrOrderId,
notes
});
_logger.Info(module, $"Trade gespeichert: {action} {quantity}x {symbol} @ {price:F2}");
}
public async Task> GetRecentTradesAsync(int limit = 100)
=> await _db.QueryAsync(
"SELECT * FROM `core_trade_history` ORDER BY traded_at DESC LIMIT @limit",
new { limit });
}