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

Date: 1/1/0001 12:00:02 AM ?

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

    Date: 1/1/0001 12:00:02 AM ?

    Requesting

    Code:
     
    datFirst = new DateTime(ToDay(Time[0]));
    only once when script is started returns

    1/1/0001 12:00:02 AM

    while first bar on the 5min chart would be 4/24/2009 etc.


    How can I get the date and time of the very first bar of a chart to avoid referencing to a non existing bar date and time?

    I don't think
    Code:
    FirstBarOfSession
    is of any help, and I also do not understand why
    Code:
    datFirst = new DateTime(ToDay(Time[CurrentBar]))
    or
    Code:
    datFirst = new DateTime(ToDay(Time[CurrentBar-1]))
    isn't working?

    Thanks!

    #2
    BillCh,

    Why are you trying to create new DateTime objects? Time[0] is already a DateTime. If you want to store out the date and the time into ints, just create int variables and store the returns from ToDay() and ToTime().
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh,

      I need to add days to the dates so I do not want to get integers, and I also want to compare them etc. So I guess I better use DateTime objects, right?

      My overall goal is: I want to read current date and time of the actual 5min bar, then I will look back to the 10previous days 5min bars of the same time to build an high/low average of the let's say 10 last 9:45 AM 5min bars.

      So I loop back 10 times subtracting one day from the current day, plus add one lookback for every Saturday and Sunday I run into, to really get 10 (or whatever I set) values to build the average from.

      Now I dont want to look back to a date which doesnt even exist! Thats why I am trying so hard to solve the most simple issue: what is the date and time stamp of the very first bar I have in my set of data (=my current chart on which I will run that indicator).

      Any hints for a reliable way to get first bars data and also about my general goal are very much appreciated.

      Thanks

      Comment


        #4
        BillCh,

        What you are trying to does not require you to use DateTime. Just use the ints. Add and subtract as you need to adjust the day/times. When you want to compare to different times just change those times to ints too.

        Code:
        someDate = ToDay(Time[0]);
        someTime = ToTime(Time[0]);
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by BillCh View Post
          ...what is the date and time stamp of the very first bar I have in my set of data (=my current chart on which I will run that indicator)...
          Code:
          private bool runTimeInit;
          private DateTime DateTimeOfVeryFirstBar;
          [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] OnBarUpdate()[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
          [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (runTimeInit == [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New] {[/FONT][/SIZE]
          [SIZE=2][FONT=Courier New]   DateTimeOfVeryFirstBar = Time[0];[/FONT][/SIZE]
          [/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]   runTimeInit = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];[/SIZE][/FONT][/SIZE][/FONT]
          [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] }[/SIZE][/FONT][/SIZE][/FONT]
          [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]}[/SIZE][/FONT]
          [/SIZE][/FONT]
          Regards
          Ralph

          Comment


            #6
            Thanks guys!

            I had tried it like Ralph showed, but got this weird date 1/1/0001. So I thought the approach was wrong, but as I now tried it on another chart I strangely did get the right time of the very first bar, hmm?

            @Josh: I still guess to prefer a DateTime object (if that thing is called that way) using the correct way DateTimeOfVeryFirstBar = Time[0] as if I subtract one day from a date integer like 20090401 I would end up with the date 20090400 instead of 20090331, while dateXY.AddDays(-1) would result in a correct date. Please correct me if I am wrong.

            Just fyi, here my simple apporach so far:

            Code:
             
            if (FirstTickOfBar)
            {
            int iCount = period;
            DateTime datum = DateTime.Now.Date;
            sumHiLo = 0;
            for (int i = 1; i <= iCount; i++) 
            {
            DateTime datumRead = datum.AddDays(-i); 
            if (datumRead < DateTimeOfVeryFirstBar)
            {
            // Print( "We ran into a date further back than actually available: " + datumRead.ToString() );
            break;
            }
            if ( (datumRead.DayOfWeek == DayOfWeek.Saturday)
            || (datumRead.DayOfWeek == DayOfWeek.Sunday) )
            {
            Print( "Weekend" );
            iCount = iCount + 1;
            }
            else
            {
            // sumHiLo of same bar i days agao = ...
            }
             
            }
             
            if (period > 0) 
            avgHiLo = sumHiLo / period;
             
            }

            Comment


              #7
              Originally posted by BillCh View Post
              ...but got this weird date 1/1/0001...
              Everey variable of a class object is initiated with zero during instantiation of the concerning class object. I guess 1/1/0001 represents the initialised variable. And I guess in this case you accessed this variable before you ever initialised it according to your requirements.

              Regards
              Ralph

              Comment


                #8
                Originally posted by Ralph View Post
                I guess 1/1/0001 represents the initialised variable. And I guess in this case you accessed this variable before you ever initialised it according to your requirements.
                Ah, thank you! I am anyway pretty sure I messed something up with this object stuff and you showing me how to use it made it quite clear.
                Just as a remark: the weird data was 1/1/0001 12:00:02 AM, so not exactely 0 but 2 seconds more.

                Comment


                  #9
                  Originally posted by BillCh View Post
                  Just as a remark: the weird data was 1/1/0001 12:00:02 AM, so not exactely 0 but 2 seconds more.
                  Looks funny indeed but I can't confirm that. I checked it on my side, printed the content of a DateTime variable in Initialize() and the result was: 01.01.0001 00:00:00

                  Regards
                  Ralph

                  Comment


                    #10
                    Have you tried the simple way?
                    protected overridevoid OnBarUpdate()
                    {
                    if (CurrentBar == 0)
                    {
                    DateTimeOfVeryFirstBar = Time[0];

                    }
                    }

                    Comment


                      #11
                      Originally posted by roonius View Post
                      Have you tried the simple way?
                      if (CurrentBar == 0)
                      DateTimeOfVeryFirstBar = Time[0];

                      ...
                      I guess I did, but I think it didnt work which I assume was just because I overcomplicated the DateTime thing? I think I prefer the "runTimeInit" one-time loop principle as I have such a thing always in my setups to set/check some stuff once in a script.

                      But I wonder. Shouldn't
                      Code:
                       
                      DateTimeOfVeryFirstBar = Time[CurrentBar];
                      also always deliver time of the very first bar? It is always looking back exactely the amount of bars available, no matter what the actual bar is at the moment?

                      Comment


                        #12
                        Originally posted by BillCh View Post
                        I guess I did, but I think it didnt work which I assume was just because I overcomplicated the DateTime thing? I think I prefer the "runTimeInit" one-time loop principle as I have such a thing always in my setups to set/check some stuff once in a script.

                        But I wonder. Shouldn't
                        Code:
                         
                        DateTimeOfVeryFirstBar = Time[CurrentBar];
                        also always deliver time of the very first bar? It is always looking back exactely the amount of bars available, no matter what the actual bar is at the moment?
                        That's true too

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by jaybedreamin, Today, 05:56 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post jaybedreamin  
                        Started by DJ888, 04-16-2024, 06:09 PM
                        6 responses
                        18 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by Jon17, Today, 04:33 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post Jon17
                        by Jon17
                         
                        Started by Javierw.ok, Today, 04:12 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post Javierw.ok  
                        Started by timmbbo, Today, 08:59 AM
                        2 responses
                        10 views
                        0 likes
                        Last Post bltdavid  
                        Working...
                        X