Files
QuanTAlib/quantower/_IndicatorBarBase.cs
T

136 lines
4.8 KiB
C#
Raw Normal View History

2024-09-22 17:31:24 -07:00
using System.Drawing;
using TradingPlatform.BusinessLayer;
using TradingPlatform.BusinessLayer.Chart;
using System.Runtime.CompilerServices;
using System.Drawing.Drawing2D;
using System.Collections;
using TradingPlatform.BusinessLayer.TimeSync;
2024-09-22 20:10:05 -07:00
namespace QuanTAlib;
2024-09-22 17:31:24 -07:00
#pragma warning disable CA1416 // Validate platform compatibility
2024-09-30 06:46:07 -07:00
public abstract class IndicatorBarBase : Indicator, IWatchlistIndicator
2024-09-22 17:31:24 -07:00
{
[InputParameter("Show cold values", sortIndex: 20)]
public bool ShowColdValues { get; set; } = true;
2024-09-30 10:31:57 -07:00
public int MinHistoryDepths { get; set; }
2024-09-22 17:31:24 -07:00
// LineSeries.LineSeries(string, Color, int, LineStyle)'
protected LineSeries? Series;
2024-09-30 06:46:07 -07:00
protected abstract AbstractBarBase QuanTAlib { get; }
2024-09-22 17:31:24 -07:00
int IWatchlistIndicator.MinHistoryDepths => 0;
2024-09-30 06:46:07 -07:00
protected IndicatorBarBase()
2024-09-22 17:31:24 -07:00
{
OnBackGround = true;
SeparateWindow = false;
Series = new(name: $"{Name}", color: Color.RoyalBlue, width: 2, style: LineStyle.Solid);
AddLineSeries(Series);
}
2024-09-30 10:31:57 -07:00
protected abstract void InitIndicator();
2024-09-22 17:31:24 -07:00
protected override void OnInit()
{
InitIndicator();
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TBar bar = new(Time: Time(),
Open: GetPrice(PriceType.Open),
High: GetPrice(PriceType.High),
Low: GetPrice(PriceType.Low),
Close: GetPrice(PriceType.Close),
Volume: GetPrice(PriceType.Volume),
IsNew: args.Reason == UpdateReason.NewBar || args.Reason == UpdateReason.HistoricalBar);
2024-09-30 06:46:07 -07:00
TValue result = QuanTAlib.Calc(bar);
2024-09-22 17:31:24 -07:00
Series!.SetValue(result.Value);
Series!.SetMarker(0, Color.Transparent);
}
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
List<Point> allPoints = new List<Point>();
2024-09-30 10:31:57 -07:00
if (CurrentChart == null) { return; }
2024-09-22 17:31:24 -07:00
Graphics gr = args.Graphics;
var mainWindow = this.CurrentChart.Windows[args.WindowIndex];
var converter = mainWindow.CoordinatesConverter;
var clientRect = mainWindow.ClientRectangle;
gr.SetClip(clientRect);
DateTime leftTime = new[] { converter.GetTime(clientRect.Left), Time(this.Count - 1) }.Max();
DateTime rightTime = new[] { converter.GetTime(clientRect.Right), Time(0) }.Min();
int leftIndex = (int)HistoricalData.GetIndexByTime(leftTime.Ticks) + 1;
int rightIndex = (int)HistoricalData.GetIndexByTime(rightTime.Ticks);
for (int i = rightIndex; i < leftIndex; i++)
{
int barX = (int)converter.GetChartX(Time(i));
int barY = (int)converter.GetChartY(Series![i]);
int halfBarWidth = CurrentChart.BarsWidth / 2;
Point point = new Point(barX + halfBarWidth, barY);
allPoints.Add(point);
}
if (allPoints.Count > 1)
{
DrawSmoothCombinedCurve(gr, allPoints, this.Count - QuanTAlib.WarmupPeriod - rightIndex);
}
}
private void DrawSmoothCombinedCurve(Graphics gr, List<Point> allPoints, int hotCount)
{
2024-09-30 10:31:57 -07:00
if (allPoints.Count < 2) { return; }
2024-09-22 17:31:24 -07:00
using (Pen defaultPen = new(Series!.Color, Series.Width) { DashStyle = ConvertLineStyleToDashStyle(Series.Style) })
using (Pen coldPen = new(Series!.Color, Series.Width) { DashStyle = DashStyle.Dot })
{
// Draw the hot part
if (hotCount > 0)
{
var hotPoints = allPoints.Take(Math.Min(hotCount + 1, allPoints.Count)).ToArray();
gr.DrawCurve(defaultPen, hotPoints, 0, hotPoints.Length - 1, (float)0.1);
}
// Draw the cold part
if (ShowColdValues && hotCount < allPoints.Count)
{
var coldPoints = allPoints.Skip(Math.Max(0, hotCount)).ToArray();
gr.DrawCurve(coldPen, coldPoints, 0, coldPoints.Length - 1, (float)0.1);
}
}
}
2024-09-30 10:31:57 -07:00
private static DashStyle ConvertLineStyleToDashStyle(LineStyle lineStyle)
2024-09-22 17:31:24 -07:00
{
return lineStyle switch
{
LineStyle.Solid => DashStyle.Solid,
LineStyle.Dash => DashStyle.Dash,
LineStyle.Dot => DashStyle.Dot,
LineStyle.DashDot => DashStyle.DashDot,
_ => DashStyle.Solid,
};
}
2024-09-30 10:31:57 -07:00
protected static void DrawText(Graphics gr, string text, Rectangle clientRect)
2024-09-22 17:31:24 -07:00
{
Font font = new Font("Inter", 8);
SizeF textSize = gr.MeasureString(text, font);
RectangleF textRect = new RectangleF(clientRect.Left + 5,
clientRect.Bottom - textSize.Height - 10,
textSize.Width + 10, textSize.Height + 10);
gr.FillRectangle(SystemBrushes.ControlDarkDark, textRect);
gr.DrawString(text, font, Brushes.White, new PointF(textRect.X + 6, textRect.Y + 5));
}
}