Files
MycLib/Src/Myc.Trade.DataPoint.pas
T
Michael Schimmel a3da63ad6a DataPoint
2025-06-24 08:11:46 +02:00

208 lines
5.7 KiB
ObjectPascal

unit Myc.Trade.DataPoint;
interface
type
// A data record for an Ask/Bid price pair.
TAskBidItem = packed record
Ask: Single;
Bid: Single;
constructor Create(AAsk, ABid: Single);
end;
// Represents a time-stamped data point in a series.
TDataPoint<T> = record
Time: TDateTime;
Data: T;
constructor Create(ATime: TDateTime; const AData: T);
end;
// A time-ordered series of data points, optimized for chronological additions.
// The most recently added element has the logical index 0.
IDataSeries<T> = interface
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
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;
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 property Null: IDataSeries<T> read FNull;
property Count: Int64 read GetCount;
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
property Data[Idx: Int64]: T read GetData;
property Time[Idx: Int64]: TDateTime read GetTime;
property MaxLookback: Int64 read GetMaxLookback;
property TotalCount: Int64 read GetTotalCount;
end;
implementation
uses
Myc.Trade.Core.DataPoint;
{ TAskBidItem }
constructor TAskBidItem.Create(AAsk, ABid: Single);
begin
Ask := AAsk;
Bid := ABid;
end;
{ TDataPoint<T> }
constructor TDataPoint<T>.Create(ATime: TDateTime; const AData: T);
begin
Time := ATime;
Data := AData;
end;
{ TDataSeries<T> Interface Helper }
class constructor TDataSeries<T>.CreateClass;
begin
FNull := TNullDataSeries<T>.Create;
end;
constructor TDataSeries<T>.Create(ADataSeries: IDataSeries<T>);
begin
if Assigned(ADataSeries) then
FDataSeries := ADataSeries
else
FDataSeries := FNull;
end;
class function TDataSeries<T>.CreateArray(MaxLookback: Int64): IDataArray<T>;
begin
Result := TDataArray<T>.Create(MaxLookback);
end;
class operator TDataSeries<T>.Finalize(var Dest: TDataSeries<T>);
begin
Dest.FDataSeries := nil;
end;
class operator TDataSeries<T>.Initialize(out Dest: TDataSeries<T>);
begin
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;
var
low, high, mid: Int64;
dataPointTime: TDateTime;
begin
Result := -1;
if Count = 0 then
Exit;
low := 0;
high := Count - 1;
while (low <= high) do
begin
mid := low + (high - low) div 2;
dataPointTime := GetItems(mid).Time; // Use GetItems to stay within the class
if (dataPointTime = TimeStamp) then
begin
Result := mid;
break;
end
else if (dataPointTime < TimeStamp) then
begin
Result := mid;
high := mid - 1;
end
else
begin
low := mid + 1;
end;
end;
end;
end.