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

OnMarketData/OnBarUpdate - Clarification

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

    OnMarketData/OnBarUpdate - Clarification

    OK, sorry for rehashing a question that seems to have been asked in many forms many times, but I've read many of those and still not quite clear.

    I'm using NT8. I need bid, ask, depth, so I'm using OnMarketData. I'm counting ticks/volume per bar.

    Like many others, I try to reset those volume counters in OnBarUpdate using IsFirstTickOfBar when a new bar is formed. However, due to the threading etc, I'm not sure if I am loosing ticks and I think this is incorrect. You can't use the isfirsttickofbar in onMarketData, so I wonder, what is the best way to detect a new bar and reset your counters in a way that you can be sure all the ticks are accounted for if you are using OnMarketData.

    Assuming that onmarketdata runs in a single thread and 2 or more of them do not run at the same time, should I use 'CurrentBar' to detect these changes and reset my counters within OnMarketData itself?

    A lot of people (including me) seem a little confused by this, so helpfully this helps others too in the way I tried to phrase it.

    Thank.

    #2
    Hello pjsmith, and thank you for your question.

    In a multi-threaded application like NinjaTrader 8, it is sometimes necessary to create what is called a critical section. In C# we can do this with the lock statement. Here is some publicly available MSDN documentation on this statement.

    https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

    There is an example where this is used in NinjaTrader here,

    http://ninjatrader.com/support/helpG...ghtsub=account

    In plain language, the lock statement takes a variable and ensures that only one thread at a time can use it. So if you have a section of code like this,

    Code:
    lock(myVariable)
    {
        DoSomething();
        DoSomethingElse();
    }
    Then if two threads tried to get at that section of code at the same time, the second would have to wait until the first had called DoSomething() and DoSomethingElse() .

    We can take advantage of this when trying to keep OnMarketData and OnBarUpdate in sync. If for instance we want to know whether this really is the first tick of the bar in OnMarketData, we could use code like this,

    Code:
    
    public bool getIsFirstTickOfBar(bool setIsFirstTickOfBar)
    {
        bool outputIsFirstTickOfBar = false;
        lock(_storedIsFirstTickOfBar)
        {
            bool outputIsFirstTickOfBar = _storedIsFirstTickOfBar;
            _storedIsFirstTickOfBar = setIsFirstTickOfBar;
        }
        return outputIsFirstTickOfBar || setIsFirstTickOfBar;
    }
    private bool _storedIsFirstTickOfBar = false;
    If you call that with IsFirstTickOfBar as its argument from OnBarUpdate, and false as its argument from OnMarketData, then it will return true only on the first tick of the bar in both OnMarketData and OnBarUpdate.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for this.

      For the benefit of anyone else, what I decided to do instead was just place a lock() around the same variable on the parts in onmarketdelta that add to my data to the collection and the part in the onbarupdate that clears that collection down and resets for a new bar. This should ensure the onmarketdata code that adds the data cannot add data to the collection before the onbarupdate has done what it needs to do on first tick. i.e., it would wait for the lock whilst onbarupdate clears the collection, then onmarketdata can re-add data to the collection again.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Nolyn, Today, 10:59 AM
      0 responses
      1 view
      0 likes
      Last Post Nolyn
      by Nolyn
       
      Started by Gkomi, Today, 10:02 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by JTizz, Today, 08:58 AM
      6 responses
      30 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by Bob49, Today, 08:40 AM
      0 responses
      19 views
      0 likes
      Last Post Bob49
      by Bob49
       
      Started by Malik_demoacct, Today, 08:31 AM
      0 responses
      17 views
      0 likes
      Last Post Malik_demoacct  
      Working...
      X