diff --git a/.github/workflows/Publish.yml b/.github/workflows/Publish.yml
index 34c762bf..a8c75357 100644
--- a/.github/workflows/Publish.yml
+++ b/.github/workflows/Publish.yml
@@ -15,72 +15,6 @@ permissions:
security-events: write # Required for CodeQL analysis and uploading SARIF results
jobs:
- SonarCloud:
- runs-on: ubuntu-latest
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
- with:
- fetch-depth: 0
-
- - name: Setup .NET SDK
- uses: actions/setup-dotnet@v3
- with:
- dotnet-version: '8.x'
-
- - name: Install JDK11 for Sonar Scanner
- uses: actions/setup-java@v3
- with:
- java-version: '11'
- distribution: 'zulu'
-
- - name: Install dotnet-sonarscanner
- run: |
- dotnet tool install --global dotnet-sonarscanner
- dotnet tool install JetBrains.dotCover.GlobalTool --global
- dotnet tool install dotnet-coverage --global
- dotnet restore
-
- - name: SonarCloud Scanner Start
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- run: |
- if [[ ${{ github.event_name }} == 'pull_request' ]]; then
- PR_PARAMS="/d:sonar.pullrequest.key=${{ github.event.pull_request.number }} \
- /d:sonar.pullrequest.branch=${{ github.head_ref }} \
- /d:sonar.pullrequest.base=${{ github.base_ref }}"
- elif [[ ${{ github.event_name }} == 'push' ]]; then
- BRANCH_PARAMS="/d:sonar.branch.name=${{ github.ref_name }}"
- else
- BRANCH_PARAMS="/d:sonar.branch.name=${{ github.ref_name }}"
- fi
-
- dotnet sonarscanner begin \
- /k:"mihakralj_QuanTAlib" \
- /o:"mihakralj" \
- /d:sonar.token="${{ secrets.SONAR_TOKEN }}" \
- /d:sonar.host.url="https://sonarcloud.io" \
- /d:sonar.cs.dotcover.reportsPaths=dotcover* \
- /d:sonar.scanner.scanAll=false \
- /d:sonar.scm.provider=git \
- $PR_PARAMS $BRANCH_PARAMS
-
- - name: Build
- run: |
- dotnet build --no-restore --configuration Debug
- dotnet build ./lib/quantalib.csproj --configuration Release --nologo
- dotnet build ./quantower/Averages/_Averages.csproj --configuration Release --nologo
- dotnet build ./quantower/Statistics/_Statistics.csproj --configuration Release --nologo
- dotnet build ./quantower/Volatility/_Volatility.csproj --configuration Release --nologo
- dotnet build ./SyntheticVendor/SyntheticVendor.csproj --configuration Release --nologo
- dotnet dotcover test Tests/Tests.csproj --dcReportType=HTML --dcoutput=./dotcover.html
-
- - name: SonarCloud Scanner End
- env:
- SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- run: dotnet sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
-
Code_Coverage:
runs-on: ubuntu-latest
steps:
@@ -160,9 +94,24 @@ jobs:
- name: Build
run: dotnet build --no-restore --configuration Debug
+ - name: Run Codacy Analysis CLI
+ uses: codacy/codacy-analysis-cli-action@v4
+ with:
+ project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
+ verbose: true
+ output: results.sarif
+ format: sarif
+ gh-code-scanning-compat: true
+ max-allowed-issues: 2147483647
+
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
+ - name: Upload SARIF results file
+ uses: github/codeql-action/upload-sarif@v3
+ with:
+ sarif_file: results.sarif
+
SecurityCodeScan:
runs-on: windows-latest
@@ -200,35 +149,9 @@ jobs:
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v3
- Codacy_Scan:
- runs-on: ubuntu-latest
- permissions:
- contents: read
- security-events: write
- actions: read
- steps:
- - name: Checkout code
- uses: actions/checkout@v4
- with:
- fetch-depth: 0
-
- - name: Run Codacy Analysis CLI
- uses: codacy/codacy-analysis-cli-action@v4
- with:
- project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
- verbose: true
- output: results.sarif
- format: sarif
- gh-code-scanning-compat: true
- max-allowed-issues: 2147483647
-
- - name: Upload SARIF results file
- uses: github/codeql-action/upload-sarif@v3
- with:
- sarif_file: results.sarif
build_publish:
- needs: [SonarCloud, Code_Coverage, CodeQL, Codacy_Scan, SecurityCodeScan]
+ needs: [Code_Coverage, CodeQL, SecurityCodeScan]
if: |
success() &&
(github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev')) ||
diff --git a/.sonarlint/QuanTAlib.json b/.sonarlint/QuanTAlib.json
index c44cece3..f75a1bc1 100644
--- a/.sonarlint/QuanTAlib.json
+++ b/.sonarlint/QuanTAlib.json
@@ -1,4 +1,4 @@
{
- "sonarCloudOrganization": "mihakralj",
+ "sonarCloudOrganization": "mihakralj-quantalib",
"projectKey": "mihakralj_QuanTAlib"
}
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 75b2bf41..dbbc4e17 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -7,11 +7,11 @@
}
],
"sarif-viewer.connectToGithubCodeScanning": "on",
+ "dotnet.dotnetPath": "C:/Program Files/dotnet",
+ "omnisharp.useModernNet": true,
+ "omnisharp.sdkPath": "C:/Program Files/dotnet/sdk",
"sonarlint.connectedMode.project": {
"connectionId": "mihakralj",
"projectKey": "mihakralj_QuanTAlib"
- },
- "dotnet.dotnetPath": "C:/Program Files/dotnet",
- "omnisharp.useModernNet": true,
- "omnisharp.sdkPath": "C:/Program Files/dotnet/sdk"
+ }
}
\ No newline at end of file
diff --git a/Directory.Build.props b/Directory.Build.props
index d9353501..df85d4db 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -42,6 +42,9 @@
true
false
true
+
+
+ S1944,S2053,S2222,S2259,S2583,S2589,S3329,S3655,S3900,S3949,S3966,S4158,S4347,S5773,S6781
diff --git a/Tests/test_iTBar.cs b/Tests/test_iTBar.cs
index 83aac9b4..1cf5f787 100644
--- a/Tests/test_iTBar.cs
+++ b/Tests/test_iTBar.cs
@@ -3,8 +3,6 @@ using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
-#pragma warning disable S1944, S2053, S2222, S2259, S2583, S2589, S3329, S3655, S3900, S3949, S3966, S4158, S4347, S5773, S6781
-
namespace QuanTAlib;
///
@@ -27,6 +25,7 @@ public class BarIndicatorTests
private static readonly ITValue[] indicators = new ITValue[]
{
new Atr(period: 14),
+ new Jvolty(period: 14)
// Add other TBar-based indicators here
};
diff --git a/Tests/test_quantower.cs b/Tests/test_quantower.cs
index 440040f6..844f1733 100644
--- a/Tests/test_quantower.cs
+++ b/Tests/test_quantower.cs
@@ -95,6 +95,8 @@ namespace QuanTAlib
// Volatility Indicators
[Fact] public void Atr() => TestIndicator("atr");
+ [Fact] public void Jvolty() => TestIndicator("jvolty");
+
[Fact] public void Historical() => TestIndicator("historical");
[Fact] public void Realized() => TestIndicator("realized");
[Fact] public void Rvi() => TestIndicator("rvi");
diff --git a/Tests/test_updates_volatility.cs b/Tests/test_updates_volatility.cs
new file mode 100644
index 00000000..901006cc
--- /dev/null
+++ b/Tests/test_updates_volatility.cs
@@ -0,0 +1,104 @@
+using Xunit;
+using System.Security.Cryptography;
+
+namespace QuanTAlib.Tests;
+
+public class VolatilityUpdateTests
+{
+ 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 Atr_Update()
+ {
+ var indicator = new Atr(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 Historical_Update()
+ {
+ var indicator = new Historical(period: 14);
+ double initialValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: true));
+
+ for (int i = 0; i < RandomUpdates; i++)
+ {
+ indicator.Calc(GetRandomBar(false));
+ }
+ double finalValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: false));
+
+ Assert.Equal(initialValue, finalValue, precision);
+ }
+
+ [Fact]
+ public void Jvolty_Update()
+ {
+ var indicator = new Jvolty(period: 14);
+ double initialValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: true));
+
+ for (int i = 0; i < RandomUpdates; i++)
+ {
+ indicator.Calc(GetRandomBar(false));
+ }
+ double finalValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: false));
+
+ Assert.Equal(initialValue, finalValue, precision);
+ }
+
+ [Fact]
+ public void Realized_Update()
+ {
+ var indicator = new Realized(period: 14);
+ double initialValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: true));
+
+ for (int i = 0; i < RandomUpdates; i++)
+ {
+ indicator.Calc(GetRandomBar(false));
+ }
+ double finalValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: false));
+
+ Assert.Equal(initialValue, finalValue, precision);
+ }
+
+ [Fact]
+ 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));
+
+ for (int i = 0; i < RandomUpdates; i++)
+ {
+ indicator.Calc(GetRandomBar(false));
+ }
+ double finalValue = indicator.Calc(new TBar(DateTime.Now, ReferenceValue, ReferenceValue, ReferenceValue, ReferenceValue, 1000, IsNew: false));
+
+ Assert.Equal(initialValue, finalValue, precision);
+ }
+}
diff --git a/lib/volatility/Jvolty.cs b/lib/volatility/Jvolty.cs
new file mode 100644
index 00000000..dba3208b
--- /dev/null
+++ b/lib/volatility/Jvolty.cs
@@ -0,0 +1,141 @@
+///
+/// Represents a Jurik Volatility (Jvolty) calculator, a measure of market volatility based on Jurik Moving Average (JMA) concepts.
+///
+
+namespace QuanTAlib;
+
+public class Jvolty : AbstractBase
+{
+ private readonly int _period;
+ private readonly CircularBuffer _values;
+ private readonly CircularBuffer _voltyShort;
+ private readonly CircularBuffer _vsumBuff;
+ private readonly CircularBuffer _avoltyBuff;
+
+ private double _len1;
+ private double _pow1;
+ private double _upperBand;
+ private double _lowerBand;
+ private double _p_upperBand;
+ private double _p_lowerBand;
+
+ ///
+ /// Initializes a new instance of the Jvolty class with the specified parameters.
+ ///
+ /// The period over which to calculate the Jvolty.
+ /// The phase parameter for the JMA-style calculation.
+ /// The short-term volatility period.
+ ///
+ /// Thrown when period is less than 1.
+ ///
+ public Jvolty(int period, int vshort = 10)
+ {
+ if (period < 1)
+ {
+ throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
+ }
+ _period = period;
+ int _vlong = 65;
+
+ _values = new CircularBuffer(period);
+ _voltyShort = new CircularBuffer(vshort);
+ _vsumBuff = new CircularBuffer(_vlong);
+ _avoltyBuff = new CircularBuffer(2);
+
+ WarmupPeriod = period * 2;
+ Name = $"JVOLTY({period},{vshort})";
+ }
+
+ ///
+ /// Initializes a new instance of the Jvolty class with the specified source and parameters.
+ ///
+ /// The source object to subscribe to for bar updates.
+ /// The period over which to calculate the Jvolty.
+ /// The phase parameter for the JMA-style calculation.
+ /// The short-term volatility period.
+ public Jvolty(object source, int period, int vshort = 10) : this(period, vshort)
+ {
+ var pubEvent = source.GetType().GetEvent("Pub");
+ pubEvent?.AddEventHandler(source, new BarSignal(Sub));
+ }
+
+ ///
+ /// Initializes the Jvolty instance by setting up the initial state.
+ ///
+ public override void Init()
+ {
+ base.Init();
+ _upperBand = _lowerBand = 0.0;
+ _p_upperBand = _p_lowerBand = 0.0;
+ _len1 = Math.Max((Math.Log(Math.Sqrt(_period - 1)) / Math.Log(2.0)) + 2.0, 0);
+ _pow1 = Math.Max(_len1 - 2.0, 0.5);
+ _avoltyBuff.Clear();
+ _avoltyBuff.Add(0, true);
+ _avoltyBuff.Add(0, true);
+ }
+
+ ///
+ /// Manages the state of the Jvolty instance based on whether a new bar is being processed.
+ ///
+ /// Indicates whether the current input is a new bar.
+ protected override void ManageState(bool isNew)
+ {
+ if (isNew)
+ {
+ _index++;
+ _p_upperBand = _upperBand;
+ _p_lowerBand = _lowerBand;
+ }
+ else
+ {
+ _upperBand = _p_upperBand;
+ _lowerBand = _p_lowerBand;
+ }
+ }
+
+ ///
+ /// Performs the Jvolty calculation for the current bar.
+ ///
+ ///
+ /// The calculated Jvolty value for the current bar.
+ ///
+ protected override double Calculation()
+ {
+ ManageState(BarInput.IsNew);
+
+ _values.Add(BarInput.Close, BarInput.IsNew);
+
+ if (_index == 1)
+ {
+ return 0;
+ }
+
+ double hprice = _values.Max();
+ double lprice = _values.Min();
+
+ double del1 = hprice - _upperBand;
+ double del2 = lprice - _lowerBand;
+ double volty = Math.Max(Math.Abs(del1), Math.Abs(del2));
+
+ _voltyShort.Add(volty, BarInput.IsNew);
+ double vsum = _vsumBuff.Newest() + 0.1 * (volty - _voltyShort.Oldest());
+ _vsumBuff.Add(vsum, BarInput.IsNew);
+
+ double prevAvolty = _avoltyBuff.Newest();
+ double avolty = prevAvolty + 2.0 / (Math.Max(4.0 * _period, 30) + 1.0) * (vsum - prevAvolty);
+ _avoltyBuff.Add(avolty, BarInput.IsNew);
+
+ double dVolty = (avolty > 0) ? volty / avolty : 0;
+ dVolty = Math.Min(Math.Max(dVolty, 1.0), Math.Pow(_len1, 1.0 / _pow1));
+
+ double pow2 = Math.Pow(dVolty, _pow1);
+ double len2 = Math.Sqrt(0.5 * (_period - 1)) * _len1;
+ double Kv = Math.Pow(len2 / (len2 + 1), Math.Sqrt(pow2));
+
+ _upperBand = (del1 > 0) ? hprice : hprice - (Kv * del1);
+ _lowerBand = (del2 < 0) ? lprice : lprice - (Kv * del2);
+
+ IsHot = _index >= WarmupPeriod;
+ return volty;
+ }
+}
diff --git a/lib/volatility/todo.md b/lib/volatility/todo.md
index 2ce815c6..5a4aa170 100644
--- a/lib/volatility/todo.md
+++ b/lib/volatility/todo.md
@@ -2,7 +2,7 @@
## Single Value Input (Typically Closing Prices)
-- Jurik Volatility (Volty)
+- **Jurik Volatility (Volty)**
- **Standard Deviation**
- **Relative Volatility Index (RVI)**
- Ulcer Index
diff --git a/quantower/Volatility/JvoltyIndicator.cs b/quantower/Volatility/JvoltyIndicator.cs
new file mode 100644
index 00000000..9db23343
--- /dev/null
+++ b/quantower/Volatility/JvoltyIndicator.cs
@@ -0,0 +1,41 @@
+using System.Drawing;
+using TradingPlatform.BusinessLayer;
+
+namespace QuanTAlib;
+
+public class JvoltyIndicator : Indicator, IWatchlistIndicator
+{
+ [InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
+ public int Periods { get; set; } = 20;
+
+ private Jvolty? jvolty;
+ protected LineSeries? JvoltySeries;
+ public static int MinHistoryDepths => 2;
+ int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
+
+ public JvoltyIndicator()
+ {
+ Name = "JVOLTY - Mark Jurik's Volatility";
+ Description = "Measures market volatility according to Mark Jurik.";
+ SeparateWindow = true;
+
+ JvoltySeries = new("JVOLTY", Color.Blue, 2, LineStyle.Solid);
+ AddLineSeries(JvoltySeries);
+ }
+
+ protected override void OnInit()
+ {
+ jvolty = new (Periods);
+ base.OnInit();
+ }
+
+ protected override void OnUpdate(UpdateArgs args)
+ {
+ TBar input = IndicatorExtensions.GetInputBar(this, args);
+ TValue result = jvolty!.Calc(input);
+
+ JvoltySeries!.SetValue(result.Value);
+ }
+
+ public override string ShortName => $"JVOLTY ({Periods})";
+}