Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ToTime(Time[0]) Confusion

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

    ToTime(Time[0]) Confusion

    if((ToTime(Time[0]) >= 000000 && ToTime(Time[0]) < 070000) || (ToTime(Time[0]) >= 190000 && ToTime(Time[0]) < 235900))
    {
    night =
    true;
    }
    if((ToTime(Time[0]) >= 070000 && ToTime(Time[0]) < 151500))
    {
    night =
    false;
    }

    I'm using the above code to designate night sessions. The night flag should prevent the strategy from buying during night sessions. I can only sell. However it bought this morning at 064500. The only thing I can imagine is that (ToTime(Time[0])) is grabbing the end of bar time and not the actual time.

    Any ideas?

    #2
    That is absolutely correct. ToTime(Time[0]) grabs the time of the bar's timestamp. The bar's timestamp can be into the future.

    Instead you can try using ToTime(DateTime.Now) for the exact time as determined by your PC clock. This will obviously not work in backtesting though.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      On 5 min bars interval: The very first trade during (NYSE market hours) would then be the 9:35:00 bar (= 93500 from ToTime)?

      So I guess your sample script SampleTimeFilter.zip which includes
      if ((ToTime(Time[0]) >= 93000 && ...
      is not coded as it was meant to be, because one would enter already from 9:25 am on instead of 9:30 am which is more typical. At least it tends to be a bit missleading when new to NT. So I think to start at market open one would better use
      if ((ToTime(Time[0]) >= 93500 && ...
      or
      if ((ToTime(Time[0]) > 93000 && ...
      right?

      Comment


        #4
        Hi BillCh, NinjaTrader uses the closing bar time to stamp it. Therefore on your 5 min chart the bar with the 9:35 timestamp will contain the first 5 minutes of activity after then open (9:30 - 9:35).

        If you have a strategy using this snippet and you have set CalculateOnBarClose to true the >= 93000 line will work as expected, also consider this is a general sample how to write time filters and it wasn't specifically designed for 5 min bars.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          SampleTimeFilter - Work on Strategy Analyzer?

          Hello:

          It should be noted that this code doesn't appear to work when using Strategy Analyzer to optimize personal trading parameters for trading, especially using any minute data. Many trades occur outside the traditional trading hours selected, which is not good. The Optimizer window also allows one to set the trading hours, and in combination with SampleTimeFilter, one would expect trading hour restrictions to hold true. But neither restrict trading to those hours over all trades. I am anxious to hear of what changes to the code might make this more relevant to us when backtesting strategies. Thanks.

          Tony

          Comment


            #6
            Tony,

            Unfortunately I am not quite following you. When you run in the Strategy Analyzer if you set the start/end times to exclude certain times it will definitely not process anything during those times. Whether your trades remain active through that time is a different story, but setting the session times will definitely prevent any logic from processing in out of session time periods.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              SampleTimeFilter

              Hello Josh:

              I must disagree with you. I have set the Session begins and end values to 9:30 AM and 4:00 PM and trades occurred either before 09:30 AM or after 4:00 PM. I added the SampleTimeFilter to my code to attempt to eliminate this problem. Here is an example from the "Trades" tab of one optimizer run. Note the entry time for trade 15:

              15 HYG Back101 Tony Long 100 87.81 87.81 3/5/2010 4:10:00 PM 3/22/2010 9:50:00 AM ...

              Trades 14 and 16 were within parameters. I know that the last open trade/"Exit on close" timestamp will be after market close as it is still open and waiting for next trading day.

              Perhaps there is a setting I am missing other than making "Exit on close" equal to false.

              Thanks.

              Tony

              Comment


                #8
                Tony,

                I guarantee you if you filtered your data through a session template no entries would be placed outside of that session template's session definitions.

                When you use 9:30-4 as your session definition literally no data is processed till 9:30. There is no way you can submit any orders before that point in time. You can test this for yourself by simply printing the timestamp of every bar processed.

                Print(Time[0]);

                You will see no processing occurs at 9:00 or 8:30 or any time earlier. I suggest you print the times as to when exactly you are trying to submit orders. Then you will be able to see what exactly is happening.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  SampleTimeFilter - Strategy Analyzer (9:30 - 4:00)

                  Hello Josh:

                  Here is the output window of a 20 minute strategy run using print function as you suggested:

                  3/23/2010 9:50:00 AM
                  3/23/2010 10:10:00 AM
                  ...
                  ...
                  3/23/2010 3:50:00 PM
                  3/23/2010 4:10:00 PM

                  As you can see, the bar outside the 4:00 PM Session end is included. This is true for the entire data set, EXCEPT the last day in data. Shouldn't the last time be 3:40 PM? It appears there is something I am missing. Thanks.

                  Tony

                  Comment


                    #10
                    Tony,

                    Think about the time increments you are going with. 20 minute bars starting at 9:30. First 20 minute bar will be stamped as 9:50. Next bar after that is +20 mins so 10:10.

                    10:10, 10:30, 10:50, 11:10, 11:30, 11:50, 12:10, 12:30, 12:50, 1:10, 1:30, 1:50, 2:10, 2:30, 2:50, 3:10, 3:30, 3:50, 4:10.

                    You are using 20 minute bars so the last bar is timestamped according to the ending timestamp of the bar which is 4:10. This is accurate behavior.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      SampleTimeFrame - 20 Minute Bars

                      Hello Josh:

                      I understand that the 4:10 bar must be there for 20 minute bars. What you are saying is that if Session ends is set to 4:00 PM, trades will only be allowed up to 4:00 PM. In this instance, if a trade occurs between 3:50 PM and 4:00 PM, it is reflected on 4:10 PM bar as a trade. Thus the Trades tab of Strategy Analyzer will show a trade that occurs between 3:50 PM and 4:00 PM with a timestamp of 4:10 PM. Thanks. This clears up what I thought were discrepancies.

                      Tony

                      Comment


                        #12
                        Right. That is correct.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          if (ToTime(Time[0]) &lt;= 80000 || ToTime(Time[0]) &gt;= 134500){ return;}.

                          I'm having a problem testing the following:
                          if (ToTime(Time[
                          0]) <= 80000 || ToTime(Time[0]) >= 134500){ return;}.
                          I'm in backtest101. Is it true that you can only test ToTime
                          in real time instead of backtesting. I should be getting zero data in my
                          Strategy Analyzer. Instead it's like the ToTime didn't work. In my test data
                          it says I have a profit when I shouldn't have any profit because the computer
                          time is 205400 which is greater than the 134500 end time. The system should
                          not give me any profit. But it's as though the ToTime didn't work. Maybe I
                          have to test this in sym. Any opinions would be welcome.

                          Comment


                            #14
                            bill897, Time[0] refers to the current bar's timestamp. It is false that you can only use ToTime() in realtime. You can use it whenever. If you are looking to get the current actual time indicated by your computer clock, you can use DateTime.Now.

                            I would recommend testing this to see exactly how it works.
                            AustinNinjaTrader Customer Service

                            Comment


                              #15
                              OK, Thanks. Austin.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              14 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Yesterday, 08:42 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Yesterday, 07:51 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,983 views
                              3 likes
                              Last Post jhudas88  
                              Working...
                              X