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

Indicator using two data series

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Indicator using two data series

    Hello.

    I have two charts (30 minute chart and 1 minute chart)

    I'm trying to have a 24 EMA using the data series of the 1 min chart to show up on the 30 min chart.

    When I add several data series to the chart the chart has gaps. I don't want this.

    I have no problem coding this solution but I would like to be pointed in the right direction.

    Ideally I would only have one data series on the chart and my indicators would use another time frame as the input data source.

    How can I do this?

    thanks!
    CPG


    #2
    Hello CopyPasteGhost,

    Are these in separate panels or the same panel?

    Is Equidistant Bar Spacing checked and enabled in the chart properties?
    https://ninjatrader.com/support/help...AndDefinitions
    https://ninjatrader.com/support/help...ipleDataSeries


    It is possible to add a data series to an indicator with AddDataSeries().
    https://ninjatrader.com/support/help...dataseries.htm
    https://ninjatrader.com/support/helpGuides/nt8/barsarray.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,
      Thanks so much for getting back to me so quickly!

      They are in the same panel.
      Equidistant Bar Spacing has been set to false.


      As you can see the top chart has spacing.
      I want to remove this spacing...


      What did I do wrong?
      Thanks!

      Comment


        #4
        Hello CopyPasteGhost,

        They are in different panels. There are 3 panels, and there is a data series in panel 1 and a data series in panel 3. Check this in the Data Series window.

        However by being on the same chart in different panels, they still have to share the same time scale at the bottom.


        That said, adding the data series to the indicator would not have this issue.

        Also, I forgot I had an example.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you very much for that example. I'm still trying to get something really simple to work and for some reason it's evading me!

          The image I have a simple 15 Min ES chart.
          I have a 165 Indicator in the 2nd panel using the 15M Data series (no custom indicators)

          In the 3rd panel I have the custom indicator I wrote. (I basically took the code from the @EMA file and changed the data series. Nothing seems to be printing for this chart. I have attached my .CS file.

          Any ideas?
          CPG

          Comment


            #6
            Here is the file.
            Attached Files

            Comment


              #7
              Hello CopyPasteGhost,

              Are you ensuring a value is set for every primary bar as demonstrated in the example I have provided a link to?

              Print the value of IsValidDataPoint for the Values series on every primary bar when BarsInProgress is 0 at the end of OnBarUpdate.


              Is the value set on every primary bar?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thank you for your help. This is actually my first go at writing anything for NinjaTrader. Thanks for your patience with me.

                This is the error I'm getting:

                Time Category Message
                4/14/2020 11:28:17 AM Default Indicator 'EMA 165 One Min Chart': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                I have updated the OnBarUpdate method to contain this.

                protected override void OnBarUpdate()
                {
                var pointToPlot = CurrentBar == 0 ? OneMinuteChart[0] : OneMinuteChart[0] * constant1 + constant2 * Value[1];
                Print(pointToPlot);
                if (Value.IsValidDataPoint(CurrentBar) && BarsInProgress == 0)
                {
                Print("Bar #: " + CurrentBar + " Value To Plot: " + pointToPlot);
                Value[0] = pointToPlot;
                }
                }

                As you see from the error... Nothing is happening and it's crashing.
                Any thoughts?

                Comment


                  #9
                  Hello CopyPasteGhost,

                  If CurrentBar is 0, then Value[1] does not exist. Using 1 barAgo on the first bar is not a valid barsAgo index.

                  Below is a link to a forum post on index errors.
                  Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Since you are not interested in the primary data series, you should skip it by adding this

                    Code:
                    protected override void OnBarUpdate()
                    {
                        if(BarsInProgress == 0 || CurrentBars[0] < 1) {
                             return;
                        }
                        Value[0] = EMA(OneMinuteChart, 24)[0];
                    Edit: Code clean up & CurrentBars check
                    Last edited by MojoJojo; 04-14-2020, 10:15 AM.

                    Comment


                      #11
                      Thank you to both of you. I'm reviewing all your suggestions now. I'll post back with some code examples for any other questions.

                      Comment


                        #12
                        MojoJojo - Why would we be checking the CurrentBars[0]. Wouldn't we want to check the CurrentBars[1]. as we want this EMA to print for the Added Data Series and not the primary data series.

                        Did I miss something?

                        Comment


                          #13
                          Hello CopyPasteGhost,

                          You would want to check all series to ensure that the barsAgo index used is valid.

                          See the link to the help guide on CurrentBars in the post about indexing errors.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Alright Thanks. I am no longer getting any index errors. Here is my current code:

                            private Series<double> OneMinuteChart;
                            private int period = 165;

                            protected override void OnStateChange()
                            {
                            if (State == State.SetDefaults)
                            {
                            Description = @"Description";
                            Name = "EMA " + period + " One Min Chart";
                            Calculate = Calculate.OnEachTick;
                            IsOverlay = false;
                            BarsRequiredToPlot = 1;
                            AddPlot(Brushes.Goldenrod, "1M EMA " + period);
                            }
                            else if (State == State.Configure)
                            {
                            AddDataSeries("NQ 06-20", BarsPeriodType.Minute, 1);
                            }
                            else if (State == State.DataLoaded)
                            {
                            OneMinuteChart = new Series<double>(BarsArray[1]);
                            }
                            }

                            protected override void OnBarUpdate()
                            {
                            if (!CurrentBars.Any(v => v < 1))
                            {
                            if (BarsInProgress == 0)
                            {
                            //Base Chart Time Frame - Skip
                            return;
                            }
                            else if (BarsInProgress == 1)
                            {
                            Value[0] = EMA(OneMinuteChart, 24)[0];
                            return;
                            }
                            }
                            }

                            I am still not getting the results I expect. The chart is just staying on the "0" Line as shown in the image.

                            I must be close at this point....right?



                            Comment


                              #15
                              Originally posted by CopyPasteGhost View Post
                              Why would we be checking the CurrentBars[0]. Wouldn't we want to check the CurrentBars[1]
                              The problem is that your primary data series is 30 min and the secondary 1 min, that is why there will be no bar for the 30 min until the 1 min reaches 30 bars.
                              I guess this only applies if you set the calculation to OnBarClose, but I haven't tested it and it's good to err on the side of caution.

                              Since I was interested in how to best synchronize the two series, I've done some testing.
                              The solution I've arrived at is.

                              Code:
                                          if(BarsInProgress == 0 || CurrentBars[0] < 1) {
                                              return;
                                          }
                              
                                          if( Calculate != Calculate.OnBarClose
                                              || (Calculate == Calculate.OnBarClose
                                                  && Times[1][0].CompareTo(Times[0][0]) >= 0)
                                              ) {
                                              int barsAgo = CurrentBars[1] - BarsArray[1].GetBar(Times[0][0]);
                                              Value[0] = EMA(Inputs[1], 24)[barsAgo];
                                          }
                              The drawback is that for calculation OnBarClose you won't get an EMA value for the current 30 min bar. You could switch to OnEachTick, if that is what you need.

                              Edit:
                              The old time comparision required tweaking
                              Old:
                              Code:
                                          if(Times[1][0].CompareTo(Times[0][0]) > 0) {
                                              int barsAgo = CurrentBars[1] - BarsArray[1].GetBar(Times[0][0]);
                                              Value[0] = EMA(Inputs[1], 24)[barsAgo];
                                          }
                              Last edited by MojoJojo; 04-14-2020, 01:05 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kujista, Today, 05:44 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by elderan, Yesterday, 08:03 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by samish18, Yesterday, 08:57 AM
                              8 responses
                              25 views
                              0 likes
                              Last Post samish18  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              3 responses
                              10 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by RookieTrader, Today, 07:41 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X