charts for Quantower
This commit is contained in:
Miha Kralj
2024-11-06 20:56:32 -08:00
parent 0bae9ce15b
commit 582a0256ec
75 changed files with 652 additions and 281 deletions
+53
View File
@@ -0,0 +1,53 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class AdxIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int Periods { get; set; } = 14;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Adx? adx;
protected LineSeries? AdxSeries;
public int MinHistoryDepths => Math.Max(5, Periods * 3); // Need extra periods for ADX calculation
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public AdxIndicator()
{
Name = "ADX - Average Directional Movement Index";
Description = "Measures the strength of a trend, regardless of its direction.";
SeparateWindow = true;
AdxSeries = new($"ADX {Periods}", color: IndicatorExtensions.Momentum, 2, LineStyle.Solid);
AddLineSeries(AdxSeries);
}
protected override void OnInit()
{
adx = new Adx(Periods);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TBar input = IndicatorExtensions.GetInputBar(this, args);
TValue result = adx!.Calc(input);
AdxSeries!.SetValue(result.Value);
AdxSeries!.SetMarker(0, Color.Transparent);
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"ADX ({Periods})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintSmoothCurve(args, AdxSeries!, adx!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+55
View File
@@ -0,0 +1,55 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class AdxrIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int Periods { get; set; } = 14;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Adxr? adxr;
protected LineSeries? AdxrSeries;
public int MinHistoryDepths => Math.Max(5, Periods * 4); // Need extra periods for ADXR calculation
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public AdxrIndicator()
{
Name = "ADXR - Average Directional Movement Index Rating";
Description = "Measures trend strength by comparing current ADX with historical ADX values.";
SeparateWindow = true;
AdxrSeries = new($"ADXR {Periods}", Color.Blue, 2, LineStyle.Solid);
AddLineSeries(AdxrSeries);
}
protected override void OnInit()
{
adxr = new Adxr(Periods);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TBar input = IndicatorExtensions.GetInputBar(this, args);
TValue result = adxr!.Calc(input);
AdxrSeries!.SetValue(result.Value);
AdxrSeries!.SetMarker(0, Color.Transparent);
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"ADXR ({Periods})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintHLine(args, 25, new Pen(color: IndicatorExtensions.Momentum, width: 1)); // Strong trend line
this.PaintHLine(args, 20, new Pen(color: IndicatorExtensions.Momentum, width: 1)); // Weak trend line
this.PaintSmoothCurve(args, AdxrSeries!, adxr!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+71
View File
@@ -0,0 +1,71 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class ApoIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Fast Period", sortIndex: 1, 1, 2000, 1, 0)]
public int FastPeriod { get; set; } = 12;
[InputParameter("Slow Period", sortIndex: 2, 1, 2000, 1, 0)]
public int SlowPeriod { get; set; } = 26;
[InputParameter("Data source", sortIndex: 4, variants: [
"Open", SourceType.Open,
"High", SourceType.High,
"Low", SourceType.Low,
"Close", SourceType.Close,
"HL/2 (Median)", SourceType.HL2,
"OC/2 (Midpoint)", SourceType.OC2,
"OHL/3 (Mean)", SourceType.OHL3,
"HLC/3 (Typical)", SourceType.HLC3,
"OHLC/4 (Average)", SourceType.OHLC4,
"HLCC/4 (Weighted)", SourceType.HLCC4
])]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Apo? apo;
protected LineSeries? ApoSeries;
public int MinHistoryDepths => Math.Max(FastPeriod, SlowPeriod) * 2;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public ApoIndicator()
{
Name = "APO - Absolute Price Oscillator";
Description = "Shows the difference between two moving averages of different periods.";
SeparateWindow = true;
ApoSeries = new($"APO {FastPeriod},{SlowPeriod}", color: IndicatorExtensions.Momentum, 2, LineStyle.Solid);
AddLineSeries(ApoSeries);
}
protected override void OnInit()
{
apo = new Apo(FastPeriod, SlowPeriod);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TValue input = this.GetInputValue(args, Source);
TValue result = apo!.Calc(input);
ApoSeries!.SetValue(result.Value);
ApoSeries!.SetMarker(0, Color.Transparent);
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"APO ({FastPeriod},{SlowPeriod})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintSmoothCurve(args, ApoSeries!, apo!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+59
View File
@@ -0,0 +1,59 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class DmiIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int Periods { get; set; } = 14;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Dmi? dmi;
protected LineSeries? PlusDiSeries;
protected LineSeries? MinusDiSeries;
public int MinHistoryDepths => Math.Max(5, Periods * 2);
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public DmiIndicator()
{
Name = "DMI - Directional Movement Index";
Description = "Identifies the directional movement of a price by comparing successive highs and lows.";
SeparateWindow = true;
PlusDiSeries = new($"+DI {Periods}", color: Color.Red, 2, LineStyle.Solid);
MinusDiSeries = new($"-DI {Periods}", color: Color.Blue, 2, LineStyle.Solid);
AddLineSeries(PlusDiSeries);
AddLineSeries(MinusDiSeries);
}
protected override void OnInit()
{
dmi = new Dmi(Periods);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TBar input = IndicatorExtensions.GetInputBar(this, args);
var result = dmi!.Calc(input);
PlusDiSeries!.SetValue(dmi.PlusDI);
MinusDiSeries!.SetValue(dmi.MinusDI);
PlusDiSeries!.SetMarker(0, Color.Transparent);
MinusDiSeries!.SetMarker(0, Color.Transparent);
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"DMI ({Periods})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintSmoothCurve(args, PlusDiSeries!, dmi!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
this.PaintSmoothCurve(args, MinusDiSeries!, dmi!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+68
View File
@@ -0,0 +1,68 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class DmxIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("DMI Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int DmiPeriods { get; set; } = 14;
[InputParameter("JMA Smoothing Periods", sortIndex: 2, 1, 2000, 1, 0)]
public int JmaPeriods { get; set; } = 12;
[InputParameter("JMA Phase", sortIndex: 3, -100, 100, 1, 0)]
public int JmaPhase { get; set; } = 100;
[InputParameter("JMA Factor", sortIndex: 4, 0.01, 1, 0.01, 2)]
public double JmaFactor { get; set; } = 0.3;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Dmx? dmx;
protected LineSeries? PlusDiSeries;
protected LineSeries? MinusDiSeries;
public int MinHistoryDepths => Math.Max(5, (DmiPeriods + JmaPeriods) * 2);
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public DmxIndicator()
{
Name = "DMX - Enhanced Directional Movement Index";
Description = "An enhanced version of DMI using JMA smoothing for better noise reduction and responsiveness.";
SeparateWindow = true;
PlusDiSeries = new($"+DI {DmiPeriods}", color: Color.Red, 2, LineStyle.Solid);
MinusDiSeries = new($"-DI {DmiPeriods}", color: Color.Blue, 2, LineStyle.Solid);
AddLineSeries(PlusDiSeries);
AddLineSeries(MinusDiSeries);
}
protected override void OnInit()
{
dmx = new Dmx(DmiPeriods, JmaPeriods, JmaPhase, JmaFactor);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TBar input = IndicatorExtensions.GetInputBar(this, args);
var result = dmx!.Calc(input);
PlusDiSeries!.SetValue(dmx.PlusDI);
MinusDiSeries!.SetValue(dmx.MinusDI);
PlusDiSeries!.SetMarker(0, Color.Transparent);
MinusDiSeries!.SetMarker(0, Color.Transparent);
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"DMX ({DmiPeriods})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintSmoothCurve(args, PlusDiSeries!, dmx!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
this.PaintSmoothCurve(args, MinusDiSeries!, dmx!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+67
View File
@@ -0,0 +1,67 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class DpoIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 2000, 1, 0)]
public int Period { get; set; } = 20;
[InputParameter("Data source", sortIndex: 2, variants: [
"Open", SourceType.Open,
"High", SourceType.High,
"Low", SourceType.Low,
"Close", SourceType.Close,
"HL/2 (Median)", SourceType.HL2,
"OC/2 (Midpoint)", SourceType.OC2,
"OHL/3 (Mean)", SourceType.OHL3,
"HLC/3 (Typical)", SourceType.HLC3,
"OHLC/4 (Average)", SourceType.OHLC4,
"HLCC/4 (Weighted)", SourceType.HLCC4
])]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 3)]
public bool ShowColdValues { get; set; } = true;
private Dpo? Dpo;
protected LineSeries? DpoSeries;
public int MinHistoryDepths => Period * 2;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public DpoIndicator()
{
Name = "DPO - Detrended Price Oscillator";
Description = "Removes trend from price by comparing current price to a past moving average, helping identify cycles in the price.";
SeparateWindow = true;
DpoSeries = new($"DPO {Period}", color: IndicatorExtensions.Momentum, 2, LineStyle.Solid);
AddLineSeries(DpoSeries);
}
protected override void OnInit()
{
Dpo = new Dpo(Period);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TBar input = this.GetInputBar(args);
TValue result = Dpo!.Calc(input);
DpoSeries!.SetValue(result.Value);
DpoSeries!.SetMarker(0, Color.Transparent);
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"DPO ({Period})";
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintSmoothCurve(args, DpoSeries!, Dpo!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+146
View File
@@ -0,0 +1,146 @@
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Drawing.Drawing2D;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class MacdIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Slow EMA", sortIndex: 1, 1, 1000, 1, 0)]
public int Slow { get; set; } = 26;
[InputParameter("Fast EMA", sortIndex: 2, 1, 2000, 1, 0)]
public int Fast { get; set; } = 12;
[InputParameter("Signal line", sortIndex: 3, 1, 2000, 1, 0)]
public int Signal { get; set; } = 9;
[InputParameter("Use SMA for warmup period", sortIndex: 2)]
public bool UseSMA { get; set; } = false;
[InputParameter("Data source", sortIndex: 3, variants: [
"Open", SourceType.Open,
"High", SourceType.High,
"Low", SourceType.Low,
"Close", SourceType.Close,
"HL/2 (Median)", SourceType.HL2,
"OC/2 (Midpoint)", SourceType.OC2,
"OHL/3 (Mean)", SourceType.OHL3,
"HLC/3 (Typical)", SourceType.HLC3,
"OHLC/4 (Average)", SourceType.OHLC4,
"HLCC/4 (Weighted)", SourceType.HLCC4
])]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Macd? macd;
private Slope? histSlope;
protected LineSeries? MainSeries;
protected LineSeries? SignalSeries;
protected LineSeries? HistogramSeries;
protected LineSeries? HistSlopeSeries;
protected string? SourceName;
public int MinHistoryDepths => Slow;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"MACD {Slow}:{Fast}:{Signal}";
public MacdIndicator()
{
OnBackGround = true;
SeparateWindow = true;
SourceName = Source.ToString();
Name = "MACD - Moving Average Convergence Divergence";
Description = "MACD";
MainSeries = new(name: $"MAIN", color: Color.RoyalBlue, width: 2, style: LineStyle.Solid);
SignalSeries = new(name: $"SIGNAL", color: Color.Red, width: 2, style: LineStyle.Solid);
HistogramSeries = new(name: $"HISTOGRAM", color: Color.White, width: 2, style: LineStyle.Solid);
HistSlopeSeries = new(name: $"SLOPE", color: Color.Transparent, width: 2, style: LineStyle.Solid);
HistSlopeSeries.Visible = false;
AddLineSeries(MainSeries);
AddLineSeries(SignalSeries);
AddLineSeries(HistogramSeries);
AddLineSeries(HistSlopeSeries);
}
protected override void OnInit()
{
macd = new(fastPeriod: Fast, slowPeriod: Slow, signalPeriod: Signal);
histSlope = new(2);
SourceName = Source.ToString();
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TValue input = this.GetInputValue(args, Source);
macd!.Calc(input);
double main = macd.MacdLine;
double signal = macd.SignalLine;
double histogram = macd.Value;
histSlope!.Calc(histogram);
MainSeries!.SetValue(main);
MainSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
SignalSeries!.SetValue(signal);
SignalSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
HistogramSeries!.SetValue(histogram);
HistogramSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
HistSlopeSeries!.SetValue(histSlope.Value);
HistSlopeSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
}
#pragma warning disable CA1416 // Validate platform compatibility
public override void OnPaintChart(PaintChartEventArgs args)
{
Graphics gr = args.Graphics;
gr.SmoothingMode = 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);
for (int i = rightIndex; i < leftIndex; i++)
{
int barX = (int)converter.GetChartX(this.HistoricalData.Time(i));
int barY = (int)converter.GetChartY(HistogramSeries![i] * 2.0);
int barY0 = (int)converter.GetChartY(0);
int HistBarWidth = this.CurrentChart.BarsWidth - 2;
Brush lowGreen = new SolidBrush(Color.FromArgb(255, 0, 100, 0));
Brush highGreen = new SolidBrush(Color.FromArgb(255, 50, 255, 50));
Brush lowRed = new SolidBrush(Color.FromArgb(255, 100, 0, 0));
Brush highRed = new SolidBrush(Color.FromArgb(255, 255, 50, 50));
if (HistogramSeries[i] > 0)
{
Brush col = HistSlopeSeries![i] > 0 ? highGreen : lowGreen;
gr.FillRectangle(col, barX, barY, HistBarWidth, Math.Abs(barY - barY0));
}
else
{
Brush col = HistSlopeSeries![i] < 0 ? highRed : lowRed;
gr.FillRectangle(col, barX, barY0, HistBarWidth, Math.Abs(barY0 - barY));
}
}
this.PaintSmoothCurve(args, MainSeries!, macd!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.3);
this.PaintSmoothCurve(args, SignalSeries!, macd!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
base.OnPaintChart(args);
}
}
+1 -1
View File
@@ -27,4 +27,4 @@
<Copy SourceFiles="$(OutputPath)\Momentum.dll" DestinationFolder="$(QuantowerRoot)\Settings\Scripts\Indicators\QuanTAlib\Momentum" />
</Target>
</Project>
</Project>