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

Used indicator will not .Update() in multi-instrument indicator..

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

    Used indicator will not .Update() in multi-instrument indicator..

    I create an ES 06-12, 3-minute chart, and add an indicator defined as follows:
    Code:
    public class TestIndicator : Indicator {
        #region Variables
        private string[] symbols = {
                "A",
                "AA",
                "BAC",
            };
    
        DataSeries sumHistory;
    
        private PeriodType periodType = PeriodType.Minute;
        private int periodValue = 3;
    
        #endregion
    
        protected override void Initialize() {
    
            int i;
    
            // Add each symbol with the calculation period we're told, since there's no way to get Ninja****ter's.
            for (i = 0; i < symbols.Length; i++) {
                Add(symbols[i], periodType, periodValue);
            }
    
            sumHistory = new DataSeries(this);
    
            BarsRequired = 30;
        }
    
        long calcbar = 0;
        double sumprice = 0;
       
        protected override void OnBarUpdate() {
    
            //Thank you, NinjaTrader.
            if (CurrentBars[0] < BarsRequired || CurrentBars[BarsInProgress] < BarsRequired)
                return;
    
            // At bar change, reset.
            if (CurrentBars[0] != calcbar) {
                Print("");
                Print("New bar: " + CurrentBar);
                calcbar = CurrentBars[0];
                sumprice = 0;
            }
    
            // For example, sum the closing prices and take exponential average of that sum.
            Print("Summing price for: " + BarsInProgress + " (at " + CurrentBars[0] + "): " + Close[0]);
            sumprice += Close[0];
    
            // Update the history for the current bar.
            sumHistory.Set(sumprice);
    
            // This is documented as calling OnBarUpdate again, and so should update the
            // value that EMA returns for this bar.
            EMA(sumHistory, 3).Update();
    
            // For clarity, lets see what we're taking the exponential average over, and the return value.
            Print(Math.Round(sumHistory[0], 2) + "; " + Math.Round(sumHistory[1], 2) + "; " +
                  Math.Round(sumHistory[2], 2) + "; average: " + EMA(sumHistory, 3)[0]);
        }
    }
    Note that the extra instruments are also being added with a three minute bar. The expected behavior is that sumprice will increase with the close of each bar, and given .Update() before I use the EMA indicator each time, I should get a new value for EMA taking into account the new value of sumprice.

    Output:
    Code:
    New bar: 30
    Summing price for: 0 (at 30): 1392
    1392; 0; 0; average: 696
    Summing price for: 2 (at 30): 9.69
    1401.69; 0; 0; average: 696
    Summing price for: 3 (at 30): 8.07
    1409.76; 0; 0; average: 696
    
    New bar: 31
    Summing price for: 0 (at 31): 1392.75
    1392.75; 1409.76; 0; average: 1044.375
    Summing price for: 2 (at 31): 9.69
    1402.44; 1409.76; 0; average: 1044.375
    Summing price for: 3 (at 31): 8.09
    1410.53; 1409.76; 0; average: 1044.375
    ....
    At bar 30, it starts with 1392 for the close of the primary instrument, and taken the exponential average. Then instrument "AA" closes, adds 9.69 to sumprice. I would then expect EMA to recalculate with this new value and the same previous two values to determine the exponential average... but it yields the _same_ result. This seems to be contradictory to the documentation for .Update(). Could you tell me what's going wrong?

    #2
    DrkShadow,

    I noticed you are not filtering by BarsInProgress here at all. Is this something you are aware of and did not want to use?

    Update() calls OnBarUpdate() again, but you are calling an indicator here. As such it shouldn't need this update() since the line :

    EMA(dataseries,3)[0] would automatically calculate based on the "dataseries" you have constructed.

    I am also noticing that you are summing a large number with 2 smaller numbers. It is possible you are only modifying the average by a very small amount here.
    Last edited by NinjaTrader_AdamP; 05-07-2012, 07:45 AM.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_AdamP View Post
      DrkShadow,

      I noticed you are not filtering by BarsInProgress here at all. Is this something you are aware of and did not want to use?
      Correct. This is an example. I want the EMA value recalculated on every additional bar's close, taking the exponential average of the total. Here I added three instruments; if I sum the values and _wait_ until I have all three instruments before calculating the EMA, there are times when the EMA will not calculate because only two bars have closed. The situation of having not all bars is demonstrated in the example. If I wait for all bars, I would be skipping a bar in my indicator's output.

      Originally posted by NinjaTrader_AdamP View Post
      Update() calls OnBarUpdate() again, but you are calling an indicator here. As such it shouldn't need this update() since the line :

      EMA(dataseries,3)[0] would automatically calculate based on the "dataseries" you have constructed.
      See example; the EMA indicator calculates its value the first time I .Set() a value and does not recalculate when I change the value. In the code, it makes no difference whether or not I have .Update(); it has no effect.

      Originally posted by NinjaTrader_AdamP View Post
      I am also noticing that you are summing a large number with 2 smaller numbers. It is possible you are only modifying the average by a very small amount here.
      See example; this is not the case. Does that mean, then, that there is a bug in NinjaTrader? (I'm using 7.0.1000.8.)

      Code Changes:
      The code has been changed to make the numbers closer together, provide the time, and note that I'm using ES 06-12 with three-minute bars. The code changes is one variable name and a few lines of the OnBarUpdate() method. I also added the .Reset() method call, just to test, just because it was a new idea. This has no effect on the output.

      Code:
              long calcbar = 0;
              double closedgreater = 0;
              
              protected override void OnBarUpdate() {
      
                  //Thank you, NinjaTrader.
                  if (CurrentBars[BarsInProgress] < BarsRequired || CurrentBars[0] < BarsRequired)
                      return;
      
                  // At bar change, reset.
                  if (CurrentBars[0] != calcbar) {
                      Print("");
                      Print(Time[0] + " New bar: " + CurrentBar);
                      calcbar = CurrentBars[0];
                      closedgreater = 0;
                  }
      
                  // For example, sum the closing prices and take exponential average of that sum.
                  Print("Did " + BarsInProgress + " close greater than last bar? (" + Close[0] + " > " + Close[1] + ")");
                  if (Close[0] > Close[1])
                      closedgreater++;
      
                  // Update the history for the current bar.
                  sumHistory.Reset();
                  sumHistory.Set(closedgreater);
      
                  // This is documented as calling OnBarUpdate again, and so should update the
                  // value that EMA returns for this bar.
                  EMA(sumHistory, 3).Update();
      
                  // For clarity, lets see what we're taking the exponential average over, and the return value.
                  Print(Math.Round(sumHistory[0], 2) + "; " + Math.Round(sumHistory[1], 2) + "; " + 
                        Math.Round(sumHistory[2], 2) + "; average: " + EMA(sumHistory, 3)[0]);
              }
      Example data:
      Code:
      2012/04/30 09:30:00 New bar: 3809
      Did 0 close greater than last bar? (1393 > 1391.75)
      1; 0; 2; average: 0.59626977853646
      Did 2 close greater than last bar? (9.69 > 9.68)
      2; 0; 2; average: 0.59626977853646
      Did 3 close greater than last bar? (8.11 > 8.09)
      3; 0; 2; average: 0.59626977853646
      
      2012/05/02 11:00:00 New bar: 4099
      Did 0 close greater than last bar? (1397 > 1397)
      0; 1; 0; average: 0.252192085443595
      Did 2 close greater than last bar? (9.71 > 9.71)
      0; 1; 0; average: 0.252192085443595
      Did 3 close greater than last bar? (8.16 > 8.15)
      1; 1; 0; average: 0.252192085443595
      
      2012/05/04 12:48:00 New bar: 4395
      Did 0 close greater than last bar? (1364.75 > 1364.5)
      1; 0; 0; average: 0.503001358910879
      Did 2 close greater than last bar? (9.38 > 9.36)
      2; 0; 0; average: 0.503001358910879
      
      2012/05/04 13:51:00 New bar: 4416
      Did 0 close greater than last bar? (1368.25 > 1368)
      1; 0; 2; average: 0.696558715344123
      Did 2 close greater than last bar? (9.4 > 9.39)
      2; 0; 2; average: 0.696558715344123

      Comment


        #4
        Drkshadow,

        This has to do with CalculateOnBarClose = true. With this property set, the most current value of a data series will be overwritten by the DataSeries.Set() method until a new bar occurs. As such you are setting your series values to the last sum you created, not adding the 3 sums you wanted to the series. Once the new bar occurs, the indicator value would be set from the most current data which is why you are seeing the same value here.

        Here is a link for reference : http://www.ninjatrader.com/support/h...nstruments.htm

        It should help you setup this indicator to work as expected.

        Another few things you could use are for example :







        Note: In NinjaTrader 8 It is no longer needed to use an indicator to sync a secondary series. This can be done directly from the Series&lt;T&gt; (https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?seriest.htm) constructor. This post is left for historical purposes. Series objects are useful for
        Last edited by NinjaTrader_AdamP; 05-08-2012, 09:30 AM.
        Adam P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by lightsun47, Today, 03:51 PM
        0 responses
        5 views
        0 likes
        Last Post lightsun47  
        Started by 00nevest, Today, 02:27 PM
        1 response
        8 views
        0 likes
        Last Post 00nevest  
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        45 views
        0 likes
        Last Post futtrader  
        Started by Option Whisperer, Today, 09:55 AM
        1 response
        14 views
        0 likes
        Last Post bltdavid  
        Started by port119, Today, 02:43 PM
        0 responses
        9 views
        0 likes
        Last Post port119
        by port119
         
        Working...
        X