mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 04:58:08 +00:00
Merging dev into main (#38)
This commit is contained in:
+44
-41
@@ -17,39 +17,39 @@ public class EventingTests
|
|||||||
var input = new TSeries();
|
var input = new TSeries();
|
||||||
int p = 10;
|
int p = 10;
|
||||||
|
|
||||||
// Create a list of indicator pairs (direct calculation and event-based)
|
// Create a list of indicator pairs (direct calculation and event-based) with names
|
||||||
var indicators = new List<(AbstractBase Direct, AbstractBase EventBased)>
|
var indicators = new List<(string Name, AbstractBase Direct, AbstractBase EventBased)>
|
||||||
{
|
{
|
||||||
(new Afirma(p,p,Afirma.WindowType.BlackmanHarris), new Afirma(input, p,p,Afirma.WindowType.BlackmanHarris)),
|
("Afirma", new Afirma(p,p,Afirma.WindowType.BlackmanHarris), new Afirma(input, p,p,Afirma.WindowType.BlackmanHarris)),
|
||||||
(new Alma(p), new Alma(input, p)),
|
("Alma", new Alma(p), new Alma(input, p)),
|
||||||
(new Convolution([1,2,3,2,1]), new Convolution(input, [1,2,3,2,1])),
|
("Convolution", new Convolution(new double[] {1,2,3,2,1}), new Convolution(input, new double[] {1,2,3,2,1})),
|
||||||
(new Dema(p), new Dema(input, p)),
|
("Dema", new Dema(p), new Dema(input, p)),
|
||||||
(new Dsma(p), new Dsma(input, p)),
|
("Dsma", new Dsma(p), new Dsma(input, p)),
|
||||||
(new Dwma(p), new Dwma(input, p)),
|
("Dwma", new Dwma(p), new Dwma(input, p)),
|
||||||
(new Ema(p), new Ema(input, p)),
|
("Ema", new Ema(p), new Ema(input, p)),
|
||||||
(new Epma(p), new Epma(input, p)),
|
("Epma", new Epma(p), new Epma(input, p)),
|
||||||
(new Frama(p), new Frama(input, p)),
|
("Frama", new Frama(p), new Frama(input, p)),
|
||||||
(new Fwma(p), new Fwma(input, p)),
|
("Fwma", new Fwma(p), new Fwma(input, p)),
|
||||||
(new Gma(p), new Gma(input, p)),
|
("Gma", new Gma(p), new Gma(input, p)),
|
||||||
(new Hma(p), new Hma(input, p)),
|
("Hma", new Hma(p), new Hma(input, p)),
|
||||||
(new Htit(), new Htit(input)),
|
("Htit", new Htit(), new Htit(input)),
|
||||||
(new Hwma(p), new Hwma(input, p)),
|
("Hwma", new Hwma(p), new Hwma(input, p)),
|
||||||
(new Jma(p), new Jma(input, p)),
|
("Jma", new Jma(p), new Jma(input, p)),
|
||||||
(new Kama(p), new Kama(input, p)),
|
("Kama", new Kama(p), new Kama(input, p)),
|
||||||
(new Ltma(gamma: 0.2), new Ltma(input, gamma: 0.2)),
|
("Ltma", new Ltma(gamma: 0.2), new Ltma(input, gamma: 0.2)),
|
||||||
(new Maaf(p), new Maaf(input, p)),
|
("Maaf", new Maaf(p), new Maaf(input, p)),
|
||||||
(new Mama(p), new Mama(input, p)),
|
("Mama", new Mama(p), new Mama(input, p)),
|
||||||
(new Mgdi(p), new Mgdi(input, p)),
|
("Mgdi", new Mgdi(p, kFactor: 0.6), new Mgdi(input, p, kFactor: 0.6)),
|
||||||
(new Mma(p), new Mma(input, p)),
|
("Mma", new Mma(p), new Mma(input, p)),
|
||||||
(new Qema(k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2), new Qema(input, k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2)),
|
("Qema", new Qema(k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2), new Qema(input, k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2)),
|
||||||
(new Rema(p), new Rema(input, p)),
|
("Rema", new Rema(p), new Rema(input, p)),
|
||||||
(new Rma(p), new Rma(input, p)),
|
("Rma", new Rma(p), new Rma(input, p)),
|
||||||
(new Sma(p), new Sma(input, p)),
|
("Sma", new Sma(p), new Sma(input, p)),
|
||||||
(new Wma(p), new Wma(input, p)),
|
("Wma", new Wma(p), new Wma(input, p)),
|
||||||
(new Rma(p), new Rma(input, p)),
|
("Rma", new Rma(p), new Rma(input, p)),
|
||||||
(new Tema(p), new Tema(input, p)),
|
("Tema", new Tema(p), new Tema(input, p)),
|
||||||
(new Kama(2, 30, 6), new Kama(input, 2, 30, 6)),
|
("Kama", new Kama(2, 30, 6), new Kama(input, 2, 30, 6)),
|
||||||
(new Zlema(p), new Zlema(input, p))
|
("Zlema", new Zlema(p), new Zlema(input, p))
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generate 200 random values and feed them to both direct and event-based indicators
|
// Generate 200 random values and feed them to both direct and event-based indicators
|
||||||
@@ -59,23 +59,26 @@ public class EventingTests
|
|||||||
input.Add(randomValue);
|
input.Add(randomValue);
|
||||||
|
|
||||||
// Calculate direct indicators
|
// Calculate direct indicators
|
||||||
foreach (var (direct, _) in indicators)
|
foreach (var (_, direct, _) in indicators)
|
||||||
{
|
{
|
||||||
direct.Calc(randomValue);
|
direct.Calc(randomValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare the results of direct and event-based calculations
|
// Compare the results of direct and event-based calculations
|
||||||
foreach (var (direct, eventBased) in indicators)
|
for (int i = 0; i < indicators.Count; i++)
|
||||||
{
|
{
|
||||||
Assert.Equal(direct.Value, eventBased.Value, 9);
|
var (name, direct, eventBased) = indicators[i];
|
||||||
|
bool areEqual = (double.IsNaN(direct.Value) && double.IsNaN(eventBased.Value)) ||
|
||||||
|
Math.Abs(direct.Value - eventBased.Value) < 1e-9;
|
||||||
|
Assert.True(areEqual, $"Indicator {name} failed: Expected {direct.Value}, Actual {eventBased.Value}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static double GetRandomDouble(RandomNumberGenerator rng)
|
private static double GetRandomDouble(RandomNumberGenerator rng)
|
||||||
{
|
{
|
||||||
byte[] bytes = new byte[8];
|
byte[] bytes = new byte[8];
|
||||||
rng.GetBytes(bytes);
|
rng.GetBytes(bytes);
|
||||||
return (double)BitConverter.ToUInt64(bytes, 0) / ulong.MaxValue;
|
return (double)BitConverter.ToUInt64(bytes, 0) / ulong.MaxValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,6 @@ using System.Reflection;
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
#pragma warning disable S1944, S2053, S2222, S2259, S2583, S2589, S3329, S3655, S3900, S3949, S3966, S4158, S4347, S5773, S6781
|
|
||||||
|
|
||||||
namespace QuanTAlib;
|
namespace QuanTAlib;
|
||||||
|
|
||||||
public class IndicatorTests
|
public class IndicatorTests
|
||||||
|
|||||||
+1
-3
@@ -3,8 +3,6 @@ using TALib;
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
#pragma warning disable S1944, S2053, S2222, S2259, S2583, S2589, S3329, S3655, S3900, S3949, S3966, S4158, S4347, S5773, S6781
|
|
||||||
|
|
||||||
namespace QuanTAlib;
|
namespace QuanTAlib;
|
||||||
|
|
||||||
public class TAlibTests
|
public class TAlibTests
|
||||||
@@ -123,7 +121,7 @@ public class TAlibTests
|
|||||||
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
|
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
|
||||||
Core.Wma(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
|
Core.Wma(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
|
||||||
Assert.Equal(QL.Length, TALIB.Count());
|
Assert.Equal(QL.Length, TALIB.Count());
|
||||||
for (int i = QL.Length - 1; i > period * 10; i--)
|
for (int i = QL.Length - 1; i > 1000; i--)
|
||||||
{
|
{
|
||||||
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
|
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//not working yet
|
//not working yet
|
||||||
//TODO consistency test
|
//TODO fails consistency test
|
||||||
|
|
||||||
namespace QuanTAlib;
|
namespace QuanTAlib;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
namespace QuanTAlib;
|
namespace QuanTAlib;
|
||||||
//TODO consistency test
|
//TODO fails consistency test
|
||||||
public class Jma : AbstractBase
|
public class Jma : AbstractBase
|
||||||
{
|
{
|
||||||
public readonly int Period;
|
public readonly int Period;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//TODO: consistency test
|
//TODO: fails consistency test
|
||||||
|
|
||||||
namespace QuanTAlib;
|
namespace QuanTAlib;
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class Mgdi : AbstractBase
|
|||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Mgdi(object source, int period, double kFactor = 1.0) : this(period, kFactor)
|
public Mgdi(object source, int period, double kFactor = 0.6) : this(period, kFactor)
|
||||||
{
|
{
|
||||||
var pubEvent = source.GetType().GetEvent("Pub");
|
var pubEvent = source.GetType().GetEvent("Pub");
|
||||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||||
@@ -58,8 +58,9 @@ public class Mgdi : AbstractBase
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
double ratio = _prevMd != 0 ? value / _prevMd : 1;
|
||||||
double md = _prevMd + ((value - _prevMd) /
|
double md = _prevMd + ((value - _prevMd) /
|
||||||
(_kFactor * _period * Math.Pow(value / _prevMd, 4)));
|
(_kFactor * _period * Math.Pow(ratio, 4)));
|
||||||
_prevMd = md;
|
_prevMd = md;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user