mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-07-29 10:07:43 +00:00
refactor: Simplify TSeries and Dema constructors, and streamline benchmark methods for improved readability
This commit is contained in:
@@ -11,12 +11,12 @@ namespace QuanTAlib;
|
||||
/// </summary>
|
||||
public class TBarSeries : IReadOnlyList<TBar>
|
||||
{
|
||||
protected readonly List<long> _t = new();
|
||||
protected readonly List<double> _o = new();
|
||||
protected readonly List<double> _h = new();
|
||||
protected readonly List<double> _l = new();
|
||||
protected readonly List<double> _c = new();
|
||||
protected readonly List<double> _v = new();
|
||||
protected readonly List<long> _t;
|
||||
protected readonly List<double> _o;
|
||||
protected readonly List<double> _h;
|
||||
protected readonly List<double> _l;
|
||||
protected readonly List<double> _c;
|
||||
protected readonly List<double> _v;
|
||||
|
||||
public string Name { get; set; } = "Bar";
|
||||
public event Action<TBar>? Pub;
|
||||
@@ -34,13 +34,8 @@ public class TBarSeries : IReadOnlyList<TBar>
|
||||
public TSeries C => Close;
|
||||
public TSeries V => Volume;
|
||||
|
||||
public TBarSeries()
|
||||
public TBarSeries() : this(0)
|
||||
{
|
||||
Open = new TSeries(_t, _o) { Name = "Open" };
|
||||
High = new TSeries(_t, _h) { Name = "High" };
|
||||
Low = new TSeries(_t, _l) { Name = "Low" };
|
||||
Close = new TSeries(_t, _c) { Name = "Close" };
|
||||
Volume = new TSeries(_t, _v) { Name = "Volume" };
|
||||
}
|
||||
|
||||
public TBarSeries(int capacity)
|
||||
|
||||
@@ -20,10 +20,8 @@ namespace QuanTAlib;
|
||||
|
||||
public event Action<TValue>? Pub;
|
||||
|
||||
public TSeries()
|
||||
public TSeries() : this(0)
|
||||
{
|
||||
_t = new List<long>();
|
||||
_v = new List<double>();
|
||||
}
|
||||
|
||||
public TSeries(int capacity)
|
||||
|
||||
@@ -120,7 +120,7 @@ public sealed class Dema : ITValuePublisher
|
||||
|
||||
public TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
|
||||
if (source.Count == 0) return new TSeries();
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
|
||||
@@ -149,7 +149,7 @@ public sealed class Ema : ITValuePublisher
|
||||
|
||||
public TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
|
||||
if (source.Count == 0) return new TSeries();
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
@@ -291,7 +291,7 @@ public sealed class Ema : ITValuePublisher
|
||||
|
||||
if (source.Length == 0) return;
|
||||
|
||||
State state = State.New();
|
||||
var state = State.New();
|
||||
double lastValid = 0;
|
||||
|
||||
CalculateCore(source, output, alpha, ref state, ref lastValid);
|
||||
|
||||
@@ -137,7 +137,7 @@ public sealed class Sma : ITValuePublisher
|
||||
|
||||
public TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
|
||||
if (source.Count == 0) return new TSeries();
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
|
||||
@@ -129,7 +129,7 @@ public sealed class Tema : ITValuePublisher
|
||||
|
||||
public TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
|
||||
if (source.Count == 0) return new TSeries();
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
|
||||
@@ -141,7 +141,7 @@ public sealed class Trima : ITValuePublisher
|
||||
|
||||
public TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
|
||||
if (source.Count == 0) return new TSeries();
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
|
||||
@@ -141,7 +141,7 @@ public sealed class Wma : ITValuePublisher
|
||||
|
||||
public TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
|
||||
if (source.Count == 0) return new TSeries();
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
|
||||
+5
-45
@@ -176,15 +176,7 @@ public class IndicatorBenchmarks
|
||||
|
||||
[BenchmarkCategory("SMA")]
|
||||
[Benchmark(Description = "Skender SMA")]
|
||||
public double Skender_Sma()
|
||||
{
|
||||
double sum = 0;
|
||||
foreach (var r in _quotes.GetSma(Period))
|
||||
{
|
||||
sum += (double)(r.Sma ?? 0);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public object Skender_Sma() => _quotes.GetSma(Period);
|
||||
|
||||
// ==================== EMA ====================
|
||||
[BenchmarkCategory("EMA")]
|
||||
@@ -229,15 +221,7 @@ public class IndicatorBenchmarks
|
||||
|
||||
[BenchmarkCategory("EMA")]
|
||||
[Benchmark(Description = "Skender EMA")]
|
||||
public double Skender_Ema()
|
||||
{
|
||||
double sum = 0;
|
||||
foreach (var r in _quotes.GetEma(Period))
|
||||
{
|
||||
sum += (double)(r.Ema ?? 0);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public object Skender_Ema() => _quotes.GetEma(Period);
|
||||
|
||||
// ==================== WMA ====================
|
||||
[BenchmarkCategory("WMA")]
|
||||
@@ -282,15 +266,7 @@ public class IndicatorBenchmarks
|
||||
|
||||
[BenchmarkCategory("WMA")]
|
||||
[Benchmark(Description = "Skender WMA")]
|
||||
public double Skender_Wma()
|
||||
{
|
||||
double sum = 0;
|
||||
foreach (var r in _quotes.GetWma(Period))
|
||||
{
|
||||
sum += (double)(r.Wma ?? 0);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public object Skender_Wma() => _quotes.GetWma(Period);
|
||||
|
||||
// ==================== TRIMA ====================
|
||||
[BenchmarkCategory("TRIMA")]
|
||||
@@ -376,15 +352,7 @@ public class IndicatorBenchmarks
|
||||
|
||||
[BenchmarkCategory("DEMA")]
|
||||
[Benchmark(Description = "Skender DEMA")]
|
||||
public double Skender_Dema()
|
||||
{
|
||||
double sum = 0;
|
||||
foreach (var r in _quotes.GetDema(Period))
|
||||
{
|
||||
sum += (double)(r.Dema ?? 0);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public object Skender_Dema() => _quotes.GetDema(Period);
|
||||
|
||||
// ==================== TEMA ====================
|
||||
[BenchmarkCategory("TEMA")]
|
||||
@@ -429,13 +397,5 @@ public class IndicatorBenchmarks
|
||||
|
||||
[BenchmarkCategory("TEMA")]
|
||||
[Benchmark(Description = "Skender TEMA")]
|
||||
public double Skender_Tema()
|
||||
{
|
||||
double sum = 0;
|
||||
foreach (var r in _quotes.GetTema(Period))
|
||||
{
|
||||
sum += (double)(r.Tema ?? 0);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public object Skender_Tema() => _quotes.GetTema(Period);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user