Sample SMA strategy
This commit is contained in:
@@ -32,7 +32,6 @@ uses
|
||||
class function TRttiAstHelper.JsonToRecordDefinition(const AJson: string): TScalarRecordDefinition;
|
||||
var
|
||||
jsonValue: TJSONValue;
|
||||
rootObj: TJSONObject;
|
||||
fieldsArray: TJSONArray;
|
||||
i: Integer;
|
||||
fieldObj: TJSONObject;
|
||||
@@ -44,12 +43,11 @@ begin
|
||||
exit;
|
||||
|
||||
try
|
||||
if not (jsonValue is TJSONObject) then
|
||||
// The root element is expected to be a JSON array directly.
|
||||
if not (jsonValue is TJSONArray) then
|
||||
exit;
|
||||
|
||||
rootObj := jsonValue as TJSONObject;
|
||||
if not rootObj.TryGetValue<TJSONArray>('fields', fieldsArray) then
|
||||
exit;
|
||||
fieldsArray := jsonValue as TJSONArray;
|
||||
|
||||
SetLength(fields, fieldsArray.Count);
|
||||
for i := 0 to fieldsArray.Count - 1 do
|
||||
@@ -75,11 +73,9 @@ var
|
||||
rttiType: TRttiType;
|
||||
rttiRecordType: TRttiRecordType;
|
||||
field: TRttiField;
|
||||
rootObj: TJSONObject;
|
||||
fieldsArray: TJSONArray;
|
||||
fieldObj: TJSONObject;
|
||||
begin
|
||||
// 1. Create TRttiContext and get TRttiRecordType.
|
||||
ctx := TRttiContext.Create;
|
||||
rttiType := ctx.GetType(ATypeInfo);
|
||||
|
||||
@@ -88,32 +84,21 @@ begin
|
||||
|
||||
rttiRecordType := rttiType as TRttiRecordType;
|
||||
|
||||
// 2. Create root TJSONObject and a TJSONArray for 'fields'.
|
||||
rootObj := TJSONObject.Create;
|
||||
// The root element is now the array itself.
|
||||
fieldsArray := TJSONArray.Create;
|
||||
try
|
||||
fieldsArray := TJSONArray.Create;
|
||||
rootObj.AddPair('fields', fieldsArray);
|
||||
|
||||
// 3. Loop through all fields of the record type.
|
||||
for field in rttiRecordType.GetFields do
|
||||
begin
|
||||
// 4a. Create a TJSONObject for the field definition.
|
||||
fieldObj := TJSONObject.Create;
|
||||
|
||||
// 4b. Add field name.
|
||||
fieldObj.AddPair('name', TJSONString.Create(field.Name));
|
||||
|
||||
// 4c. Call TypeToScalarKind to get the type and add it.
|
||||
fieldObj.AddPair('kind', TJSONString.Create(GetEnumName(TypeInfo(TScalarKind), Ord(TypeToScalarKind(field.FieldType)))));
|
||||
|
||||
// 4d. Add the field object to the 'fields' array.
|
||||
fieldsArray.Add(fieldObj);
|
||||
end;
|
||||
|
||||
// 5. Convert the root JSON object to a string and set it as Result.
|
||||
Result := rootObj.ToJSON;
|
||||
// Convert the array directly to a JSON string.
|
||||
Result := fieldsArray.ToJSON;
|
||||
finally
|
||||
rootObj.Free;
|
||||
fieldsArray.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
+162
-41
@@ -161,18 +161,35 @@ type
|
||||
|
||||
TScalarMemberSeries = record
|
||||
private
|
||||
FDef: TScalarRecordDefinition;
|
||||
FArray: TChunkArray<TScalarValue>;
|
||||
FElement: Integer;
|
||||
function GetCount: Int64; inline;
|
||||
FElementIdx: Integer;
|
||||
FKind: TScalarKind;
|
||||
FElemCount: Integer;
|
||||
function GetCount: Int64;
|
||||
function GetItems(Idx: Integer): TScalar;
|
||||
public
|
||||
constructor Create(const ADef: TScalarRecordDefinition; const AArray: TChunkArray<TScalarValue>; AElement: Integer);
|
||||
function GetKind: TScalarKind; inline;
|
||||
constructor Create(AKind: TScalarKind; const AArray: TChunkArray<TScalarValue>; AElementIdx, AElemCount: Integer);
|
||||
property Count: Int64 read GetCount;
|
||||
property Element: Integer read FElement;
|
||||
property Items[Idx: Integer]: TScalar read GetItems; default;
|
||||
property Kind: TScalarKind read GetKind;
|
||||
property Kind: TScalarKind read FKind;
|
||||
end;
|
||||
|
||||
// A time series of scalar tuples, optimized for memory and access speed.
|
||||
TScalarTupleSeries = record
|
||||
private
|
||||
FDef: TArray<TScalarKind>;
|
||||
FArray: TChunkArray<TScalarValue>;
|
||||
FTotalCount: Int64;
|
||||
function GetCount: Int64;
|
||||
function GetItems(Idx: Integer): TScalarTuple;
|
||||
public
|
||||
constructor Create(const ADef: TArray<TScalarKind>);
|
||||
procedure Add(const Item: TScalarTuple; Lookback: Int64 = -1);
|
||||
function CreateMemberSeries(Idx: Integer): TScalarMemberSeries;
|
||||
property Count: Int64 read GetCount;
|
||||
property Def: TArray<TScalarKind> read FDef;
|
||||
property Items[Idx: Integer]: TScalarTuple read GetItems; default;
|
||||
property TotalCount: Int64 read FTotalCount;
|
||||
end;
|
||||
|
||||
// A time series of scalar records, optimized for memory and access speed.
|
||||
@@ -181,12 +198,15 @@ type
|
||||
FDef: TScalarRecordDefinition;
|
||||
FArray: TChunkArray<TScalarValue>;
|
||||
FTotalCount: Int64;
|
||||
function GetCount: Int64;
|
||||
function GetDef: TScalarRecordDefinition; inline;
|
||||
function GetItems(Idx: Integer): TScalarRecord;
|
||||
public
|
||||
constructor Create(const ADef: TScalarRecordDefinition);
|
||||
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||
function AsTupleSeries: TScalarTupleSeries;
|
||||
function CreateMemberSeries(const Field: String): TScalarMemberSeries;
|
||||
property Count: Int64 read GetCount;
|
||||
property Def: TScalarRecordDefinition read GetDef;
|
||||
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
|
||||
property TotalCount: Int64 read FTotalCount;
|
||||
@@ -452,74 +472,109 @@ begin
|
||||
Result := FItems;
|
||||
end;
|
||||
|
||||
{ TScalarRecordSeries }
|
||||
{ TScalarTupleSeries }
|
||||
|
||||
// Implements a time series of scalar records using a chunk array for efficient storage.
|
||||
// Each record's fields are stored sequentially in the TChunkArray.
|
||||
|
||||
constructor TScalarRecordSeries.Create(const ADef: TScalarRecordDefinition);
|
||||
constructor TScalarTupleSeries.Create(const ADef: TArray<TScalarKind>);
|
||||
begin
|
||||
Assert(Length(ADef.Fields) > 0);
|
||||
// Implements a time series of scalar tuples using a chunk array for efficient storage.
|
||||
Assert(Length(ADef) > 0, 'Tuple definition cannot be empty.');
|
||||
FDef := ADef;
|
||||
FArray := Default(TChunkArray<TScalarValue>);
|
||||
FTotalCount := 0;
|
||||
end;
|
||||
|
||||
procedure TScalarRecordSeries.Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||
procedure TScalarTupleSeries.Add(const Item: TScalarTuple; Lookback: Int64 = -1);
|
||||
var
|
||||
values: TArray<TScalarValue>;
|
||||
i: Integer;
|
||||
tupleSize: Integer;
|
||||
maxCount: Integer;
|
||||
begin
|
||||
FArray.Add(Item.Fields, Length(FDef.Fields) * Lookback);
|
||||
tupleSize := Length(FDef);
|
||||
Assert(Length(Item) = tupleSize, 'Tuple does not match series definition.');
|
||||
|
||||
// Extract TScalarValue from TScalarTuple.
|
||||
SetLength(values, tupleSize);
|
||||
for i := 0 to tupleSize - 1 do
|
||||
begin
|
||||
Assert(Item[i].Kind = FDef[i], 'Tuple element kind mismatch.');
|
||||
values[i] := Item[i].Value;
|
||||
end;
|
||||
|
||||
if Lookback < 0 then
|
||||
maxCount := -1
|
||||
else
|
||||
maxCount := tupleSize * Lookback;
|
||||
|
||||
FArray.Add(values, maxCount);
|
||||
inc(FTotalCount);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.CreateMemberSeries(const Field: String): TScalarMemberSeries;
|
||||
function TScalarTupleSeries.CreateMemberSeries(Idx: Integer): TScalarMemberSeries;
|
||||
var
|
||||
tupleSize: Integer;
|
||||
begin
|
||||
var elem := FDef.IndexOf(Field);
|
||||
if elem < 0 then
|
||||
raise EArgumentException.CreateFmt('Field "%s" not found in record definition.', [Field]);
|
||||
tupleSize := Length(FDef);
|
||||
if (Idx < 0) or (Idx >= tupleSize) then
|
||||
raise EArgumentException.CreateFmt('Index %d is out of bounds for a tuple of size %d.', [Idx, tupleSize]);
|
||||
|
||||
Result := TScalarMemberSeries.Create(FDef, FArray, elem);
|
||||
// Creates a series view for a specific member of the tuple.
|
||||
Result := TScalarMemberSeries.Create(FDef[Idx], FArray, Idx, tupleSize);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetDef: TScalarRecordDefinition;
|
||||
function TScalarTupleSeries.GetCount: Int64;
|
||||
begin
|
||||
Result := FDef;
|
||||
if Length(FDef) = 0 then
|
||||
exit(0);
|
||||
Result := FArray.Count div Length(FDef);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetItems(Idx: Integer): TScalarRecord;
|
||||
function TScalarTupleSeries.GetItems(Idx: Integer): TScalarTuple;
|
||||
var
|
||||
values: TArray<TScalarValue>;
|
||||
fieldCount: Integer;
|
||||
tupleSize, i: Integer;
|
||||
begin
|
||||
fieldCount := Length(FDef.Fields);
|
||||
SetLength(values, fieldCount);
|
||||
Move(FArray.ItemRef[(FArray.Count - fieldCount) - (Idx * fieldCount)]^, values[0], sizeof(TScalarValue) * fieldCount);
|
||||
Result.Create(FDef, values);
|
||||
tupleSize := Length(FDef);
|
||||
Assert(tupleSize > 0);
|
||||
Assert((Idx >= 0) and (Idx < (FArray.Count div tupleSize)));
|
||||
|
||||
// Create a temporary array to hold the values for one tuple.
|
||||
SetLength(values, tupleSize);
|
||||
// Copy the tuple data from the chunk array (latest item is at the end).
|
||||
Move(FArray.ItemRef[(FArray.Count - tupleSize) - (Idx * tupleSize)]^, values[0], sizeof(TScalarValue) * tupleSize);
|
||||
|
||||
// Reconstruct the TScalarTuple from the values and the definition.
|
||||
SetLength(Result, tupleSize);
|
||||
for i := 0 to tupleSize - 1 do
|
||||
Result[i] := TScalar.Create(FDef[i], values[i]);
|
||||
end;
|
||||
|
||||
constructor TScalarMemberSeries.Create(const ADef: TScalarRecordDefinition; const AArray: TChunkArray<TScalarValue>; AElement: Integer);
|
||||
{ TScalarMemberSeries }
|
||||
|
||||
constructor TScalarMemberSeries.Create(AKind: TScalarKind; const AArray: TChunkArray<TScalarValue>; AElementIdx, AElemCount: Integer);
|
||||
begin
|
||||
FDef := ADef;
|
||||
Assert(AArray.Count mod AElemCount = 0);
|
||||
Assert(AElementIdx >= 0);
|
||||
Assert(AElementIdx < AElemCount);
|
||||
|
||||
FKind := AKind;
|
||||
FArray := AArray;
|
||||
FElement := AElement;
|
||||
FElementIdx := AElementIdx;
|
||||
FElemCount := AElemCount;
|
||||
end;
|
||||
|
||||
function TScalarMemberSeries.GetCount: Int64;
|
||||
begin
|
||||
Result := FArray.Count div Length(FDef.Fields);
|
||||
if FElemCount = 0 then
|
||||
exit(0);
|
||||
Result := FArray.Count div FElemCount;
|
||||
end;
|
||||
|
||||
function TScalarMemberSeries.GetItems(Idx: Integer): TScalar;
|
||||
var
|
||||
fieldCount: Integer;
|
||||
begin
|
||||
fieldCount := Length(FDef.Fields);
|
||||
Result.Create(FDef.Fields[FElement].Kind, FArray.Items[(FArray.Count - fieldCount) - (Idx * fieldCount) + FElement]);
|
||||
Result.Create(FKind, FArray.Items[(FArray.Count - FElemCount) - (Idx * FElemCount) + FElementIdx]);
|
||||
end;
|
||||
|
||||
function TScalarMemberSeries.GetKind: TScalarKind;
|
||||
begin
|
||||
Result := FDef.Fields[FElement].Kind;
|
||||
end;
|
||||
{ TScalarRecordDefinition }
|
||||
|
||||
constructor TScalarRecordDefinition.Create(const AFields: TArray<TScalarRecordField>);
|
||||
begin
|
||||
@@ -538,6 +593,72 @@ begin
|
||||
Result := -1;
|
||||
end;
|
||||
|
||||
{ TScalarRecordSeries }
|
||||
|
||||
// Implements a time series of scalar records using a chunk array for efficient storage.
|
||||
// Each record's fields are stored sequentially in the TChunkArray.
|
||||
|
||||
constructor TScalarRecordSeries.Create(const ADef: TScalarRecordDefinition);
|
||||
begin
|
||||
Assert(Length(ADef.Fields) > 0);
|
||||
FDef := ADef;
|
||||
FTotalCount := 0;
|
||||
end;
|
||||
|
||||
procedure TScalarRecordSeries.Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||
begin
|
||||
FArray.Add(Item.Fields, Length(FDef.Fields) * Lookback);
|
||||
inc(FTotalCount);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.AsTupleSeries: TScalarTupleSeries;
|
||||
var
|
||||
tupleDef: TArray<TScalarKind>;
|
||||
i: Integer;
|
||||
begin
|
||||
// Extract the kinds from the record definition to create a tuple definition.
|
||||
SetLength(tupleDef, Length(FDef.Fields));
|
||||
for i := 0 to High(FDef.Fields) do
|
||||
tupleDef[i] := FDef.Fields[i].Kind;
|
||||
|
||||
// Create the tuple series with the new definition.
|
||||
Result := TScalarTupleSeries.Create(tupleDef);
|
||||
|
||||
// The underlying data is compatible, so we can share it.
|
||||
Result.FArray := FArray;
|
||||
Result.FTotalCount := FTotalCount;
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.CreateMemberSeries(const Field: String): TScalarMemberSeries;
|
||||
begin
|
||||
var elem := FDef.IndexOf(Field);
|
||||
if elem < 0 then
|
||||
raise EArgumentException.CreateFmt('Field "%s" not found in record definition.', [Field]);
|
||||
|
||||
Result := TScalarMemberSeries.Create(FDef.Fields[elem].Kind, FArray, elem, Length(FDef.Fields));
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetCount: Int64;
|
||||
begin
|
||||
Result := FArray.Count div Length(FDef.Fields);
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetDef: TScalarRecordDefinition;
|
||||
begin
|
||||
Result := FDef;
|
||||
end;
|
||||
|
||||
function TScalarRecordSeries.GetItems(Idx: Integer): TScalarRecord;
|
||||
var
|
||||
values: TArray<TScalarValue>;
|
||||
fieldCount: Integer;
|
||||
begin
|
||||
fieldCount := Length(FDef.Fields);
|
||||
SetLength(values, fieldCount);
|
||||
Move(FArray.ItemRef[(FArray.Count - fieldCount) - (Idx * fieldCount)]^, values[0], sizeof(TScalarValue) * fieldCount);
|
||||
Result.Create(FDef, values);
|
||||
end;
|
||||
|
||||
initialization
|
||||
Assert(sizeof(TScalarValue) = 8);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user