Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Casting Day of Week to an integer

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

    Casting Day of Week to an integer

    Hello:

    I am trying to cast the day of the week to an integer. I would like Sunday = 0, Monday = 1 ... Saturday = 6

    I use:

    int TheDayOfWeek = (int) DateTime.Now.DayOfWeek;

    I am getting an error stating TheDayOfWeek does not exist in the current context.

    How do you do this?

    Thank you

    #2
    Hi geekodude, thanks for posting. This snippet works on my end:

    Code:
    int x = (int)DateTime.Now.DayOfWeek;
    Print(x);

    Comment


      #3
      Hi Chris:

      Thanks for the reply. I just ran your snippet in a script under playback Market Data for the week of 5/7/22 to 5/11/22. I get a value of 5 for the whole session of 5/7/22 to 5/11/22. This makes sense since today is Friday. However, the result is not what is desired.

      For example, I want to compare a trading date range in which the user inputs the StartTradingDay = 1 and EndTradingDay.=3 corresponding to trading on Monday through Wednesday. So how would I cast a statement whereby the date corresponds to Market Replay Date? The data output should be 6 for 5/7/22, 0 for 5/8/22 and so on.

      Thank you

      Comment


        #4
        Hi geekodude, you would want to use the Time[] array to get historical timestamps. DateTime.Now will return the current system time.

        Print((int)Time[0].DayOfWeek);

        Comment


          #5
          Perfect, thank you!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          61 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          32 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          44 views
          1 like
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          58 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          47 views
          0 likes
          Last Post CarlTrading  
          Working...
          X