Session Color Indicator Code
//------------------------------------------------------------------------------ // // SessionColor. Copyright (c) 2019 Ilya Smirnov. All rights reserved. // //------------------------------------------------------------------------------ using System; using System.ComponentModel; using System.Runtime.Serialization; using System.Windows; using System.Windows.Media; using TigerTrade.Chart.Base; using TigerTrade.Chart.Indicators.Common; using TigerTrade.Chart.Indicators.Enums; using TigerTrade.Dx; namespace TigerTrade.Chart.Indicators.Custom { [DataContract(Name = "SessionColorIndicator", Namespace = "http://schemas.datacontract.org/2004/07/TigerTrade.Chart.Indicators.Custom")] [Indicator("X_SessionColor", "*SessionColor", true, Type = typeof(SessionColorIndicator))] internal sealed class SessionColorIndicator : IndicatorBase { private TimeSpan _startTime; [DataMember(Name = "StartTime")] [Category("Параметры"), DisplayName("Начальное время")] public TimeSpan StartTime { get => _startTime; set { if (value == _startTime) { return; } _startTime = value; OnPropertyChanged(); } } private TimeSpan _endTime; [DataMember(Name = "EndTime")] [Category("Параметры"), DisplayName("Конечное время")] public TimeSpan EndTime { get => _endTime; set { if (value == _endTime) { return; } _endTime = value; OnPropertyChanged(); } } private XBrush _backBrush; private XColor _backColor; [DataMember(Name = "BackColor")] [Category("Стиль"), DisplayName("Цвет фона")] public XColor BackColor { get => _backColor; set { if (value == _backColor) { return; } _backColor = value; _backBrush = new XBrush(_backColor); OnPropertyChanged(); } } [Browsable(false)] public override bool ShowIndicatorValues => false; [Browsable(false)] public override bool ShowIndicatorLabels => false; [Browsable(false)] public override IndicatorCalculation Calculation => IndicatorCalculation.OnEachTick; public SessionColorIndicator() { ShowIndicatorTitle = false; StartTime = TimeSpan.FromHours(0); EndTime = TimeSpan.FromHours(12); BackColor = Color.FromArgb(127, 30, 144, 255); } protected override void Execute() { } public override void Render(DxVisualQueue visual) { var st = DateTime.MinValue; var et = DateTime.MinValue; var sp = new Point(); var ep = new Point(); for (var i = Canvas.Count - 1; i >= -Canvas.AfterBars; i--) { var index = Canvas.GetIndex(i); var d = Canvas.ConvertTimeToLocal(Canvas.IndexToDate(index)); if (StartTime < EndTime) { var newStartDate = d.Date.Add(StartTime); var newEndDate = d.Date.Add(EndTime); if (st == DateTime.MinValue && d >= newStartDate && d < newEndDate) { sp = new Point(Canvas.GetXX(i) - Canvas.ColumnWidth / 2.0, 0); st = d; et = DateTime.MinValue; ep = new Point(); } if (st != DateTime.MinValue && st.Date < d.Date) { ep = new Point(Canvas.GetXX(i + 1) + Canvas.ColumnWidth / 2.0, Canvas.Rect.Bottom); et = d; } else { if (d >= newEndDate) { ep = new Point(Canvas.GetXX(i + 1) + Canvas.ColumnWidth / 2.0, Canvas.Rect.Bottom); et = d; } } } else { var newStartDate = d.Date.Add(StartTime); var newEndDate = d.Date.Add(EndTime); if (st == DateTime.MinValue && (d >= newStartDate || d < newEndDate)) { sp = new Point(Canvas.GetXX(i) - Canvas.ColumnWidth / 2.0, 0); st = d; et = DateTime.MinValue; ep = new Point(); } if (d >= newEndDate && d < newStartDate) { ep = new Point(Canvas.GetXX(i + 1) + Canvas.ColumnWidth / 2.0, Canvas.Rect.Bottom); et = d; } } if (st == DateTime.MinValue || et == DateTime.MinValue) { continue; } visual.FillRectangle(_backBrush, new Rect(sp, ep)); st = DateTime.MinValue; et = DateTime.MinValue; sp = new Point(); ep = new Point(); } if (st != DateTime.MinValue) { visual.FillRectangle(_backBrush, new Rect(sp, new Point(Canvas.Rect.Right, Canvas.Rect.Bottom))); } } public override void ApplyColors(IChartTheme theme) { BackColor = theme.GetNextColor(); base.ApplyColors(theme); } public override void CopyTemplate(IndicatorBase indicator, bool style) { var i = (SessionColorIndicator)indicator; BackColor = i.BackColor; StartTime = i.StartTime; EndTime = i.EndTime; base.CopyTemplate(indicator, style); } } }