TSeries
This commit is contained in:
+34
-46
@@ -6,25 +6,24 @@ uses
|
|||||||
System.SysUtils;
|
System.SysUtils;
|
||||||
|
|
||||||
type
|
type
|
||||||
// A specific data record structure for Ask and Bid prices.
|
// A data record for an Ask/Bid price pair.
|
||||||
// Contains Ask and Bid prices as Single-precision floating-point numbers.
|
|
||||||
TAskBidItem = packed record
|
TAskBidItem = packed record
|
||||||
Ask: Single;
|
Ask: Single;
|
||||||
Bid: Single;
|
Bid: Single;
|
||||||
constructor Create(AAsk, ABid: Single);
|
constructor Create(AAsk, ABid: Single);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Represents a single data point in a time series.
|
// Represents a time-stamped data point in a series.
|
||||||
// Combines a timestamp with generic data.
|
|
||||||
TDataPoint<T> = record
|
TDataPoint<T> = record
|
||||||
Time: TDateTime;
|
Time: TDateTime;
|
||||||
Data: T;
|
Data: T;
|
||||||
constructor Create(ATime: TDateTime; const AData: T);
|
constructor Create(ATime: TDateTime; const AData: T);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// A time-ordered array whose most recently added element has index 0.
|
// A time-ordered series of data points, optimized for chronological additions.
|
||||||
// This managed record stores a series of TDataPoint<T> elements.
|
// The most recently added element has the logical index 0.
|
||||||
TDataSeries<T: record> = record
|
TDataSeries<T> = record
|
||||||
|
private
|
||||||
const
|
const
|
||||||
ChunkSize = 1024;
|
ChunkSize = 1024;
|
||||||
type
|
type
|
||||||
@@ -36,18 +35,19 @@ type
|
|||||||
function GetItems(Idx: Int64): TDataPoint<T>;
|
function GetItems(Idx: Int64): TDataPoint<T>;
|
||||||
public
|
public
|
||||||
// Adds a new data point to the series.
|
// Adds a new data point to the series.
|
||||||
// Asserts that the new data point's time is not older than the current newest item.
|
|
||||||
procedure Add(const Data: TDataPoint<T>);
|
procedure Add(const Data: TDataPoint<T>);
|
||||||
|
|
||||||
// Searches for a data point by its timestamp.
|
// Searches for a data point by its timestamp.
|
||||||
// Returns the logical index of the matching item.
|
// Returns the logical index of the matching item.
|
||||||
// If no exact match, returns the index of the item immediately preceding the timestamp.
|
// 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.
|
// Returns -1 if the timestamp is before the oldest item in the series.
|
||||||
function IndexOf(TimeStamp: TDateTime): Int64;
|
function IndexOf(TimeStamp: TDateTime): Int64;
|
||||||
// Initializes the TDataSeries record.
|
|
||||||
class operator Initialize(out Dest: TDataSeries<T>);
|
class operator Initialize(out Dest: TDataSeries<T>);
|
||||||
class operator Finalize(var Dest: TDataSeries<T>);
|
class operator Finalize(var Dest: TDataSeries<T>);
|
||||||
// The total number of data points in the series.
|
|
||||||
property Count: Int64 read GetCount;
|
property Count: Int64 read GetCount;
|
||||||
|
|
||||||
// Accesses data points by their logical index.
|
// Accesses data points by their logical index.
|
||||||
// Index 0 is the newest element, Index (Count - 1) is the oldest.
|
// Index 0 is the newest element, Index (Count - 1) is the oldest.
|
||||||
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
|
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
|
||||||
@@ -55,10 +55,15 @@ type
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses
|
{ TAskBidItem }
|
||||||
Winapi.Windows;
|
|
||||||
|
|
||||||
{ TDataPoint }
|
constructor TAskBidItem.Create(AAsk, ABid: Single);
|
||||||
|
begin
|
||||||
|
Ask := AAsk;
|
||||||
|
Bid := ABid;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TDataPoint<T> }
|
||||||
|
|
||||||
constructor TDataPoint<T>.Create(ATime: TDateTime; const AData: T);
|
constructor TDataPoint<T>.Create(ATime: TDateTime; const AData: T);
|
||||||
begin
|
begin
|
||||||
@@ -70,8 +75,8 @@ end;
|
|||||||
|
|
||||||
procedure TDataSeries<T>.Add(const Data: TDataPoint<T>);
|
procedure TDataSeries<T>.Add(const Data: TDataPoint<T>);
|
||||||
begin
|
begin
|
||||||
// Assert that the new data point's time is NOT OLDER than the current newest item.
|
// Enforce chronological order: new items cannot be older than the newest existing item.
|
||||||
// This means data must be added in chronologically ascending order (or equal timestamp).
|
// This is a prerequisite for the binary search in IndexOf to work correctly.
|
||||||
Assert((Length(FChunks) = 0) or (Data.Time >= GetItems(0).Time), 'Time stamp older than last item');
|
Assert((Length(FChunks) = 0) or (Data.Time >= GetItems(0).Time), 'Time stamp older than last item');
|
||||||
|
|
||||||
var ci := FCount div ChunkSize;
|
var ci := FCount div ChunkSize;
|
||||||
@@ -86,7 +91,13 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
FChunks[ci][di] := Data;
|
FChunks[ci][di] := Data;
|
||||||
inc(FCount);
|
Inc(FCount);
|
||||||
|
end;
|
||||||
|
|
||||||
|
class operator TDataSeries<T>.Finalize(var Dest: TDataSeries<T>);
|
||||||
|
begin
|
||||||
|
Dest.FChunks := nil;
|
||||||
|
Dest.FCount := 0;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TDataSeries<T>.GetCount: Int64;
|
function TDataSeries<T>.GetCount: Int64;
|
||||||
@@ -97,15 +108,21 @@ end;
|
|||||||
function TDataSeries<T>.GetItems(Idx: Int64): TDataPoint<T>;
|
function TDataSeries<T>.GetItems(Idx: Int64): TDataPoint<T>;
|
||||||
begin
|
begin
|
||||||
Assert((Idx >= 0) and (Idx < FCount));
|
Assert((Idx >= 0) and (Idx < FCount));
|
||||||
|
// Convert logical index (0 = newest) to physical index (0 = oldest).
|
||||||
Idx := FCount - Idx - 1;
|
Idx := FCount - Idx - 1;
|
||||||
Result := FChunks[Idx div ChunkSize][Idx mod ChunkSize];
|
Result := FChunks[Idx div ChunkSize][Idx mod ChunkSize];
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
class operator TDataSeries<T>.Initialize(out Dest: TDataSeries<T>);
|
||||||
|
begin
|
||||||
|
Dest.FCount := 0;
|
||||||
|
Dest.FChunks := nil;
|
||||||
|
end;
|
||||||
|
|
||||||
function TDataSeries<T>.IndexOf(TimeStamp: TDateTime): Int64;
|
function TDataSeries<T>.IndexOf(TimeStamp: TDateTime): Int64;
|
||||||
var
|
var
|
||||||
low, high, mid: Int64;
|
low, high, mid: Int64;
|
||||||
dataPointTime: TDateTime;
|
dataPointTime: TDateTime;
|
||||||
debugStr: string;
|
|
||||||
begin
|
begin
|
||||||
Result := -1;
|
Result := -1;
|
||||||
if FCount = 0 then
|
if FCount = 0 then
|
||||||
@@ -114,55 +131,26 @@ begin
|
|||||||
low := 0;
|
low := 0;
|
||||||
high := FCount - 1;
|
high := FCount - 1;
|
||||||
|
|
||||||
// Start der Debug-Ausgabe
|
|
||||||
OutputDebugString(PChar(Format('--- IndexOf Search for %.15f ---', [TimeStamp])));
|
|
||||||
|
|
||||||
while (low <= high) do
|
while (low <= high) do
|
||||||
begin
|
begin
|
||||||
mid := low + (high - low) div 2;
|
mid := low + (high - low) div 2;
|
||||||
dataPointTime := GetItems(mid).Time;
|
dataPointTime := GetItems(mid).Time;
|
||||||
|
|
||||||
// Ausgabe der Werte in jeder Iteration
|
|
||||||
debugStr := Format('low: %d, high: %d, mid: %d, dataPointTime: %.15f',
|
|
||||||
[low, high, mid, dataPointTime]);
|
|
||||||
OutputDebugString(PChar(debugStr));
|
|
||||||
|
|
||||||
if (dataPointTime = TimeStamp) then
|
if (dataPointTime = TimeStamp) then
|
||||||
begin
|
begin
|
||||||
OutputDebugString(' -> Match found!');
|
|
||||||
Result := mid;
|
Result := mid;
|
||||||
Exit;
|
Exit;
|
||||||
end
|
end
|
||||||
else if (dataPointTime < TimeStamp) then
|
else if (dataPointTime < TimeStamp) then
|
||||||
begin
|
begin
|
||||||
OutputDebugString(' -> dataPointTime < TimeStamp');
|
|
||||||
Result := mid;
|
Result := mid;
|
||||||
high := mid - 1;
|
high := mid - 1;
|
||||||
end
|
end
|
||||||
else // dataPointTime > TimeStamp
|
else // dataPointTime > TimeStamp
|
||||||
begin
|
begin
|
||||||
OutputDebugString(' -> dataPointTime > TimeStamp');
|
|
||||||
low := mid + 1;
|
low := mid + 1;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
OutputDebugString(PChar('--- Search Finished ---'));
|
|
||||||
end;
|
|
||||||
|
|
||||||
class operator TDataSeries<T>.Finalize(var Dest: TDataSeries<T>);
|
|
||||||
begin
|
|
||||||
Dest.FChunks := nil;
|
|
||||||
Dest.FCount := 0;
|
|
||||||
end;
|
|
||||||
|
|
||||||
class operator TDataSeries<T>.Initialize(out Dest: TDataSeries<T>);
|
|
||||||
begin
|
|
||||||
Dest.FCount := 0;
|
|
||||||
end;
|
|
||||||
|
|
||||||
constructor TAskBidItem.Create(AAsk, ABid: Single);
|
|
||||||
begin
|
|
||||||
Ask := AAsk;
|
|
||||||
Bid := ABid;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|||||||
Reference in New Issue
Block a user