Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

GetTime while backtesting

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

    GetTime while backtesting

    Hi, in my strategy i need the date from today and i access it via "var time = DateTime.Today; ". Unfortunately when backtesting the strategy will always use the "real today" as the "time" and it wont use the actual date that is backtestet.

    How can i get the DateTime from the current bar [0]?
    it wont work like this:

    "var date1 = new DateTime();
    date1 = Time[0];
    Print(date1);
    "
    Also while compiling there is no error. Only when i backtest the strategy there comes an error like that:
    "The object reference was not set to an object instance"
    Thanks for your help
    Felix
    Last edited by FelixMMM; 09-29-2019, 03:12 PM.

    #2
    Hello Felix,

    The Time[0] series is a DateTime object for each bar.

    Below is a link to the help guide.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea,
      i have tried it several times also in different ways.
      Even the official example:
      "Print(Time[0].ToString());" doesnt give an error when compiling.But when i backtest the strategy there comes this error in the output. "The object reference was not set to an object instance". And it comes only if i add that Print() to the OnBarUpdate Method.
      I dont know what to do.
      Thanks Felix

      Comment


        #4
        Hello FelixMMM,

        Are you certain that is the line causing the error?

        Is that line of code in OnBarUpdate()?

        Attached is an export of a simple script with only that line of code in OnBarUpdate().

        Are you able to reproduce the behavior using this reduced test script to test only that code?
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea,
          error is solved. I had the line inside a method that i was calling in Onbarupdate(). When i created a DateTime object inside the OnBarUpdate() method itself and hand over that object to the method i am calling inside OnBarUpdate() the error was gone. I dont know why but that doesnt matter now xD.
          Thanks for your help, appreciate it
          Felix

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Mindset, 04-21-2026, 06:46 AM
          0 responses
          88 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by M4ndoo, 04-20-2026, 05:21 PM
          0 responses
          134 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by M4ndoo, 04-19-2026, 05:54 PM
          0 responses
          68 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by cmoran13, 04-16-2026, 01:02 PM
          0 responses
          119 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          67 views
          0 likes
          Last Post PaulMohn  
          Working...
          X