mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 12:08:05 +00:00
normalization of methods
This commit is contained in:
@@ -30,13 +30,15 @@ namespace QuanTAlib;
|
||||
/// Pine Script implementation: https://github.com/mihakralj/pinescript/blob/main/indicators/channels/abber.pine
|
||||
/// </remarks>
|
||||
[SkipLocalsInit]
|
||||
public sealed class Abber : ITValuePublisher
|
||||
public sealed class Abber : ITValuePublisher, IDisposable
|
||||
{
|
||||
private readonly int _period;
|
||||
private readonly double _multiplier;
|
||||
private readonly RingBuffer _sourceBuffer;
|
||||
private readonly RingBuffer _deviationBuffer;
|
||||
private readonly TValuePublishedHandler _handler;
|
||||
private ITValuePublisher? _source;
|
||||
private bool _disposed;
|
||||
|
||||
private const int ResyncInterval = 1000;
|
||||
|
||||
@@ -116,8 +118,9 @@ public sealed class Abber : ITValuePublisher
|
||||
/// </summary>
|
||||
public Abber(TSeries source, int period, double multiplier = 2.0) : this(period, multiplier)
|
||||
{
|
||||
_source = source ?? throw new ArgumentNullException(nameof(source));
|
||||
Prime(source);
|
||||
source.Pub += _handler;
|
||||
_source.Pub += _handler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -125,7 +128,8 @@ public sealed class Abber : ITValuePublisher
|
||||
/// </summary>
|
||||
public Abber(ITValuePublisher source, int period, double multiplier = 2.0) : this(period, multiplier)
|
||||
{
|
||||
source.Pub += _handler;
|
||||
_source = source ?? throw new ArgumentNullException(nameof(source));
|
||||
_source.Pub += _handler;
|
||||
}
|
||||
|
||||
private void HandleValue(object? sender, in TValueEventArgs e) => Update(e.Value, e.IsNew);
|
||||
@@ -673,4 +677,21 @@ public sealed class Abber : ITValuePublisher
|
||||
var results = abber.Update(source);
|
||||
return (results, abber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the Abber instance, unsubscribing from the source publisher.
|
||||
/// This method is idempotent.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (_source != null)
|
||||
{
|
||||
_source.Pub -= _handler;
|
||||
_source = null;
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,22 +347,22 @@ public class ApchannelTests
|
||||
|
||||
// Alpha must be > 0 and <= 1
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Apchannel.Calculate(high, low, upperBand, lowerBand, 0.0));
|
||||
Apchannel.Batch(high, low, upperBand, lowerBand, 0.0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Apchannel.Calculate(high, low, upperBand, lowerBand, 1.5));
|
||||
Apchannel.Batch(high, low, upperBand, lowerBand, 1.5));
|
||||
|
||||
// Arrays must be same length
|
||||
double[] wrongSizeLow = new double[3];
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Apchannel.Calculate(high, wrongSizeLow, upperBand, lowerBand, 0.2));
|
||||
Apchannel.Batch(high, wrongSizeLow, upperBand, lowerBand, 0.2));
|
||||
|
||||
double[] wrongSizeUpper = new double[3];
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Apchannel.Calculate(high, low, wrongSizeUpper, lowerBand, 0.2));
|
||||
Apchannel.Batch(high, low, wrongSizeUpper, lowerBand, 0.2));
|
||||
|
||||
double[] wrongSizeLower = new double[3];
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Apchannel.Calculate(high, low, upperBand, wrongSizeLower, 0.2));
|
||||
Apchannel.Batch(high, low, upperBand, wrongSizeLower, 0.2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -377,7 +377,7 @@ public class ApchannelTests
|
||||
double[] lowerBandSpan = new double[100];
|
||||
|
||||
// Calculate using span
|
||||
Apchannel.Calculate(high, low, upperBandSpan, lowerBandSpan, 0.2);
|
||||
Apchannel.Batch(high, low, upperBandSpan, lowerBandSpan, 0.2);
|
||||
|
||||
// Calculate iteratively
|
||||
var apc = new Apchannel(0.2);
|
||||
@@ -407,7 +407,7 @@ public class ApchannelTests
|
||||
double[] upperBand = new double[5];
|
||||
double[] lowerBand = new double[5];
|
||||
|
||||
Apchannel.Calculate(high, low, upperBand, lowerBand, 0.2);
|
||||
Apchannel.Batch(high, low, upperBand, lowerBand, 0.2);
|
||||
|
||||
foreach (var val in upperBand)
|
||||
{
|
||||
@@ -437,7 +437,7 @@ public class ApchannelTests
|
||||
}
|
||||
|
||||
// Warm up
|
||||
Apchannel.Calculate(high, low, upperBand, lowerBand, 0.2);
|
||||
Apchannel.Batch(high, low, upperBand, lowerBand, 0.2);
|
||||
|
||||
// Verify method completes without OOM or stack overflow
|
||||
Assert.True(double.IsFinite(upperBand[^1]));
|
||||
|
||||
@@ -66,7 +66,7 @@ public sealed class ApchannelValidationTests : IDisposable
|
||||
double[] spanUpper = new double[bars.Count];
|
||||
double[] spanLower = new double[bars.Count];
|
||||
|
||||
Apchannel.Calculate(high, low, spanUpper, spanLower, alpha);
|
||||
Apchannel.Batch(high, low, spanUpper, spanLower, alpha);
|
||||
|
||||
// 3. Batch Mode (Calculate)
|
||||
var (batchResults, _) = Apchannel.Calculate(bars, alpha);
|
||||
@@ -156,7 +156,7 @@ public sealed class ApchannelValidationTests : IDisposable
|
||||
double[] apchannelUpper = new double[high.Length];
|
||||
double[] apchannelLower = new double[low.Length];
|
||||
|
||||
Apchannel.Calculate(high, low, apchannelUpper, apchannelLower, alpha);
|
||||
Apchannel.Batch(high, low, apchannelUpper, apchannelLower, alpha);
|
||||
|
||||
// Calculate using Skender EMA for comparison
|
||||
var skenderQuotesForHigh = bars.Select(b => new Quote
|
||||
@@ -333,7 +333,7 @@ public sealed class ApchannelValidationTests : IDisposable
|
||||
double[] low = bars.Select(b => b.Low).ToArray();
|
||||
double[] spanUpper = new double[size];
|
||||
double[] spanLower = new double[size];
|
||||
Apchannel.Calculate(high, low, spanUpper, spanLower, alpha);
|
||||
Apchannel.Batch(high, low, spanUpper, spanLower, alpha);
|
||||
|
||||
// Compare last values
|
||||
Assert.Equal(streamingApc.UpperBand, spanUpper[^1], ValidationHelper.SkenderTolerance);
|
||||
|
||||
@@ -283,7 +283,7 @@ public sealed class Apchannel : AbstractBase
|
||||
/// Calculates the Adaptive Price Channel using span-based batch processing.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> sourceHigh,
|
||||
ReadOnlySpan<double> sourceLow,
|
||||
Span<double> upperBand,
|
||||
|
||||
@@ -214,7 +214,7 @@ public class BbandsTests
|
||||
TSeries source = bars.Close;
|
||||
|
||||
// Act
|
||||
TSeries result = Bbands.Calculate(source, period: 5, multiplier: 2.0);
|
||||
TSeries result = Bbands.Batch(source, period: 5, multiplier: 2.0);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(source.Count, result.Count);
|
||||
@@ -236,9 +236,9 @@ public class BbandsTests
|
||||
double[] middleArray = new double[source.Count];
|
||||
double[] upperArray = new double[source.Count];
|
||||
double[] lowerArray = new double[source.Count];
|
||||
Bbands.Calculate(sourceArray.AsSpan(), middleArray, upperArray, lowerArray, period, multiplier);
|
||||
Bbands.Batch(sourceArray.AsSpan(), middleArray, upperArray, lowerArray, period, multiplier);
|
||||
|
||||
TSeries seriesResult = Bbands.Calculate(source, period, multiplier);
|
||||
TSeries seriesResult = Bbands.Batch(source, period, multiplier);
|
||||
|
||||
// Assert - Compare last 10 values
|
||||
for (int i = source.Count - 10; i < source.Count; i++)
|
||||
@@ -258,7 +258,7 @@ public class BbandsTests
|
||||
|
||||
// Act & Assert
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(
|
||||
() => Bbands.Calculate(sourceArr.AsSpan(), middleArr.AsSpan(), upperArr.AsSpan(), lowerArr.AsSpan()));
|
||||
() => Bbands.Batch(sourceArr.AsSpan(), middleArr.AsSpan(), upperArr.AsSpan(), lowerArr.AsSpan()));
|
||||
Assert.Equal("source", exception.ParamName);
|
||||
}
|
||||
|
||||
@@ -305,14 +305,14 @@ public class BbandsTests
|
||||
}
|
||||
|
||||
// Batch
|
||||
TSeries batchResult = Bbands.Calculate(source, period, multiplier);
|
||||
TSeries batchResult = Bbands.Batch(source, period, multiplier);
|
||||
|
||||
// Span - Copy arrays before using to avoid ref local lambda issue
|
||||
double[] sourceArray = source.Values.ToArray();
|
||||
double[] middleArray = new double[source.Count];
|
||||
double[] upperArray = new double[source.Count];
|
||||
double[] lowerArray = new double[source.Count];
|
||||
Bbands.Calculate(sourceArray.AsSpan(), middleArray, upperArray, lowerArray, period, multiplier);
|
||||
Bbands.Batch(sourceArray.AsSpan(), middleArray, upperArray, lowerArray, period, multiplier);
|
||||
|
||||
// Assert - Compare last 50 values (streaming only has last value)
|
||||
Assert.Equal(batchResult[^1].Value, streamingBbands.Middle.Value, precision: 8);
|
||||
|
||||
@@ -100,7 +100,7 @@ public sealed class BbandsValidationTests : IDisposable
|
||||
double[] qMiddle = new double[sourceData.Length];
|
||||
double[] qUpper = new double[sourceData.Length];
|
||||
double[] qLower = new double[sourceData.Length];
|
||||
Bbands.Calculate(sourceData.AsSpan(), qMiddle.AsSpan(), qUpper.AsSpan(), qLower.AsSpan(), period, multiplier);
|
||||
Bbands.Batch(sourceData.AsSpan(), qMiddle.AsSpan(), qUpper.AsSpan(), qLower.AsSpan(), period, multiplier);
|
||||
|
||||
// Calculate Skender Bollinger Bands
|
||||
var sResult = _testData.SkenderQuotes.GetBollingerBands(period, multiplier).ToList();
|
||||
@@ -215,7 +215,7 @@ public sealed class BbandsValidationTests : IDisposable
|
||||
double[] qMiddle = new double[sourceData.Length];
|
||||
double[] qUpper = new double[sourceData.Length];
|
||||
double[] qLower = new double[sourceData.Length];
|
||||
Bbands.Calculate(sourceData.AsSpan(), qMiddle.AsSpan(), qUpper.AsSpan(), qLower.AsSpan(), period, multiplier);
|
||||
Bbands.Batch(sourceData.AsSpan(), qMiddle.AsSpan(), qUpper.AsSpan(), qLower.AsSpan(), period, multiplier);
|
||||
|
||||
// Calculate TA-Lib Bollinger Bands
|
||||
var retCode = Functions.Bbands<double>(
|
||||
@@ -321,7 +321,7 @@ public sealed class BbandsValidationTests : IDisposable
|
||||
double[] qMiddle = new double[sourceData.Length];
|
||||
double[] qUpper = new double[sourceData.Length];
|
||||
double[] qLower = new double[sourceData.Length];
|
||||
Bbands.Calculate(sourceData.AsSpan(), qMiddle.AsSpan(), qUpper.AsSpan(), qLower.AsSpan(), period, multiplier);
|
||||
Bbands.Batch(sourceData.AsSpan(), qMiddle.AsSpan(), qUpper.AsSpan(), qLower.AsSpan(), period, multiplier);
|
||||
|
||||
// Calculate Tulip Bollinger Bands
|
||||
var bbandsIndicator = Tulip.Indicators.bbands;
|
||||
|
||||
@@ -181,7 +181,7 @@ public sealed class Bbands : AbstractBase
|
||||
Span<double> upperSpan = upperRented.AsSpan(0, len);
|
||||
Span<double> lowerSpan = lowerRented.AsSpan(0, len);
|
||||
|
||||
Calculate(sourceSpan, middleSpan, upperSpan, lowerSpan, _period, _multiplier);
|
||||
Batch(sourceSpan, middleSpan, upperSpan, lowerSpan, _period, _multiplier);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
@@ -230,7 +230,7 @@ public sealed class Bbands : AbstractBase
|
||||
/// <summary>
|
||||
/// Calculates Bollinger Bands for the entire series and returns the middle band series.
|
||||
/// </summary>
|
||||
public static TSeries Calculate(TSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
public static TSeries Batch(TSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
{
|
||||
Bbands bbands = new(period, multiplier);
|
||||
return bbands.Update(source);
|
||||
@@ -239,7 +239,7 @@ public sealed class Bbands : AbstractBase
|
||||
/// <summary>
|
||||
/// Calculates Bollinger Bands across all input values using SIMD-optimized operations where possible.
|
||||
/// </summary>
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> source,
|
||||
Span<double> middle,
|
||||
Span<double> upper,
|
||||
@@ -360,4 +360,12 @@ public sealed class Bbands : AbstractBase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Bbands Indicator) Calculate(TSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
{
|
||||
var indicator = new Bbands(period, multiplier);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -269,8 +269,8 @@ public sealed class Dchannel : ITValuePublisher
|
||||
|
||||
try
|
||||
{
|
||||
QuanTAlib.Highest.Calculate(high, top.AsSpan(0, len), period);
|
||||
QuanTAlib.Lowest.Calculate(low, bot.AsSpan(0, len), period);
|
||||
QuanTAlib.Highest.Batch(high, top.AsSpan(0, len), period);
|
||||
QuanTAlib.Lowest.Batch(low, bot.AsSpan(0, len), period);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
|
||||
@@ -428,8 +428,8 @@ public sealed class Decaychannel : ITValuePublisher
|
||||
|
||||
try
|
||||
{
|
||||
QuanTAlib.Highest.Calculate(high, rawMaxArr.AsSpan(0, len), period);
|
||||
QuanTAlib.Lowest.Calculate(low, rawMinArr.AsSpan(0, len), period);
|
||||
QuanTAlib.Highest.Batch(high, rawMaxArr.AsSpan(0, len), period);
|
||||
QuanTAlib.Lowest.Batch(low, rawMinArr.AsSpan(0, len), period);
|
||||
|
||||
double currentMax = double.NaN;
|
||||
double currentMin = double.NaN;
|
||||
|
||||
@@ -202,11 +202,11 @@ public class JbandsTests
|
||||
double[] shortOut = new double[2];
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Jbands.Calculate(source.AsSpan(), shortOut.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14));
|
||||
Jbands.Batch(source.AsSpan(), shortOut.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14));
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Jbands.Calculate(source.AsSpan(), middle.AsSpan(), shortOut.AsSpan(), lower.AsSpan(), 14));
|
||||
Jbands.Batch(source.AsSpan(), middle.AsSpan(), shortOut.AsSpan(), lower.AsSpan(), 14));
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Jbands.Calculate(source.AsSpan(), middle.AsSpan(), upper.AsSpan(), shortOut.AsSpan(), 14));
|
||||
Jbands.Batch(source.AsSpan(), middle.AsSpan(), upper.AsSpan(), shortOut.AsSpan(), 14));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -223,7 +223,7 @@ public class JbandsTests
|
||||
double[] upper = new double[100];
|
||||
double[] lower = new double[100];
|
||||
|
||||
Jbands.Calculate(source.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14);
|
||||
Jbands.Batch(source.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14);
|
||||
|
||||
var jStream = new Jbands(14);
|
||||
for (int i = 0; i < source.Length; i++)
|
||||
|
||||
@@ -103,7 +103,7 @@ public class JbandsValidationTests
|
||||
double[] upper = new double[200];
|
||||
double[] lower = new double[200];
|
||||
|
||||
Jbands.Calculate(source.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14);
|
||||
Jbands.Batch(source.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14);
|
||||
|
||||
var jStream = new Jbands(14);
|
||||
for (int i = 0; i < source.Length; i++)
|
||||
@@ -144,7 +144,7 @@ public class JbandsValidationTests
|
||||
double[] middleSpan = new double[150];
|
||||
double[] upperSpan = new double[150];
|
||||
double[] lowerSpan = new double[150];
|
||||
Jbands.Calculate(rawValues.AsSpan(), middleSpan.AsSpan(), upperSpan.AsSpan(), lowerSpan.AsSpan(), 14, 25, 0.45);
|
||||
Jbands.Batch(rawValues.AsSpan(), middleSpan.AsSpan(), upperSpan.AsSpan(), lowerSpan.AsSpan(), 14, 25, 0.45);
|
||||
|
||||
// Mode 4: Event-based
|
||||
var jEvent = new Jbands(14, 25, 0.45);
|
||||
@@ -240,7 +240,7 @@ public class JbandsValidationTests
|
||||
double[] middle = new double[50];
|
||||
double[] upper = new double[50];
|
||||
double[] lower = new double[50];
|
||||
Jbands.Calculate(rawValues.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14);
|
||||
Jbands.Batch(rawValues.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 14);
|
||||
|
||||
// After warmup, values should be stable and match
|
||||
for (int i = warmupPeriod; i < rawValues.Length; i++)
|
||||
|
||||
@@ -386,7 +386,7 @@ public sealed class Jbands : ITValuePublisher, IDisposable
|
||||
return jbands.Update(source);
|
||||
}
|
||||
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> source,
|
||||
Span<double> middle,
|
||||
Span<double> upper,
|
||||
|
||||
@@ -252,8 +252,8 @@ public sealed class Mmchannel : ITValuePublisher
|
||||
return;
|
||||
}
|
||||
|
||||
Highest.Calculate(high, upper, period);
|
||||
Lowest.Calculate(low, lower, period);
|
||||
Highest.Batch(high, upper, period);
|
||||
Lowest.Batch(low, lower, period);
|
||||
}
|
||||
|
||||
public static (TSeries Upper, TSeries Lower) Batch(TBarSeries source, int period)
|
||||
|
||||
@@ -376,8 +376,8 @@ public sealed class Pchannel : ITValuePublisher
|
||||
|
||||
try
|
||||
{
|
||||
Highest.Calculate(high, top.AsSpan(0, len), period);
|
||||
Lowest.Calculate(low, bot.AsSpan(0, len), period);
|
||||
Highest.Batch(high, top.AsSpan(0, len), period);
|
||||
Lowest.Batch(low, bot.AsSpan(0, len), period);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
|
||||
@@ -279,7 +279,7 @@ public class StbandsTests
|
||||
TBarSeries bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Act
|
||||
TSeries result = Stbands.Calculate(bars, period: 5, multiplier: 2.0);
|
||||
TSeries result = Stbands.Batch(bars, period: 5, multiplier: 2.0);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(bars.Count, result.Count);
|
||||
@@ -302,7 +302,7 @@ public class StbandsTests
|
||||
double[] trend = new double[bars.Count];
|
||||
|
||||
// Act
|
||||
Stbands.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(), upper.AsSpan(), lower.AsSpan(), trend.AsSpan(), period, multiplier);
|
||||
Stbands.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(), upper.AsSpan(), lower.AsSpan(), trend.AsSpan(), period, multiplier);
|
||||
|
||||
// Assert
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
@@ -327,7 +327,7 @@ public class StbandsTests
|
||||
|
||||
// Act & Assert
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(
|
||||
() => Stbands.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(), upper.AsSpan(), lower.AsSpan(), trend.AsSpan()));
|
||||
() => Stbands.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(), upper.AsSpan(), lower.AsSpan(), trend.AsSpan()));
|
||||
Assert.Equal("high", exception.ParamName);
|
||||
}
|
||||
|
||||
@@ -348,7 +348,7 @@ public class StbandsTests
|
||||
}
|
||||
|
||||
// Batch
|
||||
TSeries batchResult = Stbands.Calculate(bars, period, multiplier);
|
||||
TSeries batchResult = Stbands.Batch(bars, period, multiplier);
|
||||
|
||||
// Assert - Last values should match
|
||||
Assert.Equal(batchResult[^1].Value, streamingStbands.Last.Value, precision: 8);
|
||||
@@ -377,7 +377,7 @@ public class StbandsTests
|
||||
double[] upper = new double[bars.Count];
|
||||
double[] lower = new double[bars.Count];
|
||||
double[] trend = new double[bars.Count];
|
||||
Stbands.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(), upper.AsSpan(), lower.AsSpan(), trend.AsSpan(), period, multiplier);
|
||||
Stbands.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(), upper.AsSpan(), lower.AsSpan(), trend.AsSpan(), period, multiplier);
|
||||
|
||||
// Assert - Last values should match
|
||||
Assert.Equal(upper[^1], streamingStbands.Upper.Value, precision: 8);
|
||||
|
||||
@@ -69,7 +69,7 @@ public sealed class StbandsValidationTests : IDisposable
|
||||
}
|
||||
|
||||
// Batch mode
|
||||
var batchResult = Stbands.Calculate(bars, period, multiplier);
|
||||
var batchResult = Stbands.Batch(bars, period, multiplier);
|
||||
|
||||
// Compare last 100 values
|
||||
int compareCount = Math.Min(100, bars.Count - period);
|
||||
@@ -118,7 +118,7 @@ public sealed class StbandsValidationTests : IDisposable
|
||||
double[] spanLower = new double[bars.Count];
|
||||
double[] spanTrend = new double[bars.Count];
|
||||
|
||||
Stbands.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
Stbands.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
spanUpper.AsSpan(), spanLower.AsSpan(), spanTrend.AsSpan(), period, multiplier);
|
||||
|
||||
// Compare last 100 values
|
||||
@@ -155,7 +155,7 @@ public sealed class StbandsValidationTests : IDisposable
|
||||
double[] lower = new double[bars.Count];
|
||||
double[] trend = new double[bars.Count];
|
||||
|
||||
Stbands.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
Stbands.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
upper.AsSpan(), lower.AsSpan(), trend.AsSpan(), period, multiplier);
|
||||
|
||||
// Verify Upper >= Lower for all points
|
||||
@@ -192,7 +192,7 @@ public sealed class StbandsValidationTests : IDisposable
|
||||
double[] lower = new double[bars.Count];
|
||||
double[] trend = new double[bars.Count];
|
||||
|
||||
Stbands.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
Stbands.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
upper.AsSpan(), lower.AsSpan(), trend.AsSpan(), period, multiplier);
|
||||
|
||||
int trendChanges = 0;
|
||||
|
||||
@@ -286,7 +286,7 @@ public sealed class Stbands : AbstractBase
|
||||
/// <summary>
|
||||
/// Calculates Super Trend Bands for the entire bar series.
|
||||
/// </summary>
|
||||
public static TSeries Calculate(TBarSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
public static TSeries Batch(TBarSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
{
|
||||
Stbands stbands = new(period, multiplier);
|
||||
return stbands.Update(source);
|
||||
@@ -295,7 +295,7 @@ public sealed class Stbands : AbstractBase
|
||||
/// <summary>
|
||||
/// Calculates Super Trend Bands across OHLC data using spans.
|
||||
/// </summary>
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> high,
|
||||
ReadOnlySpan<double> low,
|
||||
ReadOnlySpan<double> close,
|
||||
@@ -399,4 +399,12 @@ public sealed class Stbands : AbstractBase
|
||||
prevClose = c;
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Stbands Indicator) Calculate(TBarSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
{
|
||||
var indicator = new Stbands(period, multiplier);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -285,7 +285,7 @@ public class UbandsTests
|
||||
TSeries series = bars.Close;
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchResult = Ubands.Calculate(series, period, multiplier);
|
||||
var batchResult = Ubands.Batch(series, period, multiplier);
|
||||
double batchLast = batchResult.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
@@ -293,7 +293,7 @@ public class UbandsTests
|
||||
double[] spanUpper = new double[source.Length];
|
||||
double[] spanMiddle = new double[source.Length];
|
||||
double[] spanLower = new double[source.Length];
|
||||
Ubands.Calculate(source.AsSpan(), spanUpper.AsSpan(), spanMiddle.AsSpan(), spanLower.AsSpan(), period, multiplier);
|
||||
Ubands.Batch(source.AsSpan(), spanUpper.AsSpan(), spanMiddle.AsSpan(), spanLower.AsSpan(), period, multiplier);
|
||||
double spanLast = spanMiddle[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
@@ -319,13 +319,13 @@ public class UbandsTests
|
||||
|
||||
// Period must be >= 1
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Ubands.Calculate(source.AsSpan(), upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), 0));
|
||||
Ubands.Batch(source.AsSpan(), upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Ubands.Calculate(source.AsSpan(), upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), -1));
|
||||
Ubands.Batch(source.AsSpan(), upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), -1));
|
||||
|
||||
// All arrays must be same length
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Ubands.Calculate(source.AsSpan(), wrongSize.AsSpan(), middle.AsSpan(), lower.AsSpan(), 3));
|
||||
Ubands.Batch(source.AsSpan(), wrongSize.AsSpan(), middle.AsSpan(), lower.AsSpan(), 3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -336,7 +336,7 @@ public class UbandsTests
|
||||
double[] middle = new double[5];
|
||||
double[] lower = new double[5];
|
||||
|
||||
Ubands.Calculate(source.AsSpan(), upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), 3, 1.0);
|
||||
Ubands.Batch(source.AsSpan(), upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), 3, 1.0);
|
||||
|
||||
foreach (var val in middle)
|
||||
{
|
||||
@@ -404,7 +404,7 @@ public class UbandsTests
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries series = bars.Close;
|
||||
|
||||
var result = Ubands.Calculate(series, 10, 1.0);
|
||||
var result = Ubands.Batch(series, 10, 1.0);
|
||||
|
||||
Assert.Equal(50, result.Count);
|
||||
Assert.True(double.IsFinite(result.Last.Value));
|
||||
|
||||
@@ -71,7 +71,7 @@ public sealed class UbandsValidationTests : IDisposable
|
||||
}
|
||||
|
||||
// Batch mode
|
||||
var batchResult = Ubands.Calculate(series, period, multiplier);
|
||||
var batchResult = Ubands.Batch(series, period, multiplier);
|
||||
|
||||
// Compare last 100 values
|
||||
int compareCount = Math.Min(100, series.Count - period);
|
||||
@@ -119,7 +119,7 @@ public sealed class UbandsValidationTests : IDisposable
|
||||
double[] spanMiddle = new double[series.Count];
|
||||
double[] spanLower = new double[series.Count];
|
||||
|
||||
Ubands.Calculate(source.AsSpan(), spanUpper.AsSpan(), spanMiddle.AsSpan(),
|
||||
Ubands.Batch(source.AsSpan(), spanUpper.AsSpan(), spanMiddle.AsSpan(),
|
||||
spanLower.AsSpan(), period, multiplier);
|
||||
|
||||
// Compare last 100 values
|
||||
|
||||
@@ -268,7 +268,7 @@ public sealed class Ubands : AbstractBase
|
||||
/// <summary>
|
||||
/// Calculates Ultimate Bands for the entire series.
|
||||
/// </summary>
|
||||
public static TSeries Calculate(TSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
public static TSeries Batch(TSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
{
|
||||
Ubands ubands = new(period, multiplier);
|
||||
return ubands.Update(source);
|
||||
@@ -277,7 +277,7 @@ public sealed class Ubands : AbstractBase
|
||||
/// <summary>
|
||||
/// Calculates Ultimate Bands across data using spans.
|
||||
/// </summary>
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> source,
|
||||
Span<double> upper,
|
||||
Span<double> middle,
|
||||
@@ -397,4 +397,12 @@ public sealed class Ubands : AbstractBase
|
||||
lower[i] = usf - bandOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Ubands Indicator) Calculate(TSeries source, int period = DefaultPeriod, double multiplier = DefaultMultiplier)
|
||||
{
|
||||
var indicator = new Ubands(period, multiplier);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -306,7 +306,7 @@ public class UchannelTests
|
||||
double[] spanUpper = new double[highArr.Length];
|
||||
double[] spanMiddle = new double[highArr.Length];
|
||||
double[] spanLower = new double[highArr.Length];
|
||||
Uchannel.Calculate(highArr.AsSpan(), lowArr.AsSpan(), closeArr.AsSpan(),
|
||||
Uchannel.Batch(highArr.AsSpan(), lowArr.AsSpan(), closeArr.AsSpan(),
|
||||
spanUpper.AsSpan(), spanMiddle.AsSpan(), spanLower.AsSpan(),
|
||||
strPeriod, centerPeriod, multiplier);
|
||||
double spanLast = spanMiddle[^1];
|
||||
@@ -336,15 +336,15 @@ public class UchannelTests
|
||||
|
||||
// Period must be >= 1
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Uchannel.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
Uchannel.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Uchannel.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
Uchannel.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), -1));
|
||||
|
||||
// All arrays must be same length
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Uchannel.Calculate(high.AsSpan(), wrongSize.AsSpan(), close.AsSpan(),
|
||||
Uchannel.Batch(high.AsSpan(), wrongSize.AsSpan(), close.AsSpan(),
|
||||
upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), 3));
|
||||
}
|
||||
|
||||
@@ -358,7 +358,7 @@ public class UchannelTests
|
||||
double[] middle = new double[5];
|
||||
double[] lower = new double[5];
|
||||
|
||||
Uchannel.Calculate(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
Uchannel.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(),
|
||||
upper.AsSpan(), middle.AsSpan(), lower.AsSpan(), 3, 3, 1.0);
|
||||
|
||||
foreach (var val in middle)
|
||||
|
||||
@@ -70,7 +70,7 @@ public class UchannelValidationTests
|
||||
double[] spanUpper = new double[highArr.Length];
|
||||
double[] spanMiddle = new double[highArr.Length];
|
||||
double[] spanLower = new double[highArr.Length];
|
||||
Uchannel.Calculate(highArr.AsSpan(), lowArr.AsSpan(), closeArr.AsSpan(),
|
||||
Uchannel.Batch(highArr.AsSpan(), lowArr.AsSpan(), closeArr.AsSpan(),
|
||||
spanUpper.AsSpan(), spanMiddle.AsSpan(), spanLower.AsSpan(),
|
||||
DefaultStrPeriod, DefaultCenterPeriod, DefaultMultiplier);
|
||||
|
||||
|
||||
@@ -377,7 +377,7 @@ public sealed class Uchannel : AbstractBase
|
||||
/// <param name="strPeriod">Period for STR smoothing.</param>
|
||||
/// <param name="centerPeriod">Period for centerline smoothing.</param>
|
||||
/// <param name="multiplier">Band multiplier.</param>
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> high,
|
||||
ReadOnlySpan<double> low,
|
||||
ReadOnlySpan<double> close,
|
||||
|
||||
@@ -446,12 +446,12 @@ public class VwapbandsTests
|
||||
|
||||
// Multiplier must be >= MinMultiplier
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapbands.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
upper1.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 0));
|
||||
|
||||
// All arrays must be same length
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapbands.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
wrongSize.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0));
|
||||
}
|
||||
|
||||
@@ -467,7 +467,7 @@ public class VwapbandsTests
|
||||
double[] vwap = new double[5];
|
||||
double[] stdDev = new double[5];
|
||||
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapbands.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
upper1.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0);
|
||||
|
||||
foreach (var val in vwap)
|
||||
|
||||
@@ -126,7 +126,7 @@ public sealed class VwapbandsValidationTests : IDisposable
|
||||
double[] spanLower2 = new double[bars.Count];
|
||||
double[] spanStdDev = new double[bars.Count];
|
||||
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapbands.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
spanUpper1.AsSpan(), spanLower1.AsSpan(),
|
||||
spanUpper2.AsSpan(), spanLower2.AsSpan(),
|
||||
spanVwap.AsSpan(), spanStdDev.AsSpan(), multiplier);
|
||||
|
||||
@@ -364,7 +364,7 @@ public sealed class Vwapbands : AbstractBase
|
||||
/// <param name="vwap">Output span for VWAP values</param>
|
||||
/// <param name="stdDev">Output span for standard deviation values</param>
|
||||
/// <param name="multiplier">Band multiplier (default 1.0)</param>
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> price,
|
||||
ReadOnlySpan<double> volume,
|
||||
Span<double> upper1,
|
||||
|
||||
@@ -472,17 +472,17 @@ public class VwapsdTests
|
||||
|
||||
// NumDevs must be >= MinNumDevs
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapsd.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 0));
|
||||
|
||||
// NumDevs must be <= MaxNumDevs
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapsd.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 6.0));
|
||||
|
||||
// All arrays must be same length
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapsd.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
wrongSize.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0));
|
||||
}
|
||||
|
||||
@@ -496,7 +496,7 @@ public class VwapsdTests
|
||||
double[] vwap = new double[5];
|
||||
double[] stdDev = new double[5];
|
||||
|
||||
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapsd.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0);
|
||||
|
||||
foreach (var val in vwap)
|
||||
|
||||
@@ -120,7 +120,7 @@ public sealed class VwapsdValidationTests : IDisposable
|
||||
double[] spanLower = new double[bars.Count];
|
||||
double[] spanStdDev = new double[bars.Count];
|
||||
|
||||
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
Vwapsd.Batch(price.AsSpan(), volume.AsSpan(),
|
||||
spanUpper.AsSpan(), spanLower.AsSpan(),
|
||||
spanVwap.AsSpan(), spanStdDev.AsSpan(), numDevs);
|
||||
|
||||
|
||||
@@ -344,7 +344,7 @@ public sealed class Vwapsd : AbstractBase
|
||||
/// <param name="vwap">Output span for VWAP values</param>
|
||||
/// <param name="stdDev">Output span for standard deviation values</param>
|
||||
/// <param name="numDevs">Number of standard deviations for bands (default 2.0)</param>
|
||||
public static void Calculate(
|
||||
public static void Batch(
|
||||
ReadOnlySpan<double> price,
|
||||
ReadOnlySpan<double> volume,
|
||||
Span<double> upper,
|
||||
|
||||
Reference in New Issue
Block a user