![]() |
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
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jul 2009
Location: San Francisco Bay Area
Posts: 216
Thanks: 0
Thanked 4 times in 3 posts
|
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]){
.....
}
}
Code:
if(SMA(10)[0] > SMA(10)[1]){
.....
}
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? |
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jul 2009
Location: San Francisco Bay Area
Posts: 216
Thanks: 0
Thanked 4 times in 3 posts
|
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. |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,411
Thanks: 252
Thanked 976 times in 959 posts
|
Thanks for taking the time to voice your thoughts and input aviat72, Happy New Year!
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Aug 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
![]() |
| Tags |
| cache, indicator |
| Thread Tools | |
| Display Modes | |
|
|
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 |