Generic indicator factory
This commit is contained in:
+11
-11
@@ -312,13 +312,13 @@ begin
|
|||||||
|
|
||||||
var Closes := Ohlc.Field<Double>('Close');
|
var Closes := Ohlc.Field<Double>('Close');
|
||||||
|
|
||||||
var Hull := Closes.Chain<Double>(TIndicators.CreateHMA(250)).MakeParallel;
|
var Hull := Closes.Chain<Double>(THMA.CreateHMA(250)).MakeParallel;
|
||||||
var Sma := Closes.Chain<Double>(TIndicators.CreateSMA(200)).MakeParallel;
|
var Sma := Closes.Chain<Double>(TSMA.CreateSMA(200)).MakeParallel;
|
||||||
|
|
||||||
var Lowest: Double := Double.MaxValue;
|
var Lowest: Double := Double.MaxValue;
|
||||||
var Highest: Double := Double.MinValue;
|
var Highest: Double := Double.MinValue;
|
||||||
|
|
||||||
var ATR := Ohlc.Chain<Double>(TIndicators.CreateATR(50)).MakeParallel;
|
var ATR := Ohlc.Chain<Double>(TATR.CreateATR(50)).MakeParallel;
|
||||||
|
|
||||||
// next stage
|
// next stage
|
||||||
|
|
||||||
@@ -471,7 +471,7 @@ begin
|
|||||||
panel.AddDoubleSeries(Signal.Producer.Field<Double>('Entry'), TAlphaColors.Green, 1);
|
panel.AddDoubleSeries(Signal.Producer.Field<Double>('Entry'), TAlphaColors.Green, 1);
|
||||||
panel.AddDoubleSeries(Signal.Producer.Field<Double>('SL'), TAlphaColors.Red, 2);
|
panel.AddDoubleSeries(Signal.Producer.Field<Double>('SL'), TAlphaColors.Red, 2);
|
||||||
|
|
||||||
var mean := TConverter<TArray<Double>, Double>.CreateConverter(TIndicators.CreateMean());
|
var mean := TConverter<TArray<Double>, Double>.CreateConverter(TMean.CreateMean());
|
||||||
|
|
||||||
TConverter.Join<Double>([Hull, Sma]).Chain<Double>(mean);
|
TConverter.Join<Double>([Hull, Sma]).Chain<Double>(mean);
|
||||||
|
|
||||||
@@ -616,13 +616,13 @@ begin
|
|||||||
var Ohlc := OhlcPoint.Field<TOhlcItem>('Data');
|
var Ohlc := OhlcPoint.Field<TOhlcItem>('Data');
|
||||||
var Closes := Ohlc.Field<Double>('Close');
|
var Closes := Ohlc.Field<Double>('Close');
|
||||||
|
|
||||||
var Hull := Closes.Chain<Double>(TIndicators.CreateHMA(150));
|
var Hull := Closes.Chain<Double>(THMA.CreateHMA(150));
|
||||||
var Sma := Closes.Chain<Double>(TIndicators.CreateSMA(50));
|
var Sma := Closes.Chain<Double>(TSMA.CreateSMA(50));
|
||||||
var Ema := Closes.Chain<Double>(TIndicators.CreateEMA(21));
|
var Ema := Closes.Chain<Double>(TEMA.CreateEMA(21));
|
||||||
var Boli := Closes.MakeParallel.Chain<TBollingerBandsResult>(TIndicators.CreateBollingerBands(20, 2.0));
|
var Boli := Closes.MakeParallel.Chain<TBollingerBands.TResult>(TBollingerBands.CreateBollingerBands(20, 2.0));
|
||||||
var Rsi := Closes.Chain<Double>(TIndicators.CreateRSI(14));
|
var Rsi := Closes.Chain<Double>(TRSI.CreateRSI(14));
|
||||||
var Macd := Closes.MakeParallel.Chain<TMacdResult>(TIndicators.CreateMACD(12, 26, 9));
|
var Macd := Closes.MakeParallel.Chain<TMacd.TResult>(TMACD.CreateMACD(12, 26, 9));
|
||||||
var Stoch := Ohlc.Chain<TStochasticResult>(TIndicators.CreateStochastic(14, 3));
|
var Stoch := Ohlc.Chain<TStochastic.TResult>(TStochastic.CreateStochastic(14, 3));
|
||||||
|
|
||||||
chart.SetXAxisSeries(timeframe, Timestamps);
|
chart.SetXAxisSeries(timeframe, Timestamps);
|
||||||
|
|
||||||
|
|||||||
@@ -35,9 +35,9 @@ begin
|
|||||||
|
|
||||||
var Closes := Ohlc.Field<Double>('Close');
|
var Closes := Ohlc.Field<Double>('Close');
|
||||||
|
|
||||||
var Hull := Closes.Chain<Double>(TIndicators.CreateHMA(250)).MakeParallel;
|
var Hull := Closes.Chain<Double>(THMA.CreateHMA(250)).MakeParallel;
|
||||||
var Sma := Closes.Chain<Double>(TIndicators.CreateSMA(200)).MakeParallel;
|
var Sma := Closes.Chain<Double>(TSMA.CreateSMA(200)).MakeParallel;
|
||||||
var ATR := Ohlc.Chain<Double>(TIndicators.CreateATR(50)).MakeParallel;
|
var ATR := Ohlc.Chain<Double>(TATR.CreateATR(50)).MakeParallel;
|
||||||
|
|
||||||
var conv := TConverter.Join<Double>([Ohlc.Field<Double>('Low'), Ohlc.Field<Double>('High'), Closes, ATR, Hull, Sma]);
|
var conv := TConverter.Join<Double>([Ohlc.Field<Double>('Low'), Ohlc.Field<Double>('High'), Closes, ATR, Hull, Sma]);
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ uses
|
|||||||
System.SysUtils,
|
System.SysUtils,
|
||||||
System.Classes,
|
System.Classes,
|
||||||
Myc.Data.Records,
|
Myc.Data.Records,
|
||||||
|
Myc.Data.Pipeline,
|
||||||
Myc.Trade.Indicators;
|
Myc.Trade.Indicators;
|
||||||
|
|
||||||
type
|
type
|
||||||
@@ -61,7 +62,7 @@ uses
|
|||||||
class function TMyWorker.CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>;
|
class function TMyWorker.CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>;
|
||||||
begin
|
begin
|
||||||
Result :=
|
Result :=
|
||||||
function(const Params: TParams): TIndicatorProc<TArgs, TResult>
|
function(const Params: TParams): TConvertFunc<TArgs, TResult>
|
||||||
begin
|
begin
|
||||||
var Log := TStrings(Params.Log);
|
var Log := TStrings(Params.Log);
|
||||||
var text := Params.text;
|
var text := Params.text;
|
||||||
@@ -101,7 +102,7 @@ end;
|
|||||||
class function TSmaIndicator.CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>;
|
class function TSmaIndicator.CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>;
|
||||||
begin
|
begin
|
||||||
Result :=
|
Result :=
|
||||||
function(const Params: TParams): TIndicatorProc<TArgs, TResult>
|
function(const Params: TParams): TConvertFunc<TArgs, TResult>
|
||||||
var
|
var
|
||||||
// State for the indicator closure
|
// State for the indicator closure
|
||||||
period: Integer;
|
period: Integer;
|
||||||
@@ -150,7 +151,7 @@ procedure TestSma(const Log: TStrings);
|
|||||||
var
|
var
|
||||||
fact: TGenericIndicatorFactory;
|
fact: TGenericIndicatorFactory;
|
||||||
params: TDataRecord;
|
params: TDataRecord;
|
||||||
indi: TIndicatorProc<TDataRecord, TDataRecord>;
|
indi: TConvertFunc<TDataRecord, TDataRecord>;
|
||||||
args: TDataRecord;
|
args: TDataRecord;
|
||||||
res: TDataRecord;
|
res: TDataRecord;
|
||||||
smaValue: Double;
|
smaValue: Double;
|
||||||
|
|||||||
@@ -136,15 +136,8 @@ end;
|
|||||||
|
|
||||||
procedure TDataRecord.CopyFrom<T>(const Src: T);
|
procedure TDataRecord.CopyFrom<T>(const Src: T);
|
||||||
begin
|
begin
|
||||||
var ctx := TRttiContext.Create;
|
|
||||||
var rttiType := ctx.GetType(TypeInfo(T));
|
|
||||||
|
|
||||||
for var i := 0 to High(FLayout.FFields) do
|
for var i := 0 to High(FLayout.FFields) do
|
||||||
begin
|
|
||||||
var P: PByte := @Src;
|
|
||||||
inc(P, FLayout.Fields[i].Offset);
|
|
||||||
FLayout.Fields[i].FromType(FBuffer, Src);
|
FLayout.Fields[i].FromType(FBuffer, Src);
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TDataRecord.CopyValue(const SrcRec: TDataRecord; SrcIdx, DstIdx: Integer);
|
procedure TDataRecord.CopyValue(const SrcRec: TDataRecord; SrcIdx, DstIdx: Integer);
|
||||||
|
|||||||
+800
-244
File diff suppressed because it is too large
Load Diff
@@ -3,13 +3,13 @@ unit Myc.Trade.Indicators;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
|
Myc.Data.Pipeline,
|
||||||
Myc.Data.Records;
|
Myc.Data.Records;
|
||||||
|
|
||||||
{$M+}
|
{$M+}
|
||||||
|
|
||||||
type
|
type
|
||||||
TIndicatorProc<TValue, TResult> = reference to function(const Value: TValue): TResult;
|
TIndicatorFactoryProc<TParams, TValue, TResult> = reference to function(const Params: TParams): TConvertFunc<TValue, TResult>;
|
||||||
TIndicatorFactoryProc<TParams, TValue, TResult> = reference to function(const Params: TParams): TIndicatorProc<TValue, TResult>;
|
|
||||||
|
|
||||||
(*
|
(*
|
||||||
Sample definition of an indicator template:
|
Sample definition of an indicator template:
|
||||||
@@ -33,6 +33,9 @@ type
|
|||||||
|
|
||||||
[IndicatorFactory]
|
[IndicatorFactory]
|
||||||
class function CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>; static;
|
class function CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>; static;
|
||||||
|
|
||||||
|
// Hard coded version:
|
||||||
|
class function CreateHMA( Period: Integer ): TConvertFunc<Double, Double>; static;
|
||||||
end;
|
end;
|
||||||
*)
|
*)
|
||||||
|
|
||||||
@@ -66,7 +69,7 @@ type
|
|||||||
function GetResultLayout: TDataRecord.TLayout;
|
function GetResultLayout: TDataRecord.TLayout;
|
||||||
{$endregion}
|
{$endregion}
|
||||||
|
|
||||||
function CreateIndicator(const Params: TDataRecord): TIndicatorProc<TDataRecord, TDataRecord>;
|
function CreateIndicator(const Params: TDataRecord): TConvertFunc<TDataRecord, TDataRecord>;
|
||||||
|
|
||||||
property ParameterLayout: TDataRecord.TLayout read GetParameterLayout;
|
property ParameterLayout: TDataRecord.TLayout read GetParameterLayout;
|
||||||
property ArgumentLayout: TDataRecord.TLayout read GetArgumentLayout;
|
property ArgumentLayout: TDataRecord.TLayout read GetArgumentLayout;
|
||||||
@@ -94,7 +97,7 @@ type
|
|||||||
|
|
||||||
class function CreateFromTemplate<T>: TGenericIndicatorFactory;
|
class function CreateFromTemplate<T>: TGenericIndicatorFactory;
|
||||||
|
|
||||||
function CreateIndicator(const Params: TDataRecord): TIndicatorProc<TDataRecord, TDataRecord>;
|
function CreateIndicator(const Params: TDataRecord): TConvertFunc<TDataRecord, TDataRecord>;
|
||||||
|
|
||||||
property ParameterLayout: TDataRecord.TLayout read GetParameterLayout;
|
property ParameterLayout: TDataRecord.TLayout read GetParameterLayout;
|
||||||
property ArgumentLayout: TDataRecord.TLayout read GetArgumentLayout;
|
property ArgumentLayout: TDataRecord.TLayout read GetArgumentLayout;
|
||||||
@@ -297,7 +300,7 @@ begin
|
|||||||
// Create the main factory procedure. This is a double-nested anonymous method
|
// Create the main factory procedure. This is a double-nested anonymous method
|
||||||
// that wraps the template's specific factory and worker functions.
|
// that wraps the template's specific factory and worker functions.
|
||||||
factoryProc :=
|
factoryProc :=
|
||||||
function(const Params: TDataRecord): TIndicatorProc<TDataRecord, TDataRecord>
|
function(const Params: TDataRecord): TConvertFunc<TDataRecord, TDataRecord>
|
||||||
begin
|
begin
|
||||||
// Outer anonymous method: This is the factory proc.
|
// Outer anonymous method: This is the factory proc.
|
||||||
// It gets called with a TDataRecord of parameters.
|
// It gets called with a TDataRecord of parameters.
|
||||||
@@ -364,7 +367,7 @@ begin
|
|||||||
Result := TGenericIndicatorFactory.Create(parameterLayout, argumentLayout, resultLayout, factoryProc, shortName, name, hint);
|
Result := TGenericIndicatorFactory.Create(parameterLayout, argumentLayout, resultLayout, factoryProc, shortName, name, hint);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TGenericIndicatorFactory.CreateIndicator(const Params: TDataRecord): TIndicatorProc<TDataRecord, TDataRecord>;
|
function TGenericIndicatorFactory.CreateIndicator(const Params: TDataRecord): TConvertFunc<TDataRecord, TDataRecord>;
|
||||||
begin
|
begin
|
||||||
Assert(
|
Assert(
|
||||||
(not Assigned(FParameterLayout.Fields)) or (Params.Layout = FParameterLayout),
|
(not Assigned(FParameterLayout.Fields)) or (Params.Layout = FParameterLayout),
|
||||||
|
|||||||
Reference in New Issue
Block a user