mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-15 17:18:05 +00:00
style patterns
This commit is contained in:
@@ -43,7 +43,10 @@ public class NormalizeIndicator : Indicator, IWatchlistIndicator
|
||||
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
if (_normalize == null || _selector == null) return;
|
||||
if (_normalize == null || _selector == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var item = HistoricalData[0, SeekOriginHistory.End];
|
||||
double value = _selector(item);
|
||||
|
||||
@@ -152,7 +152,9 @@ public class NormalizeTests
|
||||
var norm = new Normalize(5);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
norm.Update(new TValue(DateTime.UtcNow, i * 10));
|
||||
}
|
||||
|
||||
Assert.True(norm.IsHot);
|
||||
|
||||
@@ -198,7 +200,9 @@ public class NormalizeTests
|
||||
var series = _gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var tseries = new TSeries();
|
||||
foreach (var bar in series)
|
||||
{
|
||||
tseries.Add(new TValue(bar.Time, bar.Close), true);
|
||||
}
|
||||
|
||||
// Static calculation
|
||||
var staticResult = Normalize.Calculate(tseries, 14);
|
||||
@@ -207,7 +211,9 @@ public class NormalizeTests
|
||||
var streamNorm = new Normalize(14);
|
||||
var streamResult = new TSeries();
|
||||
foreach (var bar in series)
|
||||
{
|
||||
streamResult.Add(streamNorm.Update(new TValue(bar.Time, bar.Close)), true);
|
||||
}
|
||||
|
||||
// Compare last 50 values
|
||||
for (int i = 50; i < 100; i++)
|
||||
@@ -261,4 +267,4 @@ public class NormalizeTests
|
||||
var result = norm.Update(new TValue(DateTime.UtcNow, 60));
|
||||
Assert.Equal(0.2, result.Value, 1e-10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,9 @@ public class NormalizeValidationTests
|
||||
double[] values = { 10, 20, 30, 40, 50 };
|
||||
|
||||
foreach (var v in values)
|
||||
{
|
||||
norm.Update(new TValue(DateTime.UtcNow, v));
|
||||
}
|
||||
|
||||
// Max value (50) should normalize to 1.0
|
||||
Assert.Equal(1.0, norm.Last.Value, 1e-10);
|
||||
@@ -55,7 +57,9 @@ public class NormalizeValidationTests
|
||||
double[] values = { 50, 40, 30, 20, 10 };
|
||||
|
||||
foreach (var v in values)
|
||||
{
|
||||
norm.Update(new TValue(DateTime.UtcNow, v));
|
||||
}
|
||||
|
||||
// Min value (10) should normalize to 0.0
|
||||
Assert.Equal(0.0, norm.Last.Value, 1e-10);
|
||||
@@ -98,7 +102,9 @@ public class NormalizeValidationTests
|
||||
|
||||
// All same values
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
norm.Update(new TValue(DateTime.UtcNow, 42.0));
|
||||
}
|
||||
|
||||
// Flat range: should return 0.5
|
||||
Assert.Equal(0.5, norm.Last.Value, 1e-10);
|
||||
@@ -223,7 +229,10 @@ public class NormalizeValidationTests
|
||||
// Mode 2: Batch via Update(TSeries)
|
||||
var tseries = new TSeries();
|
||||
foreach (var bar in series)
|
||||
{
|
||||
tseries.Add(new TValue(bar.Time, bar.Close), true);
|
||||
}
|
||||
|
||||
var results2 = Normalize.Calculate(tseries, period);
|
||||
|
||||
// Mode 3: Static span Calculate
|
||||
@@ -235,7 +244,10 @@ public class NormalizeValidationTests
|
||||
var source = new TSeries();
|
||||
var norm4 = new Normalize(source, period);
|
||||
foreach (var bar in series)
|
||||
{
|
||||
source.Add(new TValue(bar.Time, bar.Close), true);
|
||||
}
|
||||
|
||||
var results4 = norm4.Last.Value;
|
||||
|
||||
// Compare all modes (use last 50 values for stability)
|
||||
@@ -300,4 +312,4 @@ public class NormalizeValidationTests
|
||||
|
||||
Assert.True(norm.IsHot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,9 @@ public sealed class Normalize : AbstractBase
|
||||
public Normalize(int period = 14)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentException("Period must be >= 1", nameof(period));
|
||||
}
|
||||
|
||||
_period = period;
|
||||
_buffer = new RingBuffer(period);
|
||||
@@ -64,7 +66,9 @@ public sealed class Normalize : AbstractBase
|
||||
private static (double min, double max) FindMinMax(ReadOnlySpan<double> values)
|
||||
{
|
||||
if (values.Length == 0)
|
||||
{
|
||||
return (double.MaxValue, double.MinValue);
|
||||
}
|
||||
|
||||
double min = values[0];
|
||||
double max = values[0];
|
||||
@@ -72,8 +76,15 @@ public sealed class Normalize : AbstractBase
|
||||
for (int i = 1; i < values.Length; i++)
|
||||
{
|
||||
double v = values[i];
|
||||
if (v < min) min = v;
|
||||
if (v > max) max = v;
|
||||
if (v < min)
|
||||
{
|
||||
min = v;
|
||||
}
|
||||
|
||||
if (v > max)
|
||||
{
|
||||
max = v;
|
||||
}
|
||||
}
|
||||
|
||||
return (min, max);
|
||||
@@ -83,9 +94,13 @@ public sealed class Normalize : AbstractBase
|
||||
public override TValue Update(TValue input, bool isNew = true)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_p_state = _state;
|
||||
}
|
||||
else
|
||||
{
|
||||
_state = _p_state;
|
||||
}
|
||||
|
||||
double value = input.Value;
|
||||
double result;
|
||||
@@ -151,11 +166,19 @@ public sealed class Normalize : AbstractBase
|
||||
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period = 14)
|
||||
{
|
||||
if (source.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("Source cannot be empty", nameof(source));
|
||||
}
|
||||
|
||||
if (output.Length < source.Length)
|
||||
{
|
||||
throw new ArgumentException("Output length must be >= source length", nameof(output));
|
||||
}
|
||||
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentException("Period must be >= 1", nameof(period));
|
||||
}
|
||||
|
||||
double lastValid = 0.5;
|
||||
|
||||
@@ -181,8 +204,15 @@ public sealed class Normalize : AbstractBase
|
||||
double v = source[j];
|
||||
if (double.IsFinite(v))
|
||||
{
|
||||
if (v < min) min = v;
|
||||
if (v > max) max = v;
|
||||
if (v < min)
|
||||
{
|
||||
min = v;
|
||||
}
|
||||
|
||||
if (v > max)
|
||||
{
|
||||
max = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user