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

OnBarUpdate() problem

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

    OnBarUpdate() problem

    Hi Guys,

    Instrument EURUSD, 3 minutes: having an issue with an indicator called inside a strategy OnBarUpdate.

    Code:
        public class StratDebug : Strategy
        {
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
            protected override void OnBarUpdate()
            {
                DebugThis();    
            }
            
            #region Misc
            private void DebugThis()
            {
                int lowEndBarsAgo = ZigZag(DeviationType.Points,0.0013,true).LowBar(0,1,CurrentBar);
                if (Time[0].Hour == 21 && Time[0].Minute == 3)
                Print(Time[0].Date + " " + Time[0].Hour + ":" + Time[0].Minute.ToString("00") + " Print at the end of the strategy OnBarUpdate  " + lowEndBarsAgo + " " + Low[lowEndBarsAgo]);
                
            }
            #endregion
        }
    }

    where the following code has been added at the end of the ZIGZAG OnbarUpdate:
    Code:
                int lo = LowBar(0,1,CurrentBar);
                if (Time[0].Hour == 21 && Time[0].Minute == 3)
                Print(Time[0].Date + " " + Time[0].Hour + ":" + Time[0].Minute.ToString("00") + " Print at the end of the indicator OnbarUpdate " + lo + " " + Low[lo]);
    Output:
    8/24/2009 12:00:00 AM 21:03 Print at the end of the strategy OnBarUpdate 16 1.4289
    8/24/2009 12:00:00 AM 21:03 Print at the end of the indicator OnbarUpdate 1 1.4286
    The right bar value should be 1 not 16. So, does it mean that the indicator is fully updated after being called? I will appreciate any help.

    #2
    The indicator values pulled from the strategy should be the correct values at the time they are accessed. Be sure that your indicator does NOT have any CalculateOnBarClose setting as that will cause conflicting issues even if it is set the same as the strategy's setting.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply Josh. Indeed, the indicator CalculateOnBarClose is set to true. When I remove this setting, the problem remains.

      Comment


        #4
        LowBar is likely not meant to be called from within ZigZag. Please pull up a chart and visually see what value is correct at that point in time. This can be difficult because ZigZag repaints itself in the future.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          In my case the right value is 1. I also checked on a 3 minutes chart with the ZigZag indicator. For an unkown reason, the LowBar(0,1,CurrentBar) cannot be equal to 1 inside the strategy itself. Used as an indicator, ZigZag is ok and LowBar(0,1,CurrentBar) can be equal to 1 in that case.

          Comment


            #6
            Could then be just the sequence of events in updating. You may need to migrate the zigzag logic into a method in the strategy, but remember, that the ZigZag indicator is a very difficult indicator to actually create a strategy with due to its repainting nature. The reason it looks so good on a chart is because it changes its values when something happens a few bars later making it seem like it detected such a move in the past.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Ok I will do that. As for the zigzag logic, I love it: Brownian motion with a volatility zest.


              Originally posted by NinjaTrader_Josh View Post
              Could then be just the sequence of events in updating. You may need to migrate the zigzag logic into a method in the strategy, but remember, that the ZigZag indicator is a very difficult indicator to actually create a strategy with due to its repainting nature. The reason it looks so good on a chart is because it changes its values when something happens a few bars later making it seem like it detected such a move in the past.

              Comment


                #8
                Finally, I found a solution:
                - In ZigZag declare zigZagHighSeries & zigZagLowSeries as ArrayList not DataSeries.
                - In ZigZag add or remove the right values .i.e.
                Code:
                if (addHigh || updateHigh)
                                {
                                    zigZagHighZigZags.Set(1, saveValue);
                                    zigZagHighZigZags.Set(0, 0);
                
                                    currentZigZagHigh = saveValue;
                                    if (updateHigh)
                                        zigZagHighSeries.RemoveAt(zigZagHighSeries.Count-1);
                                        
                                    zigZagHighSeries.Add(currentZigZagHigh);
                                }
                - To get the last value of zigZagLowSeries in the strategy :
                Code:
                                int ii = ZigZag(DeviationType.Points,0.0013,true).ZigZagLow.Count-1;
                                double lastValue = (double) ZigZag(DeviationType.Points,0.0013,true).ZigZagLow[ii];

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by burtoninlondon, Today, 12:38 AM
                0 responses
                5 views
                0 likes
                Last Post burtoninlondon  
                Started by AaronKoRn, Yesterday, 09:49 PM
                0 responses
                12 views
                0 likes
                Last Post AaronKoRn  
                Started by carnitron, Yesterday, 08:42 PM
                0 responses
                11 views
                0 likes
                Last Post carnitron  
                Started by strategist007, Yesterday, 07:51 PM
                0 responses
                13 views
                0 likes
                Last Post strategist007  
                Started by StockTrader88, 03-06-2021, 08:58 AM
                44 responses
                3,982 views
                3 likes
                Last Post jhudas88  
                Working...
                X