mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +00:00
normalization of methods
This commit is contained in:
@@ -115,7 +115,7 @@ public class AlmaTests
|
||||
double[] input = series.Values.ToArray();
|
||||
double[] output = new double[input.Length];
|
||||
|
||||
Alma.Calculate(input.AsSpan(), output.AsSpan(), 10);
|
||||
Alma.Batch(input.AsSpan(), output.AsSpan(), 10);
|
||||
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
{
|
||||
@@ -284,7 +284,7 @@ public class AlmaTests
|
||||
var tValues = series.Values.ToArray();
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Alma.Calculate(spanInput, spanOutput, period);
|
||||
Alma.Batch(spanInput, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
@@ -317,12 +317,12 @@ public class AlmaTests
|
||||
double[] output = new double[5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, sigma: 0));
|
||||
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, sigma: -1));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, offset: -0.1));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, offset: 1.1));
|
||||
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
|
||||
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, sigma: 0));
|
||||
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, sigma: -1));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, offset: -0.1));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, offset: 1.1));
|
||||
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -331,7 +331,7 @@ public class AlmaTests
|
||||
double[] source = [100, 110, double.NaN, 120, 130];
|
||||
double[] output = new double[5];
|
||||
|
||||
Alma.Calculate(source.AsSpan(), output.AsSpan(), 3);
|
||||
Alma.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
|
||||
@@ -102,7 +102,7 @@ public sealed class AlmaValidationTests : IDisposable
|
||||
{
|
||||
// Calculate QuanTAlib ALMA (Span API)
|
||||
double[] qOutput = new double[sourceData.Length];
|
||||
global::QuanTAlib.Alma.Calculate(sourceData, qOutput.AsSpan(), period, offset, sigma);
|
||||
global::QuanTAlib.Alma.Batch(sourceData, qOutput.AsSpan(), period, offset, sigma);
|
||||
|
||||
// Calculate Skender ALMA
|
||||
var sResult = _testData.SkenderQuotes.GetAlma(period, offset, sigma).ToList();
|
||||
|
||||
@@ -26,6 +26,7 @@ public sealed class Alma : AbstractBase
|
||||
private readonly ITValuePublisher? _source;
|
||||
private readonly TValuePublishedHandler? _pubHandler;
|
||||
private bool _isNew = true;
|
||||
private bool _disposed;
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private record struct State(double LastValidValue, bool IsInitialized);
|
||||
@@ -83,9 +84,13 @@ public sealed class Alma : 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);
|
||||
}
|
||||
@@ -181,7 +186,7 @@ public sealed class Alma : AbstractBase
|
||||
var tSpan = CollectionsMarshal.AsSpan(t);
|
||||
var vSpan = CollectionsMarshal.AsSpan(v);
|
||||
|
||||
Calculate(source.Values, vSpan, _period, _offset, _sigma);
|
||||
Batch(source.Values, vSpan, _period, _offset, _sigma);
|
||||
source.Times.CopyTo(tSpan);
|
||||
|
||||
// Restore state
|
||||
@@ -305,7 +310,7 @@ public sealed class Alma : AbstractBase
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period, double offset = 0.85, double sigma = 6.0)
|
||||
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period, double offset = 0.85, double sigma = 6.0)
|
||||
{
|
||||
if (period <= 0)
|
||||
{
|
||||
@@ -433,6 +438,13 @@ public sealed class Alma : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Alma Indicator) Calculate(TSeries source, int period, double offset = 0.85, double sigma = 6.0)
|
||||
{
|
||||
var indicator = new Alma(period, offset, sigma);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
_buffer.Clear();
|
||||
@@ -440,4 +452,4 @@ public sealed class Alma : AbstractBase
|
||||
_p_state = _state;
|
||||
Last = default;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user