feat: Add Prime method to various indicators for initializing state with historical data

- Implemented Prime method in Vel, Ao, Apo, Frama, Adl, Adosc, Aobv, Cmf, Efi, Eom, Iii, Kvo, Mfi, Nvi, Obv, Pvd, Pvi, Pvo, Pvr, Pvt, Tvi, Twap, Va, Vf, Vo, Vroc, Vwad, Vwap, and Vwma classes.
- The Prime method resets the indicator state and processes the provided historical bar data to initialize the indicator.
- Added warmup period property to Adl and Wad classes to define the minimum number of data points required for validity.
- Updated benchmark tests to use Batch methods for performance evaluation.
This commit is contained in:
Miha Kralj
2026-02-11 20:38:38 -08:00
parent 75c6a9f135
commit 653aafacd8
71 changed files with 10527 additions and 242 deletions
+18
View File
@@ -175,6 +175,24 @@ public sealed class Bop : ITValuePublisher
return new TSeries(t, v);
}
/// <summary>
/// Initializes the indicator state using the provided bar series history.
/// </summary>
/// <param name="source">Historical bar data.</param>
public void Prime(TBarSeries source)
{
Reset();
if (source.Count == 0)
{
return;
}
for (int i = 0; i < source.Count; i++)
{
Update(source[i], isNew: true);
}
}
public static (TSeries Results, Bop Indicator) Calculate(TBarSeries source)
{
var indicator = new Bop();
-10
View File
@@ -68,16 +68,6 @@ public sealed class Cci : ITValuePublisher
/// </summary>
public int WarmupPeriod => _period;
/// <summary>
/// Returns the default warmup period (<see cref="DefaultPeriod"/>).
/// </summary>
/// <remarks>
/// This static accessor is provided for backward compatibility. Prefer the instance
/// <see cref="WarmupPeriod"/> property which returns the actual configured period.
/// </remarks>
[Obsolete("Use the instance WarmupPeriod property instead. This static accessor returns the default period (20) and will be removed in a future major version.")]
public static int DefaultWarmupPeriod => DefaultPeriod;
/// <summary>
/// Creates a CCI indicator with specified period.
/// </summary>
+20 -1
View File
@@ -299,6 +299,25 @@ public sealed class Cfb : ITValuePublisher, IDisposable
return new TSeries(t, v);
}
/// <summary>
/// Initializes the indicator state using the provided value series history.
/// </summary>
/// <param name="source">Historical input data.</param>
public void Prime(TSeries source)
{
Reset();
if (source.Count == 0)
{
return;
}
for (int i = 0; i < source.Count; i++)
{
Update(source[i], isNew: true);
}
}
public static TSeries Batch(TSeries source, int[]? lengths = null)
{
var cfb = new Cfb(lengths);
@@ -440,4 +459,4 @@ public sealed class Cfb : ITValuePublisher, IDisposable
TSeries results = indicator.Update(source);
return (results, indicator);
}
}
}
+20 -1
View File
@@ -129,6 +129,25 @@ public sealed class Macd : ITValuePublisher, IDisposable
return new TSeries(t, v);
}
/// <summary>
/// Initializes the indicator state using the provided series history.
/// </summary>
/// <param name="source">Historical data.</param>
public void Prime(TSeries source)
{
Reset();
if (source.Count == 0)
{
return;
}
for (int i = 0; i < source.Count; i++)
{
Update(new TValue(new DateTime(source.Times[i], DateTimeKind.Utc), source.Values[i]), isNew: true);
}
}
public static TSeries Batch(TSeries source, int fastPeriod = 12, int slowPeriod = 26, int signalPeriod = 9)
{
var indicator = new Macd(fastPeriod, slowPeriod, signalPeriod);
@@ -178,4 +197,4 @@ public sealed class Macd : ITValuePublisher, IDisposable
TSeries results = indicator.Update(source);
return (results, indicator);
}
}
}
+19
View File
@@ -214,6 +214,25 @@ public sealed class Rsx : ITValuePublisher
return new TSeries(t, v);
}
/// <summary>
/// Initializes the indicator state using the provided series history.
/// </summary>
/// <param name="source">Historical data.</param>
public void Prime(TSeries source)
{
Reset();
if (source.Count == 0)
{
return;
}
for (int i = 0; i < source.Count; i++)
{
Update(new TValue(new DateTime(source.Times[i], DateTimeKind.Utc), source.Values[i]), isNew: true);
}
}
public static TSeries Batch(TSeries source, int period)
{
var rsx = new Rsx(period);
+19
View File
@@ -119,6 +119,25 @@ public sealed class Vel : ITValuePublisher, IDisposable
return new TSeries(t, v);
}
/// <summary>
/// Initializes the indicator state using the provided series history.
/// </summary>
/// <param name="source">Historical data.</param>
public void Prime(TSeries source)
{
Reset();
if (source.Count == 0)
{
return;
}
for (int i = 0; i < source.Count; i++)
{
Update(new TValue(new DateTime(source.Times[i], DateTimeKind.Utc), source.Values[i]), isNew: true);
}
}
public static TSeries Batch(TSeries source, int period)
{
int len = source.Count;