NinjaTrader script redundancy in variables calculation
Hallo, I'm a beginner. My NT script calculates the values of several variables when a price condition is reached. I noticed (with the Print command on the monitor) that it is repeated each minute (that is my market data time-frame) thousand of times until the condition remains true! I guess it takes a lot of work and time for RAM and CPU and it is absolutely redundant as it is needed that the variables assignment happen only once when the condition is reached (and once again when the condition is no longer true).
How can I avoid that?
Last edited by Astrogirl; August 6th, 2015 at 06:15 AM.
This post has been selected as an answer to the original posters question
Just a simple 'bool' flag, set when the calc is needed, clear it when not.
Unless it's just that the code is being called on each Historical bar, depends what you want, without seeing the code, hard to say.
Redundancy in code is not always a problem, so long as you are aware of what is needed and whether there are spare resources or not, Ghz are very different form human hurts.
Task Manager/Resource Manager in Windows will show if much cpu/ram is getting used, Print statements will slug it but are great for fast debugging visiblity. Can always be left in code and just switched on/off by a bool variable as well.
Should I let the script doing each of hundreds operations on each updating bar as it will not affect the efficiency and backtest speed anyway?
In effect the script does its job quite fine and it doesn't take so much time: about 3 minutes on each backtest. The matter is on optimization where my PC seems really suffering...
No, I didn't change it. It is on true... I'm going to make some tests and see what happen.
Last edited by Astrogirl; August 6th, 2015 at 01:30 PM.
My idea is to use a C# block as such as the "protected override void Initialize()" and then to call it only once until the relative condition in the "protected override void OnBarUpdate()"
is no longer true.
(At the same time I'm wondering if it could be just a nice way to complexify instead of simplify ! )
Last edited by Astrogirl; August 6th, 2015 at 02:41 PM.
Unless I am misunderstanding just moving the calculation blocks inside the conditionals as I have illustrated will help - so long as it is actually CPU bound and not hard page faulting from excessive ram use. You can see that in the Windows Task Manager and Resource Manager to check what is different during the optimisation run.
If you are using too much ram cut down the degrees of freedom or ranges of the optimisation parameters. Guessing here as I do know systems inside out but I don't use the optimiser.
The following user says Thank You to ratfink for this post:
As you said, after several tests with Task Manager (great advice!) I can confirm that RAM and CPU were not used so much during a simple backtest. I'm really not competent on PC's hardware but what matters, in backtest speed, seems to be the CPU GHz rate. (Any comment by experts is welcome )
Thanks! Using the logic of your modified code and switch on/off boolean variables, the backtest takes 15% less time!
Last edited by Astrogirl; August 6th, 2015 at 05:28 PM.
The following user says Thank You to Astrogirl for this post: