2024-10-12 20:36:37 -07:00
|
|
|
using System.Drawing;
|
2026-01-18 19:02:03 -08:00
|
|
|
using System.Runtime.CompilerServices;
|
2024-10-12 20:36:37 -07:00
|
|
|
using TradingPlatform.BusinessLayer;
|
|
|
|
|
|
2024-10-10 16:23:23 -07:00
|
|
|
namespace QuanTAlib;
|
2024-09-22 17:31:24 -07:00
|
|
|
|
2026-01-18 19:02:03 -08:00
|
|
|
[SkipLocalsInit]
|
2024-10-12 20:36:37 -07:00
|
|
|
public class EmaIndicator : 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; } = 10;
|
2024-09-22 17:31:24 -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;
|
|
|
|
|
|
2026-01-18 19:02:03 -08:00
|
|
|
private Ema 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 => $"EMA {Period}:{SourceName}";
|
2024-10-13 11:19:27 -07:00
|
|
|
|
2024-10-12 20:36:37 -07:00
|
|
|
public EmaIndicator()
|
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 = "EMA - Exponential Moving Average";
|
2024-10-12 20:36:37 -07:00
|
|
|
Description = "Exponential Moving Average";
|
2026-01-18 19:02:03 -08:00
|
|
|
Series = new LineSeries(name: $"EMA {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
|
|
|
{
|
2026-01-18 19:02:03 -08:00
|
|
|
ma = new Ema(Period);
|
2024-10-12 20:36:37 -07:00
|
|
|
SourceName = Source.ToString();
|
2026-01-18 19:02:03 -08:00
|
|
|
_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
|
|
|
|
2026-01-18 19:02:03 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-10-12 20:36:37 -07:00
|
|
|
protected override void OnUpdate(UpdateArgs args)
|
|
|
|
|
{
|
2026-01-18 19:02:03 -08:00
|
|
|
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
|
|
|
|
|
TValue result = ma.Update(new TValue(item.TimeLeft.Ticks, _priceSelector(item)), isNew: args.IsNewBar());
|
|
|
|
|
Series.SetValue(result.Value, ma.IsHot, ShowColdValues);
|
2024-10-13 11:19:27 -07:00
|
|
|
}
|
2024-09-22 17:31:24 -07:00
|
|
|
}
|