mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-13 16:18:05 +00:00
Add implementation of CVI - Chaikin's Volatility class and related tests. * **Cvi Class Implementation:** - Add `Cvi` class in `lib/volatility/Cvi.cs` to calculate Chaikin's Volatility. - Use high and low prices for calculation. - Include a constructor with `period` parameter. - Add a method to calculate Chaikin's Volatility. * **Quantower Indicator:** - Add `CviIndicator` class in `quantower/Volatility/CviIndicator.cs`. - Use `Cvi` class for calculation. - Add input parameters for `period` and `showColdValues`. - Implement `OnInit` and `OnUpdate` methods. * **Tests:** - Add a test method for `Cvi` class in `Tests/test_updates_volatility.cs`. - Use random updates to test `Cvi`. - Ensure initial and final values are equal. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/mihakralj/QuanTAlib?shareId=XXXX-XXXX-XXXX-XXXX).
121 lines
3.9 KiB
C#
121 lines
3.9 KiB
C#
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 Hv(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 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);
|
|
}
|
|
|
|
[Fact]
|
|
public void Realized_Update()
|
|
{
|
|
var indicator = new Rv(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 Rvi_Update()
|
|
{
|
|
var indicator = new Rvi(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 Cvi_Update()
|
|
{
|
|
var indicator = new Cvi(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);
|
|
}
|
|
}
|