Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to use the MIN/MAX() function w/data series, or return value between two time?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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

    #2
    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.
    AustinNinjaTrader Customer Service

    Comment


      #3
      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?

      Comment


        #4
        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:
        [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
        [FONT=Courier New]double[/FONT][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New] maxVal = Double.MinValue;[/FONT]
        [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][FONT=Courier New]double[/FONT][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New] minVal = Double.MaxValue;[/FONT]
        [FONT=Courier New]TimeSpan startTime = [/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] TimeSpan([/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]13[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2], [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]30[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2], [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New]);[/FONT]
        [FONT=Courier New]TimeSpan stopTime = [/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] TimeSpan([/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]14[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2], [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]00[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2], [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New]);[/FONT]
        [FONT=Courier New]...[/FONT]
        [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][FONT=Courier New]if[/FONT][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][FONT=Courier New][SIZE=2][SIZE=2] (Time[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]].TimeOfDay >= startTime && Time[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New]].TimeOfDay <= stopTime)[/FONT]
        [FONT=Courier New]{[/FONT]
        [FONT=Courier New]  HH1 = Math.Max(maxVal, High[[/FONT][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New]]);[/FONT]
        [FONT=Courier New]  LL1 = Math.Min(minVal, Low[[/FONT][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]);
        [FONT=Courier New]}[/FONT]
        [/SIZE][/FONT][/SIZE][/FONT]

        Comment


          #5
          Hello,

          This link may help also:
          DenNinjaTrader Customer Service

          Comment


            #6
            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.

            Comment


              #7
              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

              Comment


                #8
                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

                Comment


                  #9
                  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....
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    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, 01:54 PM.

                    Comment


                      #11
                      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.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        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

                        Comment


                          #13
                          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]);
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            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 Files

                            Comment


                              #15
                              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.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by andrewtrades, Today, 04:57 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              436 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post FAQtrader  
                              Started by rocketman7, Today, 09:41 AM
                              5 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X