mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 20:48:04 +00:00
refactoring
This commit is contained in:
@@ -117,7 +117,7 @@ public class ConvIndicatorTests
|
||||
{
|
||||
var indicator = new ConvIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
|
||||
var method = indicator.GetType().GetMethod("OnPaintChart");
|
||||
Assert.NotNull(method);
|
||||
Assert.Equal(typeof(ConvIndicator), method.DeclaringType);
|
||||
@@ -170,10 +170,10 @@ public class ConvIndicatorTests
|
||||
public void ConvIndicator_InvalidWeights_FallsBackToDefault()
|
||||
{
|
||||
var indicator = new ConvIndicator { WeightsInput = "invalid" };
|
||||
|
||||
|
||||
// Should not throw, but fallback
|
||||
indicator.Initialize();
|
||||
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public class ConvIndicator : Indicator, IWatchlistIndicator
|
||||
var weights = WeightsInput.Split(',')
|
||||
.Select(s => double.Parse(s.Trim()))
|
||||
.ToArray();
|
||||
|
||||
|
||||
if (weights.Length == 0)
|
||||
throw new ArgumentException("Weights cannot be empty");
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ public class ConvTests
|
||||
source.Add(new TValue(DateTime.UtcNow, 3));
|
||||
source.Add(new TValue(DateTime.UtcNow, 4));
|
||||
|
||||
var result = Conv.Calculate(source, kernel);
|
||||
var result = Conv.Batch(source, kernel);
|
||||
|
||||
Assert.Equal(1.0, result.Values[0]);
|
||||
Assert.Equal(2.5, result.Values[1]);
|
||||
@@ -173,14 +173,14 @@ public class ConvTests
|
||||
var series = bars.Close;
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchSeries = Conv.Calculate(series, kernel);
|
||||
var batchSeries = Conv.Batch(series, kernel);
|
||||
double expected = batchSeries.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
var tValues = series.Values.ToArray();
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Conv.Calculate(spanInput, spanOutput, kernel);
|
||||
Conv.Batch(spanInput, spanOutput, kernel);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
@@ -214,8 +214,8 @@ public class ConvTests
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
double[] kernel = [0.5, 0.5];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Conv.Calculate(source.AsSpan(), output.AsSpan(), Array.Empty<double>()));
|
||||
Assert.Throws<ArgumentException>(() => Conv.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), kernel));
|
||||
Assert.Throws<ArgumentException>(() => Conv.Batch(source.AsSpan(), output.AsSpan(), Array.Empty<double>()));
|
||||
Assert.Throws<ArgumentException>(() => Conv.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), kernel));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -225,7 +225,7 @@ public class ConvTests
|
||||
double[] output = new double[5];
|
||||
double[] kernel = [0.5, 0.5];
|
||||
|
||||
Conv.Calculate(source.AsSpan(), output.AsSpan(), kernel);
|
||||
Conv.Batch(source.AsSpan(), output.AsSpan(), kernel);
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
|
||||
+25
-19
@@ -19,7 +19,7 @@ namespace QuanTAlib;
|
||||
/// Update: O(K) where K is kernel length.
|
||||
/// </remarks>
|
||||
[SkipLocalsInit]
|
||||
public sealed class Conv : ITValuePublisher
|
||||
public sealed class Conv : AbstractBase
|
||||
{
|
||||
private readonly int _period;
|
||||
private readonly double[] _kernel;
|
||||
@@ -29,10 +29,7 @@ public sealed class Conv : ITValuePublisher
|
||||
private State _state;
|
||||
private State _p_state;
|
||||
|
||||
public string Name { get; }
|
||||
public TValue Last { get; private set; }
|
||||
public bool IsHot => _buffer.IsFull;
|
||||
public event Action<TValue>? Pub;
|
||||
public override bool IsHot => _buffer.IsFull;
|
||||
|
||||
public Conv(double[] kernel)
|
||||
{
|
||||
@@ -44,6 +41,7 @@ public sealed class Conv : ITValuePublisher
|
||||
Array.Copy(kernel, _kernel, _period);
|
||||
_buffer = new RingBuffer(_period);
|
||||
Name = $"Conv({_period})";
|
||||
WarmupPeriod = _period;
|
||||
_state.LastValidValue = double.NaN;
|
||||
_p_state.LastValidValue = double.NaN;
|
||||
}
|
||||
@@ -65,7 +63,7 @@ public sealed class Conv : ITValuePublisher
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public TValue Update(TValue input, bool isNew = true)
|
||||
public override TValue Update(TValue input, bool isNew = true)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
@@ -92,29 +90,29 @@ public sealed class Conv : ITValuePublisher
|
||||
{
|
||||
int count = _buffer.Count;
|
||||
int kernelOffset = _period - count;
|
||||
ReadOnlySpan<double> kernelSpan = _kernel.AsSpan().Slice(kernelOffset);
|
||||
ReadOnlySpan<double> kernelSpan = _kernel.AsSpan()[kernelOffset..];
|
||||
ReadOnlySpan<double> internalBuf = _buffer.InternalBuffer;
|
||||
|
||||
if (count < _period)
|
||||
{
|
||||
result = internalBuf.Slice(0, count).DotProduct(kernelSpan);
|
||||
result = internalBuf[..count].DotProduct(kernelSpan);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Full: data is split at StartIndex (which points to oldest)
|
||||
int head = _buffer.StartIndex;
|
||||
int part1Len = _period - head;
|
||||
result = internalBuf.Slice(head, part1Len).DotProduct(kernelSpan.Slice(0, part1Len))
|
||||
+ internalBuf.Slice(0, head).DotProduct(kernelSpan.Slice(part1Len));
|
||||
result = internalBuf.Slice(head, part1Len).DotProduct(kernelSpan[..part1Len])
|
||||
+ internalBuf[..head].DotProduct(kernelSpan[part1Len..]);
|
||||
}
|
||||
}
|
||||
|
||||
Last = new TValue(input.Time, result);
|
||||
Pub?.Invoke(Last);
|
||||
PubEvent(Last);
|
||||
return Last;
|
||||
}
|
||||
|
||||
public TSeries Update(TSeries source)
|
||||
public override TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return [];
|
||||
|
||||
@@ -130,7 +128,7 @@ public sealed class Conv : ITValuePublisher
|
||||
source.Times.CopyTo(tSpan);
|
||||
var sourceValues = source.Values;
|
||||
|
||||
Calculate(sourceValues, vSpan, _kernel);
|
||||
Batch(sourceValues, vSpan, _kernel);
|
||||
|
||||
// Restore state
|
||||
// We need to replay the last few updates to restore _buffer and _lastValidValue
|
||||
@@ -172,14 +170,22 @@ public sealed class Conv : ITValuePublisher
|
||||
return new TSeries(t, v);
|
||||
}
|
||||
|
||||
public static TSeries Calculate(TSeries source, double[] kernel)
|
||||
public override void Prime(ReadOnlySpan<double> source)
|
||||
{
|
||||
foreach (var value in source)
|
||||
{
|
||||
Update(new TValue(DateTime.MinValue, value));
|
||||
}
|
||||
}
|
||||
|
||||
public static TSeries Batch(TSeries source, double[] kernel)
|
||||
{
|
||||
var conv = new Conv(kernel);
|
||||
return conv.Update(source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, double[] kernel)
|
||||
public static void Batch(ReadOnlySpan<double> source, Span<double> output, double[] kernel)
|
||||
{
|
||||
if (source.Length != output.Length)
|
||||
throw new ArgumentException("Source and output must have the same length");
|
||||
@@ -223,21 +229,21 @@ public sealed class Conv : ITValuePublisher
|
||||
{
|
||||
int kernelOffset = period - count;
|
||||
// Window is [0..count-1]
|
||||
sum = window.Slice(0, count).DotProduct(kernelSpan.Slice(kernelOffset));
|
||||
sum = window[..count].DotProduct(kernelSpan[kernelOffset..]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Full buffer - branchless version
|
||||
int part1Len = period - windowIdx;
|
||||
sum = window.Slice(windowIdx, part1Len).DotProduct(kernelSpan.Slice(0, part1Len))
|
||||
+ window.Slice(0, windowIdx).DotProduct(kernelSpan.Slice(part1Len));
|
||||
sum = window.Slice(windowIdx, part1Len).DotProduct(kernelSpan[..part1Len])
|
||||
+ window[..windowIdx].DotProduct(kernelSpan[part1Len..]);
|
||||
}
|
||||
|
||||
output[i] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
public override void Reset()
|
||||
{
|
||||
_buffer.Clear();
|
||||
_state.LastValidValue = double.NaN;
|
||||
|
||||
@@ -55,7 +55,7 @@ double[] weights = { 0.1, 0.2, 0.3, 0.4 };
|
||||
ReadOnlySpan<double> input = ...;
|
||||
Span<double> output = new double[input.Length];
|
||||
|
||||
Conv.Calculate(input, output, weights);
|
||||
Conv.Batch(input, output, weights);
|
||||
```
|
||||
|
||||
### Bar Correction
|
||||
|
||||
Reference in New Issue
Block a user