I am trying to develop an indicator that uses multiple time frames. In the following code - the attempt is to have access to both the "active" time frame indicator in the chart or Market Analyzer and the higher TF indicator - Monthly in this case. Going baby steps - step 1 - replicate and plot the monthly Keltner Channel type indicator on a daily chart. The following code plots a Higher TF keltner channel on the chart - however, it is wrong - checked by plotting on a monthly chart using the regular indicator and the values are off quite a bit - so doesn't look like rounding error. Can someone point our the error and guide to the correct code. I am a total novice - so not sure where i could be wrong. Looking for code on how to correctly code HTF indicator and access the higher TF OHLC.
namespace NinjaTrader.NinjaScript.Indicators
{
/// <summary>
///
/// </summary>
public class AKShortSetUp : Indicator
{
private Series<double> diff;
private Series<double> htfClose;
private Series<double> localHigh;
private EMA emaDiff;
private EMA emaTypical;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionKeltnerChannel;
Name = "AKShortSetUp";
Period = 20;
IsOverlay = true;
IsSuspendedWhileInactive = true;
OffsetMultiplier = 0.5;
AddPlot(Brushes.DarkGray, NinjaTrader.Custom.Resource.KeltnerChannelMidline);
AddPlot(Brushes.DodgerBlue, NinjaTrader.Custom.Resource.NinjaScriptIndicatorUpper);
AddPlot(Brushes.DodgerBlue, NinjaTrader.Custom.Resource.NinjaScriptIndicatorLower);
}
else if (State == State.Configure)
{
// Add a One Month Bars object to the strategy
AddDataSeries(Data.BarsPeriodType.Month, 1);
}
else if (State == State.DataLoaded)
{
diff = new Series<double>(this);
htfClose = new Series<double>(this);
localHigh = new Series<double>(this);
emaDiff = EMA(diff, Period);
emaTypical = EMA(htfClose, Period);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
{
return;
}
foreach(int CurrentBarForSeries in CurrentBars)
{
if (CurrentBarForSeries < 1)
{
return;
}
}
diff[0] = Highs[1][0] - Lows[1][0];
htfClose[0] = Closes[1][0];
double middle = emaTypical[0];
double offset = emaDiff[0] * OffsetMultiplier;
double upper = middle + offset;
double lower = middle - offset;
Midline[0] = middle;
Upper[0] = upper;
Lower[0] = lower;
}
#region Properties
[Browsable(false)]
[XmlIgnore()]
public Series<double> Lower
{
get { return Values[2]; }
}
[Browsable(false)]
[XmlIgnore()]
public Series<double> Midline
{
get { return Values[0]; }
}
[Range(0.01, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "OffsetMultiplier", GroupName = "NinjaScriptParameters", Order = 0)]
public double OffsetMultiplier
{ get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 1)]
public int Period
{ get; set; }
[Browsable(false)]
[XmlIgnore()]
public Series<double> Upper
{
get { return Values[1]; }
}
#endregion
}
}

Comment