Generic indicator factory

This commit is contained in:
Michael Schimmel
2025-07-27 21:58:15 +02:00
parent 7842c3bd87
commit 78e89f345e
4 changed files with 85 additions and 51 deletions
+44 -37
View File
@@ -148,7 +148,9 @@ type
DPeriod: Integer;
end;
TArgs = record
Value: TOhlcItem;
High: Double;
Low: Double;
Close: Double;
end;
TResult = record
K: Double; // %K line
@@ -156,12 +158,12 @@ type
end;
[IndicatorFactory]
class function CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>; static;
class function CreateStochastic(KPeriod, DPeriod: Integer): TConvertFunc<TOhlcItem, TResult>; overload; static;
// Creates a Stochastic Oscillator using an injectable moving average for the %D line.
class function CreateStochastic(KPeriod, DPeriod: Integer): TConvertFunc<TArgs, TResult>; overload; static;
class function CreateStochastic(
KPeriod: Integer;
const SmaD: TConvertFunc<Double, Double>
): TConvertFunc<TOhlcItem, TStochastic.TResult>; overload; static;
): TConvertFunc<TArgs, TStochastic.TResult>; overload; static;
end;
[IndicatorName('StdDev', 'Standard Deviation')]
@@ -214,15 +216,17 @@ type
Period: Integer;
end;
TArgs = record
Value: TOhlcItem;
High: Double;
Low: Double;
Close: Double;
end;
TResult = record
ATR: Double;
end;
[IndicatorFactory]
class function CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>; static;
class function CreateATR(Period: Integer): TConvertFunc<TOhlcItem, Double>; overload; static;
class function CreateATR(const MovAvgTR: TConvertFunc<Double, Double>): TConvertFunc<TOhlcItem, Double>; overload; static;
class function CreateATR(Period: Integer): TConvertFunc<TArgs, Double>; overload; static;
class function CreateATR(const MovAvgTR: TConvertFunc<Double, Double>): TConvertFunc<TArgs, Double>; overload; static;
end;
[IndicatorName('KC', 'Keltner Channels')]
@@ -235,7 +239,9 @@ type
Multiplier: Double;
end;
TArgs = record
Value: TOhlcItem;
High: Double;
Low: Double;
Close: Double;
end;
TResult = record
UpperBand: Double;
@@ -244,12 +250,12 @@ type
end;
[IndicatorFactory]
class function CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>; static;
class function CreateKeltnerChannels(Period: Integer; Multiplier: Double): TConvertFunc<TOhlcItem, TResult>; overload; static;
class function CreateKeltnerChannels(Period: Integer; Multiplier: Double): TConvertFunc<TArgs, TResult>; overload; static;
class function CreateKeltnerChannels(
const MovAvgMiddle: TConvertFunc<Double, Double>;
const AtrFunc: TConvertFunc<TOhlcItem, Double>;
const AtrFunc: TConvertFunc<TATR.TArgs, Double>;
Multiplier: Double
): TConvertFunc<TOhlcItem, TKeltnerChannels.TResult>; overload; static;
): TConvertFunc<TArgs, TKeltnerChannels.TResult>; overload; static;
end;
[IndicatorName('Mean', 'Mean Value')]
@@ -708,25 +714,22 @@ begin
Result :=
function(const Params: TParams): TConvertFunc<TArgs, TResult>
var
stochFunc: TConvertFunc<TOhlcItem, TResult>;
stochFunc: TConvertFunc<TArgs, TResult>;
begin
stochFunc := CreateStochastic(Params.KPeriod, Params.DPeriod);
Result := function(const Value: TArgs): TResult begin Result := stochFunc(Value.Value); end;
Result := function(const Value: TArgs): TResult begin Result := stochFunc(Value); end;
end;
end;
class function TStochastic.CreateStochastic(KPeriod, DPeriod: Integer): TConvertFunc<TOhlcItem, TStochastic.TResult>;
class function TStochastic.CreateStochastic(KPeriod, DPeriod: Integer): TConvertFunc<TArgs, TResult>;
begin
Result := CreateStochastic(KPeriod, TSMA.CreateSMA(DPeriod));
end;
// Creates a Stochastic Oscillator using an injectable moving average for the %D line.
class function TStochastic.CreateStochastic(
KPeriod: Integer;
const SmaD: TConvertFunc<Double, Double>
): TConvertFunc<TOhlcItem, TStochastic.TResult>;
class function TStochastic.CreateStochastic(KPeriod: Integer; const SmaD: TConvertFunc<Double, Double>): TConvertFunc<TArgs, TResult>;
var
buffer: TArray<TOhlcItem>;
buffer: TArray<TArgs>;
highDeque: TLightDeque;
lowDeque: TLightDeque;
valueCount: Int64;
@@ -734,7 +737,7 @@ begin
if (KPeriod <= 0) then
begin
Result :=
function(const Value: TOhlcItem): TResult
function(const Value: TArgs): TResult
begin
Result.K := Double.NaN;
Result.D := Double.NaN;
@@ -748,7 +751,7 @@ begin
valueCount := 0;
Result :=
function(const Value: TOhlcItem): TStochastic.TResult
function(const Value: TArgs): TStochastic.TResult
var
currentIndex, firstIndex, lastIndex: Integer;
highestHigh, lowestLow: Double;
@@ -926,25 +929,25 @@ begin
Result :=
function(const Params: TParams): TConvertFunc<TArgs, TResult>
var
atrFunc: TConvertFunc<TOhlcItem, Double>;
atrFunc: TConvertFunc<TArgs, Double>;
begin
atrFunc := CreateATR(Params.Period);
Result := function(const Value: TArgs): TResult begin Result.ATR := atrFunc(Value.Value); end;
Result := function(const Value: TArgs): TResult begin Result.ATR := atrFunc(Value); end;
end;
end;
class function TATR.CreateATR(Period: Integer): TConvertFunc<TOhlcItem, Double>;
class function TATR.CreateATR(Period: Integer): TConvertFunc<TArgs, Double>;
begin
Result := CreateATR(TEMA.CreateEMA(Period));
end;
// Calculates the Average True Range (ATR) using an injectable moving average.
class function TATR.CreateATR(const MovAvgTR: TConvertFunc<Double, Double>): TConvertFunc<TOhlcItem, Double>;
class function TATR.CreateATR(const MovAvgTR: TConvertFunc<Double, Double>): TConvertFunc<TArgs, Double>;
begin
var sourceData: TSeries<TOhlcItem>;
var sourceData: TSeries<TArgs>;
Result :=
function(const Value: TOhlcItem): Double
function(const Value: TArgs): Double
var
tr: Double;
begin
@@ -968,20 +971,19 @@ end;
{ TKeltnerChannels }
class function TKeltnerChannels.CreateFactory:
TIndicatorFactoryProc<TKeltnerChannels.TParams, TKeltnerChannels.TArgs, TKeltnerChannels.TResult>;
class function TKeltnerChannels.CreateFactory: TIndicatorFactoryProc<TParams, TArgs, TResult>;
begin
Result :=
function(const Params: TParams): TConvertFunc<TArgs, TResult>
var
kcFunc: TConvertFunc<TOhlcItem, TResult>;
kcFunc: TConvertFunc<TArgs, TResult>;
begin
kcFunc := CreateKeltnerChannels(Params.Period, Params.Multiplier);
Result := function(const Value: TArgs): TResult begin Result := kcFunc(Value.Value); end;
Result := function(const Value: TArgs): TResult begin Result := kcFunc(Value); end;
end;
end;
class function TKeltnerChannels.CreateKeltnerChannels(Period: Integer; Multiplier: Double): TConvertFunc<TOhlcItem, TResult>;
class function TKeltnerChannels.CreateKeltnerChannels(Period: Integer; Multiplier: Double): TConvertFunc<TArgs, TResult>;
begin
Result := CreateKeltnerChannels(TEMA.CreateEMA(Period), TATR.CreateATR(Period), Multiplier);
end;
@@ -989,12 +991,12 @@ end;
// Calculates Keltner Channels using an injectable ATR and middle band moving average.
class function TKeltnerChannels.CreateKeltnerChannels(
const MovAvgMiddle: TConvertFunc<Double, Double>;
const AtrFunc: TConvertFunc<TOhlcItem, Double>;
const AtrFunc: TConvertFunc<TATR.TArgs, Double>;
Multiplier: Double
): TConvertFunc<TOhlcItem, TKeltnerChannels.TResult>;
): TConvertFunc<TArgs, TKeltnerChannels.TResult>;
begin
Result :=
function(const Value: TOhlcItem): TKeltnerChannels.TResult
function(const Value: TArgs): TKeltnerChannels.TResult
var
atrValue, middleValue, typicalPrice: Double;
begin
@@ -1003,7 +1005,12 @@ begin
// Get values from the provided indicator functions.
middleValue := MovAvgMiddle(typicalPrice);
atrValue := AtrFunc(Value);
var atrArgs: TATR.TArgs;
atrArgs.High := Value.High;
atrArgs.Low := Value.Low;
atrArgs.Close := Value.Close;
atrValue := AtrFunc(atrArgs);
// Set default NaN values for the warm-up period.
Result.MiddleBand := middleValue;
@@ -1064,6 +1071,6 @@ initialization
IndicatorRegistry.RegisterTemplate<TBollingerBands>;
IndicatorRegistry.RegisterTemplate<TATR>;
IndicatorRegistry.RegisterTemplate<TKeltnerChannels>;
IndicatorRegistry.RegisterTemplate<TMean>;
// IndicatorRegistry.RegisterTemplate<TMean>;
end.