Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Thread Safe Collections in NT8 Optimizations

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

    Thread Safe Collections in NT8 Optimizations

    Hi,

    I have a strategy that I have been testing with the strategy analyzer. All seems to run fine when I do a single backtest but when I try to optimize this strategy I am running into errors that I believe are due to the Multi-threaded optimization in NT8.

    Background, I have a collection List<MyOrderType> that I used to manage both entry and exit orders. I Add entries (ie. Entry Orders) into this list just prior to calling EnterLong() / EnterShort(). Within OnExecutionUpdate() I am searching my collection for the given entry trade and then updating order parameters like AverageFillPrice, etc.

    This all works fine when backtesting. But when optimizing I am getting exceptions because the orders are not yet updated in the collection.

    I believe this is due to the 4 optimization threads that are running.

    How can I make the usage of these collections thread safe for optimization senarios?

    Thanks,

    #2
    Hello,

    Thank you for the post.

    Out of context it would be hard to say what may be happening. Do you have a simple example that demonstrates how the list is being defined and how it is used?

    I would likely need to run the same test and see the result you are getting based on the syntax being used to provide an accurate answer on this.


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

    Comment


      #3
      Hello Jesse,

      I don't have a "simple" example right now. I may have to parse this down to something with less complex logic.

      I do have a couple of questions regarding optimization and the multiple threads. I have a 4 core processor and I believe I end up with up to 4 "Optimization Threads".

      Is each thread processing its own backtest with a different set of optimization values? Meaning if I have a parameter (OptimizationParam1) that I am optimizing from 0:1:1 will I end up with
      1) Optimization Thread 0 processing backtest for OptimizationParam1 = 0 and
      2) Optimization Thread 1 processing backtest for OptimizationParam1 = 1?

      IOW, is Position object independent of Optimization Thread 0 and Optimization Thread 1?

      Are any parameters / variable values shared between these Optimization Threads?

      Another question, say I have
      Code:
      protected override void OnBarUpdate()
      {
      // Need to Add Order with InitStopLoss Price as entering Stop Loss after Entry 
                      LocalOrders.Add(new LocalOrder() {
                          Name = l_EntrySignalName,
                          InitStopPrice = initStopLoss,
                          StopLossSignalName = l_StopLossSignalName
                      });
                      
                      EnterLong(initPosSize, l_EntrySignalName);
      }
      Can I be guaranteed that this Add statement completes prior to the OnOrderUpdate(), OnExecutionUpdate, OnPositionUpdate() etc running?

      Also Can I be guaranteed that each of the Order event handlers complete prior to running any other code after EnterLong()?

      Lastly, is there an option to turn off the multithreading for optimizations ?

      Thanks,
      Last edited by tornadoatc; 09-26-2016, 03:19 PM.

      Comment


        #4
        Hello,

        The amount of cores would not directly influence the threads, the threads and cores would be managed by the .net framework and PC used. Depending on what is being done, you may see all of the cores used or not. The Optimization would create many threads though to create the different iterations of the optimization. The work would be split into multiple threads which would be a test per thread.

        The position would be independent for each test, but there are items which may not be depending on how you have developed the script. Depending on how and where the list is defined could play a role in the outcome you are seeing. Additionally the options you use like IsInstantiatedOnEachOptimizationIteration would play a role.



        In your sample, the Add statement has added to the list before you called the Entry method, logically this happened first because the code is executed from top to bottom. This would occur before you had called the Entry so it would happen before the OnOrderUpdate event.

        In realtime there is no guarantee of timing because this is delegated by both the market data and also the brokers sending and receiving of the order events. In the simulation engine this would be happening in a specific order rather than delegated by the outside influencing factors.

        There are no options to control the threading from the user interface, this is handled by the .net framework and internally by the platform.

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by trilliantrader, 04-18-2024, 08:16 AM
        4 responses
        18 views
        0 likes
        Last Post trilliantrader  
        Started by mgco4you, Today, 09:46 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by wzgy0920, Today, 09:53 PM
        0 responses
        9 views
        0 likes
        Last Post wzgy0920  
        Started by Rapine Heihei, Today, 08:19 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 08:25 PM
        0 responses
        10 views
        0 likes
        Last Post Rapine Heihei  
        Working...
        X