Atr, FlowIndicator and fixes

This commit is contained in:
Miha Kralj
2024-10-21 16:06:47 -07:00
parent fbe4046b5d
commit e3d7cd9896
15 changed files with 335 additions and 189 deletions
+14 -2
View File
@@ -8,9 +8,12 @@ public class AtrIndicator : Indicator, IWatchlistIndicator
[InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int Periods { get; set; } = 20;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Atr? atr;
protected LineSeries? AtrSeries;
public static int MinHistoryDepths => 2;
public int MinHistoryDepths => Math.Max(5, Periods * 2);
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public AtrIndicator()
@@ -19,7 +22,7 @@ public class AtrIndicator : Indicator, IWatchlistIndicator
Description = "Measures market volatility by calculating the average range between high and low prices.";
SeparateWindow = true;
AtrSeries = new("ATR", Color.Blue, 2, LineStyle.Solid);
AtrSeries = new($"ATR {Periods}", Color.Blue, 2, LineStyle.Solid);
AddLineSeries(AtrSeries);
}
@@ -35,7 +38,16 @@ public class AtrIndicator : Indicator, IWatchlistIndicator
TValue result = atr!.Calc(input);
AtrSeries!.SetValue(result.Value);
AtrSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
}
public override string ShortName => $"ATR ({Periods})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintHLine(args, 0.05, new Pen(Color.DarkRed, width: 2));
this.PaintSmoothCurve(args, AtrSeries!, atr!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+78
View File
@@ -0,0 +1,78 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class FlowIndicator : Indicator, IWatchlistIndicator
{
protected string? SourceName;
public static int MinHistoryDepths => 2;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public FlowIndicator()
{
Name = "Flow Visualization";
SeparateWindow = false;
}
protected override void OnInit()
{
// placeholder
}
protected override void OnUpdate(UpdateArgs args)
{
// placeholder
}
#pragma warning disable CA1416 // Validate platform compatibility
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
Graphics gr = args.Graphics;
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
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), this.HistoricalData.Time(this!.Count - 1) }.Max();
DateTime rightTime = new[] { converter.GetTime(clientRect.Right), this.HistoricalData.Time(0) }.Min();
int leftIndex = (int)this.HistoricalData.GetIndexByTime(leftTime.Ticks) + 1;
int rightIndex = (int)this.HistoricalData.GetIndexByTime(rightTime.Ticks);
int width = this.CurrentChart.BarsWidth;
for (int i = rightIndex; i < leftIndex; i++)
{
int barX1 = (int)converter.GetChartX(this.HistoricalData.Time(i));
int barY1 = (int)converter.GetChartY(this.HistoricalData.Open(i));
int barYHigh = (int)converter.GetChartY(this.HistoricalData.High(i));
int barYLow = (int)converter.GetChartY(this.HistoricalData.Low(i));
int barX2 = barX1 + width;
int barY2 = (int)converter.GetChartY(this.HistoricalData.Close(i));
using (Brush transparentBrush = new SolidBrush(Color.FromArgb(250, 70, 70, 70)))
gr.FillRectangle(transparentBrush, barX1, barYHigh - 1, CurrentChart.BarsWidth, Math.Abs(barYLow - barYHigh) + 2);
using (Pen defaultPen = new(Color.Yellow, 3))
{
defaultPen.StartCap = LineCap.Round;
defaultPen.EndCap = LineCap.Round;
gr.DrawLine(defaultPen, barX1, barY1, barX2, barY2);
}
if (i > 0)
{
int barX0 = (int)converter.GetChartX(this.HistoricalData.Time(i - 1));
int barY0 = (int)converter.GetChartY(this.HistoricalData.Open(i - 1));
using (Pen dottedPen = new(Color.Yellow, 1))
{
dottedPen.DashStyle = DashStyle.Dot;
gr.DrawLine(dottedPen, barX2, barY2, barX0, barY0);
}
}
}
}
}
+9 -6
View File
@@ -25,7 +25,8 @@ public class JbandsIndicator : Indicator, IWatchlistIndicator
[InputParameter("vShort", sortIndex: 6, -100, 100, 1, 0)]
public int Phase { get; set; } = 10;
private Jma? jma;
private Jma? jmaUp;
private Jma? jmaLo;
protected LineSeries? UbSeries;
protected LineSeries? LbSeries;
protected string? SourceName;
@@ -46,18 +47,20 @@ public class JbandsIndicator : Indicator, IWatchlistIndicator
protected override void OnInit()
{
jma = new(Periods, phase: Phase);
jmaUp = new(Periods, phase: Phase);
jmaLo = new(Periods, phase: Phase);
SourceName = Source.ToString();
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TValue input = this.GetInputValue(args, Source);
jma!.Calc(input);
TBar input = IndicatorExtensions.GetInputBar(this, args);
jmaUp!.Calc(input.High);
jmaLo!.Calc(input.Low);
UbSeries!.SetValue(jma.UpperBand);
LbSeries!.SetValue(jma.LowerBand);
UbSeries!.SetValue(jmaUp.UpperBand);
LbSeries!.SetValue(jmaLo.LowerBand);
}
public override string ShortName => $"JBands ({Periods}:{Phase})";