mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 21:48:03 +00:00
normalization of methods
This commit is contained in:
@@ -108,7 +108,7 @@ public class HammaTests
|
||||
double[] input = series.Values.ToArray();
|
||||
double[] output = new double[input.Length];
|
||||
|
||||
Hamma.Calculate(input.AsSpan(), output.AsSpan(), 10);
|
||||
Hamma.Batch(input.AsSpan(), output.AsSpan(), 10);
|
||||
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
{
|
||||
@@ -277,7 +277,7 @@ public class HammaTests
|
||||
var tValues = series.Values.ToArray();
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Hamma.Calculate(spanInput, spanOutput, period);
|
||||
Hamma.Batch(spanInput, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
@@ -310,8 +310,8 @@ public class HammaTests
|
||||
double[] output = new double[5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Hamma.Calculate(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentException>(() => Hamma.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
|
||||
Assert.Throws<ArgumentException>(() => Hamma.Batch(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentException>(() => Hamma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -320,7 +320,7 @@ public class HammaTests
|
||||
double[] source = [100, 110, double.NaN, 120, 130];
|
||||
double[] output = new double[5];
|
||||
|
||||
Hamma.Calculate(source.AsSpan(), output.AsSpan(), 3);
|
||||
Hamma.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
|
||||
@@ -75,7 +75,7 @@ public sealed class HammaValidationTests : IDisposable
|
||||
{
|
||||
// Calculate QuanTAlib HAMMA (Span API)
|
||||
double[] spanOutput = new double[sourceData.Length];
|
||||
Hamma.Calculate(sourceData, spanOutput.AsSpan(), period);
|
||||
Hamma.Batch(sourceData, spanOutput.AsSpan(), period);
|
||||
|
||||
// Calculate QuanTAlib HAMMA (batch TSeries)
|
||||
var hammaBatch = new Hamma(period);
|
||||
|
||||
@@ -24,6 +24,7 @@ public sealed class Hamma : AbstractBase
|
||||
private readonly ITValuePublisher? _source;
|
||||
private readonly TValuePublishedHandler? _pubHandler;
|
||||
private bool _isNew = true;
|
||||
private bool _disposed;
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private record struct State
|
||||
@@ -78,9 +79,13 @@ public sealed class Hamma : AbstractBase
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && _source != null && _pubHandler != null)
|
||||
if (!_disposed)
|
||||
{
|
||||
_source.Pub -= _pubHandler;
|
||||
if (disposing && _source != null && _pubHandler != null)
|
||||
{
|
||||
_source.Pub -= _pubHandler;
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
@@ -183,7 +188,7 @@ public sealed class Hamma : AbstractBase
|
||||
var tSpan = CollectionsMarshal.AsSpan(t);
|
||||
var vSpan = CollectionsMarshal.AsSpan(v);
|
||||
|
||||
Calculate(source.Values, vSpan, _period);
|
||||
Batch(source.Values, vSpan, _period);
|
||||
source.Times.CopyTo(tSpan);
|
||||
|
||||
// Restore state
|
||||
@@ -271,7 +276,7 @@ public sealed class Hamma : AbstractBase
|
||||
/// <param name="period">Lookback period for the Hamming window (default: 10)</param>
|
||||
/// <exception cref="ArgumentException">Thrown when output length doesn't match source length.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period = 10)
|
||||
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period = 10)
|
||||
{
|
||||
if (period <= 0)
|
||||
{
|
||||
@@ -389,6 +394,13 @@ public sealed class Hamma : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Hamma Indicator) Calculate(TSeries source, int period = 10)
|
||||
{
|
||||
var indicator = new Hamma(period);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
_buffer.Clear();
|
||||
@@ -397,4 +409,4 @@ public sealed class Hamma : AbstractBase
|
||||
_p_state = _state;
|
||||
Last = default;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user