ResolutionFarming haelt sich an Pause und Handelsmodi

Behebt #569.

GlobalTradingPaused, LiveTradingMode und DemoTradingMode wertete bisher allein
CopyTrading aus. Die Farming-Ausfuehrung pruefte nur RfSettings.Enabled und
eroeffnete weiter Positionen, auch wenn "Trading pausieren" gedrueckt war oder
der Demo-Modus auf Inaktiv stand. Der Pause-Knopf hielt damit nur die Haelfte
an.

Echtes Geld war noch nicht betroffen: RF fuehrt nur Demo-Konten aus, die
Live-Ausfuehrung wird als Zielland-Arbeit uebersprungen. Mit der
Live-Anbindung haette eine Pause ohne Wirkung vor echten Positionen gestanden.

Jetzt gelten dieselben Regeln wie im CopyTrading. Die Pause sperrt, und der
Modus des Kontotyps (Demo oder Live) muss auf Aktiv stehen. "Nur Verkauf"
sperrt ebenfalls, denn Farming kauft ausschliesslich. Protokolliert wird nur
der Wechsel zwischen gesperrt und frei, nicht jeder Fuenf-Minuten-Durchlauf.

VERHALTENSAENDERUNG: Demo-Farming eroeffnet nur noch bei Demo-Modus = Aktiv.
Bisher lief es unabhaengig vom Modus, und der steht nach jedem Start auf
Inaktiv.

Bewusst nicht gesperrt ist die Aufloesungsueberwachung. Sie bucht nur Maerkte
ab, die bereits aufgeloest sind, und sendet keine Orders. Hielte man sie an,
wiche der ausgewiesene PnL von der Wirklichkeit ab. Der Scanner schreibt nur
Kandidaten und bleibt ebenfalls aktiv.

Der Schleifenrumpf liegt jetzt als RunCycle() vor und ist damit testbar.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Richard
2026-09-13 13:40:45 +02:00
co-authored by Claude Opus 5
parent b70d5e5f00
commit 2278d1ccf3
2 changed files with 191 additions and 16 deletions
@@ -58,22 +58,7 @@ namespace PolyTrader.Modules.ResolutionFarming.Services
{
try
{
foreach (var settings in _settingsRepo.GetAll().Where(s => s.Enabled))
{
if (stoppingToken.IsCancellationRequested) break;
if (!_state.Accounts.TryGetValue(settings.AccountId, out var account)) continue;
if (account.IsDemo)
{
var opened = RunDemoForAccount(settings.AccountId, account.TotalBalance, settings);
if (opened.Count > 0)
_logger.Trade($"🌱 [RF-Demo] Konto {account.Name}: {opened.Count} Position(en) eröffnet.");
}
else
{
_logger.Info($"[RF-Execution] Konto {account.Name}: Live-Execution (Maker-GTC via CLOB) ist Zielland-Arbeit – übersprungen.");
}
}
RunCycle(stoppingToken);
}
catch (OperationCanceledException) { break; }
catch (Exception ex) { _logger.Error($"RF-Execution Fehler: {ex.Message}"); }
@@ -82,6 +67,83 @@ namespace PolyTrader.Modules.ResolutionFarming.Services
}
}
/// <summary>
/// Ein Durchlauf über alle freigeschalteten Konten - der testbare Kern der Schleife.
/// Liefert die Zahl der eröffneten Positionen.
/// </summary>
internal int RunCycle(CancellationToken ct = default)
{
int total = 0;
foreach (var settings in _settingsRepo.GetAll().Where(s => s.Enabled))
{
if (ct.IsCancellationRequested) break;
if (!_state.Accounts.TryGetValue(settings.AccountId, out var account)) continue;
string? blocked = EntryBlockReason(_state, account.IsDemo);
ReportBlockChange(account, blocked);
if (blocked != null) continue;
if (account.IsDemo)
{
var opened = RunDemoForAccount(settings.AccountId, account.TotalBalance, settings);
total += opened.Count;
if (opened.Count > 0)
_logger.Trade($"🌱 [RF-Demo] Konto {account.Name}: {opened.Count} Position(en) eröffnet.");
}
else
{
_logger.Info($"[RF-Execution] Konto {account.Name}: Live-Execution (Maker-GTC via CLOB) ist Zielland-Arbeit – übersprungen.");
}
}
return total;
}
/// <summary>
/// Warum dieses Konto gerade keine neuen Positionen eröffnen darf, oder <c>null</c>, wenn es darf (#569).
///
/// <para>Bis zum 13.09.2026 prüfte die Farming-Ausführung weder die globale Pause noch die
/// Handelsmodi - beides wertete allein CopyTrading aus. "Trading pausieren" hielt damit nur die
/// Hälfte an. Hier gelten jetzt dieselben Regeln wie dort: Pause sperrt, und der Modus des
/// Kontotyps (Demo oder Live) muss auf Aktiv stehen. "Nur Verkauf" sperrt ebenfalls, denn
/// Farming kauft ausschließlich.</para>
///
/// <para>Die Auflösungsüberwachung bleibt davon bewusst unberührt: Sie bucht nur Märkte ab, die
/// bereits aufgelöst sind, und sendet keine Orders. Hielte man sie an, wiche der ausgewiesene
/// PnL von der Wirklichkeit ab.</para>
/// </summary>
internal static string? EntryBlockReason(TradingState state, bool isDemo)
{
if (state.GlobalTradingPaused)
return "globale Pause ist aktiv";
TradingMode mode = isDemo ? state.DemoTradingMode : state.LiveTradingMode;
if (mode == TradingMode.Active)
return null;
string which = isDemo ? "Demo" : "Live";
return mode == TradingMode.SellOnly
? $"{which}-Modus steht auf \"Nur Verkauf\" (Farming kauft ausschließlich)"
: $"{which}-Modus ist inaktiv";
}
private readonly Dictionary<int, string?> _lastBlockReason = new();
/// <summary>
/// Protokolliert nur den Wechsel zwischen gesperrt und frei. Die Schleife läuft alle fünf
/// Minuten; eine Zeile je Durchlauf und Konto würde das Protokoll zuschütten.
/// </summary>
private void ReportBlockChange(AccountState account, string? reason)
{
_lastBlockReason.TryGetValue(account.AccountId, out var last);
if (last == reason) return;
_lastBlockReason[account.AccountId] = reason;
if (reason != null)
_logger.Info($"⏸️ [RF-Execution] Konto {account.Name}: keine neuen Positionen – {reason}.");
else
_logger.Info($"▶️ [RF-Execution] Konto {account.Name}: Einstiege wieder freigegeben.");
}
/// <summary>
/// Plant und eröffnet Demo-Positionen für einen Account (testbarer Kern). Liefert die eröffneten
/// Positionen. Bankroll = Referenz für die %-Limits; die Demo-Balance wird bewusst NICHT mutiert