Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Convert String to Date Time

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

    Convert String to Date Time

    I want to use the date part of Time[0] and add 21:00 to it for the time part in a Strategy to create a DateTime object.
    I tried the following but got an error

    Convert.ToDateTime.Time[0].ToString("MM/dd/yyyy" + " " + "21:00")

    Convert.ToDateTime not valid in this context. CS0119.

    How else can I accomplish this?
    Last edited by freeway; 10-29-2011, 12:39 AM.

    #2
    try
    Time[0].AddHour(21).ToString("MM/dd/yyyy")

    Comment


      #3
      Get Compile Error CS1061 no definition for AddHour.

      Comment


        #4
        should be AddHours, missed the s

        Comment


          #5
          Thanks that is close to what I want to do but not quite it yet. By add 21:00 I mean I want to take the date part of Time[0] and have the time part be "21:00". I don't want to add 21 hours to what ever the time part of Time[0] is. So, if Time[0] is 08/01/2011 15:00 I want to end up with 08/01/2011 21:00.
          In effect I just want to replace the time part of Tim[0] with 21:00.
          Last edited by freeway; 10-29-2011, 10:04 AM.

          Comment


            #6
            Freeway,

            I am happy to assist you.

            Why not just get the day, month, and year, then create a new datetime object? For more information on methods associated with datetime objects, please see the following link.

            Represents an instant in time, typically expressed as a date and time of day.


            Please let me know if I may assist further.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Here is the code I have now. I want to get the Daily ATR while processing on another time frame.
              There is a Add(PeriodType.Day, 1); in the Initialze method

              if (BarsInProgress == 1)
              {
              Print(Time[0] + " Index Date = " + Time[0].Date.AddHours(21).ToString("MM/dd/yyyy HH:mm:ss"));
              Index = GetBar(Time[0].Date.AddHours(21));
              dailyATR = ATR(BarsArray[1],10)[Index];
              Print(Time[0] + " Index = " + Index.ToString() + " dailyATR = " + dailyATR.ToString());
              }

              The Index variable always has a value of 0. Can you help? I am in the Pacific Time zone. I noted that on the daily charts on my computer that the close time is 21:00. That is why I add 21 hours to the date.
              Last edited by freeway; 10-29-2011, 02:16 PM.

              Comment


                #8
                OK I did note that the code should be Bars.GetBar.... But I don't think the values for dailyATR are correct yet.

                Comment


                  #9
                  Originally posted by freeway View Post
                  Thanks that is close to what I want to do but not quite it yet. By add 21:00 I mean I want to take the date part of Time[0] and have the time part be "21:00". I don't want to add 21 hours to what ever the time part of Time[0] is. So, if Time[0] is 08/01/2011 15:00 I want to end up with 08/01/2011 21:00.
                  In effect I just want to replace the time part of Tim[0] with 21:00.
                  Code:
                  TimeSpan tsTest = new TimeSpan(21, 00, 00);
                  DateTime dtResult = Time[0].Date.Add(tsTest);

                  Comment


                    #10
                    I have the following code to calculate the daily ATR when on another time frame chart.
                    if (BarsInProgress == 1)
                    {
                    Index = Count - CurrentBars[1] - 2;
                    dailyATR = ATR(BarsArray[1],10)[Index];
                    }

                    When I compare the value calculated here to the values on a Daily chart they are not the same. Most of them are close but not =. Should they be the same?
                    Last edited by freeway; 10-29-2011, 05:20 PM.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by SiebertCowen, Yesterday, 11:58 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post SiebertCowen  
                    Started by hunghnguyen2016, Yesterday, 08:00 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post hunghnguyen2016  
                    Started by cbentrikin, Yesterday, 03:49 PM
                    0 responses
                    11 views
                    0 likes
                    Last Post cbentrikin  
                    Started by MiCe1999, 04-14-2025, 06:54 PM
                    7 responses
                    77 views
                    0 likes
                    Last Post b.j.d
                    by b.j.d
                     
                    Started by NISNOS69, Yesterday, 02:20 PM
                    0 responses
                    18 views
                    0 likes
                    Last Post NISNOS69  
                    Working...
                    X