Update JMA_chart indicator to use Indicator base class and add painting logic

This commit is contained in:
Miha Kralj
2023-04-12 13:47:25 -07:00
parent b99d451ea8
commit b655c05d72
8 changed files with 117 additions and 105 deletions
-55
View File
@@ -1,55 +0,0 @@
using TradingPlatform.BusinessLayer;
using System.Drawing;
using QuanTAlib;
using System;
using TradingPlatform.BusinessLayer.Chart;
namespace QuanTAlib;
public abstract class QuanTAlib_Indicator : Indicator {
protected TBars bars;
protected IChartWindow mainWindow;
protected Graphics graphics;
protected int firstOnScreenBarIndex, lastOnScreenBarIndex;
protected HistoricalData History;
protected int HistPeriod;
protected override void OnInit() {
base.OnInit();
bars = new();
var dur1 = this.HistoricalData.FromTime;
var dur = this.HistoricalData.Period.Duration.TotalSeconds * (HistPeriod*4) ; //seconds of two periods
this.History = this.Symbol.GetHistory(period: this.HistoricalData.Period, fromTime: HistoricalData.FromTime);
for (int i = this.History.Count-1; i >= 0; i--) {
var rec = this.History[i, SeekOriginHistory.Begin];
bars.Add(rec.TimeLeft, rec[PriceType.Open],
rec[PriceType.High], rec[PriceType.Low],
rec[PriceType.Close], rec[PriceType.Volume]);
}
}
protected override void OnUpdate(UpdateArgs args) {
base.OnUpdate(args);
bars.Add(Time(), GetPrice(PriceType.Open),
GetPrice(PriceType.High),
GetPrice(PriceType.Low),
GetPrice(PriceType.Close),
GetPrice(PriceType.Volume),
update: !(args.Reason == UpdateReason.NewBar || args.Reason == UpdateReason.HistoricalBar));
}
public override void OnPaintChart(PaintChartEventArgs args) {
base.OnPaintChart(args);
if (this.CurrentChart == null) return;
graphics = args.Graphics;
mainWindow = this.CurrentChart.MainWindow;
DateTime leftTime = mainWindow.CoordinatesConverter.GetTime(mainWindow.ClientRectangle.Left);
DateTime rightTime = mainWindow.CoordinatesConverter.GetTime(mainWindow.ClientRectangle.Right);
firstOnScreenBarIndex = (int)mainWindow.CoordinatesConverter.GetBarIndex(leftTime);
lastOnScreenBarIndex = (int)Math.Ceiling(mainWindow.CoordinatesConverter.GetBarIndex(rightTime));
}
}
+43 -1
View File
@@ -3,9 +3,10 @@ using System.Diagnostics;
using System.Drawing;
using System.Linq;
using TradingPlatform.BusinessLayer;
using TradingPlatform.BusinessLayer.Chart;
namespace QuanTAlib;
public class JMA_chart : QuanTAlib_Indicator {
public class JMA_chart : Indicator {
#region Parameters
[InputParameter("Data source", 0, variants: new object[]
@@ -31,6 +32,12 @@ public class JMA_chart : QuanTAlib_Indicator {
private JMA_Series indicator;
///////
protected TBars bars;
protected IChartWindow mainWindow;
protected Graphics graphics;
protected int firstOnScreenBarIndex, lastOnScreenBarIndex;
protected HistoricalData History;
protected int HistPeriod;
public JMA_chart() :base() {
Name = "JMA - Jurik Moving Avg";
Description = "Jurik Moving Average description";
@@ -42,6 +49,21 @@ public class JMA_chart : QuanTAlib_Indicator {
protected override void OnInit() {
base.OnInit();
bars = new();
var dur1 = this.HistoricalData.FromTime;
var dur = this.HistoricalData.Period.Duration.TotalSeconds * (HistPeriod * 4); //seconds of two periods
this.History = this.Symbol.GetHistory(period: this.HistoricalData.Period, fromTime: HistoricalData.FromTime);
for (int i = this.History.Count - 1; i >= 0; i--) {
var rec = this.History[i, SeekOriginHistory.Begin];
bars.Add(rec.TimeLeft, rec[PriceType.Open],
rec[PriceType.High], rec[PriceType.Low],
rec[PriceType.Close], rec[PriceType.Volume]);
}
indicator = new(source: bars.Select(DataSource), period: Period,
phase: Jphase, vshort: Vshort, vlong: Vlong,
useNaN: true);
@@ -49,6 +71,26 @@ public class JMA_chart : QuanTAlib_Indicator {
protected override void OnUpdate(UpdateArgs args) {
base.OnUpdate(args);
bars.Add(Time(), GetPrice(PriceType.Open),
GetPrice(PriceType.High),
GetPrice(PriceType.Low),
GetPrice(PriceType.Close),
GetPrice(PriceType.Volume),
update: !(args.Reason == UpdateReason.NewBar || args.Reason == UpdateReason.HistoricalBar));
this.SetValue(indicator[^1].v, lineIndex: 0);
}
public override void OnPaintChart(PaintChartEventArgs args) {
base.OnPaintChart(args);
if (this.CurrentChart == null)
return;
graphics = args.Graphics;
mainWindow = this.CurrentChart.MainWindow;
DateTime leftTime = mainWindow.CoordinatesConverter.GetTime(mainWindow.ClientRectangle.Left);
DateTime rightTime = mainWindow.CoordinatesConverter.GetTime(mainWindow.ClientRectangle.Right);
firstOnScreenBarIndex = (int)mainWindow.CoordinatesConverter.GetBarIndex(leftTime);
lastOnScreenBarIndex = (int)Math.Ceiling(mainWindow.CoordinatesConverter.GetBarIndex(rightTime));
}
}
+2 -2
View File
@@ -216,7 +216,7 @@ public class MovingAverage_chart : Indicator {
overunder = new(MA1, MA2);
trades = new(MA1, MA2);
equity = new(trades,prices: bars.Open,Long:LongTrades,Short:ShortTrades,Warmup:MA1Period+MA2Period);
equity = new(trades,price: bars.Open,warmup:MA1Period+MA2Period);
}
protected override void OnUpdate(UpdateArgs args) {
@@ -253,7 +253,7 @@ public class MovingAverage_chart : Indicator {
}
public override void OnPaintChart(PaintChartEventArgs args) {
base.OnPaintChart(args);
if (this.CurrentChart == null) return;
if (this.CurrentChart == null) {return;}
Graphics graphics = args.Graphics;
var mainWindow = this.CurrentChart.MainWindow;
int leftIndex = (int)mainWindow.CoordinatesConverter.GetBarIndex(mainWindow.CoordinatesConverter.GetTime(mainWindow.ClientRectangle.Left));