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

SampleLevel2Book

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

    SampleLevel2Book

    Hello,
    Reviewing the sample indicator "Sample Level 2 Book" , at OnMarketDepth() you are using lock in order to avoid a race condition.
    // Lock the MarketDepth collection to prevent modification to the collection while we are still processing it lock (e.MarketDepth.Ask) { for (int idx = 0; idx < e.MarketDepth.Ask.Count; idx++) rows.Add(new LadderRow(e.MarketDepth.Ask[idx].Price, e.MarketDepth.Ask[idx].Volume, e.MarketDepth.Ask[idx].MarketMaker)); }
    1. Why? Are there multi threads that enter to OnMarketDepth()?
    2. if so, why not place the lock in the beginning of OnMarketDepth() ?
    3. In the continues of the function you are update the "LadderRow" without lock(critical section).

    sincerely, Kobi

    #2
    Hello,

    Thank you for the questions.

    The reason for this is that only the collections e.MarketDepth.Ask and e.MarketDepth.Bid have the potential of becoming updated while this code is being executed.

    As an example, if there was a update to either the ask or bid collection during the for loop, the collection would become modified. The lock would assist in preventing any newer data from being used that was not intended.

    Later in the Insert and Update portions of the code, the ask and bid collections are no longer used so there is no reason to Lock them any further, instead "rows" is used which does not need locked because only this script will be accessing the "rows" collection.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      SampleLevel2Book

      Hello Jesse,

      Thanks you for your response,

      Sorry but I don't understand,
      -> there is only one thread (NT main thread) that executes the OnMarketData() and the OnBarUpdate() .
      2. According to your response
      the collections e.MarketDepth.Ask and e.MarketDepth.Bid have the potential of becoming updated while this code is being executed.
      How come, the collections e.MarketDepth.Ask is an input of the OnMarketData(MarketDataEventArgs e) and this collection is in the thread stack (NT main thread) ,
      also as I mantion above there is only one thread, so how and by whom this collection could be updated.

      sincerely, Kobi

      Comment


        #4
        Hello,

        Thank you for the reply.

        There is the core market data thread and the thread which the indicator OnMarketDepth is running on, the lock is simply to prevent race conditions between the core and the indicator in this case.

        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cmtjoancolmenero, Yesterday, 03:58 PM
        3 responses
        23 views
        0 likes
        Last Post cmtjoancolmenero  
        Started by Brevo, Today, 01:45 AM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by rjbtrade1, 11-30-2023, 04:38 PM
        2 responses
        72 views
        0 likes
        Last Post DavidHP
        by DavidHP
         
        Started by suroot, 04-10-2017, 02:18 AM
        5 responses
        3,021 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by Stanfillirenfro, Today, 07:23 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X