Files

59 lines
1.9 KiB
C#
Raw Permalink Normal View History

2024-10-12 20:36:37 -07:00
using System.Drawing;
using System.Runtime.CompilerServices;
2024-10-12 20:36:37 -07:00
using TradingPlatform.BusinessLayer;
2024-09-22 20:10:05 -07:00
namespace QuanTAlib;
2024-09-22 17:31:24 -07:00
[SkipLocalsInit]
2024-10-12 20:36:37 -07:00
public class HmaIndicator : Indicator, IWatchlistIndicator
2024-09-22 17:31:24 -07:00
{
2024-11-08 17:11:18 -08:00
[InputParameter("Period", sortIndex: 1, 2, 1000, 1, 0)]
public int Period { get; set; } = 14;
2024-10-12 20:36:37 -07:00
2024-11-08 17:11:18 -08:00
[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
2024-10-13 11:19:27 -07:00
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Hma ma = null!;
protected LineSeries Series;
protected string SourceName = null!;
private Func<IHistoryItem, double> _priceSelector = null!;
public static int MinHistoryDepths => 0;
2024-10-12 20:36:37 -07:00
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
2024-09-22 17:31:24 -07:00
2024-11-08 17:11:18 -08:00
public override string ShortName => $"HMA {Period}:{SourceName}";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/trends/hma/Hma.cs";
2024-10-13 11:19:27 -07:00
2024-10-12 20:36:37 -07:00
public HmaIndicator()
2024-09-22 17:31:24 -07:00
{
2024-10-12 20:36:37 -07:00
OnBackGround = true;
SeparateWindow = false;
SourceName = Source.ToString();
2024-09-22 17:31:24 -07:00
Name = "HMA - Hull Moving Average";
Description = "Hull Moving Average for reduced lag";
Series = new LineSeries(name: $"HMA {Period}", color: IndicatorExtensions.Averages, width: 2, style: LineStyle.Solid);
2024-10-12 20:36:37 -07:00
AddLineSeries(Series);
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
ma = new Hma(Period);
2024-10-12 20:36:37 -07:00
SourceName = Source.ToString();
_priceSelector = Source.GetPriceSelector();
2024-10-12 20:36:37 -07:00
base.OnInit();
2024-09-22 17:31:24 -07:00
}
2024-10-12 20:36:37 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-12 20:36:37 -07:00
protected override void OnUpdate(UpdateArgs args)
{
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
2024-10-12 20:36:37 -07:00
TValue result = ma.Update(new TValue(item.TimeLeft.Ticks, _priceSelector(item)), isNew: args.IsNewBar());
2024-10-12 20:36:37 -07:00
Series.SetValue(result.Value, ma.IsHot, ShowColdValues);
2024-10-13 11:19:27 -07:00
}
2024-09-22 17:31:24 -07:00
}