# Projektplan: Meilenstein Daten-Infrastruktur
*Datum: 13. Juni 2025*
## Status: Design-Phase abgeschlossen, Basis-Implementierung erfolgt
---
## Stufe 1: Asynchrone Datenstrom-Schnittstelle (`IDataStream<T>`) - **ABGESCHLOSSEN**
* **Motivation**: Das ursprüngliche, zustandsbehaftete Design war für die Verarbeitung asynchron eintreffender Daten (z.B. von `TFuture`-Objekten) zu komplex und fehleranfällig.
* **Ziel**: Die Schaffung eines robusten, ereignisgesteuerten und non-blocking Modells, das den Datenproduzenten sauber vom Konsumenten entkoppelt.
* **Ergebnis**:
* Die `IDataStream<T>`-Schnittstelle wurde überarbeitet. Die `HasData`-Eigenschaft liefert nun ein `TSignal`, das Konsumenten aktiv über potenziell neue Daten informiert.
* Die Referenzimplementierung `TAuraFileStream<T>` wurde erfolgreich angepasst und nutzt eine saubere Ereignis-Kopplung (`Subscribe`) für eine robuste und wartungsarme Logik.
* Die korrekte Funktionalität wurde durch eine angepasste DUnitX-Test-Suite verifiziert.
---
## Stufe 2: Abstraktion für Handelssysteme (`IDataSeriesProvider`) - **ENTWORFEN**
* **Motivation**: Ein Handelssystem benötigt einen stets validen und kontinuierlichen Daten-Lookback. Ein roher `IDataStream<T>` kann dies nicht garantieren, da Lücken in den Daten auftreten können (z.B. beim Übergang von Historie zu Live).
* **Ziel**: Die Konzeption einer übergeordneten Abstraktionsschicht, die diese komplexe Anforderung kapselt, die Datenintegrität sicherstellt und dem Handelssystem eine einfache, sichere Schnittstelle bietet.
* **Ergebnis**:
Entworfen wurde der `IDataSeriesProvider`, der als "Black Box" für das Handelssystem fungiert und die Komplexität der Datenbeschaffung vollständig verbirgt. Er wurde mit zwei unterschiedlichen, vom Anwender wählbaren Betriebsmodi konzipiert:
### Modus 1: "Live-Handel"
* **Motivation**: Um einen echten **"Sofort-Start"** im Live-Handel zu ermöglichen, muss die Lücke zwischen den statischen, lokalen Historiendaten und dem aktuellen Zeitpunkt geschlossen werden.
* **Ziel**: Ein lückenloser, tagesaktueller Start des Handelssystems ohne manuelles Eingreifen oder lange Wartezeiten für den Nutzer.
* **Ergebnis**: Das Design einer **Drei-Phasen-Synchronisation**: (1) Lokale History laden, (2) "Catch-up"-Daten vom Broker holen, (3) auf den Live-Stream umschalten.
### Modus 2: "Simulation & Backtest"
* **Motivation**: Für Entwicklung, Test und Analyse muss die Software **völlig autonom** und ohne Abhängigkeit von einer externen, potenziell nicht verfügbaren Broker-API lauffähig sein.
* **Ziel**: Einen "Sofort-Start" für Backtests zu jedem beliebigen Zeitpunkt in der Vergangenheit zu ermöglichen, der rein auf lokalen Dateien basiert.
* **Ergebnis**: Ein Design, bei dem der relevante Datenkontext in den Speicher geladen wird, um von dort aus einen schnellen Start und ein "Playback" der Daten zu ermöglichen.
This commit is contained in:
@@ -31,18 +31,24 @@ uses
|
||||
type
|
||||
|
||||
// Represents a generic data stream capable of providing sequential data chunks.
|
||||
// IsHistory:
|
||||
// - true, if this stream is a history stream. Once HasData becomes false, it reached it's end and will not provide more data.
|
||||
// - false, we expect more Data to come. This stream has no end.
|
||||
// HasData: set, if a call to GetChunk will return new data.
|
||||
IDataStream<T> = interface
|
||||
['{A6E246A2-E84E-49AB-A63E-333E561E488C}']
|
||||
function GetIsLiveData: TMutable<Boolean>;
|
||||
function GetHasData: TSignal;
|
||||
function GetChunk(var Data: array of TDataPoint<T>): Integer;
|
||||
property IsLiveData: TMutable<Boolean> read GetIsLiveData;
|
||||
function IsHistory: Boolean;
|
||||
property HasData: TSignal read GetHasData;
|
||||
end;
|
||||
|
||||
// Abstract base class for IDataStream implementations.
|
||||
TDataStream<T> = class(TInterfacedObject, IDataStream<T>)
|
||||
protected
|
||||
function GetIsLiveData: TMutable<Boolean>; virtual; abstract;
|
||||
function GetHasData: TSignal; virtual; abstract;
|
||||
function GetChunk(var Data: array of TDataPoint<T>): Integer; virtual; abstract;
|
||||
function IsHistory: Boolean; virtual; abstract;
|
||||
end;
|
||||
|
||||
// Represents a factory for creating IDataStream instances.
|
||||
@@ -131,10 +137,10 @@ type
|
||||
end;
|
||||
|
||||
// Implements a data stream that reads from Aura-specific historical data files.
|
||||
TAuraFileStream<T: record> = class(TDataStream<T>, IDataStream<T>)
|
||||
TAuraFileStream<T: record> = class(TDataStream<T>)
|
||||
private
|
||||
FDataServer: TAuraDataServer<T>;
|
||||
FIsLiveData: TMutable<Boolean>.IWriteable;
|
||||
FHasData: TEvent;
|
||||
FCurrentFileName: string;
|
||||
FCurrentData: TFuture<TArray<TDataPoint<T>>>;
|
||||
FNextFileName: string;
|
||||
@@ -142,10 +148,12 @@ type
|
||||
FCurrPosInFile: Int64;
|
||||
FLastTimeStamp: TDateTime;
|
||||
protected
|
||||
function GetHasData: TSignal; override;
|
||||
function GetChunk(var Data: array of TDataPoint<T>): Integer; override;
|
||||
function GetIsLiveData: TMutable<Boolean>; override;
|
||||
function IsHistory: Boolean; override;
|
||||
public
|
||||
constructor Create(ADataServer: TAuraDataServer<T>; const AFilename: String);
|
||||
destructor Destroy; override;
|
||||
procedure AfterConstruction; override;
|
||||
end;
|
||||
|
||||
@@ -581,21 +589,30 @@ begin
|
||||
Assert(Assigned(ADataServer));
|
||||
FDataServer := ADataServer;
|
||||
FCurrentFileName := AFilename;
|
||||
FIsLiveData := TMutable<Boolean>.CreateWriteable(false);
|
||||
// Create an event
|
||||
FHasData := TEvent.CreateEvent; // interface helper benutzen!
|
||||
end;
|
||||
|
||||
destructor TAuraFileStream<T>.Destroy;
|
||||
begin
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TAuraFileStream<T>.AfterConstruction;
|
||||
begin
|
||||
inherited;
|
||||
FCurrentData := FDataServer.LoadDataFile(FCurrentFileName);
|
||||
FCurrPosInFile := 0;
|
||||
FIsLiveData.SetValue(false);
|
||||
FLastTimeStamp := 0;
|
||||
FCurrPosInFile := 0;
|
||||
FCurrentData := FDataServer.LoadDataFile(FCurrentFileName);
|
||||
// Forward all future done events, because this means there is new data.
|
||||
FCurrentData.Done.Subscribe( FHasData );
|
||||
|
||||
FNextFileName := FDataServer.FindNextDataFile(FCurrentFileName);
|
||||
if FNextFileName <> '' then
|
||||
FNextData := FDataServer.LoadDataFile(FNextFileName)
|
||||
else
|
||||
FNextData := nil;
|
||||
begin
|
||||
FNextData := FDataServer.LoadDataFile(FNextFileName);
|
||||
FNextData.Done.Subscribe( FHasData );
|
||||
end;
|
||||
end;
|
||||
|
||||
function TAuraFileStream<T>.GetChunk(var Data: array of TDataPoint<T>): Integer;
|
||||
@@ -604,39 +621,37 @@ var
|
||||
currData: TArray<TDataPoint<T>>;
|
||||
begin
|
||||
Result := 0;
|
||||
|
||||
// This is an asynchronous operation! We don't wait for data. It's totally valid to result nothing, if there is nothing.
|
||||
if not FCurrentData.Done.IsSet then
|
||||
exit;
|
||||
|
||||
var maxLen := Length(Data);
|
||||
currData := FCurrentData.Value;
|
||||
while Result < maxLen do
|
||||
if FCurrPosInFile >= Length(FCurrentData.Value) then
|
||||
begin
|
||||
if FCurrPosInFile >= Length(currData) then
|
||||
begin
|
||||
FCurrentData := FNextData;
|
||||
FCurrentFileName := FNextFileName;
|
||||
FCurrPosInFile := 0;
|
||||
if FCurrentFileName <> '' then
|
||||
begin
|
||||
FNextFileName := FDataServer.FindNextDataFile(FCurrentFileName);
|
||||
if FNextFileName <> '' then
|
||||
FNextData := FDataServer.LoadDataFile(FNextFileName)
|
||||
else
|
||||
FNextData := nil;
|
||||
FCurrentData := FNextData;
|
||||
FCurrentFileName := FNextFileName;
|
||||
FCurrPosInFile := 0;
|
||||
FNextData := TFuture<TArray<TDataPoint<T>>>.Null;
|
||||
|
||||
if FCurrentData.Done.IsSet then
|
||||
begin
|
||||
currData := FCurrentData.Value;
|
||||
continue;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
FIsLiveData.SetValue(true);
|
||||
end;
|
||||
break;
|
||||
if FCurrentFileName = '' then
|
||||
exit;
|
||||
|
||||
FNextFileName := FDataServer.FindNextDataFile(FCurrentFileName);
|
||||
if FNextFileName <> '' then
|
||||
begin
|
||||
FNextData := FDataServer.LoadDataFile(FNextFileName);
|
||||
FNextData.Done.Subscribe( FHasData );
|
||||
end;
|
||||
|
||||
if not FCurrentData.Done.IsSet then
|
||||
exit;
|
||||
end;
|
||||
|
||||
currData := FCurrentData.Value;
|
||||
|
||||
var maxLen := Length(Data);
|
||||
while (Result < maxLen) and (FCurrPosInFile < Length(currData)) do
|
||||
begin
|
||||
item := currData[FCurrPosInFile];
|
||||
if FLastTimeStamp < item.Time then
|
||||
begin
|
||||
@@ -646,11 +661,23 @@ begin
|
||||
end;
|
||||
Inc(FCurrPosInFile);
|
||||
end;
|
||||
|
||||
// If there is data left in the current file, signal new data. If it is finished, we do nothing, because
|
||||
// the next signal will come from the next future being done.
|
||||
if FCurrPosInFile < Length(currData) then
|
||||
begin
|
||||
FHasData.Notify;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TAuraFileStream<T>.GetIsLiveData: TMutable<Boolean>;
|
||||
function TAuraFileStream<T>.GetHasData: TSignal;
|
||||
begin
|
||||
Result := FIsLiveData;
|
||||
Result := FHasData.Signal;
|
||||
end;
|
||||
|
||||
function TAuraFileStream<T>.IsHistory: Boolean;
|
||||
begin
|
||||
Result := True;
|
||||
end;
|
||||
|
||||
{ TAuraTABFileServer }
|
||||
|
||||
Reference in New Issue
Block a user