DataPoint

This commit is contained in:
Michael Schimmel
2025-06-24 08:11:46 +02:00
parent 6c7cc2569b
commit a3da63ad6a
14 changed files with 902 additions and 633 deletions
+103 -222
View File
@@ -2,9 +2,6 @@ unit Myc.Trade.DataPoint;
interface
uses
System.SysUtils;
type
// A data record for an Ask/Bid price pair.
TAskBidItem = packed record
@@ -22,60 +19,71 @@ type
// A time-ordered series of data points, optimized for chronological additions.
// The most recently added element has the logical index 0.
TDataSeries<T> = record
private
const
ChunkSize = 1024;
type
TChunk = TArray<TDataPoint<T>>;
private
FChunks: TArray<TChunk>;
FCount: Int64;
FMaxLookback: Int64;
FTotalCount: Int64;
// Converts a logical index (0=newest) to a physical storage index (0=oldest).
function LogicalToPhysicalIndex(LogicalIndex: Int64): Int64; inline;
IDataSeries<T> = interface
function GetCount: Int64;
function GetData(Idx: Int64): T;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetTime(Idx: Int64): TDateTime;
procedure SetData(Idx: Int64; const Value: T);
// Checks if a logical index is within the defined limits.
function IsIndexInLimits(LogicalIndex: Int64): Boolean;
// Removes old chunks that are outside the Lookback limits.
procedure Trim;
public
constructor Create(AMaxLookback: Int64);
// Adds a new data point to the series.
function GetTotalCount: Int64;
property Count: Int64 read GetCount;
// Accesses data points by their logical index.
// Index 0 is the newest element, Index (Count - 1) is the oldest.
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
// The total number of items ever added to the series.
property TotalCount: Int64 read GetTotalCount;
end;
IDataArray<T> = interface(IDataSeries<T>)
procedure Add(const Data: TDataPoint<T>); overload;
procedure Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1); overload;
// Clears all data from the series.
procedure Clear;
end;
// Interface Helper for IDataSeries<T>.
// Provides a safe, value-type-like wrapper around the interface.
TDataSeries<T> = record
strict private
class var
FNull: IDataSeries<T>;
private
FDataSeries: IDataSeries<T>;
FMaxLookback: Int64;
class constructor CreateClass;
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetData(Idx: Int64): T;
function GetTime(Idx: Int64): TDateTime;
function GetMaxLookback: Int64;
function GetTotalCount: Int64;
public
constructor Create(ADataSeries: IDataSeries<T>);
class operator Initialize(out Dest: TDataSeries<T>);
class operator Finalize(var Dest: TDataSeries<T>);
class operator Implicit(const A: TDataSeries<T>): IDataSeries<T>;
class operator Implicit(const A: IDataSeries<T>): TDataSeries<T>;
class function CreateArray(MaxLookback: Int64): IDataArray<T>; static;
// Searches for a data point by its timestamp.
// Returns the logical index of the matching item.
// If no exact match, returns the index of the item immediately preceding the timestamp.
// Returns -1 if the timestamp is before the oldest item in the series.
function IndexOf(TimeStamp: TDateTime): Int64;
class operator Initialize(out Dest: TDataSeries<T>);
class operator Finalize(var Dest: TDataSeries<T>);
class property Null: IDataSeries<T> read FNull;
property Count: Int64 read GetCount;
// Accesses data points by their logical index.
// Index 0 is the newest element, Index (Count - 1) is the oldest.
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
property Data[Idx: Int64]: T read GetData;
property Time[Idx: Int64]: TDateTime read GetTime;
property Data[Idx: Int64]: T read GetData write SetData;
// The maximum number of data points to keep (0 = unlimited).
property MaxLookback: Int64 read FMaxLookback;
// The total number of items ever added to the series (resets on Clear).
property TotalCount: Int64 read FTotalCount;
property MaxLookback: Int64 read GetMaxLookback;
property TotalCount: Int64 read GetTotalCount;
end;
implementation
uses
Myc.Trade.Core.DataPoint;
{ TAskBidItem }
constructor TAskBidItem.Create(AAsk, ABid: Single);
@@ -92,153 +100,74 @@ begin
Data := AData;
end;
constructor TDataSeries<T>.Create(AMaxLookback: Int64);
{ TDataSeries<T> Interface Helper }
class constructor TDataSeries<T>.CreateClass;
begin
FMaxLookback := AMaxLookback;
FCount := 0;
FTotalCount := 0;
FChunks := nil;
FNull := TNullDataSeries<T>.Create;
end;
{ TDataSeries<T> }
procedure TDataSeries<T>.Add(const Data: TDataPoint<T>);
constructor TDataSeries<T>.Create(ADataSeries: IDataSeries<T>);
begin
// Enforce chronological order for new items.
Assert((FCount = 0) or (Data.Time >= GetTime(0)), 'Time stamp older than last item');
var ci := FCount div ChunkSize;
var di := FCount mod ChunkSize;
if (di = 0) then
begin
SetLength(FChunks, ci + 1);
SetLength(FChunks[ci], ChunkSize);
end;
FChunks[ci][di] := Data;
Inc(FCount);
Inc(FTotalCount);
// Remove old data that is now out of limits.
Trim;
end;
procedure TDataSeries<T>.Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1);
var
sourceIdx, itemsToCopy, spaceInChunk: Integer;
ci, di: Int64;
i: Integer;
resolvedNumToAdd: Integer;
begin
if NumToAdd < 0 then
resolvedNumToAdd := Length(Data)
if Assigned(ADataSeries) then
FDataSeries := ADataSeries
else
resolvedNumToAdd := NumToAdd;
if resolvedNumToAdd = 0 then
Exit;
Assert(resolvedNumToAdd <= Length(Data), 'NumToAdd cannot be larger than the source array');
// Check chronological order of the input array itself
for i := 1 to resolvedNumToAdd - 1 do
Assert(Data[i].Time >= Data[i - 1].Time, 'Input array for Add is not chronologically sorted');
// Enforce chronological order for new items against existing series.
Assert((FCount = 0) or (Data[0].Time >= GetTime(0)), 'First new item is older than last existing item');
// Increment total count immediately.
Inc(FTotalCount, resolvedNumToAdd);
var newTotalCount := FCount + resolvedNumToAdd;
var requiredChunks := (newTotalCount + ChunkSize - 1) div ChunkSize;
if requiredChunks = 0 then // case for newTotalCount = 0
requiredChunks := 1;
// Pre-allocate chunk array structure
if requiredChunks > Length(FChunks) then
SetLength(FChunks, requiredChunks);
sourceIdx := 0;
while sourceIdx < resolvedNumToAdd do
begin
ci := FCount div ChunkSize;
di := FCount mod ChunkSize;
// Allocate the actual chunk if we are at its beginning
if di = 0 then
SetLength(FChunks[ci], ChunkSize);
spaceInChunk := ChunkSize - di;
itemsToCopy := resolvedNumToAdd - sourceIdx;
if spaceInChunk < itemsToCopy then
itemsToCopy := spaceInChunk;
// Move a block of data into the available space in the current chunk.
System.Move(Data[sourceIdx], FChunks[ci][di], itemsToCopy * SizeOf(TDataPoint<T>));
Inc(FCount, itemsToCopy);
Inc(sourceIdx, itemsToCopy);
// Remove old data that is now out of limits
Trim;
end;
FDataSeries := FNull;
end;
procedure TDataSeries<T>.Clear;
class function TDataSeries<T>.CreateArray(MaxLookback: Int64): IDataArray<T>;
begin
FChunks := nil;
FCount := 0;
FTotalCount := 0;
Result := TDataArray<T>.Create(MaxLookback);
end;
class operator TDataSeries<T>.Finalize(var Dest: TDataSeries<T>);
begin
Dest.Clear;
end;
function TDataSeries<T>.GetCount: Int64;
begin
// The public Count must always reflect the addressable range.
Result := FCount;
if (FMaxLookback > 0) and (Result > FMaxLookback) then
Result := FMaxLookback;
end;
function TDataSeries<T>.GetData(Idx: Int64): T;
var
physicalIndex: Int64;
begin
Assert(IsIndexInLimits(Idx), 'Index is outside of the configured Lookback limits.');
physicalIndex := LogicalToPhysicalIndex(Idx);
Result := FChunks[physicalIndex div ChunkSize][physicalIndex mod ChunkSize].Data;
end;
function TDataSeries<T>.GetItems(Idx: Int64): TDataPoint<T>;
var
physicalIndex: Int64;
begin
Assert(IsIndexInLimits(Idx), 'Index is outside of the configured Lookback limits.');
physicalIndex := LogicalToPhysicalIndex(Idx);
Result := FChunks[physicalIndex div ChunkSize][physicalIndex mod ChunkSize];
end;
function TDataSeries<T>.GetTime(Idx: Int64): TDateTime;
var
physicalIndex: Int64;
begin
Assert(IsIndexInLimits(Idx), 'Index is outside of the configured Lookback limits.');
physicalIndex := LogicalToPhysicalIndex(Idx);
Result := FChunks[physicalIndex div ChunkSize][physicalIndex mod ChunkSize].Time;
Dest.FDataSeries := nil;
end;
class operator TDataSeries<T>.Initialize(out Dest: TDataSeries<T>);
begin
Dest.FCount := 0;
Dest.FTotalCount := 0;
Dest.FChunks := nil;
Dest.FMaxLookback := 0;
Dest.FDataSeries := FNull;
end;
class operator TDataSeries<T>.Implicit(const A: TDataSeries<T>): IDataSeries<T>;
begin
Result := A.FDataSeries;
end;
class operator TDataSeries<T>.Implicit(const A: IDataSeries<T>): TDataSeries<T>;
begin
Result.Create(A);
end;
function TDataSeries<T>.GetCount: Int64;
begin
Result := FDataSeries.GetCount;
end;
function TDataSeries<T>.GetItems(Idx: Int64): TDataPoint<T>;
begin
Result := FDataSeries[Idx];
end;
function TDataSeries<T>.GetData(Idx: Int64): T;
begin
Result := FDataSeries[Idx].Data;
end;
function TDataSeries<T>.GetTime(Idx: Int64): TDateTime;
begin
Result := FDataSeries[Idx].Time;
end;
function TDataSeries<T>.GetMaxLookback: Int64;
begin
Result := FMaxLookback;
end;
function TDataSeries<T>.GetTotalCount: Int64;
begin
Result := FDataSeries.GetTotalCount;
end;
function TDataSeries<T>.IndexOf(TimeStamp: TDateTime): Int64;
@@ -247,16 +176,16 @@ var
dataPointTime: TDateTime;
begin
Result := -1;
if Count = 0 then // Use public Count to respect limits
if Count = 0 then
Exit;
low := 0;
high := Count - 1; // Use public Count to respect limits
high := Count - 1;
while (low <= high) do
begin
mid := low + (high - low) div 2;
dataPointTime := GetTime(mid);
dataPointTime := GetItems(mid).Time; // Use GetItems to stay within the class
if (dataPointTime = TimeStamp) then
begin
@@ -268,59 +197,11 @@ begin
Result := mid;
high := mid - 1;
end
else // dataPointTime > TimeStamp
else
begin
low := mid + 1;
end;
end;
end;
function TDataSeries<T>.IsIndexInLimits(LogicalIndex: Int64): Boolean;
begin
// Check Lookback limit
Result := (FMaxLookback <= 0) or (LogicalIndex < FMaxLookback);
end;
function TDataSeries<T>.LogicalToPhysicalIndex(LogicalIndex: Int64): Int64;
begin
Assert((LogicalIndex >= 0) and (LogicalIndex < Count), 'Logical index is out of bounds.');
Result := FCount - LogicalIndex - 1;
end;
procedure TDataSeries<T>.SetData(Idx: Int64; const Value: T);
var
physicalIndex: Int64;
begin
Assert(IsIndexInLimits(Idx), 'Index is outside of the configured Lookback limits.');
physicalIndex := LogicalToPhysicalIndex(Idx);
FChunks[physicalIndex div ChunkSize][physicalIndex mod ChunkSize].Data := Value;
end;
procedure TDataSeries<T>.Trim;
var
itemsToRemove, chunksToRemove: Int64;
begin
if (FCount = 0) or (FMaxLookback <= 0) then
Exit;
itemsToRemove := 0;
if FCount > FMaxLookback then
begin
itemsToRemove := FCount - FMaxLookback;
end;
if itemsToRemove <= 0 then
Exit;
// We only free full chunks to avoid expensive partial copy operations.
chunksToRemove := itemsToRemove div ChunkSize;
if chunksToRemove > 0 then
begin
var itemsInFreedChunks := chunksToRemove * ChunkSize;
FChunks := Copy(FChunks, chunksToRemove, Length(FChunks) - chunksToRemove);
FCount := FCount - itemsInFreedChunks;
end;
end;
end.