Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why does EMA(n) vs EMA(BarsArray[1], n) show different results using same time frame?

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

    Why does EMA(n) vs EMA(BarsArray[1], n) show different results using same time frame?

    I have attached a small Strategy that shows some interesting (and quite possibly incorrect) results. Regardless of correctness, I need help in explaining the results I found.

    Here is what the attached toy strategy does:

    In Initialize:
    Create a secondary data series which effectively duplicates the primary data series (eg, call Add() with same BarType and same BarInterval as primary data series).

    In OnBarUpdate:
    Compare EMA(n)[0] against EMA(BarsArray[1], n)[0] ... I chose n = 7 ...

    [Note carefully: the intent is to create BarsArray[1] to be the same as the primary data series of the chart, in effect the data in BarsArray[1] should become an exact duplicate of data in primary BarsArray[0], right?]

    Compile:
    Run strategy in Strategy Analyzer. Run simple test. Eq, try ES, 3 min, and test just a few days, such as 11/3 -> 11/4. But choice of instrument or BarType or BarInterval doesn't seem to matter.

    Check OutputWindow:
    Note that the Strategy prints "NOT EQUALS" for quite a few of these comparisons, but eventually starts printing "EQUALS".

    Shouldn't these values *ALWAYS* be equal?
    Why are they not equal?

    I did some further experimenting with BarsRequired.

    In SA window, I changed property "Min. bars required" (same as BarsRequired
    property in the code) ... I found that ...

    When BarsRequired = 0 all comparisons are always "EQUALS".

    When BarsRequired != 0 the initial number of comparisons printing "NOT EQUALS" will vary, but the Strategy eventually starts printing "EQUALS".

    Why does BarsRequired effect the output of this strategy?

    Is this a bug in NinjaTrader?

    I'm extremely concerned because I'm trying to track down an issue in my strategy where the selection of a "higher time frame" for the EMA is showing different results when the selected HTF just happens to be the same as the primary data series of the underlying chart window.

    Yeah, I know, why select an HTF at all if you're just going to use the same period and interval as what's already on the chart, right? Well, whatever, but I'm trying not to have to limit user's selections, so was wondering why the HTF EMA vs BIP=0 EMA were giving different results? Ok, so I came up with this sample strategy, and found that BarsRequired=0 seems to fix the problem (perhaps by syncing BarsArray[0] and BarsArray[1] under the hood?)

    I'm deeply concerned and quite stumped, because setting BarsRequired=0 in my very large and complex strategy did not magically "fix" my problem like it "fixed" the sample strategy I've attached.

    In trying to track down my original issue, it seems I have found this other BarsRequired-effects-BarsArray issue. Feeling kinda stuck, so I thought I'd ask for some help.

    Can someone explain what is going on?
    I'm really at a loss, what am I missing?
    Attached Files

    #2
    Hello bltdavid,

    Thank you for your post.

    I did not see any syncing of the DataSeries' in your code to the bar series. Please refer to the following link for information on syncing to the bar series: http://www.ninjatrader.com/support/f...ead.php?t=3572

    My code provides the same values for each bar no matter the BarsRequired.
    Code:
            protected override void Initialize()
            {
    			BarsRequired = 20;
    			Add(PeriodType.Minute, 1);
            }
    
    		
            protected override void OnBarUpdate()
            {
    			if(BarsInProgress == 0)
    			{
    				Print(Times[0][0]);
    				Print(CurrentBars[0]);
    				Print("BIP 0 EMA: " + EMA(BarsArray[0], 20)[0]);
    				Print("BIP 0 Close: " + Closes[0][0]);
    			}
    			if(BarsInProgress == 1)
    			{
    				Print(Times[1][0]);
    				Print(CurrentBars[1]);
    				Print("BIP 1 EMA: " + EMA(BarsArray[1], 20)[0]);
    				Print("BIP 1 Close: " + Closes[1][0]);
    			}
            }

    Comment


      #3
      Thanks!

      But, uh, where in your example code did you sync the DataSeries to the bar series?

      The code in your reply is puzzling, as it does not seem to conform to the sample link you provided.

      I'm updating my example code now.

      Will reply again soon.

      Comment


        #4
        Is the BarsArray not available in OnStartUp?

        Isn't this code in OnStartup,

        Code:
        ds1 = EMA(BarsArray[1], 7);
        syncing to the BarsArray?

        Comment


          #5
          I now have a working example where the output always prints EQUALS.

          I moved the initialization of ds1 from OnStartUp to OnBarUpdate.

          But this code produces output where bar numbers are repeated, which seems very odd to me, see attached screenshot. Can you reproduce this behavior?

          Here is my updated sample strategy,

          Code:
          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion
          
          namespace NinjaTrader.Strategy
          {
              [Description("Test BarsArray")]
              public class Test_02 : Strategy
              {
                  private IDataSeries ds0 = null;
                  private IDataSeries ds1 = null;
          
                  protected override void Initialize()
                  {
                      /* BarsRequired = 0; */
                      /* Add(PeriodType.Minute, 3);  */
                      Add(BarsPeriod.Id, BarsPeriod.Value);
                  }
          
                  protected override void OnStartUp()
                  {
                      ClearOutputWindow();
                      ds0 = EMA(7);
                      /* ds1 = EMA(BarsArray[1], 7); */
                  }
          
                  /* --------------------------------------------------------------------------------------------------- */
          
                  protected override void OnBarUpdate()
                  {
                      if (BarsInProgress == 0)
                      {
                          if (ds1 == null)
                              ds1 = EMA(BarsArray[1], 7);
          
                          bool Equals = ds0[0] == ds1[0];
                          string szEquals = Equals ? "EQUALS" : "NOT EQUALS";
          
                          Print("==> BIP=" + BarsInProgress + " " +
                                "Bar=" + CurrentBars[BarsInProgress] + " " +
                                "Time='" + Times[BarsInProgress][0].ToString("HH:mm:ss ddd MMM d") + "' " + szEquals);
          
                          if (!Equals)
                              Print("    ds0[0]=" + ds0[0] + " ds1[0]=" + ds1[0]);
                      }
                  }
              }
          }
          Attached Files

          Comment


            #6
            Looks like I've answered all my questions.

            I now have a complete working example; it prints EQUALS for all bars and the bar count is incremented by 1 consistently and correctly.

            For the sake of others, I'll comment on what I've learned.

            First off, the link to the sample reference was very helpful.

            1. My first error was using IDataSeries rather than DataSeries.

            2. Syncing the DataSeries to ... something ... is done with the new operator.

            3. Syncing ds0 to BIP=0 EMA DataSeries in OnStartUp:
            ds0 = new DataSeries(EMA(7));
            4. Syncing ds1 to BIP=1 EMA DataSeries in OnStartUp:
            ds1 = new DataSeries(EMA(BarsArray[1], 7));
            After these changes, I get perfect output (see attached) in all time frames.

            Here is the final sample code:

            Code:
            #region Using declarations
            using System;
            using System.ComponentModel;
            using System.Diagnostics;
            using NinjaTrader.Cbi;
            using NinjaTrader.Data;
            using NinjaTrader.Indicator;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Strategy;
            #endregion
            
            namespace NinjaTrader.Strategy
            {
                [Description("Test BarsArray")]
                public class Test_02 : Strategy
                {
                    private DataSeries ds0 = null;
                    private DataSeries ds1 = null;
            
                    protected override void Initialize()
                    {
                        /* BarsRequired = 0; */
                        /* Add(PeriodType.Minute, 3);  */
                        Add(BarsPeriod.Id, BarsPeriod.Value);
                    }
            
                    protected override void OnStartUp()
                    {
                        ClearOutputWindow();
                        ds0 = new DataSeries(EMA(7));
                        ds1 = new DataSeries(EMA(BarsArray[1], 7));
                    }
            
                    /* --------------------------------------------------------------------------------------------------- */
            
                    protected override void OnBarUpdate()
                    {
                        bool Equals = ds0[0] == ds1[0];
                        string szEquals = Equals ? "EQUALS" : "NOT EQUALS";
            
                        Print("==> BIP=" + BarsInProgress + " " +
                              "Bar=" + CurrentBars[BarsInProgress] + " " +
                              "Time='" + Times[BarsInProgress][0].ToString("HH:mm:ss ddd MMM d") + "' " + szEquals);
            
                        if (!Equals)
                            Print("    ds0[0]=" + ds0[0] + " ds1[0]=" + ds1[0]);
                    }
                }
            }
            Attached Files
            Last edited by bltdavid; 12-04-2014, 06:15 PM.

            Comment


              #7
              Hello bltdavid,

              Thank you for updating the thread here.

              I am glad you were able to resolve this matter.

              Comment


                #8
                Suggestion to Improve Help Guide

                I have a suggestion for an improvement.

                I note that this section in the help guide,



                which discusses the BarsArray extensively (as well as many other multi-time frame issues) does not mention the idea of syncing the DataSeries to the BarsArray.

                I think this is a missed opportunity.

                This 'syncing' concept needs to be brought to the surface better.

                Understanding use of the BarsArray is pretty central to understanding multi-time frames, so this specific help section should be expanded with new details and examples of what is 'Syncing a DataSeries Object to a BarsArray' and why & when (and where) you need to do this.

                The sections on DataSeries, BoolSeries, DateTimeSeries, etc, could all point to this central explanation of syncing objects to a BarsArray as necessary.

                Wherever you decide to put it, this idea of syncing (which only became important in my brain via these little experiments) needs a better explanation and more examples.

                Just my 2c.

                Comment


                  #9
                  Thanks for the follow up and suggestion how we could improve our educational resources David, I will forward this to the helguide editor to have it mentioned as well on the MTF environment overview page.

                  For the DataSeries itself we discuss it here in our helpguide and also point to the reference sample -

                  Last edited by NinjaTrader_Bertrand; 12-08-2014, 10:13 AM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  656 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  371 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  109 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  574 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  579 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X