Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with Strategy and OnBarUpdate()

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

    Problem with Strategy and OnBarUpdate()

    I have a strategy that is set to execute on 1 minute bars as as such I have set the "CalcluateOnBarClose" setting to "true" to force a end-of bar update. Unfortuntately, when executing the strategy in real time, I can see the OnBarUpdate being called continously (via log messages using "Print").

    The codee backtests fine, but in simulated trading or using replay data it goes crazy.

    Here is the Start of the strategy:

    Code:
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Test
        /// </summary>
        [Description("Test")]
        public class Test : Strategy
        {
            #region Variables
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                //Add Signals
                Add(MySignals(TrendStength,TrendPeriod));
                VOL().Panel = 2;
                VOLMA(25).Panel = 2;
                Add(VOL());
                Add(VOLMA(25));
                Add(HMA(TrendPeriod));
                
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if (Bars.FirstBarOfSession && FirstTickOfBar) 
                {
                    tickValue = Math.Round(Instrument.MasterInstrument.PointValue * TickSize, 2); //Value of a Tick    
                    Print(Time[0].ToString() + " TickSize is: " + TickSize + "  and TickValue is : " + tickValue);
                }    
                
                Print(Time[0].ToString() + "Test values: Bear: " + MySignals(TrendStength, TrendPeriod).Bear[0] 
                + " and Bull: " + MySignals(TrendStength, TrendPeriod).Bull[0]);
    
    etc.....
    The first Print statement only ocurrs at the start of each session as expected so this is fine.

    I would expect the second Print Statement to be called once per minute (for each 1 minute bar update), but this is a snipnet of the log that is being produced, so I have no idea of what is happening here? It seems it being called each Tick, when clearly the strategy is stating that it should on close of the bar.

    Code:
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0
    2/19/2009 12:08:00 PM Test values: Bear: 0 and Bull: 0

    #2
    Hello,

    Just to be sure, close out the chart and re-attach the strategy to the chart to see if this changes.
    DenNinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick reply. I tried closing the chart, but unfortunately nothing changed. Any other ideas or thoughts?

      It seems like it is being called every tick and ignoring that 'CalculateOnBarClose" setting.

      Comment


        #4
        Please try running this without the FirstTickOfBar call, as this is only relevant if CalculateOnBarClose = false. Thanks!
        BertrandNinjaTrader Customer Service

        Comment


          #5
          So let me ask this and see if this is part of the issue. For sizing positions for order and scaling of positions I am using TickSize and Ticks to set stoploss etc. Is that "allowed" with 1 minute charts? or do I need to remove all use of anything "Tick" with using 1 minute bars?

          Comment


            #6
            No, dnoff this is valid. What is your CalculateOnBarClose setting for the referenced indicators, especially your MySignals?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Here is the top of MySignals Indicator:

              Code:
              namespace NinjaTrader.Indicator
              {
                  /// <summary>
                  /// MySignals Pattern Recognittion
                  /// </summary>
                  [Description("MySignals Pattern Recognition")]
                  public class MySignals : Indicator
                  {
                      #region Variables
                      private int    volmaLen        = 25; //Volume Moving Average Length
                      private int    hmaLen            = 15; //HMA Trend Moving Average Length
              
                      private DataSeries test1;
                      private DataSeries test2;
                      private DataSeries test3;
                      private DataSeries test4;
                      private Font textFont = new Font("Arial", 10, FontStyle.Bold);
              
                      #endregion
              
                      /// <summary>
                      /// This method is used to configure the indicator and is called once before any bar data is loaded.
                      /// </summary>
              protected override void Initialize()
                      {
              //            Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                          Overlay                = true;
                          PriceTypeSupported    = false;
                          
                          test1        =    new DataSeries(this);
                          test2        =    new DataSeries(this);
                          test3        =    new DataSeries(this);
                          test4        =    new DataSeries(this);
                          
              //            CalculateOnBarClose    = true;
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                          if (CurrentBar < 4) return;
                          etc....
              So in this instance it is currently running commented out.

              The only other indicators used are the default indicators HMA, VOL, and VOLMA

              Comment


                #8
                Please try this : leave the 'MySignals' as is, and set the strategy to CalculateOnBarClose = false, then use this call for the second Print part...

                Code:
                if (FirstTickOfBar)
                Print(Time[0].ToString() + "Test values: Bear: " + MySignals(TrendStength, TrendPeriod).Bear[0] 
                            + " and Bull: " + MySignals(TrendStength, TrendPeriod).Bull[0]);
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the reply. I am trying this now, but correct me if I am wrong, but doesn't this now indicate that the strategy is going to execute on every Tick?

                  Comment


                    #10
                    No, since we also indicated to only process the 'test trading logic' on the FirstTickOfBar, so once per bar. Thanks!
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks for the reply again. So with the changes to set the "CalculateOnBarUpdate = false" and then wrapping all of my logic in the "If (FirstTickOfBar)" construct, the strategy works as I thought it would.

                      I guess the confusing part for me is that the NinjaTrader documentation states that using "CalculateOnBarUpdate = true" in the strategy causes the OnBarUpdate() to only be called at the completion of each timeframe in the strategy (which is me is just is the provided timeframe on adding to the strategy setup). It also states that this overrides all settings on any underlying Indicator.

                      So this either appears to not work at all in which case the documentation is inaccurate or there is a bug in using Strategies with Indicators. Is this a fare assumption?

                      Comment


                        #12
                        You are welcome - yes, this is a known bug with CalculateOnBarClose sometimes not overriding the indicator settings for it, this way now you have a workaround that should do it. This is already on our list and will be fixed.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks for clarification. It this likely to be a NT7 update or a bug fix to NT6.5?

                          Comment


                            #14
                            dnoff,

                            This is on our list to address. Announcements will be made when available. Thank you for your patience.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              So I think that I was too soon in pronouncing that the option you presented enables a solution automated trading. The issue is that the indicator uses completed bars to make entry decisions, and as such "calculateOnBarClose" being false is causing signals to be generated out of sequence to the strategy that is calling them.

                              So I have two questions:

                              1. Do I have to move all of my indicator logic into the Strategy in the shortun to get something I test in a live market?

                              2. Is there anyone actually using a strategy for live, automated trading, that uses "CalculateOnBarClose=true", if so, what are the tricks to get this working?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by 6bobclose5, Yesterday, 08:33 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post 6bobclose5  
                              Started by several, Today, 09:46 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Uregon, Yesterday, 11:53 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by elite12968, 02-01-2025, 07:30 AM
                              3 responses
                              31 views
                              0 likes
                              Last Post bltdavid  
                              Started by mdsvtr, 11-18-2013, 05:56 PM
                              10 responses
                              3,369 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X