Moving Average

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using TigerTrade.Chart.Indicators.Common;
using TigerTrade.Chart.Indicators.Enums;
using TigerTrade.Chart.Indicators.Sources;
 
namespace TigerTrade.Chart.Indicators.Custom.Sources
{
    [DataContract(Name = "MASourceDemo",
        Namespace = "http://schemas.datacontract.org/2004/07/TigerTrade.Chart.Indicators.Custom.Sources")]
    [IndicatorSource("MADemo", Type = typeof(MASourceDemo))]
    public sealed class MASourceDemo : IndicatorSourceBase
    {
        private int _period;
 
        [DataMember(Name = "Period")]
        [Category("Параметры"), DisplayName("Период")]
        public int Period
        {
            get => _period;
            set
            {
                value = Math.Max(1, value);
 
                if (value == _period)
                {
                    return;
                }
 
                _period = value;
 
                OnPropertyChanged();
            }
        }
 
        private IndicatorMaType _maType;
 
        [DataMember(Name = "MaType")]
        [Category("Параметры"), DisplayName("Тип MA")]
        public IndicatorMaType MaType
        {
            get => _maType;
            set
            {
                if (value == _maType)
                {
                    return;
                }
 
                _maType = value;
 
                OnPropertyChanged();
            }
        }
 
        private IndicatorSourceBase _source;
 
        [DataMember(Name = "Source")]
        [Category("Параметры"), DisplayName("Источник")]
        public IndicatorSourceBase Source
        {
            get => _source ?? (_source = new StockSource());
            set
            {
                if (value == _source)
                {
                    return;
                }
 
                _source = value;
 
                OnPropertyChanged();
            }
        }
 
        public MASourceDemo()
        {
            Period = 14;
            MaType = IndicatorMaType.EMA;
 
            SelectedSeries = "MA";
        }
 
        public override IEnumerable<string> GetSeriesList()
        {
            return new List<string> { "MA" };
        }
 
        public override double[] GetSeries(IndicatorsHelper helper)
        {
            var source = _source.GetSeries(helper);
 
            return source == null ? null : helper.MovingAverage(_maType, source, _period);
        }
 
        public override void CopySettings(IndicatorSourceBase source)
        {
            if (!(source is MASourceDemo s))
            {
                return;
            }
 
            SelectedSeries = s.SelectedSeries;
 
            Period = s.Period;
            MaType = s.MaType;
 
            Source = s.Source.CloneSource();
        }
 
        public override string ToString()
        {
            return $"{MaType}({Source}, {Period})";
        }
    }
}

Last updated