Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multithreading problem

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

    Multithreading problem

    I need to run a strategy many times to train an AI agent that saves and restores its state on each run. It uses a static PythonEngine instance. When a Back Test type is set to Backtest, the indicator works properly. However, when a Back Test type is set to Optimization, NijaTrader crashes attempting to multithread.

    Is there a way to run a strategy many times besides setting Back Test to Optimization to bypass multithreading problems?

    #2
    Hello AiTrading,

    There is not a way to run the strategy many times without using the optimization tool. Unfortunately python is not a supported language with NinjaTrader so I would be unable to provide any guidance in that use case.

    Comment


      #3
      Hy Jesse,

      Thank you for the replay.

      All I need is to repeat a Back Test N times. Without this simple option there is no way to train an AI in NinjaTrader environment. There has to be something that can be done...

      Regards

      Comment


        #4
        Hello AiTrading,

        All that I can suggest would be to use a C# implementation for what you are trying to do. Python is a separate process and language so that is not supported to be run in NinjaTrader. You may see problems when trying to execute python code using C# implementations. If there is some AI framework made for C# that would be a better way to approach the problem.

        Comment


          #5
          Josse,

          The issue is not with Python. Except for a tiny wrapper all the code is in C#. The issue has to do with the fact that training cannot be broken down into parts that can run on separate threads. As is, the environment starts all available threads and the GPU. Besides, there is no need for tuning any parameters of the strategy. All that is needed is to run a Back Test N times. Otherwise, someone would have to click a Run button possibly hundreds of times.

          Comment


            #6
            Hello AiTrading,

            The analyzer uses the available resources based on the .net framework, there is no way modify that to account for an external language or application.

            Comment


              #7
              Hi Jesse,

              I solved the crashing issue, but it doesn’t help.

              A Back Test for Optimization starts threads on all available processors simultaneously for each increment of a parameter to tune (in my case a dummy counter). While I need on each run of a Back Test to load the state of the AI agent at the start of training and save it at the end.

              What is needed is a repetitive execution of a strategy without optimization. This simple fix would allow use of the NinjaTrader environment for AI training.

              Best regards
              AiTrading.Tools

              Comment


                #8
                Hello AiTrading,

                Unfortunately there is nothing I can suggest, if you can execute your code in C# you could use NinjaScript to do that however the optimizer is meant to run the way it does for efficiency. There is not a way to change how that tool operates to accommodate external software.

                Comment


                  #9
                  Hi Jesse,

                  Here is a round about solution to a simple counter to sequentially run a strategy a number of times... which should be one of the Backtest types.

                  Code:
                          private bool initialized = false;​
                          private static SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
                          private static int counter = 0;
                          private static bool terminate = false;​
                  ...
                          protected override void OnStateChange()
                          {​
                  ...
                              else if (State == State.Terminated)
                              {
                                  if (initialized) // block 3 NinjaTrader initialization phases
                                  {​
                                      Interlocked.Decrement(ref counter);
                                      terminate = counter == 0;
                                      semaphore.Release();​
                                  }
                              }​
                  ...
                          protected override void OnBarUpdate()
                          {
                              try
                              {
                                  if (CurrentBar == 0) // initialization...
                                  {
                                      if (terminate)
                                      {
                                          terminate = false;
                                          SetState(State.Terminated);
                                          return;
                                      }​
                  
                                      Interlocked.Increment(ref counter);
                                      semaphore.Wait();​
                  ​                    initialized = true;
                  ...
                          [NinjaScriptProperty]
                          [Range(1, 100)]
                          [Display(Name = "Training Counter", Description = "Number of times to run.")]
                          public int TrainingCounter { get; set; } = 20;​

                  Regards,
                  AiTrading
                  Last edited by AiTrading; 04-03-2025, 04:52 PM.

                  Comment


                    #10
                    Why can't you just devise a public integer property,
                    like a run counter, but is otherwise never really used
                    inside your strategy, except maybe in a Print statement
                    to identify the run.

                    The point is, you invoke strategy optimization on that
                    integer 'run counter' property for, say, from 1 to 10,000.

                    Wouldn't that cause the optimizer to effectively backtest
                    your strategy 10,000 times?

                    Wouldn't that work?

                    I mean, looks like you did that, but why do you need
                    the semaphore stuff?

                    Comment


                      #11
                      I need sequential execution, while NT only supports parallel execution.

                      Ideally, the following would do the job: if (CurrentBar==0) semaphore.Wait(); … if (State == State.Terminated) semaphore.Release();​, but NijaTrader makes an additional call and crashes. This is why I added a counter, but because of an extra call, the user will have to click strategy Run twice after each run.

                      If someone has a better solution, I would love to hear it.​

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      50 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      126 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      69 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      42 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X