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

firsttickofbar processing

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

    firsttickofbar processing

    Hello, i have an indicator which updates some data in a dictionary in onMarketDepth() and it uses the
    lock (e.Instrument.SyncMarketDepth)
    around each event processed :

    The data collected is then used in OnBarUpdate which processes every tick.

    Unlike OnMarketData and OnBarUpdate where there is some synchronicity of events between the two as illustrated in the help pages;

    whereas onMarketDepth() events are asynchronous to onBarUpdate (at least i am presuming this as there is nothing i can see in the help pages)

    I am resetting some counters in the code and i tried the following :

    testing for IsFirstTickOfBar in both onmarketdepth() and in onbarupdate() and resetting them within that - i have created versions to do it :
    1. in both onmarketdepth() and onbarupdate()
    2. only in onmarketdepth()
    3. only in onbarupdate()

    the question is if there is any recommendation of where to do this reset cleanly and accurately on the first tick of a new bar ; as both onmarketdepth() and onbarupdate() will then update and use the data for each new bar.

    I am seeing some difference in a visual tool i created so clearly the location of the reset has some implications but im not clear which is the most accurate.

    Is this clear and any thoughts ? If you need more outline let me know but the above is the gist and hopefully covers the context

    thanks

    #2
    So - i just re read the IsFirstTickOfBar property in the help guide .....

    Warning: This property should NOT be accessed outside of the OnBarUpdate() method.

    I have been running this for about 4 weeks now with this in OnMarketDepth with no issues and i also use in OnMarketData ...... which in this case with the sychronicity between OnMarketData and OnBarUpdate seemed to work better for calculation resets ...

    Is it unreliable outside of OnBarUpdate ? other than perhaps resetting a boolean is there another way to check this in OnMarketDepth() ? but after you review my question below ... the answer to this will perhaps be clearer.... so it seems to work in OnMarketDepth and OnMarketData but is what is being stated is that it is unsupported outside of OnBarUpdate - any clarification on this from development would be appreciated
    thanks

    Comment


      #3
      I've been using an int declared at the class level to store the number of the bar being processed.

      Code:
      protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
      {
         if (CurrentBar != _i_activebar) [COLOR="Green"]// a new bar to process[/COLOR]
         {
            [COLOR="Green"]// reset some class vars, create a dictionary, etc.[/COLOR]
            _i_activebar = CurrentBar;[COLOR="Green"] // remember that this bar's dictionary has been created[/COLOR]
         }
         if (CurrentBar == _i_activebar)
         {
            lock (marketDepthUpdate.Instrument.SyncMarketDepth)
            {
              [COLOR="Green"] // populate the dictionary, assign some class vars, etc.[/COLOR]
            }
         }
      }
      However, I do have an older indicator, which does use IsFirstTickOfBar in OnMarketData. Near as I can tell, it's working fine and doing what it's supposed to do.
      Code:
      if (CurrentBar > 0 
         && ResetEOD 
         && Bars.IsResetOnNewTradingDay 
         && Bars.IsFirstBarOfSession 
         && [B]IsFirstTickOfBar[/B])
      Last edited by tradesmart; 03-31-2017, 10:13 PM.

      Comment


        #4
        Hello, thanks for that - yes i have used the :
        if (CurrentBar != _i_activebar) logic previously in other indicators so i will apply that in the Onmarketdepth method to determine the resets as needed.
        Likewise i have noted in earlier ninja code which looks like has changed referenced the isfirsttickofbar in onmarketdata() - and i also currently use and it 'appears' to be fine.
        So for now will use the currentbar check ... but would also be good to hear back from Ninja on this and if it is supported or not for onmarketdata and onmarketdepth ..... if we can access the current bar for the Bars object then i would have thought accessing isfirsttickofbar would be safe - if not then could it be made so for scripts and strategies as there is often a valid need for testing the firsttickofbar ... in the meantime i will use the currentbar checks
        thanks again

        Comment


          #5
          If indeed the help guide warning is correct that IsFirstTickOfBar should not be accessed outside of OnBarUpdate, we could assign a class bool therein and check it in OnMarketData and OnMarketDepth to avoid using either CurrentBar or IsFirstTickOfBar outside of OnBarUpdate.

          NT Staff: May we have your recommendations please?

          OnBarUpdate()
          _b_firsttickofbar = IsFirstTickOfBar ; // update the bool

          OnMarketDepth()
          if (_b_firsttickofbar) // ... check the bool condition

          OnMarketData()
          if (_b_firsttickofbar) // ... check the bool condition

          Comment


            #6
            I'll post my 2˘ as food for thought.

            The help pages warn you away from using FirstTickOfBar outside of OnBarUpdate() probably for reasons like this:

            First, which Bar series is FirstTickOfBar referring to?

            In other words, is FirstTickOfBar always correct in a multi-timeframe indicator?

            If you add multiple Bar series to your Indicator or Strategy, OnBarUpdate will be called when those bars close, too.

            First the simple case (no MTF): If COBC=false, OnBarUpdate() is called every tick, and BarsInProgress will be 0, to indicate primary bar series, and FirstTickOfBar will (under the hood) also be referencing the primary bars series.

            Now the complex case (with MTF): In addition to being called for every tick with BIP=0 (because you have COBC=false) OnBarUpdate is called again when the secondary bar series bars close (not every tick, just every bar closure, because COBC does not, I think, affect the secondary bar series) but BarsInProgress will be 1.

            Ah-hah! So, when inside OnBarUpdate and BarsInProgress != 0, has FirstTickOfBar been properly updated under the hood to reflect status of those secondary bars? Or is FirstTickOfBar still reflecting the primary bar series status?

            The documentation doesn't say. But think about it, FirstTickOfBar is only really useful when COBC=false but only then when BarsInProgress is 0 (aka, the primary bar series).

            The real problem is, the documentation doesn't really say how additional bar series coupled with COBC=false affect FirstTickOfBar, but it's an interesting question.

            I think that if you add a secondary bar series, say a 1-minute series, OnBarUpdate will be called with BarsInProgress == 1 only when the 1-minute bars close -- regardless of COBC setting. Why? Well, do you really need to see every tick of the primary bar series and then see every tick (again) for the secondary bar series, too?

            Well, yes, maybe you do. What if the secondary bar series is a different instrument than the primary series? Then you might really want every tick of both series when COBC=false.

            Anyways, my whole point is: FirstTickOfBar may have (undocumented) dragons if you are using multiple time frames, but if you are using a single primary bar series only (with COBC=false) perhaps things just happen to work.

            Which means the documentation was probably intentionally warning you away, because in the MTF environment FirstTickOfBar behavior may not be as reliable for what you're doing as you think it is. Instead of explaining details, the documentation punts and basically says "FirstTickOfBar not reliable outside of OnBarUpdate".

            My best guess is: that FirstTickOfBar properly reflects the correct bar series inside OnBarUpdate, but if you reference FirstTickOfBar anywhere else, there is no guarantee which bar series FirstTickOfBar is actually referencing.

            So, if it's working for you outside OnBarUpdate, that may be because you are only using a single bar series.

            These are just guesses, really.

            Code experiments to help support or prove the guesses are left as exercises for the reader.

            Comment


              #7
              Anyways, my whole point is: FirstTickOfBar may have (undocumented) dragons if you are using multiple time frames, but if you are using a single primary bar series only (with COBC=false) perhaps things just happen to work.
              Thanks for your analysis. Since all my indicators are single-timeframe and seem to work wherever I read IsFirstTickOfBar, the distinction is moot for me. So, I'll leave the exercise of further experimentation to others of the multi-timeframe persuasion.

              Comment


                #8
                Likewise thanks for thoughts - similarly at this stage i am focusing on a primary series so as you say it may be 'safe' in that context ; i had wondered how it would work for a secondary series and was reflecting other day on this if i were to add a new idea i had ; what you summarised is probably this case at this time ; perhaps there has been no demand for this from users/developers in the past ie for multiple isfirsttickofbar values for each series in the indicator/strategy

                Comment


                  #9
                  For multi-timeframe indicators, I'd probably do something like this:

                  OnBarUpdate() with Calculate.OnEachTick
                  Code:
                  if (CurrentBars[0] != _i_barprocessed0)
                  { 
                     // reset vars for new bar series #0 
                     _i_barprocessed0 = CurrentBars[0] ;
                     _b_firsttick0 = true ;
                  }
                  else
                  {
                     _b_firsttick0 = false ;
                  }
                  if (CurrentBars[1] != _i_barprocessed1)
                  { 
                     // reset vars for new bar series #1 
                     _i_barprocessed1 = CurrentBars[1] ;
                     _b_firsttick1 = true ;
                  }
                  else
                  {
                     _b_firsttick1 = false ;
                  }

                  Comment


                    #10
                    Hi soulfx,

                    IsFirstTickOfBar should not be accessed from outside of OnBarUpdate.
                    The value of this can change while any other method is processing and is only guaranteed to be correct within OnBarUpdate.


                    IsFirstTickOfBar (within OnBarUpdate) does reference the current BarsInProgress that is processing and does work on each series as it updates in OnBarUpdate.

                    When Calculate is set to 'On each tick' OnBarUpdate will trigger before OnMarketUpdate.


                    You could choose to use a variable to store the value in OnBarUpdate for use with other methods.

                    OnMarketDepth needs a lock as there can be changes to the level 2 ladder while OnMarketDepth is processing. OnMarketData does not have these levels and only a single value will be updated each time this triggers.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      IsFirstTickOfBar should not be accessed from outside of OnBarUpdate.
                      The value of this can change while any other method is processing and is only guaranteed to be correct within OnBarUpdate.
                      Uh, ok, so let's say OnMarketDepth() has 30 lines of code.

                      Are you saying, for example, that IsFirstTickOfBar could return true for,
                      say, the first 10 lines of code and then (under the hood) gets changed to
                      false, and so remaining 20 lines of code in OnMarketDepth() this property
                      would return false?

                      Comment


                        #12
                        Thanks Chelsea for the confirmation on this as you can see various interpretations were being and 'could' be made of the statement in help guide.

                        With reference to your point on the lock i did say in the original post that i am using and aware of the fact of ladder changing whilst processing the event

                        lock (e.Instrument.SyncMarketDepth)
                        around each event processed :

                        and have been using the above - which is used in the example code i recall.
                        What i wanted to check was are you inferring that all code within onMarketDepth including any class variable i use eg _b_isfirsttickofbar which is set accordingly in onBarUpdate ; must also be placed within the lock code section?

                        Also with regard to the access of CurrentBar .... what are the rules if any regarding its useage as there is no similar restriction mentioned in the help guide - can this safely be used and checked in onmarketdepth and onmarketdata?

                        thanks

                        Comment


                          #13
                          Hi bltdavid,

                          Yes, from my understanding this value can change in OnMarketData.

                          soulfx,

                          Regarding CurrentBar, I would also say this is something that should only be used in OnBarUpdate(). I will run this by our development to make sure.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Regarding CurrentBar, I would also say this is something that should only be used in OnBarUpdate(). I will run this by our development to make sure.
                            Notice, however, that the VolumeProfile indicator references CurrentBar in the GetLastBarSessionDate() method, which is called by OnMarketData().

                            Comment


                              #15
                              Hello Chelsea, so i have made the modifications i need and testing looks good now i know to generally NOT rely on IsFirstTickOfBar or CurrentBar outside of OnBarUpdate .....
                              It would be good to have definitive closure regarding these questions .. including tradesmart question below on the ninja supplied example indicator.

                              Also can you answer the question in my post below ...

                              " and have been using the above - which is used in the example code i recall.
                              What i wanted to check was are you inferring that all code within onMarketDepth including any class variable i use eg _b_isfirsttickofbar which is set accordingly in onBarUpdate ; must also be placed within the lock code section? "

                              It would be good to have completion for this and for other developers who search on this in the future
                              thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Aviram Y, Today, 05:29 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Aviram Y  
                              Started by quantismo, 04-17-2024, 05:13 PM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by ScottWalsh, 04-16-2024, 04:29 PM
                              7 responses
                              34 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cls71, Today, 04:45 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post cls71
                              by cls71
                               
                              Started by mjairg, 07-20-2023, 11:57 PM
                              3 responses
                              217 views
                              1 like
                              Last Post PaulMohn  
                              Working...
                              X