mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 13:08:04 +00:00
style patterns
This commit is contained in:
@@ -199,7 +199,7 @@ public class DsmaIndicatorTests
|
||||
|
||||
// Process first bar
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
|
||||
// Process second bar as new
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
double afterNewBar = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
@@ -232,7 +232,7 @@ public class DsmaTests
|
||||
|
||||
// Get the last bar again after loop
|
||||
lastBar = gbm.Next(isNew: false);
|
||||
|
||||
|
||||
// Inject NaN
|
||||
var nanResult = dsma.Update(new TValue(lastBar.Time, double.NaN));
|
||||
|
||||
@@ -257,7 +257,7 @@ public class DsmaTests
|
||||
|
||||
// Get the last bar again after loop
|
||||
lastBar = gbm.Next(isNew: false);
|
||||
|
||||
|
||||
// Inject Infinity
|
||||
var infResult = dsma.Update(new TValue(lastBar.Time, double.PositiveInfinity));
|
||||
var negInfResult = dsma.Update(new TValue(lastBar.Time, double.NegativeInfinity));
|
||||
|
||||
@@ -58,7 +58,7 @@ public class DsmaValidationTests
|
||||
}
|
||||
|
||||
// In higher volatility, absolute deviation should generally be larger
|
||||
Assert.True(highVolDeviation > lowVolDeviation * 2,
|
||||
Assert.True(highVolDeviation > lowVolDeviation * 2,
|
||||
$"High volatility deviation {highVolDeviation:F2} should be significantly larger than low volatility {lowVolDeviation:F2}");
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ public class DsmaValidationTests
|
||||
double avgHighLag = highScaleLag / count;
|
||||
|
||||
// Lower scale factor should have higher average lag (smoother, less responsive)
|
||||
Assert.True(avgLowLag > avgHighLag,
|
||||
Assert.True(avgLowLag > avgHighLag,
|
||||
$"Low scale lag {avgLowLag:F4} should be greater than high scale lag {avgHighLag:F4}");
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ public class DsmaValidationTests
|
||||
double dsmaVariance = dsmaChanges.Average();
|
||||
|
||||
// DSMA should have lower variance than raw price
|
||||
Assert.True(dsmaVariance < priceVariance,
|
||||
Assert.True(dsmaVariance < priceVariance,
|
||||
$"DSMA variance {dsmaVariance:F4} should be less than price variance {priceVariance:F4}");
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ public class DsmaValidationTests
|
||||
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.01, sigma: 0.2, seed: 654);
|
||||
var series = new TSeries();
|
||||
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
@@ -254,7 +254,7 @@ public class DsmaValidationTests
|
||||
double avgLongLag = longLag / count;
|
||||
|
||||
// Longer period should have higher average lag (more smoothing)
|
||||
Assert.True(avgLongLag > avgShortLag,
|
||||
Assert.True(avgLongLag > avgShortLag,
|
||||
$"Long period lag {avgLongLag:F4} should be greater than short period lag {avgShortLag:F4}");
|
||||
}
|
||||
|
||||
@@ -319,8 +319,15 @@ public class DsmaValidationTests
|
||||
|
||||
for (int i = 1; i < prices.Count; i++)
|
||||
{
|
||||
if (prices[i] > prices[i - 1]) priceUpCount++;
|
||||
if (dsmaValues[i] > dsmaValues[i - 1]) dsmaUpCount++;
|
||||
if (prices[i] > prices[i - 1])
|
||||
{
|
||||
priceUpCount++;
|
||||
}
|
||||
|
||||
if (dsmaValues[i] > dsmaValues[i - 1])
|
||||
{
|
||||
dsmaUpCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// DSMA should have similar directional trend but smoother
|
||||
|
||||
@@ -89,9 +89,14 @@ public sealed class Dsma : AbstractBase
|
||||
public Dsma(int period, double scaleFactor = 0.5)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be >= 2.");
|
||||
}
|
||||
|
||||
if (scaleFactor < 0.01 || scaleFactor > 0.9)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(scaleFactor), "Scale factor must be between 0.01 and 0.9.");
|
||||
}
|
||||
|
||||
WarmupPeriod = period;
|
||||
_periodRecip = 1.0 / period;
|
||||
@@ -146,12 +151,16 @@ public sealed class Dsma : AbstractBase
|
||||
HandleStateSnapshot(isNew);
|
||||
value = HandleInvalidInput(value);
|
||||
if (double.IsNaN(value))
|
||||
{
|
||||
return double.NaN;
|
||||
}
|
||||
|
||||
_state.Bars++;
|
||||
|
||||
if (_state.Bars == 1)
|
||||
{
|
||||
return InitializeFirstBar(value);
|
||||
}
|
||||
|
||||
return CalculateDsma(value);
|
||||
}
|
||||
@@ -256,7 +265,10 @@ public sealed class Dsma : AbstractBase
|
||||
/// <returns>Time series containing DSMA values</returns>
|
||||
public override TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return [];
|
||||
if (source.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
@@ -325,7 +337,9 @@ public sealed class Dsma : AbstractBase
|
||||
double scaleFactor = 0.5)
|
||||
{
|
||||
if (output.Length < source.Length)
|
||||
{
|
||||
throw new ArgumentException("Output span is shorter than source span.", nameof(output));
|
||||
}
|
||||
|
||||
var dsma = new Dsma(period, scaleFactor);
|
||||
for (int i = 0; i < source.Length; i++)
|
||||
|
||||
Reference in New Issue
Block a user