Announcement

Collapse
No announcement yet.

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 Polufson, Today, 01:37 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by mohsemian, Today, 01:36 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_BrendanC  
        Started by Derjan, Today, 01:18 PM
        2 responses
        17 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by Cicero, Today, 09:41 AM
        3 responses
        17 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by johnnymustard, Today, 12:01 PM
        2 responses
        12 views
        0 likes
        Last Post johnnymustard  
        Working...
        X