NexusFi: Find Your Edge


Home Menu

 





NinjaTrader script redundancy in variables calculation


Discussion in NinjaTrader

Updated
    1. trending_up 1,813 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 8 posts
    2. attach_file 0 attachments




 
Search this Thread

NinjaTrader script redundancy in variables calculation

  #1 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12

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?

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
Cheap historycal L1 data for stocks
Stocks and ETFs
ZombieSqueeze
Platforms and Indicators
Trade idea based off three indicators.
Traders Hideout
 
  #2 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,426


Astrogirl View Post
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 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?

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.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,103



Astrogirl View Post
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?

You can avoid that by setting your indicator to "CalculateOnBarClose = true". In that case there will be only one calculation per bar.

By the way "CalculateOnBarClose = true" is the default setting for most indicators. Did you change it?

Reply With Quote
  #4 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12

 
Code
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
///// GESTIONE POSIZIONE APERTA		
		if 	(
		        Position.MarketPosition != MarketPosition.Flat
			)
			{	
                        price1 = 10*100
                        price2 = 20*100
                        price3 = 30*100
                        price4 = 40*100

			if (Close[0] < 60000 && Close[0] >59000)	
				{
				Print (" CHECK 1: " + price1 + " " + price2); 
				}
			else if (Close[0] < 59001 && Close[0] > 58000) 
				{
				Print (" CHECK 2: " + price3 + " " + price4); 
				}
	                }
	 }
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.

(Please sorry for my weak English)

Reply With Quote
  #5 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12


ratfink View Post
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.

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...



fat tails
By the way "CalculateOnBarClose = true" is the default setting for most indicators. Did you change it?

No, I didn't change it. It is on true... I'm going to make some tests and see what happen.

Reply With Quote
  #6 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,103


Astrogirl View Post
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".

Reply With Quote
Thanked by:
  #7 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12


ratfink View Post
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 ! )

Reply With Quote
  #8 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,426


Astrogirl View Post
 
Code
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
///// GESTIONE POSIZIONE APERTA		
		if 	(
		        Position.MarketPosition != MarketPosition.Flat
			)
			{	
  
			if (Close[0] < 60000 && Close[0] >59000)	
				{
                                price1 = 10*100
                                price2 = 20*100
				Print (" CHECK 1: " + price1 + " " + price2); 
				}
			else if (Close[0] < 59001 && Close[0] > 58000) 
				{
                                price3 = 30*100
                                price4 = 40*100
				Print (" CHECK 2: " + price3 + " " + price4); 
				}
	                }
	 }
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.

(Please sorry for my weak English)

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

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #9 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12


ratfink View Post
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 View Post
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!

Reply With Quote
Thanked by:




Last Updated on August 6, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts