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 09-06-2009, 12:16 PM   #1
forrestang
Senior Member
 
Join Date: Aug 2009
Location: Chicago
Posts: 213
Thanks: 39
Thanked 4 times in 4 posts
Default How to use the MIN/MAX() function w/data series, or return value between two time?

Howdy,

This is something I've been trying to get to work for some time within one of my automated strategies. It works ok the way I've done it, but I've noticed a bug as of late when running live.

My goal is to take two times, and return the highest value into a variable using the MIN() or MAX() function.

What I have done previously, is find the highest value, and say when time equals that value, look back x number of bars to return the highest value. So if time equals 14:00, and I want to return the highest and lowest values say in between 13:30-14:00 on a 5 minute chart I have done the following:
Code:
if (ToTime(Time[0])== ToTime(14, 00, 0))		//gets the HH and LL between 13:30 and 14:00
			{
			 HH1 = MAX(High,6)[0];		
			 LL1 = MIN(Low,6)[0];		
			}
This looks back 6 bars before 14:00 to return the highest and lowest values.

Now this works, but it is also limited. In that it's confusing when I want to adjust for various times cause I have to count bars, and it also does not work if I backtest with a different time frame because I would have to factor in the new time with the proper amount of bars.

I think this can be done by using, "IDataSeries?"

If not, is there a simple way to accomplish what I want to do w/o doing it the goofy/cumbersome way I have done it?

Thanks,
Forrest
forrestang is offline  
Reply With Quote
Old 09-06-2009, 01:17 PM   #2
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
Default

Hi Forrest, there is a sample that demonstrates how to get an indicator's value for a certain time. The same concept would apply for just getting a bar's price data. The sample can be found here.
NinjaTrader_Austin is offline  
Reply With Quote
Old 09-12-2009, 02:35 AM   #3
forrestang
Senior Member
 
Join Date: Aug 2009
Location: Chicago
Posts: 213
Thanks: 39
Thanked 4 times in 4 posts
Default

Quote:
Originally Posted by NinjaTrader_Austin View Post
Hi Forrest, there is a sample that demonstrates how to get an indicator's value for a certain time. The same concept would apply for just getting a bar's price data. The sample can be found here.
Thanks for your reply Austin,

But I dont think this is what I want?

What I want to do is return the highest value in between two times of the day. Not get the highest value at a certain time.

I almost tried writing a loop for this, but I am hoping there is a simpler way to do it?
forrestang is offline  
Reply With Quote
Old 09-12-2009, 07:45 AM   #4
Ralph
Senior Member
 
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
Default

I wouldn't write a loop either, Forrest. I would collect the data as the time progresses. And note, I used two TimeSpan variables to define the start- and stop-time. This way, you could implement these values as properties, just in case you think about optimizing these values too.

Regards
Ralph

Code:

double maxVal = Double.MinValue;
double minVal = Double.MaxValue;
TimeSpan startTime = new TimeSpan(13, 30, 0);
TimeSpan stopTime = new TimeSpan(14, 00, 0);
...
if (Time[0].TimeOfDay >= startTime && Time[0].TimeOfDay <= stopTime)
{
  HH1 = Math.Max(maxVal, High[0]);
  LL1 = Math.Min(minVal, Low[0]);
}
Ralph is offline  
Reply With Quote
Old 09-13-2009, 12:22 PM   #5
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

This link may help also:
http://www.ninjatrader-support2.com/...ead.php?t=8600
NinjaTrader_Ben is offline  
Reply With Quote
Old 09-13-2009, 12:25 PM   #6
forrestang
Senior Member
 
Join Date: Aug 2009
Location: Chicago
Posts: 213
Thanks: 39
Thanked 4 times in 4 posts
Default

Quote:
Originally Posted by Ralph View Post
I wouldn't write a loop either, Forrest. I would collect the data as the time progresses. And note, I used two TimeSpan variables to define the start- and stop-time. This way, you could implement these values as properties, just in case you think about optimizing these values too.

Regards
Ralph
I want to say thank you for your help Ralph. I wanted to wait till i got it working before replying. I'm still messing with it, as there are some tweaks I have made to the code you provided. It is almost working though.
forrestang is offline  
Reply With Quote
Old 09-13-2009, 02:20 PM   #7
Ralph
Senior Member
 
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
Default

Thanks Forrest. Here is one more thought which is of interest only if you are looking to tune the performance of your strategy. Time[0].TimeOfDay creates an anonymous object of type TimeSpan. This finally is compared with startTime/stopTime. In your case this instantiation happens twice. You could save a little time if you instantiate this object one time explicitely and then use it twice:

TimeSpan timeOfDay = Time[0].TimeOfDay;
if (timeOfDay >= startTime && timeOfDay <= endTime)
...

Regards
Ralph
Ralph is offline  
Reply With Quote
Old 03-12-2011, 09:41 PM   #8
djkiwi
Senior Member
 
Join Date: Mar 2010
Posts: 321
Thanks: 88
Thanked 5 times in 5 posts
Default Tie Of Day Issues

Hi guys, trying to work out the volume between 6 am and 7 am and struggling to convert it to a string. This is what I have so far but won't compile no matter what I do to it:


