Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

WeekOfYear calculation?

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

    WeekOfYear calculation?

    I want to test whether adding WeekOfYear filter would be of improvement.

    Yet I am unable to find function WeekOfYear in manual. I tried searching "C# WeekOfYear", yet it seems that C# has that function and NinjaTrader doesn't. How can I calculate WeekOfYear?

    #2
    Hello UltraNIX,

    Are you trying to make a condition that checks the week of the year as a numeric value or are you trying to filter results in some way by the week of year? You could check the week of year in code by using C# methods like the following:



    I look forward to being of further assistance.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello UltraNIX,

      Are you trying to make a condition that checks the week of the year as a numeric value or are you trying to filter results in some way by the week of year? You could check the week of year in code by using C# methods like the following:



      I look forward to being of further assistance.
      My idea is. Every year has 52 weeks. So I want to make calculation, like, e.g. If (WkOfYear == 2) enter order, if (WkOfYear == 3), do nothing. I will handle entry/exit calculations, I just need to "extract" WeekOfYear.

      Comment


        #4
        Hello UltraNIX,

        You could use the sample from the link I provided in the last post for that. There is not a native NinjaScript method to do this but you can use C# methods.

        I look forward to being of further assistance.

        Comment


          #5
          Which code would you paste into strategy and would you do any modifications?

          And where would you put in? (Which part of the strategy)?

          Comment


            #6
            Hello UltraNIX,

            Which code would you paste into strategy and would you do any modifications?
            The sample on the page is just a sample that demonstrates one concept, you would need to modify that if you were copying in the whole sample but that is more just to learn from than to copy/paste.

            It is important to understand that the Time objects in NinjaScript are C# DateTime objects, what is shown in the MSDN sample is working with a DateTime to collect the week as a number.

            You could use the code shown in the MSDN sample and supply a Time[0] to that instead of the DateTime that is being created. The code shown in the MSDN uses Console.WriteLine which would be similar to Print but is used in a C# application. You could change those parts to be Print if you were copying the whole sample over. You would otherwise just need the parts that precede the output. That would be the using statements, creating the variables and calling the myCal.GetWeekOfYear method to get the week number. Your could store that value to a variable and use it in conditions.

            And where would you put in? (Which part of the strategy)?
            Wherever you wanted to use it, the MSND sample just shows how to get the numeric value of the week based on a DateTime object. How you use that would be determined by you and how you wanted to use that in your code. It would go in OnBarUpdate, that would be about the only guideline I could give. Working with bar times requires OnBarUpdate, that would be where you could see where each bars week is a new week or not.

            I look forward to being of further assistance.

            Comment


              #7
              I don't seem to understand some of the things you've written. I would appreciate if you could adapt the code to NinjaTrader, I am sure it would be useful for other users as well.

              Comment


                #8
                Hello UltraNIX,

                The code in the linked sample is ready for use in NinjaScript, that's just C# code being displayed in the MSDN samples. You would need to use that code if you want to extract the week as a number. If you are not clear on part of what the sample is doing we could go over those parts directly. MSDN contains a large amount samples and education materials to learn C# if you are interested. https://dotnet.microsoft.com/learn/csharp

                A simple example of how that may be used would be:

                Code:
                int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW );
                if(week = 12)
                { 
                   //do your logic
                }
                I look forward to being of further assistance.

                Comment


                  #9
                  Thanks! I will test it tomorrow (it's 90 minutes to midnight here) and hopefully all works well!

                  Comment


                    #10
                    Originally posted by NinjaTrader_Jesse View Post
                    Hello UltraNIX,

                    The code in the linked sample is ready for use in NinjaScript, that's just C# code being displayed in the MSDN samples. You would need to use that code if you want to extract the week as a number. If you are not clear on part of what the sample is doing we could go over those parts directly. MSDN contains a large amount samples and education materials to learn C# if you are interested. https://dotnet.microsoft.com/learn/csharp

                    A simple example of how that may be used would be:

                    Code:
                    int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW );
                    if(week = 12)
                    {
                    //do your logic
                    }
                    I look forward to being of further assistance.
                    It does not work.

                    I tried 3 options:

                    Code:
                    1) Print(CultureInfo("en-US").Calendar.GetWeekOfYear( Time[0], CultureInfo("en-US").DateTimeFormat.CalendarWeekRule, CultureInfo("en-US").DateTimeFormat.FirstDayOfWeek ));
                    Error: The name 'CultureInfo' does not exist in the current context

                    Logic: I just replaced myCWR, myFirstDOW and myCal with their respective variable codes.
                    __________________________________________________ __________________________________

                    Code:
                    2)  public static void Main()
                    {
                    
                    // Gets the Calendar instance associated with a CultureInfo.
                    CultureInfo myCI = new CultureInfo("en-US");
                    Calendar myCal = myCI.Calendar;
                    
                    // Gets the DTFI properties required by GetWeekOfYear.
                    CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
                    DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;
                    
                    int week = myCal.GetWeekOfYear( DateTime.Now, myCWR, myFirstDOW );
                    
                    }
                    Errors:
                    The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
                    The type or namespace name 'Calendar' could not be found (are you missing a using directive or an assembly reference?)
                    The type or namespace name 'CalendarWeekRule' could not be found (are you missing a using directive or an assembly reference?)

                    Logic: I copied the code from your link and just added int week to get that result.

                    __________________________________________________ __________________________________

                    Code:
                    3) int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW );
                    Errors:
                    The name 'myCal' does not exist in the current context
                    The name 'myCWR' does not exist in the current context
                    The name 'myFirstDOW' does not exist in the current context

                    Logic: I just pasted your line. But it seems that variables need to be defined. But I tried doing that in Option: 2 and it failed as well.
                    __________________________________________________ ___________________________________

                    So any other ideas?

                    Comment


                      #11
                      Hello UltraNIX,

                      You would need the using statements from the sample shown at the first line of the sample, you would have to add those into the using statements in your script.

                      The static void main, that is a C# program entry point and for the purpose of MSDN samples it assumes you are making a C# console application. You can imagine that would be the OnBarUpdate method if you are comparing what that part of the sample is. You can ignore the static void main and just focus on the lines inside that method.

                      The only modification needed is what DateTime is being passed to the GetWeekOfYear which is what I highlighted by passing Time[0]. You would also need the 4 lines you copied in your #2, but without the static void main part. You would place the 4 lines and the int week = part before any conditions where you wanted to use that value.



                      Please let me know if I may be of further assistance.

                      Comment


                        #12
                        I found another method on the internet and was able to apply it.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Yesterday, 05:17 AM
                        0 responses
                        65 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        139 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        75 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        45 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        50 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X