I am trying to create a multi-timeframe indicator both daily and weekly data-series, plotted on a daily chart. I have identified (at least from my coding knowledge so far) there is discrepancy among the indicator results from the different method I used to create the indicators.
Default Method (Indicator plots are accurate compared to ToS)
(1) Default: Add weekly data-series directly from Chart's "Insert Instrument" menu. Than add existing EMA indicators (Mov_Avg_Exponential) directly against daily and weekly data.
Coded Methods
(2) BarsOfData: Add weekly data-series directly from Chart's "Insert Instrument" menu. Than create a custom indicator using BarsOfData to retrieve weekly data, and plot the daily/weekly EMA using XAverage.
Code: Select all
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class _aa_MTF_BarsOfData : IndicatorObject
{
public _aa_MTF_BarsOfData(object _ctx) : base(_ctx) { }
private IPlotObject plot_EMA_d;
private IPlotObject plot_EMA_w;
//CustomInstrument m_WeeklyData;
XAverage ema_day;
XAverage ema_week;
protected override void Create()
{
plot_EMA_d = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red, Color.Black, 4, EPlotStyle.Solid, false));
plot_EMA_w = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Cyan, Color.Black, 4, EPlotStyle.Solid, false));
ema_day = new XAverage(this);
ema_week = new XAverage(this);
}
protected override void StartCalc()
{
ema_day.Price = Bars.Close;
ema_day.Length = 9;
ema_week.Price = BarsOfData(2).Close;
ema_week.Length = 9;
}
protected override void CalcBar()
{
plot_EMA_d.Set(ema_day[0]);
plot_EMA_w.Set(ema_week[0]);
}
}
}
Code: Select all
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class _aa_MTF_CustomInstrument : IndicatorObject
{
public _aa_MTF_CustomInstrument(object _ctx) : base(_ctx) { }
private IPlotObject plot_EMA_d;
private IPlotObject plot_EMA_w;
CustomInstrument m_WeeklyData;
XAverage ema_day;
XAverage ema_week;
protected override void Create()
{
plot_EMA_d = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red, Color.Black, 4, EPlotStyle.Solid, false));
plot_EMA_w = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Cyan, Color.Black, 4, EPlotStyle.Solid, false));
ema_day = new XAverage(this);
ema_week = new XAverage(this);
}
protected override void StartCalc()
{
m_WeeklyData = new CustomInstrument(this, new Resolution(EResolution.Week, 1));
ema_day.Price = Bars.Close;
ema_day.Length = 9;
ema_week.Price = m_WeeklyData.Close;
ema_week.Length = 9;
}
protected override void CalcBar()
{
plot_EMA_d.Set(ema_day[0]);
plot_EMA_w.Set(ema_week[0]);
}
protected override void Dispose(bool _b)
{
m_WeeklyData.Dispose();
}
}
}
Both BarsOfData and CustomInstrument indicator plot results are the same. However they are different to the default method result, which is accurate as it is compared against ToS's result.
Did I use the BarsOfData/CustomInstrument incorrectly? Any tip or help will be truly appreciated!
Thank you everyone!