Skip to content

Commit

Permalink
Adx, Adxr, Apo, Dmi
Browse files Browse the repository at this point in the history
  • Loading branch information
mihakralj committed Oct 28, 2024
1 parent 6c67a0c commit 6b79f81
Show file tree
Hide file tree
Showing 18 changed files with 859 additions and 127 deletions.
14 changes: 10 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
}
],
"sarif-viewer.connectToGithubCodeScanning": "on",
"dotnet.dotnetPath": "/opt/homebrew/bin/",
"omnisharp.useModernNet": true,
"omnisharp.sdkPath": "/opt/homebrew/bin/dotnet",
"sonarlint.connectedMode.project": {
"connectionId": "mihakralj",
"projectKey": "mihakralj_QuanTAlib"
}
}
},
"dotnet.backgroundAnalysis.analyzerDiagnosticsScope": "fullSolution",
"dotnet.completion.showCompletionItemsFromUnimportedNamespaces": true,
"dotnetAcquisitionExtension.enableTelemetry": false,
"dotnet-test-explorer.testProjectPath": "Tests",
"dotnet-test-explorer.autoWatch": true,
"dotnet-test-explorer.showCodeLens": true,
"dotnet-test-explorer.testArguments": "/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=./TestResults/coverage.cobertura.xml"

}
9 changes: 5 additions & 4 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
<PropertyGroup>
<RootNamespace>QuanTAlib.Tests</RootNamespace>
<AssemblyName>QuanTAlib.Tests</AssemblyName>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.35">
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.42">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
25 changes: 25 additions & 0 deletions Tests/test_eventing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,37 @@ public void EventBasedCalculations()
("Tema", new Tema(p), new Tema(input, p)),
("Kama", new Kama(2, 30, 6), new Kama(input, 2, 30, 6)),
("Zlema", new Zlema(p), new Zlema(input, p)),
// Added missing averages
("Sinema", new Sinema(p), new Sinema(input, p)),
("Smma", new Smma(p), new Smma(input, p)),
("T3", new T3(p), new T3(input, p)),
("Trima", new Trima(p), new Trima(input, p)),
("Vidya", new Vidya(p), new Vidya(input, p)),
// momentum indicators
("Apo", new Apo(12, 26), new Apo(input, 12, 26)),
// oscillators
("Rsi", new Rsi(p), new Rsi(input, p)),
("Rsx", new Rsx(p), new Rsx(input, p)),
("Cmo", new Cmo(p), new Cmo(input, p)),
// statistics
("Curvature", new Curvature(p), new Curvature(input, p)),
("Entropy", new Entropy(p), new Entropy(input, p)),
("Kurtosis", new Kurtosis(p), new Kurtosis(input, p)),
("Max", new Max(p), new Max(input, p)),
("Median", new Median(p), new Median(input, p)),
("Min", new Min(p), new Min(input, p)),
("Mode", new Mode(p), new Mode(input, p)),
("Percentile", new Percentile(p, 0.5), new Percentile(input, p, 0.5)),
("Skew", new Skew(p), new Skew(input, p)),
("Slope", new Slope(p), new Slope(input, p)),
("Stddev", new Stddev(p), new Stddev(input, p)),
("Variance", new Variance(p), new Variance(input, p)),
("Zscore", new Zscore(p), new Zscore(input, p)),
// volatility
("Hv", new Hv(p), new Hv(input, p)),
("Jvolty", new Jvolty(p), new Jvolty(input, p)),
("Rv", new Rv(p), new Rv(input, p)),
("Rvi", new Rvi(p), new Rvi(input, p)),
// error classes
("Mae", new Mae(p), new Mae(input, p)),
("Mapd", new Mapd(p), new Mapd(input, p)),
Expand Down
91 changes: 91 additions & 0 deletions Tests/test_updates_momentum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Xunit;
using System.Security.Cryptography;

namespace QuanTAlib.Tests;

