mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 22:08:05 +00:00
Refactor code formatting and improve consistency across various test files
- Removed unnecessary blank lines in multiple test files to enhance readability. - Ensured consistent spacing and formatting in the `Trima`, `Usf`, `Vidya`, `Wma`, and `Atr` test classes. - Updated comments for clarity and consistency in the `Atr` and `Adl` classes. - Adjusted project files for better structure and maintainability.
This commit is contained in:
@@ -50,7 +50,7 @@ public class BlmaIndicator : Indicator, IWatchlistIndicator
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
|
||||
|
||||
|
||||
TValue result = _ma!.Update(new TValue(item.TimeLeft.Ticks, _priceSelector!(item)), isNew: args.IsNewBar());
|
||||
|
||||
_series!.SetValue(result.Value, _ma.IsHot, ShowColdValues);
|
||||
|
||||
@@ -33,15 +33,15 @@ public class BlmaTests
|
||||
{
|
||||
var blma = new Blma(3);
|
||||
var input = new[] { 10.0, 20.0, 30.0 };
|
||||
|
||||
|
||||
// Bar 1: Count=1. Weights for n=1: [1]. Result = 10.
|
||||
var r1 = blma.Update(new TValue(DateTime.UtcNow, input[0]));
|
||||
Assert.Equal(10.0, r1.Value);
|
||||
|
||||
|
||||
// Bar 2: Count=2. Weights for n=2 sum to 0. Fallback to average: (10+20)/2 = 15.
|
||||
var r2 = blma.Update(new TValue(DateTime.UtcNow, input[1]));
|
||||
Assert.Equal(15.0, r2.Value);
|
||||
|
||||
|
||||
// Bar 3: Count=3. Weights [0, 1, 0]. Sum=1. Result=20.
|
||||
var r3 = blma.Update(new TValue(DateTime.UtcNow, input[2]));
|
||||
Assert.Equal(20.0, r3.Value, 1e-6);
|
||||
@@ -82,45 +82,45 @@ public class BlmaTests
|
||||
public void NaN_Handling()
|
||||
{
|
||||
var blma = new Blma(5);
|
||||
|
||||
|
||||
blma.Update(new TValue(DateTime.UtcNow, 10));
|
||||
blma.Update(new TValue(DateTime.UtcNow, 20));
|
||||
// For N=2, weights sum to 0. Fallback to average: (10+20)/2 = 15.
|
||||
|
||||
|
||||
var result = blma.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
|
||||
Assert.Equal(15.0, result.Value); // Should return last valid value
|
||||
Assert.Equal(15.0, blma.Last.Value); // Should retain last valid value
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void IsNew_Behavior()
|
||||
{
|
||||
var blma = new Blma(3);
|
||||
|
||||
|
||||
// Bar 1
|
||||
blma.Update(new TValue(DateTime.UtcNow, 10), isNew: true);
|
||||
|
||||
|
||||
// Bar 2
|
||||
blma.Update(new TValue(DateTime.UtcNow, 20), isNew: true);
|
||||
|
||||
|
||||
// Bar 3 (Update)
|
||||
blma.Update(new TValue(DateTime.UtcNow, 30), isNew: true);
|
||||
var val1 = blma.Last.Value;
|
||||
|
||||
|
||||
// Bar 3 (Correction)
|
||||
blma.Update(new TValue(DateTime.UtcNow, 40), isNew: false);
|
||||
var val2 = blma.Last.Value;
|
||||
|
||||
|
||||
// For Blackman window, the newest value (index N-1) has weight 0.
|
||||
// So changing the newest value does NOT change the current result.
|
||||
Assert.Equal(val1, val2);
|
||||
|
||||
|
||||
// However, the internal buffer MUST be updated.
|
||||
// Case A: Bar 3 = 40 (current state)
|
||||
blma.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
var valWith40 = blma.Last.Value;
|
||||
|
||||
|
||||
// Case B: Reconstruct scenario with Bar 3 = 30
|
||||
var blma2 = new Blma(3);
|
||||
blma2.Update(new TValue(DateTime.UtcNow, 10), isNew: true);
|
||||
@@ -128,7 +128,7 @@ public class BlmaTests
|
||||
blma2.Update(new TValue(DateTime.UtcNow, 30), isNew: true);
|
||||
blma2.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
var valWith30 = blma2.Last.Value;
|
||||
|
||||
|
||||
Assert.NotEqual(valWith30, valWith40);
|
||||
}
|
||||
|
||||
@@ -138,12 +138,12 @@ public class BlmaTests
|
||||
var blma = new Blma(5);
|
||||
var input = new double[] { 1, 2, 3, 4, 5 };
|
||||
var timestamps = new List<DateTime>();
|
||||
|
||||
blma.Pub += (object? sender, TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Pub += (object? sender, in TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Prime(input);
|
||||
|
||||
|
||||
Assert.Equal(input.Length, timestamps.Count);
|
||||
// Verify timestamps are unique and increasing
|
||||
for (int i = 1; i < timestamps.Count; i++)
|
||||
@@ -157,19 +157,19 @@ public class BlmaTests
|
||||
{
|
||||
var blma = new Blma(5);
|
||||
var now = DateTime.UtcNow;
|
||||
TValue[] input =
|
||||
[
|
||||
new(now, 1),
|
||||
new(now.AddMinutes(1), 2),
|
||||
new(now.AddMinutes(2), 3)
|
||||
TValue[] input =
|
||||
[
|
||||
new(now, 1),
|
||||
new(now.AddMinutes(1), 2),
|
||||
new(now.AddMinutes(2), 3)
|
||||
];
|
||||
var timestamps = new List<DateTime>();
|
||||
|
||||
blma.Pub += (object? sender, TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Pub += (object? sender, in TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Prime(input);
|
||||
|
||||
|
||||
Assert.Equal(input.Length, timestamps.Count);
|
||||
Assert.Equal(input[0].AsDateTime, timestamps[0]);
|
||||
Assert.Equal(input[1].AsDateTime, timestamps[1]);
|
||||
|
||||
@@ -50,15 +50,15 @@ public class BlmaValidationTests
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
buffer.Add(source[i].Value);
|
||||
|
||||
|
||||
// PineScript logic:
|
||||
// int p = math.min(bar_index + 1, period)
|
||||
int p = Math.Min(buffer.Count, period);
|
||||
|
||||
|
||||
// Calculate weights
|
||||
var weights = new double[p];
|
||||
double totalWeight = 0;
|
||||
|
||||
|
||||
if (p == 1)
|
||||
{
|
||||
weights[0] = 1.0;
|
||||
@@ -88,15 +88,15 @@ public class BlmaValidationTests
|
||||
// float price = source[i] (where source[0] is newest)
|
||||
// float w = array.get(weights, i)
|
||||
// So weights[0] * newest, weights[1] * 2nd newest...
|
||||
|
||||
|
||||
// My C# buffer is chronological (0 is oldest).
|
||||
// So buffer[buffer.Count - 1] is newest.
|
||||
// buffer[buffer.Count - 1 - j] is j-th lag.
|
||||
|
||||
|
||||
// Wait, in Blma.cs I implemented:
|
||||
// sum += buffer[i] * weights[i] (where buffer[0] is oldest)
|
||||
// So weights[0] * oldest.
|
||||
|
||||
|
||||
// PineScript: weights[0] * newest.
|
||||
// Since Blackman window is symmetric, weights[0] == weights[p-1].
|
||||
// So weights[0] * newest == weights[p-1] * newest (if symmetric).
|
||||
@@ -111,11 +111,11 @@ public class BlmaValidationTests
|
||||
// cos(4pi * (1-r)) = cos(4pi - 4pi*r) = cos(4pi*r).
|
||||
// So yes, w(j) == w(p-1-j).
|
||||
// So applying weights[0] to newest or oldest doesn't matter for the sum.
|
||||
|
||||
|
||||
// However, I should match my implementation in Blma.cs.
|
||||
// In Blma.cs: sum += buffer[i] * weights[i] (buffer[0] is oldest).
|
||||
// So weights[0] * oldest.
|
||||
|
||||
|
||||
// In this reference implementation, let's do the same.
|
||||
// Use the last p elements of buffer.
|
||||
int start = buffer.Count - p;
|
||||
|
||||
@@ -29,7 +29,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
WarmupPeriod = period;
|
||||
_buffer = new RingBuffer(period);
|
||||
_weights = new double[period];
|
||||
|
||||
|
||||
// Pre-calculate weights for the full period
|
||||
_weightSum = CalculateWeights(period, _weights);
|
||||
_handler = Handle;
|
||||
@@ -102,7 +102,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
{
|
||||
Span<double> currentWeights = stackalloc double[count];
|
||||
double currentWeightSum = CalculateWeights(count, currentWeights);
|
||||
|
||||
|
||||
// Fallback for cases where weights sum to zero (e.g. N=2)
|
||||
result = Math.Abs(currentWeightSum) < double.Epsilon
|
||||
? _buffer.Average()
|
||||
@@ -130,7 +130,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
var result = new TSeries();
|
||||
Span<double> output = new double[source.Count];
|
||||
Calculate(source.Values, output, _period);
|
||||
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
result.Add(new TValue(source[i].Time, output[i]));
|
||||
@@ -182,7 +182,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
int start = buffer.StartIndex;
|
||||
int count = buffer.Count;
|
||||
int capacity = buffer.Capacity;
|
||||
|
||||
|
||||
if (start + count <= capacity)
|
||||
{
|
||||
return buffer.InternalBuffer.Slice(start, count).DotProduct(weights);
|
||||
@@ -219,7 +219,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
for (int i = 0; i < source.Length; i++)
|
||||
{
|
||||
int count = Math.Min(i + 1, period);
|
||||
|
||||
|
||||
if (count < period)
|
||||
{
|
||||
// Warmup: dynamic weights
|
||||
@@ -231,7 +231,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
{
|
||||
Span<double> currentWeights = warmupWeightsBuffer.Slice(0, count);
|
||||
double currentWeightSum = CalculateWeights(count, currentWeights);
|
||||
|
||||
|
||||
if (Math.Abs(currentWeightSum) < double.Epsilon)
|
||||
{
|
||||
// Fallback for zero sum weights (e.g. N=2)
|
||||
|
||||
Reference in New Issue
Block a user