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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            80 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            40 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            63 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            63 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            54 views
            0 likes
            Last Post CarlTrading  
            Working...
            X