normalization of methods

This commit is contained in:
Miha Kralj
2026-02-10 21:33:16 -08:00
parent 915d7a007b
commit 6d6259a47d
527 changed files with 10525 additions and 2123 deletions
+6 -6
View File
@@ -215,7 +215,7 @@ public class MidpointTests
}
// Batch
var batch = Midpoint.Calculate(source, period);
var batch = Midpoint.Batch(source, period);
// Compare last values (after warmup)
for (int i = period; i < source.Count; i++)
@@ -234,12 +234,12 @@ public class MidpointTests
var source = bars.Close;
// TSeries batch
var batchResult = Midpoint.Calculate(source, period);
var batchResult = Midpoint.Batch(source, period);
// Span calculation
var sourceArray = source.Values.ToArray();
var output = new double[count];
Midpoint.Calculate(sourceArray.AsSpan(), output.AsSpan(), period);
Midpoint.Batch(sourceArray.AsSpan(), output.AsSpan(), period);
for (int i = 0; i < source.Count; i++)
{
@@ -253,21 +253,21 @@ public class MidpointTests
Assert.Throws<ArgumentException>(() =>
{
Span<double> output = stackalloc double[10];
Midpoint.Calculate(ReadOnlySpan<double>.Empty, output, 5);
Midpoint.Batch(ReadOnlySpan<double>.Empty, output, 5);
});
Assert.Throws<ArgumentException>(() =>
{
ReadOnlySpan<double> source = stackalloc double[10];
Span<double> output = stackalloc double[5];
Midpoint.Calculate(source, output, 5);
Midpoint.Batch(source, output, 5);
});
Assert.Throws<ArgumentException>(() =>
{
ReadOnlySpan<double> source = stackalloc double[10];
Span<double> output = stackalloc double[10];
Midpoint.Calculate(source, output, 0);
Midpoint.Batch(source, output, 0);
});
}
@@ -103,7 +103,7 @@ public sealed class MidpointValidationTests : IDisposable
{
// Calculate QuanTAlib Midpoint (Span API)
double[] qOutput = new double[sourceData.Length];
Midpoint.Calculate(sourceData.AsSpan(), qOutput.AsSpan(), period);
Midpoint.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
// Calculate TA-Lib MIDPOINT
var retCode = TALib.Functions.MidPoint<double>(sourceData, 0..^0, talibOutput, out var outRange, period);
+19 -7
View File
@@ -23,6 +23,7 @@ public sealed class Midpoint : AbstractBase
private readonly Lowest _lowest;
private readonly ITValuePublisher? _source;
private readonly TValuePublishedHandler? _handler;
private bool _disposed;
public override bool IsHot => _highest.IsHot && _lowest.IsHot;
@@ -57,9 +58,13 @@ public sealed class Midpoint : AbstractBase
protected override void Dispose(bool disposing)
{
if (disposing && _source != null && _handler != null)
if (!_disposed)
{
_source.Pub -= _handler;
if (disposing && _source != null && _handler != null)
{
_source.Pub -= _handler;
}
_disposed = true;
}
base.Dispose(disposing);
}
@@ -106,7 +111,7 @@ public sealed class Midpoint : AbstractBase
}
}
public static TSeries Calculate(TSeries source, int period)
public static TSeries Batch(TSeries source, int period)
{
var indicator = new Midpoint(period);
return indicator.Update(source);
@@ -115,7 +120,7 @@ public sealed class Midpoint : AbstractBase
/// <summary>
/// Calculates rolling midpoint over a span of values.
/// </summary>
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period)
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period)
{
if (source.Length == 0)
{
@@ -150,8 +155,8 @@ public sealed class Midpoint : AbstractBase
try
{
Highest.Calculate(source, highBuffer, period);
Lowest.Calculate(source, lowBuffer, period);
Highest.Batch(source, highBuffer, period);
Lowest.Batch(source, lowBuffer, period);
for (int i = 0; i < len; i++)
{
@@ -172,10 +177,17 @@ public sealed class Midpoint : AbstractBase
}
}
public static (TSeries Results, Midpoint Indicator) Calculate(TSeries source, int period)
{
var indicator = new Midpoint(period);
TSeries results = indicator.Update(source);
return (results, indicator);
}
public override void Reset()
{
_highest.Reset();
_lowest.Reset();
Last = default;
}
}
}