NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 12-30-2009, 05:23 PM   #1
aviat72
Senior Member
 
Join Date: Jul 2009
Location: San Francisco Bay Area
Posts: 216
Thanks: 0
Thanked 4 times in 3 posts
Default Is it OK to store indicator object in your custom code?

Ninjatrader maintains an internal cache for every indicator defined on a particular instrument which compares the parameters used to make the indicator call. This cache code is visible in the "NinjaScript Generated Code..." part at the end of any indicator.

Is it OK to store the indicator object returned by a call to the indicator as a local class variable?

i.e.


In my code I have the following:

Code:
Initialize(){

    SMA mySMA = SMA(10);
}

OnBarUpdate(){
   if(mySMA[0] > mySMA[1]){
  .....
}
}
Currently we would write the barUpdate part as

Code:
if(SMA(10)[0] > SMA(10)[1]){
.....
}
I want to cache the indicator objects because:
  1. If your indicator takes multiple parameters, writing all of them out becomes laborious and error prone, especially in more complex operations. Writing CustomIndicator(param1, param2, param3, param4)[0] is more error prone than myCI[0];
  2. You reduce computation overhead since you by-pass the cache look up phase every time you make a call to access the indicator. i.e. every time you encounter SMA(...), the code traverses the cache linearly and then returns the correct object for you to access.

Since indicator do not recalculate values until you do F5, it seems the indicator objects too should not be transient and hence their values persistent and suitable for storage in local class variables.

Is there anything more to it?
aviat72 is offline  
Reply With Quote
Old 12-31-2009, 08:14 AM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Nothing more to it, what you suggest is a good approach and more efficient of course. We do that ourselves in some of our sample scripts.
NinjaTrader_Ray is offline  
Reply With Quote
Old 12-31-2009, 05:19 PM   #3
aviat72
Senior Member
 
Join Date: Jul 2009
Location: San Francisco Bay Area
Posts: 216
Thanks: 0
Thanked 4 times in 3 posts
Default

Thanks for confirming this.

On a related note, there is a potential issue lurking in the NT infrastructure when you want to write more complex indicators where the signature does not capture all that is happening under the hood.

For example, I may have a database of statistical information which the indicator queries for certain parameters (e.g. average historical volatility during different half hour periods of the day). The indicator may be using that information under the hood. Two different instances of the same indicator started at different times, will behave different but will get mapped to the same object, since the external input to the indicator is not captured by the signature.

I suggest a "IsNotCachable" property with a default value of false where the user explicitly prevents caching.. The user should be aware that they would need to store the indicator object as a local variable in their custom scripts, and call methods on that object instead of making new constructor calls. Other wise a new instance object will be created every-time the custom script runs.
aviat72 is offline  
Reply With Quote
Old 01-02-2010, 09:05 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,411
Thanks: 252
Thanked 976 times in 959 posts
Default

Thanks for taking the time to voice your thoughts and input aviat72, Happy New Year!
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 01-03-2012, 02:22 AM   #5
Pagey
Junior Member
 
Join Date: Aug 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default Calling indicator without using cache

Dear Ninjas,

The cache implemented in the indicator class is indeed very elegant and probably saves a great deal of redundant re-calculation- good job!

That said, there may be cases where multiple calls to an indicator with different parameter combinations may be used- in this case the caches fill up all available memory and may halt the machine.

Is there a way to prevent the indicator from keeping a cache, so that serial calls to an indicator would not take extra memory? The user would of course be aware that any recalculation is at his expense in this case

Any help in this direction would be greatly appreciated, and a great 2012 to you!

Pagey
Pagey is offline  
Reply With Quote
Reply

Tags
cache, indicator

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How access a custom indicator from another custom indicator? Mzsolt Indicator Development 2 07-14-2009 09:19 AM
Position Sizing Custom Code Anagoge Strategy Development 7 05-19-2009 06:55 PM
Error on running optimizer. Object reference not set to an instance of an object. vasily20011 Strategy Analyzer 5 03-09-2009 08:09 AM
Draw Object Size in code??? higler General Programming 1 05-10-2008 02:21 PM
Indicator: Using a DataSeries object to store calculations NinjaTrader_Josh Reference Samples 0 04-30-2008 01:19 AM


All times are GMT -6. The time now is 05:32 PM.