style patterns

This commit is contained in:
Miha Kralj
2026-01-25 16:01:45 -08:00
parent 2836f253c4
commit e59665c8f0
399 changed files with 6892 additions and 1323 deletions
+5
View File
@@ -584,12 +584,17 @@ public class GBMTests
{
double mean = 0;
for (int i = 0; i < values.Length; i++)
{
mean += values[i];
}
mean /= values.Length;
double sumSquares = 0;
for (int i = 0; i < values.Length; i++)
{
sumSquares += (values[i] - mean) * (values[i] - mean);
}
return Math.Sqrt(sumSquares / values.Length);
}
+77 -18
View File
@@ -75,7 +75,10 @@ public static class ValidationHelper
double qValue = qSeries[i].Value;
double? sValue = selector(sSeries[i]);
if (!sValue.HasValue) continue;
if (!sValue.HasValue)
{
continue;
}
Assert.True(
Math.Abs(qValue - sValue.Value) <= tolerance,
@@ -103,7 +106,10 @@ public static class ValidationHelper
double qValue = qResults[i];
double? sValue = selector(sSeries[i]);
if (!sValue.HasValue) continue;
if (!sValue.HasValue)
{
continue;
}
Assert.True(
Math.Abs(qValue - sValue.Value) <= tolerance,
@@ -131,7 +137,10 @@ public static class ValidationHelper
double qValue = qOutput[i];
double? sValue = selector(sSeries[i]);
if (!sValue.HasValue) continue;
if (!sValue.HasValue)
{
continue;
}
Assert.True(
Math.Abs(qValue - sValue.Value) <= tolerance,
@@ -161,10 +170,16 @@ public static class ValidationHelper
{
double qValue = qSeries[i].Value;
if (i < lookback) continue;
if (i < lookback)
{
continue;
}
int tIndex = i - lookback;
if (tIndex >= tOutput.Length) continue;
if (tIndex >= tOutput.Length)
{
continue;
}
double tValue = tOutput[tIndex];
@@ -191,10 +206,16 @@ public static class ValidationHelper
{
double qValue = qResults[i];
if (i < lookback) continue;
if (i < lookback)
{
continue;
}
int tIndex = i - lookback;
if (tIndex >= tOutput.Length) continue;
if (tIndex >= tOutput.Length)
{
continue;
}
double tValue = tOutput[tIndex];
@@ -221,10 +242,16 @@ public static class ValidationHelper
{
double qValue = qOutput[i];
if (i < lookback) continue;
if (i < lookback)
{
continue;
}
int tIndex = i - lookback;
if (tIndex >= tOutput.Length) continue;
if (tIndex >= tOutput.Length)
{
continue;
}
double tValue = tOutput[tIndex];
@@ -253,10 +280,16 @@ public static class ValidationHelper
{
double qValue = qSeries[i].Value;
if (i < lookback) continue;
if (i < lookback)
{
continue;
}
int tIndex = i - offset;
if (tIndex < 0 || tIndex >= length) continue;
if (tIndex < 0 || tIndex >= length)
{
continue;
}
double tValue = tOutput[tIndex];
@@ -285,10 +318,16 @@ public static class ValidationHelper
{
double qValue = qResults[i];
if (i < lookback) continue;
if (i < lookback)
{
continue;
}
int tIndex = i - offset;
if (tIndex < 0 || tIndex >= length) continue;
if (tIndex < 0 || tIndex >= length)
{
continue;
}
double tValue = tOutput[tIndex];
@@ -317,10 +356,16 @@ public static class ValidationHelper
{
double qValue = qOutput[i];
if (i < lookback) continue;
if (i < lookback)
{
continue;
}
int tIndex = i - offset;
if (tIndex < 0 || tIndex >= length) continue;
if (tIndex < 0 || tIndex >= length)
{
continue;
}
double tValue = tOutput[tIndex];
@@ -388,18 +433,25 @@ public static class ValidationHelper
Func<TResult, double?> selector)
{
if (qSeries.Count != sSeries.Count)
{
throw new ArgumentException("Series must have the same count", nameof(sSeries));
}
double maxDiff = 0;
for (int i = 0; i < qSeries.Count; i++)
{
double? sValue = selector(sSeries[i]);
if (!sValue.HasValue) continue;
if (!sValue.HasValue)
{
continue;
}
double diff = Math.Abs(qSeries[i].Value - sValue.Value);
if (diff > maxDiff)
{
maxDiff = diff;
}
}
return maxDiff;
@@ -415,20 +467,27 @@ public static class ValidationHelper
Func<TResult, double?> selector)
{
if (qSeries.Count != sSeries.Count)
{
throw new ArgumentException("Series must have the same count", nameof(sSeries));
}
double maxDiff = 0;
for (int i = 0; i < qSeries.Count; i++)
{
double? sValue = selector(sSeries[i]);
if (!sValue.HasValue || Math.Abs(sValue.Value) < double.Epsilon) continue;
if (!sValue.HasValue || Math.Abs(sValue.Value) < double.Epsilon)
{
continue;
}
double relDiff = Math.Abs((qSeries[i].Value - sValue.Value) / sValue.Value);
if (relDiff > maxDiff)
{
maxDiff = relDiff;
}
}
return maxDiff;
}
}
}
+3 -1
View File
@@ -178,7 +178,9 @@ public sealed class ValidationTestData : IDisposable
public ValidationTestData CreateSubset(int count)
{
if (count <= 0 || count > Count)
{
throw new ArgumentOutOfRangeException(nameof(count), count, $"Count must be between 1 and {Count}");
}
return new ValidationTestData(count, DefaultStartPrice, DefaultMu, DefaultSigma, DefaultSeed);
}
@@ -216,4 +218,4 @@ public sealed class ValidationTestData : IDisposable
// No unmanaged resources to dispose
// Implemented for IDisposable pattern compatibility with test fixtures
}
}
}
+50 -6
View File
@@ -75,22 +75,30 @@ public sealed class GBM : IFeed
{
// Validate startPrice
if (startPrice <= 0 || !double.IsFinite(startPrice))
{
throw new ArgumentOutOfRangeException(nameof(startPrice), startPrice, "Start price must be positive and finite");
}
// Validate mu
if (!double.IsFinite(mu))
{
throw new ArgumentOutOfRangeException(nameof(mu), mu, "Drift (mu) must be finite");
}
// Validate sigma
if (sigma < 0 || !double.IsFinite(sigma))
{
throw new ArgumentOutOfRangeException(nameof(sigma), sigma, "Volatility (sigma) must be non-negative and finite");
}
// Use provided timeframe or default to 1 minute
var timeframe = defaultTimeframe ?? TimeSpan.FromMinutes(1);
// Validate timeframe
if (timeframe <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(defaultTimeframe), defaultTimeframe, "Timeframe must be positive");
}
_rnd = seed.HasValue ? new Random(seed.Value) : null;
StartPrice = startPrice;
@@ -170,7 +178,9 @@ public sealed class GBM : IFeed
// Guard against log(0) which produces -Infinity
if (u1 <= double.Epsilon)
{
u1 = double.Epsilon;
}
double mag = Math.Sqrt(-2.0 * Math.Log(u1));
double angle = 2.0 * Math.PI * u2;
@@ -200,7 +210,9 @@ public sealed class GBM : IFeed
// Ensure price stays positive and finite
if (!double.IsFinite(price) || price <= 0)
{
price = _lastPrice;
}
double volume = 1000 + NextDouble() * 1000;
@@ -232,7 +244,9 @@ public sealed class GBM : IFeed
// Ensure price stays positive and finite
if (!double.IsFinite(price) || price <= 0)
{
price = _lastPrice;
}
double additionalVolume = 1000 + NextDouble() * 1000;
@@ -274,9 +288,14 @@ public sealed class GBM : IFeed
public TBarSeries Fetch(int count, long startTime, TimeSpan interval)
{
if (count <= 0)
{
throw new ArgumentException("Count must be positive", nameof(count));
}
if (interval <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(interval), interval, "Interval must be positive");
}
var series = new TBarSeries(count);
@@ -333,12 +352,35 @@ public sealed class GBM : IFeed
}
finally
{
if (rentedT != null) ArrayPool<long>.Shared.Return(rentedT);
if (rentedO != null) ArrayPool<double>.Shared.Return(rentedO);
if (rentedH != null) ArrayPool<double>.Shared.Return(rentedH);
if (rentedL != null) ArrayPool<double>.Shared.Return(rentedL);
if (rentedC != null) ArrayPool<double>.Shared.Return(rentedC);
if (rentedV != null) ArrayPool<double>.Shared.Return(rentedV);
if (rentedT != null)
{
ArrayPool<long>.Shared.Return(rentedT);
}
if (rentedO != null)
{
ArrayPool<double>.Shared.Return(rentedO);
}
if (rentedH != null)
{
ArrayPool<double>.Shared.Return(rentedH);
}
if (rentedL != null)
{
ArrayPool<double>.Shared.Return(rentedL);
}
if (rentedC != null)
{
ArrayPool<double>.Shared.Return(rentedC);
}
if (rentedV != null)
{
ArrayPool<double>.Shared.Return(rentedV);
}
}
}
@@ -371,7 +413,9 @@ public sealed class GBM : IFeed
// Ensure price stays positive and finite
if (!double.IsFinite(price) || price <= 0)
{
price = currentPrice;
}
double open = currentPrice;
double close = price;