Is there a way to run a strategy many times besides setting Back Test to Optimization to bypass multithreading problems?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Multithreading problem
Collapse
X
-
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? -
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
-
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
-
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
-
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,
AiTradingLast edited by AiTrading; 04-03-2025, 04:52 PM.
Comment
-
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
-
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
39 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
124 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
64 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
41 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
46 views
0 likes
|
Last Post
|

Comment