Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code for months in strategy

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

    Code for months in strategy

    Hi, what code can I use in my strategy to not trade all days in march for example? Also, If I don't want my strategy to trade Fridays in February? Thanks!

    #2
    Hello,

    Thank you for your post.

    You can compare a <DateTime>.Month to a specific number, which would correspond to a month. Below is the Microsoft documentation for this.





    Similarly to detect a day of the week, you could use <DateTime>.DayOfWeek.

    For example,

    Code:
    //Check if it is a Friday in February.
    if (Time[0].Month == 2 && Time[0].DayOfWeek == DayOfWeek.Friday)
    //do something

    Comment


      #3
      That works, thanks!

      Comment


        #4
        My code isn't working:

        if ((Times[0][0].TimeOfDay > new TimeSpan(7, 0, 0))

        && (Times[0][0].TimeOfDay < new TimeSpan(15, 55, 0))

        && (Times[0][0].DayOfWeek != DayOfWeek.Wednesday)

        // Specific

        && ((Time[0].Month == 2 && Time[0].DayOfWeek != DayOfWeek.Friday)

        && (Time[0].Month != 3)

        && (Time[0].Month == 5 && Time[0].DayOfWeek != DayOfWeek.Monday)

        && (Time[0].Month == 8 && Time[0].DayOfWeek != DayOfWeek.Tuesday)

        && (Time[0].Month != 9)

        && (Time[0].Month == 11 && Time[0].DayOfWeek != DayOfWeek.Monday)))


        It doesn't activate. Does is look good? Or do I need to change anything?

        Comment


          #5
          Hello,

          This statement will only be true when the month is February, May, August, AND November - which is not possible.

          Use the "or" operator instead of "and" if you're trying to check if it is February, May, August, OR November.

          Comment


            #6
            Got it thanks!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            115 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            161 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            83 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            127 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            87 views
            0 likes
            Last Post PaulMohn  
            Working...
            X