Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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?
Can you help answer these questions from other members on NexusFi?
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.
This is a sample of the code block (the real one has a lot more of complicated calculations for variables, lots of conditions to check and lots of operations to do).
The monitor says that the variables are calculated on each minute and the operations are executed each minute too. How to make it happens once until the condition remains true?
My target is to make the script much efficient and, above all, fast on backtesting.
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.
All backtests are executed in mode "CalculateOnBarClose = true". This means that all calculations are only performed once per bar.
If you look for efficiency, you need to decomplexify the code. For example the word "decomplexify" can be easily decomplexified and becomes "simplified".
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 ! )
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.
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.
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 )
ratfink
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.
Cheers
Thanks! Using the logic of your modified code and switch on/off boolean variables, the backtest takes 15% less time!