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 > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 08-29-2012, 02:30 PM   #1
savekad
Senior Member
 
Join Date: Jul 2012
Posts: 158
Thanks: 28
Thanked 4 times in 4 posts
Default Calling Update() not from the property

If indicator I1 is accessing two vars of indicator I2, is it OK to avoid a redundant call to I2 Update() from the vars get properties and instead just call I2 Update() directly?

From this (I1 code):

// I2 vars properties contain Update()
Print(I1().var1);
Print(I2().var2);

To this:

// I2 vars properties don't contain a call to Update()
I2().Update();
Print(I1().var1);
Print(I2().var2);
savekad is offline  
Reply With Quote
Old 08-29-2012, 02:36 PM   #2
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

savekad,

I can't think of any reason that would cause any issues. Let us know if you run into any.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 08-29-2012, 06:00 PM   #3
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,200
Thanks: 24
Thanked 1,226 times in 997 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by savekad View Post
If indicator I1 is accessing two vars of indicator I2, is it OK to avoid a redundant call to I2 Update() from the vars get properties and instead just call I2 Update() directly?

From this (I1 code):

// I2 vars properties contain Update()
Print(I1().var1);
Print(I2().var2);

To this:

// I2 vars properties don't contain a call to Update()
I2().Update();
Print(I1().var1);
Print(I2().var2);
I would like to be corrected but I doubt that that would work correctly. For one thing I2().Update(); seems to be what happens with OnBarUpdate(), so looks redundant.

The way I understood it, we need to Update() in the Property getter because the asynchronous, event-driven nature of NT means that the value may not yet have been updated when we try to access it, so we need to force an update. Somehow, I think this will probably be more of an issue if COBC is false, but I can still envisage a scenario where it could still be an issue with COBC as true.
koganam is online now  
Reply With Quote
Old 08-29-2012, 11:21 PM   #4
savekad
Senior Member
 
Join Date: Jul 2012
Posts: 158
Thanks: 28
Thanked 4 times in 4 posts
Default

koganam, can you elaborate?

I think that even if COBC is false, it would still be faster to call OBU just once for any further var accesses than to fire it every time you access a var from the same OBU call of the original indicator (I1 in our case), wouldn't it?
savekad is offline  
Reply With Quote
Old 08-30-2012, 09:44 AM   #5
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,200
Thanks: 24
Thanked 1,226 times in 997 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by savekad View Post
koganam, can you elaborate?

I think that even if COBC is false, it would still be faster to call OBU just once for any further var accesses than to fire it every time you access a var from the same OBU call of the original indicator (I1 in our case), wouldn't it?
It will be faster, as you are evidently calling updates less often. I doubt if it will be accurate, again because one may be accessing a value that has not yet been updated, because the system was off doing something else. Such is the nature of asynchronous events.
koganam is online now  
Reply With Quote
Old 10-30-2012, 05:29 AM   #6
savekad
Senior Member
 
Join Date: Jul 2012
Posts: 158
Thanks: 28
Thanked 4 times in 4 posts
Default

So you say that by removing Update() from a var getter, it might be inaccurate to access it even if the access is made right after an explicit call to Update() ?
savekad is offline  
Reply With Quote
Old 10-30-2012, 06:35 AM   #7
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,498
Thanks: 109
Thanked 291 times in 280 posts
Default

Hello,

Update() will only trigger if it needs an Update to get "up to speed". So you wont waste unneeded CPU cycles if the value is already up to date. Its always a good idea to have Update there as an insurance policy to make sure you always have up to date and correct values.

-Brett
NinjaTrader_Brett is offline  
Reply With Quote
Old 11-20-2012, 06:08 AM   #8
sh_daggett
Member
 
Join Date: Feb 2011
Location: Georgia, USA
Posts: 32
Thanks: 17
Thanked 15 times in 9 posts
Default Clarification on Update() please

Quote:
Originally Posted by NinjaTrader_Brett View Post
Hello,

Update() will only trigger if it needs an Update to get "up to speed". So you wont waste unneeded CPU cycles if the value is already up to date. Its always a good idea to have Update there as an insurance policy to make sure you always have up to date and correct values.

-Brett
Hello Brett,

I have been wondering about this because I am refactoring some code into multiple indicators to be used by other indicators and strategies. Can you elaborate on your statement, "Update() will only trigger if it needs an Update to get "up to speed"."?

I take this to mean that if I call Update() from within a property or from outside the indicator, the OnBarUpdate() method will only be called if it needs to "get up to speed". How does it know whether or not it needs to get up to speed?

I was thinking that, for when COBC is true, I could put something like the following in the beginning of the OnBarUpdate() method, but it sounds from your comment that this would be redundant; that NT already manages that.

Code:
private int lastBarCalced = -1;

protected override void OnBarUpdate()
{
     if (CurrentBar = lastBarCalced) return;
     lastBarCalced = CurrentBar;
     // continue with the method...
}
If my understanding is correct, does NT also "know" when an update is not needed for when COBC is false?

Many Thanks.

Scott
Scott Daggett
NinjaLaunchPad.com

NinjaScript Programmer's Launch Pad
Tutorial ebook at ninjalaunchpad.com
sh_daggett is offline  
Reply With Quote
Old 11-20-2012, 06:37 AM   #9
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,498
Thanks: 109
Thanked 291 times in 280 posts
Default

Hello,

Sure basically its a good idea to add Update() in the property so that when access a value it makes sure that you have the most update to date variables as possible.

How it works is it will update to the current CurrentBar index if it is not already there.

If COBC=False however this would always force an update since we would have to assume we are not up to date since we have no index to check against as your question lead me believe that is what you are asking on.

There is no cost saving measures we could do on COBC = false like what we can on COBC = true.

-Brett
NinjaTrader_Brett is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Brett for this post:
Old 11-20-2012, 09:00 AM   #10
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,200
Thanks: 24
Thanked 1,226 times in 997 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by sh_daggett View Post
...
Code:
private int lastBarCalced = -1;
 
protected override void OnBarUpdate()
{
     if (CurrentBar = lastBarCalced) return;
     lastBarCalced = CurrentBar;
     // continue with the method...
}
Scott
That sequence seems to be identical to just using "if (FirstTickOfBar)...". No?
koganam is online now  
Reply With Quote
The following user says thank you to koganam for this post:
Old 11-20-2012, 02:59 PM   #11
sh_daggett
Member
 
Join Date: Feb 2011
Location: Georgia, USA
Posts: 32
Thanks: 17
Thanked 15 times in 9 posts
Default

Quote:
Originally Posted by koganam View Post
That sequence seems to be identical to just using "if (FirstTickOfBar)...". No?
Hmmmm....well, I guess I was thinking that Update() might be called as a result of OnMarketData() accessing a property of a hosted indicator, in which case OnBarUpdate() of the hosted indicator might be called intrabar, even if COBC is true. I think Brett is saying that I don't need to worry about it because NT handles it in such a way that the extra calls to OnBarUpdate() in the hosted indicator don't happen. I'm probably just confused. What am I missing? (And thanks for the reply and your interest. I appreciate it!)

Scott
Scott Daggett
NinjaLaunchPad.com

NinjaScript Programmer's Launch Pad
Tutorial ebook at ninjalaunchpad.com
sh_daggett is offline  
Reply With Quote
Old 11-21-2012, 05:58 AM   #12
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,498
Thanks: 109
Thanked 291 times in 280 posts
Default

Hello,

Right if COBC = true you should not have to worry about the extra calls as NT wont force OnBarUpdate to run if it already has processed that bar. If COBC = false however then it would recalculate each time.

-Brett
NinjaTrader_Brett is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Brett for this post:
Reply

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
Basing one Property/Parameter on Another Property/Parameter gregschr General Programming 5 08-10-2010 11:31 AM
ChartOnly property David Lean Indicator Development 1 12-11-2009 07:11 AM
Historical property tamas Strategy Analyzer 2 06-28-2009 11:46 PM
Added Property to Strategy but Not Showing on Property Grid stocastix Automated Trading 2 11-07-2008 04:00 PM
Make trail stop value update intra bar without indicator update. dendy Strategy Development 26 10-21-2007 09:14 AM


All times are GMT -6. The time now is 11:11 AM.