NinjaTrader Support Forum  

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

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 01-18-2007, 10:30 AM   #1
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Post imported post

To check for the most recent occurence of a particular condition you can write something like:

Quote:
private int occuredBarsAgo = -1;

for (int barsAgo = 0; barsAgo < CurrentBar; barsAgo++)
if (SMA(10)[barsAgo] > SMA(20)[barsAgo])
{
occuredBarsAgo = barsAgo;
break;
}

Ray
NinjaTrader_Ray is offline  
Reply With Quote
Old 02-19-2007, 08:22 AM   #2
ThePatientOne
Member
 
Join Date: Feb 2007
Location: , ,
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

Wouldn't this code snippet find the FIRST occurrence of the test case ... not the most recent. I believe you'd want to go backwards or count down from Current bar to 0 to find the most recent occurrence.

Please advise if I am mistaken.
ThePatientOne is offline  
Reply With Quote
Old 02-19-2007, 08:29 AM   #3
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Post imported post

You are mistaken.

Do not confuse "CurrentBar" with an index value that represents the number of bars ago.

Ray
NinjaTrader_Ray is offline  
Reply With Quote
Old 02-19-2007, 08:34 AM   #4
ThePatientOne
Member
 
Join Date: Feb 2007
Location: , ,
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

Sorry ... you are correct. I misread how the index was being used. I thought the code was just cycling through the bars on the chart starting at index 0.
ThePatientOne is offline  
Reply With Quote
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


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