327 lines
9.0 KiB
ObjectPascal
327 lines
9.0 KiB
ObjectPascal
unit Myc.Trade.Core.DataPoint;
|
|
|
|
interface
|
|
|
|
uses
|
|
Myc.Trade.DataPoint;
|
|
|
|
type
|
|
// The implementation class for IDataSeries<T>.
|
|
TDataArray<T> = class(TInterfacedObject, IDataSeries<T>, IDataArray<T>)
|
|
const
|
|
ChunkSize = 1024;
|
|
type
|
|
TChunk = TArray<TDataPoint<T>>;
|
|
private
|
|
FChunks: TArray<TChunk>;
|
|
FChunks1: TArray<TChunk>;
|
|
FCount: Int64;
|
|
FMaxLookback: Int64;
|
|
FTotalCount: Int64;
|
|
|
|
function LogicalToPhysicalIndex(LogicalIndex: Int64): Int64; inline;
|
|
function IsIndexInLimits(LogicalIndex: Int64): Boolean;
|
|
procedure Trim;
|
|
|
|
// IDataSeries<T> implementation
|
|
function GetCount: Int64;
|
|
function GetItems(Idx: Int64): TDataPoint<T>;
|
|
function GetMaxLookback: Int64;
|
|
function GetTotalCount: Int64;
|
|
public
|
|
constructor Create(AMaxLookback: Int64);
|
|
procedure Add(const Data: TDataPoint<T>); overload;
|
|
procedure Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1); overload;
|
|
procedure Clear;
|
|
function Copy: IDataSeries<T>;
|
|
property MaxLookback: Int64 read GetMaxLookback;
|
|
end;
|
|
|
|
// Null object implementation for IDataSeries<T>
|
|
TNullDataSeries<T> = class(TInterfacedObject, IDataSeries<T>)
|
|
strict private
|
|
class var
|
|
FNull: IDataSeries<T>;
|
|
private
|
|
class constructor CreateClass;
|
|
public
|
|
function GetCount: Int64;
|
|
function GetItems(Idx: Int64): TDataPoint<T>;
|
|
function GetTotalCount: Int64;
|
|
function IndexOf(TimeStamp: TDateTime): Int64;
|
|
function Copy: IDataSeries<T>;
|
|
class property Null: IDataSeries<T> read FNull;
|
|
end;
|
|
|
|
// The implementation class for IDataSeries<T>.
|
|
TStaticDataSeries<T> = class(TInterfacedObject, IDataSeries<T>)
|
|
private
|
|
FChunks: TArray<TDataArray<T>.TChunk>;
|
|
FCount: Int64;
|
|
FMaxLookback: Int64;
|
|
FTotalCount: Int64;
|
|
|
|
function LogicalToPhysicalIndex(LogicalIndex: Int64): Int64; inline;
|
|
function IsIndexInLimits(LogicalIndex: Int64): Boolean;
|
|
|
|
// IDataSeries<T> implementation
|
|
function GetCount: Int64;
|
|
function GetItems(Idx: Int64): TDataPoint<T>;
|
|
function GetMaxLookback: Int64;
|
|
function GetTotalCount: Int64;
|
|
|
|
public
|
|
constructor Create(const AChunks: TArray<TDataArray<T>.TChunk>; ACount, AMaxLookback, ATotalCount: Int64);
|
|
function Copy: IDataSeries<T>;
|
|
property MaxLookback: Int64 read GetMaxLookback;
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TDataArray<T> }
|
|
|
|
constructor TDataArray<T>.Create(AMaxLookback: Int64);
|
|
begin
|
|
inherited Create;
|
|
FMaxLookback := AMaxLookback;
|
|
FCount := 0;
|
|
FTotalCount := 0;
|
|
FChunks := nil;
|
|
end;
|
|
|
|
procedure TDataArray<T>.Add(const Data: TDataPoint<T>);
|
|
begin
|
|
Assert((FCount = 0) or (Data.Time >= GetItems(0).Time), '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);
|
|
|
|
Trim;
|
|
end;
|
|
|
|
procedure TDataArray<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)
|
|
else
|
|
resolvedNumToAdd := NumToAdd;
|
|
|
|
if resolvedNumToAdd = 0 then
|
|
Exit;
|
|
|
|
Assert(resolvedNumToAdd <= Length(Data), 'NumToAdd cannot be larger than the source array');
|
|
|
|
for i := 1 to resolvedNumToAdd - 1 do
|
|
Assert(Data[i].Time >= Data[i - 1].Time, 'Input array for Add is not chronologically sorted');
|
|
|
|
Assert((FCount = 0) or (Data[0].Time >= GetItems(0).Time), 'First new item is older than last existing item');
|
|
|
|
Inc(FTotalCount, resolvedNumToAdd);
|
|
|
|
var newTotalCount := FCount + resolvedNumToAdd;
|
|
var requiredChunks := (newTotalCount + ChunkSize - 1) div ChunkSize;
|
|
if requiredChunks = 0 then
|
|
requiredChunks := 1;
|
|
|
|
if requiredChunks > Length(FChunks) then
|
|
SetLength(FChunks, requiredChunks);
|
|
|
|
sourceIdx := 0;
|
|
while sourceIdx < resolvedNumToAdd do
|
|
begin
|
|
ci := FCount div ChunkSize;
|
|
di := FCount mod ChunkSize;
|
|
|
|
if di = 0 then
|
|
SetLength(FChunks[ci], ChunkSize);
|
|
|
|
spaceInChunk := ChunkSize - di;
|
|
itemsToCopy := resolvedNumToAdd - sourceIdx;
|
|
if spaceInChunk < itemsToCopy then
|
|
itemsToCopy := spaceInChunk;
|
|
|
|
System.Move(Data[sourceIdx], FChunks[ci][di], itemsToCopy * SizeOf(TDataPoint<T>));
|
|
|
|
Inc(FCount, itemsToCopy);
|
|
Inc(sourceIdx, itemsToCopy);
|
|
|
|
Trim;
|
|
end;
|
|
end;
|
|
|
|
procedure TDataArray<T>.Clear;
|
|
begin
|
|
FChunks := nil;
|
|
FCount := 0;
|
|
FTotalCount := 0;
|
|
end;
|
|
|
|
function TDataArray<T>.Copy: IDataSeries<T>;
|
|
begin
|
|
Result := TStaticDataSeries<T>.Create(FChunks, FCount, FMaxLookback, FTotalCount);
|
|
end;
|
|
|
|
function TDataArray<T>.GetCount: Int64;
|
|
begin
|
|
Result := FCount;
|
|
if (FMaxLookback > 0) and (Result > FMaxLookback) then
|
|
Result := FMaxLookback;
|
|
end;
|
|
|
|
function TDataArray<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 TDataArray<T>.GetMaxLookback: Int64;
|
|
begin
|
|
Result := FMaxLookback;
|
|
end;
|
|
|
|
function TDataArray<T>.GetTotalCount: Int64;
|
|
begin
|
|
Result := FTotalCount;
|
|
end;
|
|
|
|
function TDataArray<T>.IsIndexInLimits(LogicalIndex: Int64): Boolean;
|
|
begin
|
|
Result := (FMaxLookback <= 0) or (LogicalIndex < FMaxLookback);
|
|
end;
|
|
|
|
function TDataArray<T>.LogicalToPhysicalIndex(LogicalIndex: Int64): Int64;
|
|
begin
|
|
Assert((LogicalIndex >= 0) and (LogicalIndex < FCount), 'Logical index is out of bounds.');
|
|
Result := FCount - LogicalIndex - 1;
|
|
end;
|
|
|
|
procedure TDataArray<T>.Trim;
|
|
var
|
|
itemsToRemove, chunksToRemove: Int64;
|
|
begin
|
|
if (FCount = 0) or (FMaxLookback <= 0) then
|
|
Exit;
|
|
|
|
itemsToRemove := 0;
|
|
if FCount > FMaxLookback then
|
|
itemsToRemove := FCount - FMaxLookback;
|
|
|
|
if itemsToRemove <= 0 then
|
|
Exit;
|
|
|
|
chunksToRemove := itemsToRemove div ChunkSize;
|
|
|
|
if chunksToRemove > 0 then
|
|
begin
|
|
var itemsInFreedChunks := chunksToRemove * ChunkSize;
|
|
FChunks := System.Copy(FChunks, chunksToRemove, Length(FChunks) - chunksToRemove);
|
|
FCount := FCount - itemsInFreedChunks;
|
|
end;
|
|
end;
|
|
|
|
{ TNullDataSeries<T> Interface Helper }
|
|
|
|
class constructor TNullDataSeries<T>.CreateClass;
|
|
begin
|
|
FNull := TNullDataSeries<T>.Create;
|
|
end;
|
|
|
|
function TNullDataSeries<T>.Copy: IDataSeries<T>;
|
|
begin
|
|
Result := FNull;
|
|
end;
|
|
|
|
function TNullDataSeries<T>.GetCount: Int64;
|
|
begin
|
|
Result := 0;
|
|
end;
|
|
|
|
function TNullDataSeries<T>.GetItems(Idx: Int64): TDataPoint<T>;
|
|
begin
|
|
Assert(false, 'Index out of bounds.');
|
|
Result := Default(TDataPoint<T>);
|
|
end;
|
|
|
|
function TNullDataSeries<T>.GetTotalCount: Int64;
|
|
begin
|
|
Result := 0;
|
|
end;
|
|
|
|
function TNullDataSeries<T>.IndexOf(TimeStamp: TDateTime): Int64;
|
|
begin
|
|
Result := -1;
|
|
end;
|
|
|
|
{ TStaticDataSeries<T> }
|
|
|
|
constructor TStaticDataSeries<T>.Create(const AChunks: TArray<TDataArray<T>.TChunk>; ACount, AMaxLookback, ATotalCount: Int64);
|
|
begin
|
|
inherited Create;
|
|
FChunks := AChunks;
|
|
FCount := ACount;
|
|
FMaxLookback := AMaxLookback;
|
|
FTotalCount := ATotalCount;
|
|
end;
|
|
|
|
function TStaticDataSeries<T>.Copy: IDataSeries<T>;
|
|
begin
|
|
Result := Self;
|
|
end;
|
|
|
|
function TStaticDataSeries<T>.GetCount: Int64;
|
|
begin
|
|
Result := FCount;
|
|
if (FMaxLookback > 0) and (Result > FMaxLookback) then
|
|
Result := FMaxLookback;
|
|
end;
|
|
|
|
function TStaticDataSeries<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 TDataArray<T>.ChunkSize][physicalIndex mod TDataArray<T>.ChunkSize];
|
|
end;
|
|
|
|
function TStaticDataSeries<T>.GetMaxLookback: Int64;
|
|
begin
|
|
Result := FMaxLookback;
|
|
end;
|
|
|
|
function TStaticDataSeries<T>.GetTotalCount: Int64;
|
|
begin
|
|
Result := FTotalCount;
|
|
end;
|
|
|
|
function TStaticDataSeries<T>.IsIndexInLimits(LogicalIndex: Int64): Boolean;
|
|
begin
|
|
Result := (FMaxLookback <= 0) or (LogicalIndex < FMaxLookback);
|
|
end;
|
|
|
|
function TStaticDataSeries<T>.LogicalToPhysicalIndex(LogicalIndex: Int64): Int64;
|
|
begin
|
|
Assert((LogicalIndex >= 0) and (LogicalIndex < FCount), 'Logical index is out of bounds.');
|
|
Result := FCount - LogicalIndex - 1;
|
|
end;
|
|
|
|
end.
|