mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 21:48:03 +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:
@@ -166,7 +166,7 @@ public class BilateralIndicatorTests
|
||||
indicator.Period = 20;
|
||||
indicator.SigmaSRatio = 1.0;
|
||||
indicator.SigmaRMult = 2.0;
|
||||
|
||||
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(1.0, indicator.SigmaSRatio);
|
||||
Assert.Equal(2.0, indicator.SigmaRMult);
|
||||
|
||||
@@ -56,7 +56,7 @@ public class BilateralIndicator : Indicator, IWatchlistIndicator
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
|
||||
|
||||
|
||||
TValue result = _bilateral!.Update(new TValue(item.TimeLeft.Ticks, _priceSelector!(item)), isNew: args.IsNewBar());
|
||||
|
||||
Series!.SetValue(result.Value, _bilateral.IsHot, ShowColdValues);
|
||||
|
||||
@@ -23,13 +23,13 @@ public class BilateralTests
|
||||
public void IsHot_BecomesTrueWhenBufferFull()
|
||||
{
|
||||
var indicator = new Bilateral(3);
|
||||
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 1));
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 2));
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 3));
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
@@ -42,13 +42,13 @@ public class BilateralTests
|
||||
// If sigma_r is high, range weights are ~1.
|
||||
// If sigma_s is high, spatial weights are ~1.
|
||||
// Then it becomes a simple average.
|
||||
|
||||
|
||||
var indicator = new Bilateral(3, sigmaSRatio: 100, sigmaRMult: 100);
|
||||
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 1));
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 2));
|
||||
var result = indicator.Update(new TValue(DateTime.UtcNow, 3));
|
||||
|
||||
|
||||
// Expected: (1+2+3)/3 = 2
|
||||
Assert.Equal(2.0, result.Value, 1);
|
||||
}
|
||||
@@ -57,11 +57,11 @@ public class BilateralTests
|
||||
public void Update_HandlesNaN()
|
||||
{
|
||||
var indicator = new Bilateral(3);
|
||||
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 1));
|
||||
indicator.Update(new TValue(DateTime.UtcNow, double.NaN)); // Should use 1
|
||||
var result = indicator.Update(new TValue(DateTime.UtcNow, 3));
|
||||
|
||||
|
||||
// Buffer: [1, 1, 3]
|
||||
// StDev of [1, 1, 3]: Mean=1.66, Var=((1-1.66)^2 + (1-1.66)^2 + (3-1.66)^2)/3 = (0.44 + 0.44 + 1.77)/3 = 0.88. StDev ~ 0.94
|
||||
// Calculation will proceed with these values.
|
||||
@@ -73,23 +73,23 @@ public class BilateralTests
|
||||
public void Update_IsNew_False_UpdatesCorrectly()
|
||||
{
|
||||
var indicator = new Bilateral(3);
|
||||
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 1));
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 2));
|
||||
|
||||
|
||||
// Update with 3, isNew=true
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 3), isNew: true);
|
||||
|
||||
|
||||
// Update with 4, isNew=false (correction)
|
||||
var res2 = indicator.Update(new TValue(DateTime.UtcNow, 4), isNew: false);
|
||||
|
||||
|
||||
// Verify state was updated
|
||||
// If we had updated with 4 directly: [1, 2, 4]
|
||||
var indicator2 = new Bilateral(3);
|
||||
indicator2.Update(new TValue(DateTime.UtcNow, 1));
|
||||
indicator2.Update(new TValue(DateTime.UtcNow, 2));
|
||||
var resExpected = indicator2.Update(new TValue(DateTime.UtcNow, 4));
|
||||
|
||||
|
||||
Assert.Equal(resExpected.Value, res2.Value);
|
||||
}
|
||||
|
||||
@@ -100,9 +100,9 @@ public class BilateralTests
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 1));
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 2));
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 3));
|
||||
|
||||
|
||||
indicator.Reset();
|
||||
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
Assert.Equal(1, indicator.Update(new TValue(DateTime.UtcNow, 1)).Value); // Center val 1, weights 0? No, center val is returned if weights 0.
|
||||
}
|
||||
@@ -112,10 +112,10 @@ public class BilateralTests
|
||||
{
|
||||
// Test edge case: calling Update with isNew:false before any isNew:true
|
||||
var indicator = new Bilateral(3);
|
||||
|
||||
|
||||
// This should not crash - buffer is empty, so we treat it as first value
|
||||
var result = indicator.Update(new TValue(DateTime.UtcNow, 5.0), isNew: false);
|
||||
|
||||
|
||||
// Should have added the value to the buffer
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
Assert.Equal(5.0, result.Value); // Single value, so result is that value
|
||||
@@ -126,18 +126,18 @@ public class BilateralTests
|
||||
{
|
||||
// Test edge case: calling Update with isNew:false after Reset
|
||||
var indicator = new Bilateral(3);
|
||||
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 1));
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 2));
|
||||
indicator.Reset();
|
||||
|
||||
|
||||
// Buffer is now empty, isNew:false should not crash
|
||||
var result = indicator.Update(new TValue(DateTime.UtcNow, 7.0), isNew: false);
|
||||
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
Assert.Equal(7.0, result.Value);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResult()
|
||||
{
|
||||
|
||||
@@ -115,12 +115,12 @@ public sealed class BilateralValidationTests : IDisposable
|
||||
{
|
||||
var reference = new BilateralReference(period, sigmaSRatio, sigmaRMult);
|
||||
var results = new List<double>();
|
||||
|
||||
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
results.Add(reference.Update(item.Value));
|
||||
}
|
||||
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ public sealed class BilateralValidationTests : IDisposable
|
||||
if (_history.Count == 0) return double.NaN;
|
||||
|
||||
double sigmaS = Math.Max(_length * _sigmaSRatio, 1e-10);
|
||||
|
||||
|
||||
// Calculate StDev of current window
|
||||
double stdev = CalculateStDev(_history);
|
||||
double sigmaR = Math.Max(stdev * _sigmaRMult, 1e-10);
|
||||
@@ -161,18 +161,18 @@ public sealed class BilateralValidationTests : IDisposable
|
||||
// Iterate through history
|
||||
// i=0 is newest (index Count-1)
|
||||
int loopLen = _history.Count;
|
||||
|
||||
|
||||
for (int i = 0; i < loopLen; i++)
|
||||
{
|
||||
double valI = _history[_history.Count - 1 - i];
|
||||
double diffSpatial = i;
|
||||
double diffRange = centerVal - valI;
|
||||
|
||||
|
||||
double weightSpatial = Math.Exp(-(diffSpatial * diffSpatial) / (2.0 * sigmaS * sigmaS));
|
||||
double weightRange = Math.Exp(-(diffRange * diffRange) / (2.0 * sigmaR * sigmaR));
|
||||
|
||||
|
||||
double weight = weightSpatial * weightRange;
|
||||
|
||||
|
||||
sumWeights += weight;
|
||||
sumWeightedSrc += weight * valI;
|
||||
}
|
||||
@@ -183,7 +183,7 @@ public sealed class BilateralValidationTests : IDisposable
|
||||
private static double CalculateStDev(IReadOnlyList<double> values)
|
||||
{
|
||||
if (values.Count < 2) return 0;
|
||||
|
||||
|
||||
double avg = values.Average();
|
||||
double sumSqDiff = values.Sum(d => (d - avg) * (d - avg));
|
||||
// Population StDev to match implementation
|
||||
|
||||
@@ -57,7 +57,7 @@ public sealed class Bilateral : AbstractBase
|
||||
PrecalculateSpatialWeights();
|
||||
}
|
||||
|
||||
public Bilateral(ITValuePublisher source, int period, double sigmaSRatio = 0.5, double sigmaRMult = 1.0)
|
||||
public Bilateral(ITValuePublisher source, int period, double sigmaSRatio = 0.5, double sigmaRMult = 1.0)
|
||||
: this(period, sigmaSRatio, sigmaRMult)
|
||||
{
|
||||
source.Pub += _handler;
|
||||
@@ -139,7 +139,7 @@ public sealed class Bilateral : AbstractBase
|
||||
Update(new TValue(source.Times[i], source.Values[i]));
|
||||
vSpan[i] = Last.Value;
|
||||
}
|
||||
|
||||
|
||||
return new TSeries(t, v);
|
||||
}
|
||||
|
||||
@@ -149,10 +149,10 @@ public sealed class Bilateral : AbstractBase
|
||||
if (isNew)
|
||||
{
|
||||
_p_state = _state;
|
||||
|
||||
|
||||
double val = GetValidValue(input.Value);
|
||||
double removed = _buffer.Add(val);
|
||||
|
||||
|
||||
_state.SumSq += (val * val);
|
||||
if (_buffer.IsFull)
|
||||
{
|
||||
@@ -163,12 +163,12 @@ public sealed class Bilateral : AbstractBase
|
||||
{
|
||||
// Preserve SumSq as it tracks the buffer which is already at T
|
||||
double currentSumSq = _state.SumSq;
|
||||
|
||||
|
||||
_state = _p_state;
|
||||
_state.SumSq = currentSumSq;
|
||||
|
||||
|
||||
double val = GetValidValue(input.Value);
|
||||
|
||||
|
||||
// Defensive check: if buffer is empty, treat as first value
|
||||
if (_buffer.Count == 0)
|
||||
{
|
||||
@@ -179,7 +179,7 @@ public sealed class Bilateral : AbstractBase
|
||||
{
|
||||
double oldNewest = _buffer.Newest; // Get current newest before overwriting
|
||||
_buffer.UpdateNewest(val);
|
||||
|
||||
|
||||
_state.SumSq -= (oldNewest * oldNewest);
|
||||
_state.SumSq += (val * val);
|
||||
}
|
||||
@@ -210,7 +210,7 @@ public sealed class Bilateral : AbstractBase
|
||||
// Calculate StDev
|
||||
double count = _buffer.Count;
|
||||
double sum = _buffer.Sum;
|
||||
|
||||
|
||||
// Variance = (SumSq - (Sum*Sum)/N) / N
|
||||
// Use Math.Max(0, ...) to handle potential floating point negative zero
|
||||
double variance = Math.Max(0, (_state.SumSq - (sum * sum) / count) / count);
|
||||
@@ -226,12 +226,12 @@ public sealed class Bilateral : AbstractBase
|
||||
// Iterate from 0 to Count-1
|
||||
// i=0 corresponds to Newest (src[0])
|
||||
// i corresponds to buffer[Count - 1 - i]
|
||||
|
||||
|
||||
// Use InternalBuffer to avoid allocations from GetSpan() when wrapped
|
||||
ReadOnlySpan<double> buffer = _buffer.InternalBuffer;
|
||||
int capacity = _buffer.Capacity;
|
||||
int startIndex = _buffer.StartIndex;
|
||||
|
||||
|
||||
// Newest element index
|
||||
int newestIndex = (startIndex + (int)count - 1) % capacity;
|
||||
|
||||
@@ -241,16 +241,16 @@ public sealed class Bilateral : AbstractBase
|
||||
// (newestIndex - i) handling wrap-around
|
||||
int idx = newestIndex - i;
|
||||
if (idx < 0) idx += capacity;
|
||||
|
||||
|
||||
double val = buffer[idx];
|
||||
double diffRange = centerVal - val;
|
||||
|
||||
|
||||
// weight_spatial = _spatialWeights[i]
|
||||
// weight_range = exp(-(diff^2) / (2 * sigma_r^2))
|
||||
|
||||
|
||||
double weightRange = Math.Exp(-(diffRange * diffRange) / twoSigmaRSq);
|
||||
double weight = _spatialWeights[i] * weightRange;
|
||||
|
||||
|
||||
sumWeights += weight;
|
||||
sumWeightedSrc += weight * val;
|
||||
}
|
||||
@@ -284,7 +284,7 @@ public sealed class Bilateral : AbstractBase
|
||||
|
||||
if (destination.Length < source.Length)
|
||||
throw new ArgumentException("Destination must have length >= source length", nameof(destination));
|
||||
|
||||
|
||||
// Precalculate spatial weights
|
||||
double sigmaS = Math.Max(period * sigmaSRatio, 1e-10);
|
||||
double twoSigmaSSq = 2.0 * sigmaS * sigmaS;
|
||||
@@ -306,7 +306,7 @@ public sealed class Bilateral : AbstractBase
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// If all NaNs, fill with NaN
|
||||
if (double.IsNaN(lastValid))
|
||||
{
|
||||
@@ -340,11 +340,11 @@ public sealed class Bilateral : AbstractBase
|
||||
sum -= removed;
|
||||
sumSq -= removed * removed;
|
||||
}
|
||||
|
||||
|
||||
window[windowIdx] = val;
|
||||
sum += val;
|
||||
sumSq += val * val;
|
||||
|
||||
|
||||
int currentNewestIdx = windowIdx;
|
||||
windowIdx = (windowIdx + 1) % period;
|
||||
if (count < period) count++;
|
||||
@@ -367,13 +367,13 @@ public sealed class Bilateral : AbstractBase
|
||||
// k=1 is previous...
|
||||
int idx = currentNewestIdx - k;
|
||||
if (idx < 0) idx += period;
|
||||
|
||||
|
||||
double wVal = window[idx];
|
||||
double diffRange = centerVal - wVal;
|
||||
|
||||
|
||||
double weightRange = Math.Exp(-(diffRange * diffRange) / twoSigmaRSq);
|
||||
double weight = spatialWeights[k] * weightRange;
|
||||
|
||||
|
||||
sumWeights += weight;
|
||||
sumWeightedSrc += weight * wVal;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user