public class MomentumUpdateTests
{
private readonly RandomNumberGenerator rng = RandomNumberGenerator.Create();
private const int RandomUpdates = 100;
private const double ReferenceValue = 100.0;
private const int precision = 8;

private double GetRandomDouble()
{
byte[] bytes = new byte[8];
rng.GetBytes(bytes);
return (double)BitConverter.ToUInt64(bytes, 0) / ulong.MaxValue * 200 - 100; // Range: -100 to 100
}

private TBar GetRandomBar(bool IsNew)
{
double open = GetRandomDouble();
double high = open + Math.Abs(GetRandomDouble());
double low = open - Math.Abs(GetRandomDouble());
double close = low + (high - low) * GetRandomDouble();
return new TBar(DateTime.Now, open, high, low, close, 1000, IsNew);
}

[Fact]
public void Adx_Update()
{
var indicator = new Adx(period: 14);
TBar r = GetRandomBar(true);
double initialValue = indicator.Calc(r);

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(GetRandomBar(IsNew: false));
}
double finalValue = indicator.Calc(new TBar(r.Time, r.Open, r.High, r.Low, r.Close, r.Volume, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}

[Fact]
public void Adxr_Update()
{
var indicator = new Adxr(period: 14);
TBar r = GetRandomBar(true);
double initialValue = indicator.Calc(r);

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(GetRandomBar(IsNew: false));
}
double finalValue = indicator.Calc(new TBar(r.Time, r.Open, r.High, r.Low, r.Close, r.Volume, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}

[Fact]
public void Apo_Update()
{
var indicator = new Apo(fastPeriod: 12, slowPeriod: 26);
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}

[Fact]
public void Dmi_Update()
{
var indicator = new Dmi(period: 14);
TBar r = GetRandomBar(true);
double initialValue = indicator.Calc(r);

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(GetRandomBar(IsNew: false));
}
double finalValue = indicator.Calc(new TBar(r.Time, r.Open, r.High, r.Low, r.Close, r.Volume, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}
}
64 changes: 64 additions & 0 deletions Tests/test_updates_oscillators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Xunit;
using System.Security.Cryptography;

namespace QuanTAlib.Tests;

public class OscillatorsUpdateTests
{
private readonly RandomNumberGenerator rng = RandomNumberGenerator.Create();
private const int RandomUpdates = 100;
private const double ReferenceValue = 100.0;
private const int precision = 8;

private double GetRandomDouble()
{
byte[] bytes = new byte[8];
rng.GetBytes(bytes);
return (double)BitConverter.ToUInt64(bytes, 0) / ulong.MaxValue * 200 - 100; // Range: -100 to 100
}

[Fact]
public void Rsi_Update()
{
var indicator = new Rsi(period: 14);
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}

[Fact]
public void Rsx_Update()
{
var indicator = new Rsx(period: 14);
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}

[Fact]
public void Cmo_Update()
{
var indicator = new Cmo(period: 14);
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}
}
33 changes: 24 additions & 9 deletions Tests/test_updates_volatility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,28 @@ public void Atr_Update()
public void Historical_Update()
{
var indicator = new Hv(period: 14);
double initialValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: true));
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(GetRandomBar(false));
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: false));
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}

[Fact]
public void Jvolty_Update()
{
var indicator = new Jvolty(period: 14);
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}
Expand All @@ -61,13 +76,13 @@ public void Historical_Update()
public void Realized_Update()
{
var indicator = new Rv(period: 14);
double initialValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: true));
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(GetRandomBar(false));
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: false));
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}
Expand All @@ -76,13 +91,13 @@ public void Realized_Update()
public void Rvi_Update()
{
var indicator = new Rvi(period: 14);
double initialValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: true));
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));

for (int i = 0; i < RandomUpdates; i++)
{
indicator.Calc(GetRandomBar(false));
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
}
double finalValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: false));
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));

Assert.Equal(initialValue, finalValue, precision);
}
Expand Down
28 changes: 0 additions & 28 deletions benchmark/benchmark.csproj

This file was deleted.

Loading

0 comments on commit 6b79f81

Please sign in to comment.