DrawText("Up1", true, (ToTime(Time[0])>= 70000 (Vol[0]- ToTime(Time[0])<= 80000 (Vol[0].ToString("N2")), -2, High[0] +80 * TickSize ,10, Color.Yellow, largeFont, StringAlignment.Near, Color.Red, Color.Red, 10);

Any ideas on this one?
Thanks
DJ
djkiwi is offline  
Reply With Quote
Old 03-14-2011, 07:21 AM   #9
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,566
Thanks: 261
Thanked 1,015 times in 996 posts
Default

DJ, first I would take the time condition out of the DrawText statement. Next you seem to miss a few braces in the condition and Vol would be VOL() as NinjaScript is case sensitive. I would suggest you simplify the code line until it compiles for you and only then add more complexity....
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-14-2011, 11:02 AM   #10
djkiwi
Senior Member
 
Join Date: Mar 2010
Posts: 321
Thanks: 88
Thanked 5 times in 5 posts
Default Time Issues

Thanks Bertrand, well the problem is really stemming from how to calculate the total volume between a certain timeframe, and I can't find any reference on this. Ok so I have summed the volume for the last 20 periods only between the timeframe 6.30 am to 11 am. The problem is if I change the period to say 9 .30 am to 1100 am it stays the same because it's between those times. So it appears I need to have the Time in the string somehow. I have put the time in the string as per point 2 below but says I'm missing brackets no matter where I put the brackets doesn't compile. Am I on the right track with point 2 as there is nowhere around I can find how to do this.....

Cheers
DJ

Point 1.

if (ToTime(Time[0]) >= 63000 && ToTime(Time[0]) <= 110000)

{

DrawText("Up1", true, SUM(Volume,20)[0].ToString("N0"), -15, High[0] +80 * TickSize ,10, Color.Yellow, largeFont, StringAlignment.Far, Color.Green, Color.Lime, 10);

}

Point 2.

DrawText("Up5", true, SUM(Volume,20[0](ToTime(Time[0]&& SUM(Volume,20)[0].ToString("N0") <= 110000.ToString("N0"), -15, High[0] +30 * TickSize ,10, Color.Yellow, largeFont, StringAlignment.Far, Color.Green, Color.Lime, 10);
Last edited by djkiwi; 03-14-2011 at 01:54 PM.
djkiwi is offline  
Reply With Quote
Old 03-15-2011, 03:46 AM   #11
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,566
Thanks: 261
Thanked 1,015 times in 996 posts
Default

DJ, I'm not sure what you're trying to express with this string -

SUM(Volume,20[0](ToTime(Time[0]&& SUM(Volume,20)[0].ToString("N0") <= 110000.ToString("N0")

The Sum of Volume you calculate is not limited to a certain time range here, it's just calculating over the last 20 bars.

You could for example run a custom calculation to add the volume to a double / dataseries for each bar for the time range you like to see, so that at the end of it you have the total for the period.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-15-2011, 08:30 AM   #12
djkiwi
Senior Member
 
Join Date: Mar 2010
Posts: 321
Thanks: 88
Thanked 5 times in 5 posts
Default double issue

Thanks Bertrand, yes I this is what I want to do:

"You could for example run a custom calculation to add the volume to a double / dataseries for each bar for the time range you like to see, so that at the end of it you have the total for the period"

I am not sure how to do it, any references on it?

Thanks
DJ
djkiwi is offline  
Reply With Quote
Old 03-15-2011, 09:38 AM   #13
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,566
Thanks: 261
Thanked 1,015 times in 996 posts
Default

DJ, while I would unfortunately not have a full working sample - you can use something like the below - at the session begin it resets the tracking data series and then start to sum up the bar's volume:

if (Bars.SessionBreak)
volSum.Set(0);
else
volSum.Set(volSum[1] + VOL()[0]);
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-22-2011, 10:56 AM   #14
djkiwi
Senior Member
 
Join Date: Mar 2010
Posts: 321
Thanks: 88
Thanked 5 times in 5 posts
Default Vol ema

Thanks Bertrand I am still working on this. As a related issue (I think it's related) I want to start an EMA calculation/plot from the start of the session not overlap with the previous day. Please have a look at the attached chart you can see the EMA is screwed up because the session finishes and then in the morning the start number is much less screwing up the EMA taking it awhile to catch up.

Thanks DJ
Attached Images
File Type: jpg ^ADD (3 Min) 3_22_2011.jpg (43.5 KB, 16 views)
djkiwi is offline  
Reply With Quote
Old 03-22-2011, 12:11 PM   #15
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,499
Thanks: 109
Thanked 291 times in 280 posts
Default

Hello,

This gets a little more complex in how you would need to set this up. Quick question how many bars back is this EMA your running?

I look forward to assisting you further.
NinjaTrader_Brett 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Previous session Max/Min mdm72 Strategy Development 1 05-05-2009 05:38 AM
Min Max Values of Y-axis roonius Indicator Development 1 02-14-2009 02:08 AM
Max/Min List nfeldberg General Programming 2 07-16-2008 01:22 PM
MAX/MIN limits Richard Von Indicator Development 3 07-30-2007 08:29 AM
Min / Max mydet Indicator Development 1 05-31-2007 04:47 AM


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