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 NullPointStrategies, Today, 05:17 AM
            0 responses
            41 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            124 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            64 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            41 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X