• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple Time math

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

    Simple Time math

    I've looked at the "SampleTimeFilter" strategy and need help modifying it with some simple time math. The sample code allows me to make the opposite condition, when to NOT trade, for example around the market open, or news events. If I have the following code:
    Code:
     
    if ( ToTime(Time[0]) > 65000 && ToTime(Time[0]) < 71000 )
    bTimeToPause = true;
    how would I change it to use a parameter offset around 8:30, or 7:00? I can't just use something like:
    Code:
     
    nTimeStart = 70000 - Pause_Time_Buffer * 100;
    because that would yeild 69500 when Pause_Time_Buffer = 5 mins.

    There has to be some simple time math I just can't find anywhere. I'd like my code to be more like:
    Code:
    xTimeStart = 70000;
    xTimeStart.SubtractMinutes( Pause_Time_Buffer );
    xTimeEnd = 70000;
    xTimeEnd.AddMinutes( Pause_Time_Buffer );
    if ( ToTime(Time[0]) > xTimeStart && ToTime(Time[0]) < xTimeEnd )
    bTimeToPause = true;
    
    So what type should I use for xTimeStart and xTimeEnd?

    #2
    Originally posted by TiggerTrader View Post
    I've looked at the "SampleTimeFilter" strategy and need help modifying it with some simple time math. The sample code allows me to make the opposite condition, when to NOT trade, for example around the market open, or news events. If I have the following code:
    Code:
     
    if ( ToTime(Time[0]) > 65000 && ToTime(Time[0]) < 71000 )
    bTimeToPause = true;
    how would I change it to use a parameter offset around 8:30, or 7:00? I can't just use something like:
    Code:
     
    nTimeStart = 70000 - Pause_Time_Buffer * 100;
    because that would yeild 69500 when Pause_Time_Buffer = 5 mins.

    There has to be some simple time math I just can't find anywhere. I'd like my code to be more like:
    Code:
    xTimeStart = 70000;
    xTimeStart.SubtractMinutes( Pause_Time_Buffer );
    xTimeEnd = 70000;
    xTimeEnd.AddMinutes( Pause_Time_Buffer );
    if ( ToTime(Time[0]) > xTimeStart && ToTime(Time[0]) < xTimeEnd )
    bTimeToPause = true;
    
    So what type should I use for xTimeStart and xTimeEnd?
    If you want to use the syntax that you have described, then you should be using the standard C# DateTime objects and methods, not the NT data-massage methods.

    ref: https://www.google.com/search?q=%2Bd...w=1293&bih=876

    Comment


      #3
      Yeah, that's what I thought, but ick! Doesn't really lend itself to my topic of "Simple Time math"

      I would need to construct a DateTime variable and somehow miraculusly set it to 7AM today. Not straightforward as far as I can tell, then do math on that...

      Really was hoping there was a simpler way. Even a constructor of DateTime dtTest = new DateTime( 700 AM ); or something.

      Comment


        #4
        Originally posted by TiggerTrader View Post
        Yeah, that's what I thought, but ick! Doesn't really lend itself to my topic of "Simple Time math"

        I would need to construct a DateTime variable and somehow miraculusly set it to 7AM today. Not straightforward as far as I can tell, then do math on that...

        Really was hoping there was a simpler way. Even a constructor of DateTime dtTest = new DateTime( 700 AM ); or something.
        TiggerTrader: Check out SH_Dagget's code. It takes time as inputs.. which is what you are looking for?


        http://www.ninjatrader.com/support/f...d=4&linkid=557

        Comment


          #5
          Thanks, but its not quite.

          I want to do MATH on the times. His code points me to using TimeSpans, but Time[0] is a DateTime, so let the conversions ensue...

          Just wish there was an easy way...

          Comment


            #6
            Originally posted by TiggerTrader View Post
            Thanks, but its not quite.

            I want to do MATH on the times. His code points me to using TimeSpans, but Time[0] is a DateTime, so let the conversions ensue...

            Just wish there was an easy way...
            I reused some of his code, and this work. (0005 is 5 minutes)


            if ( Time[0].TimeOfDay >= openTimeTS
            && Time[0].TimeOfDay < openTimeTS + convertTimeSetting("0005")
            {

            Comment


              #7
              Thanks for your reply but adding 30 lines of code to do what should happen in 3 is not what I was hoping for.

              Comment


                #8
                Originally posted by TiggerTrader View Post
                Thanks for your reply but adding 30 lines of code to do what should happen in 3 is not what I was hoping for.
                Code:
                private DateTime xTimeInit;
                private DateTime xTimeStart;
                private DateTime xTimeEnd;
                private bool bTimeToPause = false;
                private double Pause_Time_Buffer = 10.0;
                Code:
                if (Time[0].Date != Time[1].Date) 
                {
                xTimeInit = Time[0].Add(new TimeSpan(7, 00, 00));
                xTimeStart = xTimeInit.AddMinutes(0 - Pause_Time_Buffer ); 
                xTimeEnd = xTimeInit.AddMinutes( Pause_Time_Buffer );
                }
                if ( Time[0] > xTimeStart && Time[0] < xTimeEnd ) bTimeToPause = true;
                Last edited by koganam; 01-06-2013, 02:02 PM.

                Comment


                  #9
                  Wouldn't that be nice if that would work? Aren't we just adding 7 hours to the date & time of bar 0? I'm looking for 7:00. I run this on a range bar chart, so the cross over of the day would happen at random times.

                  Comment


                    #10
                    Originally posted by TiggerTrader View Post
                    Wouldn't that be nice if that would work? Aren't we just adding 7 hours to the date & time of bar 0? I'm looking for 7:00. I run this on a range bar chart, so the cross over of the day would happen at random times.
                    Yes, and no.

                    The code says (I added annotations):
                    Code:
                    if (Time[0].Date != Time[1].Date) //when and only when the last bar's date is not the same as this bar's date. IOW, when a new day starts, then and only then, set the start and end times to today's times. (Your code was the one that specified 7AM: I just used it).
                    {
                    xTimeInit = Time[0].Add(new TimeSpan(7, 00, 00));
                    xTimeStart = xTimeInit.AddMinutes(0 - Pause_Time_Buffer ); 
                    xTimeEnd = xTimeInit.AddMinutes( Pause_Time_Buffer );
                    }
                    IOW, that code runs ONLY once a day, and sets the begin and end times, whenever the bars say that we have crossed the day boundary, regardless of what type of bars, as it is only time dependent. Have you even run the code to see if it does what you requested?

                    Regardless you also have to decide when to make bTimeToPause = false;. As your code never mentioned it, I do not know when you want so to do.

                    Sarcasm can be cute, but you might want to discard it when you ask for help, even if you think the help is useless.
                    Last edited by koganam; 01-06-2013, 04:15 PM.

                    Comment


                      #11
                      After re-reading my post, I can see how sarcasm came thru. Believe me, it was not my intention. I guesss I am more frustrated with myself as this isn't clicking yet. I've spent 20 years as a software developer, my last 10 years doing C++. This should be a breeze for me, but WOW am I rusty!

                      I sincerely apologize if I've hurt your feelings even a twinge, and sincerely thank you for sticking with me thru this.

                      OK, so I got the code to work, thank you again, but I still don't get it. What is
                      Code:
                      xTimeInit = Time[0].Add(new TimeSpan(7, 00, 00));
                      doing? It looks like:
                      Time[0] is the date & time of the current bar.
                      Add says that it Adds the TimeSpan to the current structure, which I interpret as adding 7 hours to this bar's actual date & time. So the bar when we cross from one day to the next may have closed at midnight & 3 minutes & 30 seconds. So what is happening is that the "Pauses" occurs around 7:03:30, not 7AM. This would work on a minute bar because the first minute would be 12:00 AM.

                      But again, thanks for the help.

                      Remember, I was just hoping this would be easy, so far it has been well over 5 hours of work

                      Let me summizire what I was hoping the steps would be:
                      1. be able to set a variable to 7AM
                      2. be able to add & subtact an amount of time to make other variables (the buffer zone)
                      3. compare this bar's time to the buffer zone

                      So far, I can't do step 1.

                      Comment


                        #12
                        P.S. I tried this to get 8:30, but it failed to work:
                        Code:
                        
                        dtNYSETimeInit = Time[0].Subtract(new TimeSpan(Time[0].Hour, Time[0].Minute, Time[0].Second));
                        dtNYSETimeInit.Add(new TimeSpan(8, 30, 00));
                        

                        Comment


                          #13
                          Originally posted by TiggerTrader View Post
                          After re-reading my post, I can see how sarcasm came thru. Believe me, it was not my intention. I guesss I am more frustrated with myself as this isn't clicking yet. I've spent 20 years as a software developer, my last 10 years doing C++. This should be a breeze for me, but WOW am I rusty!

                          I sincerely apologize if I've hurt your feelings even a twinge, and sincerely thank you for sticking with me thru this.

                          OK, so I got the code to work, thank you again, but I still don't get it. What is
                          Code:
                          xTimeInit = Time[0].Add(new TimeSpan(7, 00, 00));
                          doing? It looks like:
                          Time[0] is the date & time of the current bar.
                          Add says that it Adds the TimeSpan to the current structure, which I interpret as adding 7 hours to this bar's actual date & time. So the bar when we cross from one day to the next may have closed at midnight & 3 minutes & 30 seconds. So what is happening is that the "Pauses" occurs around 7:03:30, not 7AM. This would work on a minute bar because the first minute would be 12:00 AM.

                          But again, thanks for the help.

                          Remember, I was just hoping this would be easy, so far it has been well over 5 hours of work

                          Let me summizire what I was hoping the steps would be:
                          1. be able to set a variable to 7AM
                          2. be able to add & subtact an amount of time to make other variables (the buffer zone)
                          3. compare this bar's time to the buffer zone

                          So far, I can't do step 1.
                          Actually you are right. I left out one little detail in there. That line should read:

                          Code:
                          xTimeInit = Time[0].Date.Add(new TimeSpan(7, 00, 00));
                          That centers the time at midnight, before the arithmetic. Oops.

                          Don't worry too much about the sarcasm comment: I know that it is sometimes hard to convey the real intent when writing, as opposed to when speaking. Hence, my rather liberal use of smiley's to show jocularity, if I think what I write may sound sarcastic, and that is not my real intent.

                          Comment


                            #14
                            Thanks! That was it. Now it makes perfect sense. I'll get the hang of it eventually.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by ralett, Yesterday, 07:29 PM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_Marco  
                            Started by bboyle1234, Yesterday, 10:46 PM
                            0 responses
                            3 views
                            0 likes
                            Last Post bboyle1234  
                            Started by swcooke, Yesterday, 04:29 PM
                            0 responses
                            4 views
                            0 likes
                            Last Post swcooke
                            by swcooke
                             
                            Started by bboyle1234, Yesterday, 04:27 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post bboyle1234  
                            Started by swcooke, Yesterday, 03:31 PM
                            2 responses
                            11 views
                            0 likes
                            Last Post swcooke
                            by swcooke
                             
                            Working...
                            X