Files
QuanTAlib/quantower/Statistics/ModeIndicator.cs
T

48 lines
1.3 KiB
C#
Raw Normal View History

2024-10-12 20:36:37 -07:00
using System.Drawing;
2024-09-22 17:31:24 -07:00
using TradingPlatform.BusinessLayer;
2024-10-12 20:36:37 -07:00
2024-09-22 20:10:05 -07:00
namespace QuanTAlib;
2024-09-22 17:31:24 -07:00
2024-10-12 20:36:37 -07:00
public class ModeIndicator : Indicator, IWatchlistIndicator
2024-09-22 17:31:24 -07:00
{
2024-11-08 17:11:18 -08:00
[InputParameter("Period", sortIndex: 1, 1, 1000, 1, 0)]
public int Period { get; set; } = 20;
[IndicatorExtensions.DataSourceInput]
2024-10-12 20:36:37 -07:00
public SourceType Source { get; set; } = SourceType.Close;
2024-09-22 17:31:24 -07:00
private Mode? mode;
2024-10-12 20:36:37 -07:00
protected LineSeries? ModeSeries;
protected string? SourceName;
2024-11-08 17:11:18 -08:00
public int MinHistoryDepths => Period;
2024-10-12 20:36:37 -07:00
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public ModeIndicator()
2024-09-22 17:31:24 -07:00
{
2024-10-12 20:36:37 -07:00
Name = "Mode";
Description = "Calculates the most frequent value in a specified period";
SeparateWindow = false;
SourceName = Source.ToString();
2024-11-06 20:56:32 -08:00
ModeSeries = new("Mode", color: IndicatorExtensions.Statistics, 2, LineStyle.Solid);
2024-10-12 20:36:37 -07:00
AddLineSeries(ModeSeries);
2024-09-22 17:31:24 -07:00
}
2024-10-12 20:36:37 -07:00
protected override void OnInit()
2024-09-22 17:31:24 -07:00
{
2024-11-08 17:11:18 -08:00
mode = new Mode(Period);
2024-10-12 20:36:37 -07:00
SourceName = Source.ToString();
base.OnInit();
2024-09-22 17:31:24 -07:00
}
2024-10-12 20:36:37 -07:00
protected override void OnUpdate(UpdateArgs args)
{
TValue input = this.GetInputValue(args, Source);
TValue result = mode!.Calc(input);
ModeSeries!.SetValue(result.Value);
}
2024-11-08 17:11:18 -08:00
public override string ShortName => $"Mode ({Period}:{SourceName})";
2024-10-11 18:02:09 -07:00
}