Files
QuanTAlib/quantower/Volatility/CviIndicator.cs
T
Miha Kralj f55f630ac6 Add CVI - Chaikin's Volatility class and tests
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).
2024-10-31 11:19:40 -07:00

55 lines
1.7 KiB
C#

using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class CviIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int Periods { get; set; } = 20;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Cvi? cvi;
protected LineSeries? CviSeries;
public int MinHistoryDepths => Math.Max(5, Periods * 2);
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public CviIndicator()
{
Name = "CVI - Chaikin's Volatility";
Description = "Measures the volatility of a financial instrument by comparing the spread between the high and low prices.";
SeparateWindow = true;
CviSeries = new($"CVI {Periods}", Color.Blue, 2, LineStyle.Solid);
AddLineSeries(CviSeries);
}
protected override void OnInit()
{
cvi = new Cvi(Periods);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TBar input = IndicatorExtensions.GetInputBar(this, args);
TValue result = cvi!.Calc(input);
CviSeries!.SetValue(result.Value);
CviSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"CVI ({Periods})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintHLine(args, 0.05, new Pen(Color.DarkRed, width: 2));
this.PaintSmoothCurve(args, CviSeries!, cvi!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}