Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

backtesting and DateTime.Now

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

    backtesting and DateTime.Now

    Hi,

    In a strategy that uses DateTime.Now to get the current time, is this simulated in a backtest to be the correct time for the current bar?

    Thanks

    #2
    No since DateTime.Now is a .NET property, NT has no wrapper logic around that. If you want relative time you need to access Time[0] which gives you bar time.
    RayNinjaTrader Customer Service

    Comment


      #3
      Is the reverse also true? If I use Time[0] in my strategy will it not work, and I should use DateTime.Now instead? For example, if I don't want to allow trading after 11:30am, should I check that ToTime(Time[0]) <= 113000 or ToTime(DateTime.Now) <= 113000 in my strategy when trading it live? If so, then we have to do something like

      Code:
      if (Historical)
          allowTrading = ToTime(Time[0]) >= 60000 && ToTime(Time[0]) <= 113000;
      else
          allowTrading = ToTime(DateTime.Now) >= 60000 && ToTime(DateTime.Now) <= 113000;
      ????
      Last edited by shodson; 06-28-2010, 09:13 AM.

      Comment


        #4
        Hello Shodson,

        They refer to different things and how you use will depend on what you want to express.

        Time[0] refers to the time stamp of a bar. It is the time that the bar has ended or is projected to end.

        DateTime.Now will capture your system clock at the moment of time the code is processed.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Strategy Tester and Historical Trading when startíng a strategy

          Hallo,
          I use this Now Object in my Stratgey (see below). It works fine on Replay Data when connected to "Market Replay Connection" . But it doesn't work in StrategyAnalyser and on historical data when starting my strategy in the chart.


          What can i do? Is there a special Provider to use?

          Thank you.


          private DateTime Now
          {
          get
          {
          try
          {

          DateTime now =
          ( Bars.MarketData.Connection.Options.Provider == Cbi.Provider.Replay
          ? Bars.MarketData.Connection.Now : DateTime.Now);

          return now;
          }
          catch( Exception e)
          {
          Print( headString2 + "ERROR Now Exception: " + e);
          return DateTime.Now;
          }
          }
          }

          Comment


            #6
            Originally posted by RolfTheus View Post
            Hallo,
            I use this Now Object in my Stratgey (see below). It works fine on Replay Data when connected to "Market Replay Connection" . But it doesn't work in StrategyAnalyser and on historical data when starting my strategy in the chart.


            What can i do? Is there a special Provider to use?

            Thank you.


            private DateTime Now
            {
            get
            {
            try
            {

            DateTime now =
            ( Bars.MarketData.Connection.Options.Provider == Cbi.Provider.Replay
            ? Bars.MarketData.Connection.Now : DateTime.Now);

            return now;
            }
            catch( Exception e)
            {
            Print( headString2 + "ERROR Now Exception: " + e);
            return DateTime.Now;
            }
            }
            }
            Code:
            if (Bars.MarketData.Connection.Options.Provider.ToString() != null){//...}

            Comment


              #7
              Thank You,

              ok, you mean to avoid the exception catching. But I mean, i don't get a Date-Object with the historical Date. I only get the DateTime.Now Object but on historical Data I need the historical Time in my strategy when i test in strategyanalyzer or in the chart, when historical Data are processed. I.E., I dont want trades after 5:00 p.m., not today 5:00 p.m. but on 10.8.2015 5:00 p.m.



              Thank You.

              Comment


                #8
                Hello RolfTheus,

                Thanks for your post.

                You can use the bar time to accomplish your goal.

                if (ToTime(Time[0] >= Time ((17, 0, 0))
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thank You,
                  ok 5:00 p.m. was only an example.

                  Ok, you mean, there is no other possible way to get accurate historical time during strategy testing or historical data processing because i need nanoseconds to make accurate time based decisions?

                  Thank You
                  Last edited by RolfTheus; 10-09-2015, 01:28 PM.

                  Comment


                    #10
                    Originally posted by RolfTheus View Post
                    Thank You,

                    ok, you mean to avoid the exception catching. But I mean, i don't get a Date-Object with the historical Date. I only get the DateTime.Now Object but on historical Data I need the historical Time in my strategy when i test in strategyanalyzer or in the chart, when historical Data are processed. I.E., I dont want trades after 5:00 p.m., not today 5:00 p.m. but on 10.8.2015 5:00 p.m.



                    Thank You.
                    No, you can try to catch an exception. Your line checks only one provider. I simply gave you the syntax to check that there IS a provider from whom you can get the time, or else get the system time.

                    Comment


                      #11
                      Thank you,

                      a better solution is:

                      if (Bars.MarketData.Connection.Options.Provider != null){//...}

                      because i got the exception, when i try

                      if (Bars.MarketData.Connection.Options.Provider.ToStr ing() != null){//...}

                      because the Provider is null

                      Thank You

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by mmenigma, Yesterday, 03:25 PM
                      1 response
                      11 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by kujista, Today, 05:44 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post kujista
                      by kujista
                       
                      Started by ZenCortexCLICK, Today, 04:58 AM
                      0 responses
                      9 views
                      0 likes
                      Last Post ZenCortexCLICK  
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      172 responses
                      2,281 views
                      0 likes
                      Last Post sidlercom80  
                      Started by Irukandji, Yesterday, 02:53 AM
                      2 responses
                      18 views
                      0 likes
                      Last Post Irukandji  
                      Working...
                      X