Originally posted by NinjaTrader_Kate
View Post
Hello chartish,
Thank you for your post.
You could do this in one of two ways. One would be to use a secondary data series on the chart and simply have the EMA calculate on that secondary series, but apply it to plot over your primary data series.
To add an indicator based off of a 5 minute chart, plotted over a 2 minute chart, please follow the instructions below:
* First create a 2 minute chart as usual (File>New>Chart...)
* Right click within your chart window and select Data Series...
* Add a second data series, the same instrument as your 2 minute chart, but set the parameters to be a 5 minute chart
* You should now see two-paneled chart with 2 minute on top and 5 minute below
* Next, right click and select Indicators... Find your indicator in the top left hand corner, and click New to add it.
* In the parameters to the right, set your Input Series to your 5 minute data series (Close)
* Set your Panel to 1,and click OK
* Next, move your pointer to the right of your price axis (y-axis) on the top panel of your chart window, and right click>Maximize.
You now should see a 2 minute chart, with an indicator plotted over it based off of a 5 minute Data series.
Your other option would be to create an indicator as you've started doing.
You are basically assigning the ema data series to current bar of the plot, so all you theoretically need is to change that line to this:
Value[0] = EMA(Closes[1], 20)[0];
However, working with multiple data series is a bit more complicated than using a single data series in an indicator.
Here's a simple example using some better practices, including checking on the current bar, using BarsInProgress to plot on the 2 minute data points, and using a private EMA variable to hold our EMA values from the secondary series. (I've attached the full code for this below):
private EMA EMA1; // we create a new array of EMA values
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "EMAon5minData";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
AddPlot(Brushes.LightCoral, "EMAPlot");
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, 5); //add our secondary 5 minute data series for calculating the EMA
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(BarsArray[1], 20); //set EMA here so we make sure it's calculated on the secondary data series
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 1 || CurrentBars[1] < 20) // make sure there's at least one bar for primary series and at least 20 of the secondary series prior to processing
return;
if(BarsInProgress == 0) // if OnBarUpdate was called from the primary bar series, then set the current value to the latest EMA1 value
Value[0] = EMA1[0];
}
If I may be of further assistance, please let me know.
Thank you for your post.
You could do this in one of two ways. One would be to use a secondary data series on the chart and simply have the EMA calculate on that secondary series, but apply it to plot over your primary data series.
To add an indicator based off of a 5 minute chart, plotted over a 2 minute chart, please follow the instructions below:
* First create a 2 minute chart as usual (File>New>Chart...)
* Right click within your chart window and select Data Series...
* Add a second data series, the same instrument as your 2 minute chart, but set the parameters to be a 5 minute chart
* You should now see two-paneled chart with 2 minute on top and 5 minute below
* Next, right click and select Indicators... Find your indicator in the top left hand corner, and click New to add it.
* In the parameters to the right, set your Input Series to your 5 minute data series (Close)
* Set your Panel to 1,and click OK
* Next, move your pointer to the right of your price axis (y-axis) on the top panel of your chart window, and right click>Maximize.
You now should see a 2 minute chart, with an indicator plotted over it based off of a 5 minute Data series.
Your other option would be to create an indicator as you've started doing.
You are basically assigning the ema data series to current bar of the plot, so all you theoretically need is to change that line to this:
Value[0] = EMA(Closes[1], 20)[0];
However, working with multiple data series is a bit more complicated than using a single data series in an indicator.
Here's a simple example using some better practices, including checking on the current bar, using BarsInProgress to plot on the 2 minute data points, and using a private EMA variable to hold our EMA values from the secondary series. (I've attached the full code for this below):
private EMA EMA1; // we create a new array of EMA values
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "EMAon5minData";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
AddPlot(Brushes.LightCoral, "EMAPlot");
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, 5); //add our secondary 5 minute data series for calculating the EMA
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(BarsArray[1], 20); //set EMA here so we make sure it's calculated on the secondary data series
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 1 || CurrentBars[1] < 20) // make sure there's at least one bar for primary series and at least 20 of the secondary series prior to processing
return;
if(BarsInProgress == 0) // if OnBarUpdate was called from the primary bar series, then set the current value to the latest EMA1 value
Value[0] = EMA1[0];
}
If I may be of further assistance, please let me know.

Comment