Files
QuanTAlib/lib/statistics/beta/Beta.Quantower.cs
T
Miha Kralj 84ff67fb50 Add validation tests for USF and enhance ATR indicator tests
- Introduced Usf.Validation.Tests.cs to validate the USF (Ehlers Ultimate Smoother Filter) for consistency across batch, streaming, and span modes, as well as mathematical properties and coefficient calculations.
- Added comprehensive tests for the ATR indicator in Atr.Quantower.Tests.cs, including constructor validation, historical data processing, and handling of NaN/Infinity inputs.
- Enhanced Atr.Tests.cs with additional tests for iterative corrections, warmup behavior, and true range calculations.
- Updated Atr.cs to ensure warmup period is derived from RMA.
- Added new tests for Adosc in Adosc.Tests.cs to validate handling of NaN and Infinity inputs, and to ensure batch calculations match iterative results.
- Created a new Volatility.csproj to organize volatility-related implementations.
2025-12-28 23:33:46 -08:00

69 lines
2.4 KiB
C#

using System.Drawing;
using System.Runtime.CompilerServices;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
[SkipLocalsInit]
public sealed class BetaIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 2000, 1, 0)]
public int Period { get; set; } = 20;
[InputParameter("Asset Source", sortIndex: 2)]
public SourceType AssetSource { get; set; } = SourceType.Close;
[InputParameter("Market Source", sortIndex: 3)]
public SourceType MarketSource { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Beta? _beta;
private readonly LineSeries? _series;
private Func<IHistoryItem, double>? _assetSelector;
private Func<IHistoryItem, double>? _marketSelector;
public static int MinHistoryDepths => 2;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"Beta({Period})";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/statistics/beta/Beta.Quantower.cs";
public BetaIndicator()
{
OnBackGround = true;
SeparateWindow = true;
Name = "Beta Coefficient";
Description = "Measures the volatility of an asset in relation to the overall market.";
_series = new(name: "Beta", color: IndicatorExtensions.Statistics, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnInit()
{
_beta = new Beta(Period);
_assetSelector = AssetSource.GetPriceSelector();
_marketSelector = MarketSource.GetPriceSelector();
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
var item = this.HistoricalData[this.Count - 1, SeekOriginHistory.Begin];
double assetVal = _assetSelector!(item);
double marketVal = _marketSelector!(item);
var time = this.HistoricalData.Time();
var assetInput = new TValue(time, assetVal);
var marketInput = new TValue(time, marketVal);
TValue result = _beta!.Update(assetInput, marketInput, args.IsNewBar());
_series!.SetValue(result.Value, _beta.IsHot, ShowColdValues);
}
}