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:
@@ -32,11 +32,11 @@ public class JmaTests
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
// Period must be > 0
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Jma.Calculate(source.AsSpan(), output.AsSpan(), 0, 0, 1.0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Jma.Calculate(source.AsSpan(), output.AsSpan(), -1, 0, 1.0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Jma.Batch(source.AsSpan(), output.AsSpan(), 0, 0, 1.0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Jma.Batch(source.AsSpan(), output.AsSpan(), -1, 0, 1.0));
|
||||
|
||||
// Output must be same length as source
|
||||
Assert.Throws<ArgumentException>(() => Jma.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3, 0, 1.0));
|
||||
Assert.Throws<ArgumentException>(() => Jma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3, 0, 1.0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -163,7 +163,7 @@ public class JmaTests
|
||||
var tseriesResult = Jma.Batch(series, 10);
|
||||
|
||||
// Calculate with Span API
|
||||
Jma.Calculate(source.AsSpan(), output.AsSpan(), 10);
|
||||
Jma.Batch(source.AsSpan(), output.AsSpan(), 10);
|
||||
|
||||
// Compare results
|
||||
for (int i = 0; i < 100; i++)
|
||||
@@ -189,7 +189,7 @@ public class JmaTests
|
||||
var tValues = series.Values.ToArray();
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Jma.Calculate(spanInput, spanOutput, period);
|
||||
Jma.Batch(spanInput, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
@@ -261,7 +261,7 @@ public class JmaTests
|
||||
double[] source = [100, 110, double.NaN, 120, 130];
|
||||
double[] output = new double[5];
|
||||
|
||||
Jma.Calculate(source.AsSpan(), output.AsSpan(), 3);
|
||||
Jma.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
|
||||
@@ -36,6 +36,7 @@ public sealed class Jma : AbstractBase
|
||||
private readonly RingBuffer _volBuffer;
|
||||
private readonly TValuePublishedHandler _handler;
|
||||
private readonly ITValuePublisher? _source;
|
||||
private bool _disposed;
|
||||
|
||||
// Streaming state (current + previous snapshot for isNew=false)
|
||||
private State _state;
|
||||
@@ -203,11 +204,11 @@ public sealed class Jma : AbstractBase
|
||||
private double CalculateJma(double value)
|
||||
{
|
||||
// 1. Local deviation: |price - {UpperBand, LowerBand}|
|
||||
double diffA = value - _state.UpperBand;
|
||||
double diffB = value - _state.LowerBand;
|
||||
double absA = Math.Abs(diffA);
|
||||
double absB = Math.Abs(diffB);
|
||||
double absValue = absA > absB ? absA : absB;
|
||||
double uBand = value - _state.UpperBand;
|
||||
double lBand = value - _state.LowerBand;
|
||||
double absUBand = Math.Abs(uBand);
|
||||
double absLBand = Math.Abs(lBand);
|
||||
double absValue = absUBand > absLBand ? absUBand : absLBand;
|
||||
double deviation = absValue + 1e-10;
|
||||
|
||||
// 2. 10-bar SMA of local deviation -> "volatility"
|
||||
@@ -338,9 +339,13 @@ public sealed class Jma : AbstractBase
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && _source != null)
|
||||
if (!_disposed)
|
||||
{
|
||||
_source.Pub -= _handler;
|
||||
if (disposing && _source != null)
|
||||
{
|
||||
_source.Pub -= _handler;
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
@@ -362,7 +367,7 @@ public sealed class Jma : AbstractBase
|
||||
/// <summary>
|
||||
/// Static helper compatible with your existing signature.
|
||||
/// </summary>
|
||||
public static void Calculate(ReadOnlySpan<double> source,
|
||||
public static void Batch(ReadOnlySpan<double> source,
|
||||
Span<double> output,
|
||||
int period,
|
||||
int phase = 0,
|
||||
@@ -385,6 +390,13 @@ public sealed class Jma : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Jma Indicator) Calculate(TSeries source, int period, int phase = 0, double power = 0.45)
|
||||
{
|
||||
var indicator = new Jma(period, phase, power);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double CalculateTrimmedMean(double fallback)
|
||||
{
|
||||
@@ -431,4 +443,4 @@ public sealed class Jma : AbstractBase
|
||||
int len = end - start + 1;
|
||||
return sorted.Slice(start, len).SumSIMD() / len;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user