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

How to protect custom static list during multi-thread optimization?

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

    How to protect custom static list during multi-thread optimization?

    I am working on a strategy that ranks a set of instruments, and stores the ranked instruments into a static list.

    It then buys/sells the best/worst instruments, and removes their corresponding entry from the sorted list. When this strategy runs in a multi-threaded optimization, it seems like that list is being shared from one instance to another, and it throws an error when one instance tries to access an entry that has been removed by another.

    How can I avoid this?

    Thanks,

    #2
    Hello kbeary33,

    Thank you for your post.

    You will want to Dispose() of any items that need to be removed during termination of the strategy in OnTermination().

    For information on OnTermination() please visit the following link: http://www.ninjatrader.com/support/h...ermination.htm

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

    Comment


      #3
      Hi,

      Thanks very much for the reply. However, I don't think that this solves the problem... it actually makes it worse...

      The issue is that two instances of the same strategy are trying to access the same variable, due to the multi-threading of the optimizer. If I put Clear() or Dispose() statements in the OnTermination() block, then the 3 threads that are still running immediately crash.

      Is there some way to declare the list collection such that each instance of the strategy loads its own list/dictionary variables? There is a note in the help guide saying:

      "Should you be using custom resources like text files, static members, etc. it is important to protect your resources from concurrent access. If NinjaTrader tried to use the resource at the same time you would run into errors similar to this one:

      8/20/2010 12:14:29 PM|3|128|Error on calling 'OnBarUpdate' method for strategy 'SampleStrategy/1740b50bfe5d4bd896b0533725622400': The process cannot access the file 'c:\sample.txt' because it is being used by another process.

      However, it doesn't give any instructions on how to accomplish this. (And so I'm very much over my head WRT programming skill)

      Thanks

      Comment


        #4
        List.Add is an instance member. That means it's not guaranteed to be thread-safe. What does this mean? Possibility 1. That if two threads invoke .Add on different instances, there could be an unex...


        this might be of help:

        Get started with thread-safe collections using the System.Collections.Concurrent namespace in .NET, which includes thread-safe and scalable collection classes.


        which links to this example:


        Read an example of how to add, retrieve, update, and remove items from the ConcurrentDictionary collection class in .NET.




        Originally posted by kbeary33 View Post
        Hi,

        Thanks very much for the reply. However, I don't think that this solves the problem... it actually makes it worse...

        The issue is that two instances of the same strategy are trying to access the same variable, due to the multi-threading of the optimizer. If I put Clear() or Dispose() statements in the OnTermination() block, then the 3 threads that are still running immediately crash.

        Is there some way to declare the list collection such that each instance of the strategy loads its own list/dictionary variables? There is a note in the help guide saying:

        "Should you be using custom resources like text files, static members, etc. it is important to protect your resources from concurrent access. If NinjaTrader tried to use the resource at the same time you would run into errors similar to this one:

        8/20/2010 12:14:29 PM|3|128|Error on calling 'OnBarUpdate' method for strategy 'SampleStrategy/1740b50bfe5d4bd896b0533725622400': The process cannot access the file 'c:\sample.txt' because it is being used by another process.

        However, it doesn't give any instructions on how to accomplish this. (And so I'm very much over my head WRT programming skill)

        Thanks

        Comment


          #5
          So, finally sorted it out. For anyone else that comes across this issue, the problem was in how I declared my list.

          My initial code:
          Code:
          public class sortInstrument
          {
            	public static List<KeyValuePair<int, double>> instrumentSorted = new List<KeyValuePair<int, double>>();
          }
          My corrected code:
          Code:
          private class sortInstrument
          {
          public List<KeyValuePair<int, double>> instrumentSQNSorted = new List<KeyValuePair<int, double>>();
          }
          
          private sortInstrument currentSortInstrumentClass = new sortInstrument();
          So, instead of referencing sortInstrument.instrumentSorted, I just call on sortInstrument.currentSortInstrumentClass

          Another thing to note is that this solved my memory leak issue as well. In the initial configuration, when I ran an optimization the memory usage would keep growing until it used up all 12GB available. By changing these 3 lines, it stays stable at ~1GB or less.

          Thanks everyone for your help!

          Comment


            #6
            Originally posted by kbeary33 View Post
            I am working on a strategy that ranks a set of instruments, and stores the ranked instruments into a static list.

            It then buys/sells the best/worst instruments, and removes their corresponding entry from the sorted list. When this strategy runs in a multi-threaded optimization, it seems like that list is being shared from one instance to another, and it throws an error when one instance tries to access an entry that has been removed by another.

            How can I avoid this?

            Thanks,
            If you are using a sorted list, check for the existence of the TKey before trying to access it. The pretty comprehensive example on the MSDN page covers this in multpile ways.

            ref: http://msdn.microsoft.com/en-us/library/ms132319.aspx

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by f.saeidi, Today, 12:14 PM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by aprilfool, 12-03-2022, 03:01 PM
            2 responses
            326 views
            0 likes
            Last Post BottomShark77  
            Started by AnnBarnes, Today, 12:17 PM
            1 response
            2 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by Lopat, 03-05-2023, 01:19 PM
            4 responses
            168 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by giulyko00, Today, 12:03 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X