I am looking for some advice on how to get my strategy to plot the secondary series correctly. I have a secondary data series in a test strategy and have added the MACD indicator to both the primary and secondary data. After much trial and error, the primary MACD plots correctly, but the secondary is stepped. Other than that it seems to be working correctly.
Not sure what I'm doing wrong, but is there a way to plot this correctly (not stepped)? Once I get this working, I also plan to add the Stochastics and Swing indicator to both primary and secondary series and use them in a strategy
Here is my current test code:
namespace NinjaTrader.NinjaScript.Strategies
{
public class MACDWithSecondarySeriesStrategy : Strategy
{
private MACD primaryMACD;
private MACD secondaryMACD;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Strategy with primary and secondary plots";
Name = "MACDWithSecondarySeries";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
// Default parameters
MACDFast = 12;
MACDSlow = 26;
MACDSmooth = 9;
PlotPrimaryMACD = true;
PlotSecondaryMACD = true;
SecondarySeriesPeriod = 60;
AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Line, "SecondaryMACD");
AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.Line, "SecondaryMACDAvg");
AddPlot(new Stroke(Brushes.Gray, 2), PlotStyle.Bar, "SecondaryMACDDiff");
}
else if (State == State.Configure)
{
// Add secondary bars series
AddDataSeries(BarsPeriodType.Minute, SecondarySeriesPeriod);
}
else if (State == State.DataLoaded)
{
primaryMACD = MACD(Closes[0], MACDFast, MACDSlow, MACDSmooth);
secondaryMACD = MACD(Closes[1], MACDFast, MACDSlow, MACDSmooth);
if (PlotPrimaryMACD)
{
AddChartIndicator(primaryMACD);
}
}
}
protected override void OnBarUpdate()
{
// Ensure we have enough bars to calculate
if (CurrentBars[1] < SecondarySeriesPeriod)
return;
if (PlotSecondaryMACD)
{
Values[0][0] = secondaryMACD.Default[0];
Values[1][0] = secondaryMACD.Avg[0];
Values[2][0] = secondaryMACD.Diff[0];
}
if (BarsInProgress != 0)
return;
// Example strategy logic
if (primaryMACD.Diff[0] > 0 && primaryMACD.Diff[1] <= 0)
{
EnterLong();
}
else if (primaryMACD.Diff[0] < 0 && primaryMACD.Diff[1] >= 0)
{
EnterShort();
}
}
region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="MACD Fast", Description="Fast period for MACD", Order=1, GroupName="MACD Parameters")]
public int MACDFast { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="MACD Slow", Description="Slow period for MACD", Order=2, GroupName="MACD Parameters")]
public int MACDSlow { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="MACD Smooth", Description="Smoothing period for MACD", Order=3, GroupName="MACD Parameters")]
public int MACDSmooth { get; set; }
[NinjaScriptProperty]
[Display(Name="Plot Primary MACD", Description="Show primary MACD plot", Order=4, GroupName="MACD Parameters")]
public bool PlotPrimaryMACD { get; set; }
[NinjaScriptProperty]
[Display(Name="Plot Secondary MACD", Description="Show secondary MACD plot", Order=5, GroupName="MACD Parameters")]
public bool PlotSecondaryMACD { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Secondary Series Period", Description="Period for secondary bars series (minutes)", Order=1, GroupName="Secondary Series")]
public int SecondarySeriesPeriod { get; set; }
#endregion
}
}
Thank you,
Ray

Comment