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.

    Use the C# lock statement to ensure that only a single thread exclusively reads or writes a shared resource, blocking all other threads until it completes.


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



    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:
    [FONT=Courier New]lock(myVariable)
    {
        DoSomething();
        DoSomethingElse();
    }[/FONT]
    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:
    [FONT=Courier New]
    public bool getIsFirstTickOfBar(bool setIsFirstTickOfBar)
    {
        bool outputIsFirstTickOfBar = false;
        lock(_storedIsFirstTickOfBar)
        {
            bool outputIsFirstTickOfBar = _storedIsFirstTickOfBar;
            _storedIsFirstTickOfBar = setIsFirstTickOfBar;
        }
        return outputIsFirstTickOfBar || setIsFirstTickOfBar;
    }
    private bool _storedIsFirstTickOfBar = false;[/FONT]
    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 junkone, 04-21-2024, 07:17 AM
      10 responses
      148 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by tsantospinto, 04-12-2024, 07:04 PM
      6 responses
      100 views
      0 likes
      Last Post tsantospinto  
      Started by rocketman7, Today, 02:12 AM
      5 responses
      26 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by ZenCortexReal, Today, 08:54 AM
      0 responses
      1 view
      0 likes
      Last Post ZenCortexReal  
      Started by ZenCortexReal, Today, 08:52 AM
      0 responses
      0 views
      0 likes
      Last Post ZenCortexReal  
      Working...
      X