• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MonthOfYear and WeekOfMonth

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

    MonthOfYear and WeekOfMonth

    I want to program custom filters for my strategy, like Month Of Year (and test which months perform best) and WeekOfMonth (and test which weeks - 1st, 2nd, 3rd, 4th or 5th perform best).

    How can this be done? I just need codes for MonthOfYear and WeekOfMonth (because I only found DayOfWeek code in the manual), I would do other coding myself.

    #2
    Hi UltryNIX,
    It can be done, but requires coding (although it shouldn't in my humble opinion). A lot of people are interested in various different kinds of seasonality.
    https://ninjatrader.com/support/foru...th-day-of-year
    The code for the month itself is easy. The following returns an int from 1 to 12:
    Time[0].Date.Month
    The code for the week is a bit more advanced. Try to identify the turn of the month, then log the day of the week of such event, then increment a counter variable (int) when the same day of the week occurs again while you are still in the same month and finally reset the week's counter variable to zero on each turn of the month.
    Have fun.
    NT-Roland

    Comment


      #3
      Thanks for MonthOfYear Calculation, NT-Roland.

      I found such code for WeekOfMonth:

      Code:
      static int GetWeekNumberOfMonth(DateTime date)
      {
          date = date.Date;
          DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
          DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
          if (firstMonthMonday > date)
          {
              firstMonthDay = firstMonthDay.AddMonths(-1);
              firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
          }
          return (date - firstMonthMonday).Days / 7 + 1;
      }
      However, it doesn't work just by copying and pasting. Can anybody "convert" this code into what NinjaTrader would accept?

      Comment


        #4
        Hello UltraNIX,

        Thank you for your reply.

        The code you provided worked fine for me in NinjaTrader 8. Here's an example:

        Code:
                protected override void OnBarUpdate()
                {
                    if(Bars.IsFirstBarOfSession)
                    {
                        int WeekNumber = GetWeekNumberOfMonth(Time[0]);
                        Print("The week number of the month is: "+ WeekNumber);
                    }
                }
        
                static int GetWeekNumberOfMonth(DateTime date)
                {
                    date = date.Date;
                    DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
                    DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
                    if (firstMonthMonday > date)
                    {
                        firstMonthDay = firstMonthDay.AddMonths(-1);
                        firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
                    }
                    return (date - firstMonthMonday).Days / 7 + 1;
                }
        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Thanks, Kate, I will test it with your added lines.

          By the way, can you help me form a date, like Date (Year, Month, Day), so I could have it like Date (Year(Time[0]), Month (Time[0]), 1) and get the 1st day of Time[0] month.

          Comment


            #6
            Hello UltraNIX,

            You are wanting the day of the week for of the first day of the month that this bar is in, is this correct?

            try:
            Code:
            private DateTime myDateTime;
            myDateTime = new DateTime(Time[0].Year, Time[0].Month, 1);
            Print(string.Format("Time[0]: {0} | myDateTime: {1}, myDateTime.DayOfWeek: {2}", Time[0], myDateTime, myDateTime.DayOfWeek));
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks, all working fine, case closed. Thanks Kate and ChelseaB!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ehvsdhjvsf, Today, 04:42 AM
              0 responses
              1 view
              0 likes
              Last Post ehvsdhjvsf  
              Started by webus, Today, 02:03 AM
              0 responses
              8 views
              0 likes
              Last Post webus
              by webus
               
              Started by momchi, Yesterday, 07:12 PM
              1 response
              15 views
              0 likes
              Last Post NinjaTrader_ChrisSch  
              Started by DieSlower, Yesterday, 11:02 PM
              0 responses
              5 views
              0 likes
              Last Post DieSlower  
              Started by gujungg, Yesterday, 10:59 PM
              0 responses
              13 views
              0 likes
              Last Post gujungg
              by gujungg
               
              Working...
              X