Sample SMA strategy
This commit is contained in:
@@ -130,6 +130,9 @@ type
|
||||
function VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||
function VisitIndexer(const Node: IIndexerNode): TAstValue;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
|
||||
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
|
||||
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
|
||||
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
|
||||
end;
|
||||
|
||||
TAuraWorkspace = class(TStyledControl)
|
||||
@@ -204,6 +207,9 @@ type
|
||||
function VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||
function VisitIndexer(const Node: IIndexerNode): TAstValue;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
|
||||
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
|
||||
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
|
||||
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
|
||||
end;
|
||||
|
||||
constructor TAstToTextVisitor.Create;
|
||||
@@ -213,6 +219,18 @@ end;
|
||||
|
||||
{ TAstToTextVisitor }
|
||||
|
||||
function TAstToTextVisitor.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
|
||||
var
|
||||
seriesStr, valueStr, lookbackStr: string;
|
||||
begin
|
||||
seriesStr := Node.Series.Accept(Self).AsText;
|
||||
valueStr := Node.Value.Accept(Self).AsText;
|
||||
lookbackStr := '';
|
||||
if Assigned(Node.Lookback) then
|
||||
lookbackStr := ', ' + Node.Lookback.Accept(Self).AsText;
|
||||
Result := TAstValue.FromText(Format('%s.add(%s%s)', [seriesStr, valueStr, lookbackStr]));
|
||||
end;
|
||||
|
||||
function TAstToTextVisitor.VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||
begin
|
||||
Result := TAstValue.FromText(Node.Identifier.Name + ' := ' + Node.Value.Accept(Self).AsText);
|
||||
@@ -235,6 +253,11 @@ begin
|
||||
Result := TAstValue.FromText(Node.Value.ToString);
|
||||
end;
|
||||
|
||||
function TAstToTextVisitor.VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
|
||||
begin
|
||||
Result := TAstValue.FromText('new series(' + Node.Definition + ')');
|
||||
end;
|
||||
|
||||
function TAstToTextVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TAstValue;
|
||||
var
|
||||
i: Integer;
|
||||
@@ -312,6 +335,15 @@ begin
|
||||
Result := TAstValue.FromText(Format('%s.%s', [baseStr, Node.Member.Name]));
|
||||
end;
|
||||
|
||||
function TAstToTextVisitor.VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
|
||||
var
|
||||
seriesStr: string;
|
||||
begin
|
||||
// Get the string representation of the series identifier by visiting the node.
|
||||
seriesStr := Node.Series.Accept(Self).AsText;
|
||||
Result := TAstValue.FromText(Format('length(%s)', [seriesStr]));
|
||||
end;
|
||||
|
||||
function TAstToTextVisitor.VisitTernaryExpression(const Node: ITernaryExpressionNode): TAstValue;
|
||||
begin
|
||||
var condStr := Node.Condition.Accept(Self).AsText;
|
||||
@@ -573,6 +605,62 @@ begin
|
||||
Result := Pos('{', Text) = 0;
|
||||
end;
|
||||
|
||||
function TAstToAuraNodeVisitor.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
|
||||
var
|
||||
details: string;
|
||||
inputs: TArray<IExpressionNode>;
|
||||
begin
|
||||
if (FMode = vmControlFlow) and TryGetDescr(Node, details) then
|
||||
begin
|
||||
var addNode := CreateNodeControl('Add to Series', details);
|
||||
CreateEntry(addNode);
|
||||
CreateExit(addNode, '');
|
||||
FinalizeNodeLayout(addNode);
|
||||
end
|
||||
else
|
||||
begin
|
||||
if Assigned(Node.Lookback) then
|
||||
inputs := [Node.Series, Node.Value, Node.Lookback]
|
||||
else
|
||||
inputs := [Node.Series, Node.Value];
|
||||
|
||||
VisitOperatorNode(
|
||||
inputs,
|
||||
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
|
||||
var
|
||||
addNode: TAuraNode;
|
||||
seriesPin, valuePin, lookbackPin: TControl;
|
||||
begin
|
||||
addNode := BuildNodeControl('Add to Series', '');
|
||||
CreateEntry(addNode);
|
||||
|
||||
seriesPin := CreateInput(addNode, 'Series');
|
||||
valuePin := CreateInput(addNode, 'Value');
|
||||
|
||||
if Assigned(InputResults[0].OutputPin) then
|
||||
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, seriesPin));
|
||||
if Assigned(InputResults[1].OutputPin) then
|
||||
FConnections.Add(TPinConnection.Create(InputResults[1].OutputPin, valuePin));
|
||||
|
||||
if InputResults.Count > 2 then
|
||||
begin
|
||||
lookbackPin := CreateInput(addNode, 'Lookback');
|
||||
if Assigned(InputResults[2].OutputPin) then
|
||||
FConnections.Add(TPinConnection.Create(InputResults[2].OutputPin, lookbackPin));
|
||||
end;
|
||||
|
||||
CreateExit(addNode, '');
|
||||
|
||||
FinalizeNodeLayout(addNode);
|
||||
Result.LayoutNode := addNode;
|
||||
Result.OutputPin := nil; // No data output
|
||||
end,
|
||||
vaTop
|
||||
);
|
||||
end;
|
||||
Result := TAstValue.Void;
|
||||
end;
|
||||
|
||||
function TAstToAuraNodeVisitor.VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||
begin
|
||||
var details: String;
|
||||
@@ -824,6 +912,18 @@ begin
|
||||
FinalizeNodeLayout(containerNode);
|
||||
end;
|
||||
|
||||
function TAstToAuraNodeVisitor.VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
|
||||
var
|
||||
seriesNode: TAuraNode;
|
||||
begin
|
||||
// This node creates a new series. It has no inputs, only a data output.
|
||||
// The definition is a string literal, so it's part of the node's title.
|
||||
seriesNode := CreateNodeControl('Create Series', Node.Definition);
|
||||
FLastResult.OutputPin := CreateOutput(seriesNode);
|
||||
FinalizeNodeLayout(seriesNode);
|
||||
Result := TAstValue.Void;
|
||||
end;
|
||||
|
||||
function TAstToAuraNodeVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TAstValue;
|
||||
var
|
||||
inputExpressions: TArray<IExpressionNode>;
|
||||
@@ -951,16 +1051,16 @@ begin
|
||||
|
||||
// Else branch (if it exists)
|
||||
elseEndY := thenEndY;
|
||||
FCurrentExec.Clear;
|
||||
CreateExit(ifNode, 'Else');
|
||||
if Assigned(Node.ElseBranch) then
|
||||
begin
|
||||
FCurrentExec.Clear;
|
||||
CreateExit(ifNode, 'Else');
|
||||
FCurrentPos.X := branchStartX;
|
||||
FCurrentPos.Y := thenEndY;
|
||||
Node.ElseBranch.Accept(Self);
|
||||
elseEndY := FCurrentPos.Y;
|
||||
execPathes.AddRange(FCurrentExec);
|
||||
end;
|
||||
execPathes.AddRange(FCurrentExec);
|
||||
|
||||
// Finalize layout now that all pins are added.
|
||||
FinalizeNodeLayout(ifNode);
|
||||
@@ -1157,6 +1257,38 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TAstToAuraNodeVisitor.VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
|
||||
begin
|
||||
// Use the standard helper for operator-style nodes.
|
||||
VisitOperatorNode(
|
||||
[Node.Series], // The single input for this operator is the series identifier.
|
||||
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
|
||||
var
|
||||
lengthNode: TAuraNode;
|
||||
seriesPin, outPin: TControl;
|
||||
begin
|
||||
// Create the visual node for the 'length' operation.
|
||||
lengthNode := BuildNodeControl('Series Length', '');
|
||||
|
||||
// Create an input pin for the series and an output pin for the result.
|
||||
seriesPin := CreateInput(lengthNode, 'Series');
|
||||
outPin := CreateOutput(lengthNode);
|
||||
|
||||
// Connect the output of the visited series identifier to our input pin.
|
||||
if Assigned(InputResults[0].OutputPin) then
|
||||
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, seriesPin));
|
||||
|
||||
// Finalize the node's layout to correctly position the pins.
|
||||
FinalizeNodeLayout(lengthNode);
|
||||
|
||||
// The result of this visitation is the new node and its output pin.
|
||||
Result.LayoutNode := lengthNode;
|
||||
Result.OutputPin := outPin;
|
||||
end
|
||||
);
|
||||
Result := TAstValue.Void;
|
||||
end;
|
||||
|
||||
function TAstToAuraNodeVisitor.VisitTernaryExpression(const Node: ITernaryExpressionNode): TAstValue;
|
||||
begin
|
||||
var exprStr: String;
|
||||
|
||||
Reference in New Issue
Block a user