Files
IBKRTrader/src/IBKRTrader.Modules.Accounting/Persistence/AccountingDbContext.cs
T
RichardandClaude Opus 4.8 2a312ca035 R8: Accounting- + Supervisor-Modul + Core-Datenfundament (S-0)
Portierung der beiden fehlenden Grundbausteine aus PolytraderSharp (voller Ausbau).

Core S-0 (Datenfundament fuer Analyse/Forensik):
- core_decision_journal + core_order_events (+ ReasonCode/Decision/OrderEvent-Enums),
  IDecisionJournal/IOrderEventLog mit fehlertoleranten EF-Impls (Handel bricht nie).
- SignalId-Durchreichung TradeSignal -> ExecutionService -> core_trade_history;
  ExecutionService schreibt an jeder Verzweigung Journal/Order-Events.
- JSONL-Log-Sink (LogJson + Dual-Sink), pure Analytik: RealizedPnlEngine (FIFO),
  TradeAnalytics, DossierBuilder. Migration AddAnalysisFoundation.

Accounting-Modul (acc_): unabhaengiger IBKR-Kontoauszug (Activity Flex Query) hinter
Interfaces mit Offline-Null-Stubs -> append-only Ledger + Periodenabrechnung/BWA + FX
(USD/EUR) + CSV/PDF (PDFsharp/MigraDoc). Steuerschicht bewusst offen (Platzhalter-Tab).
Kein Handel. Migration InitialAccounting.

Supervisor-Modul (sup_): read-only OpenRouter-Agent (Function-Calling-Loop) + read-only
Tool-Registry (8 Tools) + Profile + Dossier-Browser + Counterfactual-Job (Stub) +
Tagesbericht/MCP-Light (opt-in). Migration InitialSupervisor.

Verdrahtung: Program.cs (beide Module + Icons), slnx/App/Tests-Referenzen,
provision-db.ps1, AppSettings-Sektionen, docs/konzepte, README.

Tests: 79 -> 117 gruen (FIFO/KPIs/Dossier/JSONL, Classifier/Engine/FX/Idempotenz,
OpenRouter/Registry/Agent/MCP, STA-Konstruktion beider neuen Fenster).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-31 09:25:18 +02:00

97 lines
4.0 KiB
C#

using IBKRTrader.Core.Configuration;
using IBKRTrader.Modules.Accounting.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace IBKRTrader.Modules.Accounting.Persistence;
/// <summary>
/// EF-Kontext des Accounting-Moduls (gleiche MariaDB, Tabellen mit Präfix acc_). Append-only Ledger mit
/// Autoincrement-PKs und Unique-Index auf dem Idempotenz-Schlüssel (kein Doppel-Buchen).
/// </summary>
public class AccountingDbContext : DbContext
{
public AccountingDbContext(DbContextOptions<AccountingDbContext> options) : base(options) { }
public DbSet<LedgerEntry> Ledger => Set<LedgerEntry>();
public DbSet<IngestRun> IngestRuns => Set<IngestRun>();
public DbSet<RawSnapshot> RawSnapshots => Set<RawSnapshot>();
public DbSet<FxRate> FxRates => Set<FxRate>();
protected override void OnModelCreating(ModelBuilder b)
{
b.Entity<LedgerEntry>(e =>
{
e.ToTable("acc_ledger");
e.HasKey(x => x.Id);
e.Property(x => x.Id).ValueGeneratedOnAdd();
e.Property(x => x.AccountId).HasMaxLength(30);
e.Property(x => x.EventType).HasConversion<string>().HasMaxLength(20);
e.Property(x => x.Symbol).HasMaxLength(30);
e.Property(x => x.AssetClass).HasMaxLength(10);
e.Property(x => x.Currency).HasMaxLength(5);
e.Property(x => x.Side).HasMaxLength(10);
e.Property(x => x.Source).HasMaxLength(40);
e.Property(x => x.TransactionId).HasMaxLength(60);
e.Property(x => x.IdempotencyKey).HasMaxLength(120);
e.Property(x => x.Quantity).HasPrecision(28, 8);
e.Property(x => x.PriceNative).HasPrecision(18, 6);
e.Property(x => x.GrossBase).HasPrecision(28, 8);
e.Property(x => x.FeeBase).HasPrecision(28, 8);
e.Property(x => x.NetBase).HasPrecision(28, 8);
e.HasIndex(x => x.IdempotencyKey).IsUnique(); // Idempotenz: kein Doppel-Buchen
e.HasIndex(x => new { x.AccountId, x.Timestamp });
e.HasIndex(x => x.EventType);
});
b.Entity<IngestRun>(e =>
{
e.ToTable("acc_ingest_runs");
e.HasKey(x => x.Id);
e.Property(x => x.Id).ValueGeneratedOnAdd();
e.Property(x => x.AccountId).HasMaxLength(30);
e.Property(x => x.Message).HasMaxLength(1000);
e.Property(x => x.BalanceAnchorBase).HasPrecision(28, 8);
e.Property(x => x.LedgerNetBase).HasPrecision(28, 8);
e.Property(x => x.BalanceDeltaBase).HasPrecision(28, 8);
e.HasIndex(x => new { x.AccountId, x.StartedAt });
});
b.Entity<RawSnapshot>(e =>
{
e.ToTable("acc_raw");
e.HasKey(x => x.Id);
e.Property(x => x.Id).ValueGeneratedOnAdd();
e.Property(x => x.AccountId).HasMaxLength(30);
e.Property(x => x.SourceKind).HasMaxLength(20);
e.Property(x => x.Json).HasColumnType("longtext");
e.HasIndex(x => x.IngestRunId);
});
b.Entity<FxRate>(e =>
{
e.ToTable("acc_fx_rates");
e.HasKey(x => x.Date);
e.Property(x => x.Date).HasColumnType("date");
e.Property(x => x.UsdToEur).HasPrecision(18, 8);
e.Property(x => x.Source).HasMaxLength(40);
});
}
}
/// <summary>Design-Time-Factory für EF-Tooling (dotnet ef). Connection aus env IBKRTRADER_MYSQL.</summary>
public class AccountingDbContextFactory : IDesignTimeDbContextFactory<AccountingDbContext>
{
public AccountingDbContext CreateDbContext(string[] args)
{
var conn = Environment.GetEnvironmentVariable("IBKRTRADER_MYSQL")
?? "Server=localhost;Port=3306;Database=ibkrtrader;User ID=root;Password=;";
var options = new DbContextOptionsBuilder<AccountingDbContext>()
.UseMySql(conn, DatabaseServerVersion.Value)
.Options;
return new AccountingDbContext(options);
}
}