Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90


Multi timeframe ema

  • Filter
  • Time
  • Show
Clear All
new posts

    Multi timeframe ema

    I am trying to add a 5 minute 20 ema to a 2 min chart and thought I would need a new indicator to do this.

    In state configure:
    AddDataSeries(BarPeriodType.Minute, 5);

    In onbarupdate:
    Value[0] = EMA(Closes[1], 20);

    The error says EMA does not return a double.
    Last edited by chartish; 04-11-2019, 12:42 PM.

    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

    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.
    Attached Files
    Kate W.NinjaTrader Customer Service


      Thank you. You guys are very helpful.


        NinjaTrader 7 says that the above file "" is corrupted and cannot be opened. Can you send me a clean version for NT 7?



          Hello goodspirit,

          Thank you for your note.

          This post is in the NinjaTrader 8 section of the forum and the example is for NinjaTrader 8 and is not compatible with NinjaTrader 7 due to large changes in NinjaScript between the two versions. However, I have created a version for NinjaTrader 7 as well.

          Please let us know if we may be of further assistance to you.
          Attached Files
          Kate W.NinjaTrader Customer Service


            Thank you for the quick response. Your attached code worked perfectly. Exactly what I needed.

            Thanks again.


              NinjaTrader_Kate can we expand this indicator in 2 ways:
              1) select price type (open / high / low / close) - (now it's CLOSE)
              2) select more data series options (like Minute/Hour/Day/Week/Month/Year + Value (like Minute + 1 = 1 minute/ Hour + 4 = 4 hour).

              So this way we would be able to access multiple time frames data from strategy without adding data series and refactoring code,


                Hello UltraNIX,

                Thank you for your reply.

                You're perfectly welcome to modify the indicator, however, it is intended as an example - a jumping off point to design your own logic. You could certainly make modifications to allow you to select the price type. However, for your second item I would caution that dynamically adding data series is unsupported and could result in errors.


                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service


                Latest Posts


                Topics Statistics Last Post
                Started by rtwave, Today, 01:49 PM
                0 responses
                Last Post rtwave
                by rtwave
                Started by papaoutai, Today, 08:56 AM
                0 responses
                Last Post papaoutai  
                Started by jdspwe, Today, 05:22 AM
                0 responses
                Last Post jdspwe
                by jdspwe
                Started by JeremuFemstu, Today, 04:49 AM
                0 responses
                Last Post JeremuFemstu  
                Started by pjsmith, Today, 04:11 AM
                1 response
                Last Post Green Lobster CBD