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?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
WeekOfYear calculation?
Collapse
X
-
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?Tags: None
-
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.Originally posted by NinjaTrader_Jesse View PostHello 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
-
Hello UltraNIX,
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.Which code would you paste into strategy and would you do any modifications?
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.
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.And where would you put in? (Which part of the strategy)?
I look forward to being of further assistance.
Comment
-
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:
I look forward to being of further assistance.Code:int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW ); if(week = 12) { //do your logic }
Comment
-
It does not work.Originally posted by NinjaTrader_Jesse View PostHello 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:
I look forward to being of further assistance.Code:int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW ); if(week = 12) { //do your logic }
I tried 3 options:
Error: The name 'CultureInfo' does not exist in the current contextCode:1) Print(CultureInfo("en-US").Calendar.GetWeekOfYear( Time[0], CultureInfo("en-US").DateTimeFormat.CalendarWeekRule, CultureInfo("en-US").DateTimeFormat.FirstDayOfWeek ));
Logic: I just replaced myCWR, myFirstDOW and myCal with their respective variable codes.
__________________________________________________ __________________________________
Errors: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 ); }
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.
__________________________________________________ __________________________________
Errors:Code:3) int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW );
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
-
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
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
54 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
131 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
73 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment