Announcement

Collapse

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

Collapse

Multi timeframe ema

Collapse
X
 
  • 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.

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

    Comment


      #3
      Thank you. You guys are very helpful.

      Comment


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

        Thanks.

        Comment


          #5
          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

          Comment


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

            Thanks again.

            Comment


              #7
              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,

              Comment


                #8
                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

                Comment


                  #9
                  I'm looking for this exact same thing, but I'd like to have a 10000 tick chart 20 EMA on my 2000 Tick chart. Is there a way to change this indicator?

                  Comment


                    #10
                    Hello russ4444h,

                    Thank you for your reply.

                    Yes, you can modify the indicator (either the version for NinjaTrader 7 or NinjaTrader 8 above). The only necessary change would be the added data series.

                    For the NinjaTrader 7 version:

                    Add(PeriodType.Minute, 5);

                    Would need to be changed to

                    Add(PeriodType.Tick, 10000);

                    For NinjaTrader 8:

                    AddDataSeries(Data.BarsPeriodType.Minute, 5);

                    Would need to be changed to

                    AddDataSeries(Data.BarsPeriodType.Tick, 10000);

                    That should be all that's necessary to change to have this calculate off a 10000 tick series.

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

                    Comment


                      #11
                      Originally posted by NinjaTrader_Kate View Post
                      Hello russ4444h,

                      Thank you for your reply.

                      Yes, you can modify the indicator (either the version for NinjaTrader 7 or NinjaTrader 8 above). The only necessary change would be the added data series.

                      For the NinjaTrader 7 version:

                      Add(PeriodType.Minute, 5);

                      Would need to be changed to

                      Add(PeriodType.Tick, 10000);

                      For NinjaTrader 8:

                      AddDataSeries(Data.BarsPeriodType.Minute, 5);

                      Would need to be changed to

                      AddDataSeries(Data.BarsPeriodType.Tick, 10000);

                      That should be all that's necessary to change to have this calculate off a 10000 tick series.

                      Please let us know if we may be of further assistance to you.
                      That worked perfectly. Thank you for the help.

                      Comment


                        #12
                        This indicator works fine on my NT8 with the exception that is drawing the EMA like a steps? Any idea for this solution?
                        Click image for larger version

Name:	Screenshot 2023-09-16 111056.png
Views:	682
Size:	65.6 KB
ID:	1269256

                        Comment


                          #13
                          Hello satmatw,

                          Thanks for your notes.

                          This is the expected behavior of the EMAon5minuteData script shared on post # 2 when it is applied to the chart window with a lower timeframe, such as a 1-minute chart.

                          If the script is applied to a higher timeframe chart, such as a 10-minute chart, the indicator will not display as 'steps'.




                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            Hey guys. I did the simple solution using data series to plot a 15min 20SMA on a 5min chart, but if I change time frames to the 15min on this 5min chart and then return to 5min, that 15min 20SMA is now 5min 20SMA... is there anyway to fix this; I mean, to be able to change timeframes without having to plot it all again on the data series?
                            Thanks

                            Comment


                              #15
                              Hello leonardomocci,

                              Thanks for your notes.

                              If you are calling AddDataSeries() and are calculating the SMA in the script based on the BarsArray of the added series, the SMA would calculate from that added series, not from the primary series.

                              For example:

                              //class level variable
                              private SMA mySMA;

                              //State.Configure
                              AddDataSeries(BarsPeriodType.Minute, 15);

                              //State.DataLoaded
                              mySMA = SMA(BarsArray[1], 20);


                              See this help guide page for more information about Multi-TimeFrame/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              41 views
                              0 likes
                              Last Post alifarahani  
                              Working...
                              X