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
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
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.JesseNinjaTrader Customer Service
-
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,
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.
And where would you put in? (Which part of the strategy)?
I look forward to being of further assistance.
JesseNinjaTrader Customer Service
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:
Code:int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW ); if(week = 12) { //do your logic }
JesseNinjaTrader Customer Service
Comment
-
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:
Code:int week = myCal.GetWeekOfYear(Time[0], myCWR, myFirstDOW ); if(week = 12) { //do your logic }
I tried 3 options:
Code: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.
__________________________________________________ __________________________________
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.
__________________________________________________ __________________________________
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.
JesseNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by foxtrott21, Today, 08:27 AM
|
0 responses
1 view
0 likes
|
Last Post
by foxtrott21
Today, 08:27 AM
|
||
Started by jaypelle3, Today, 08:26 AM
|
0 responses
1 view
0 likes
|
Last Post
by jaypelle3
Today, 08:26 AM
|
||
Started by linkcou, Today, 07:01 AM
|
2 responses
18 views
0 likes
|
Last Post
by bltdavid
Today, 08:25 AM
|
||
Started by jaypelle3, Today, 08:25 AM
|
0 responses
1 view
0 likes
|
Last Post
by jaypelle3
Today, 08:25 AM
|
||
Started by jaypelle3, Today, 08:22 AM
|
0 responses
2 views
0 likes
|
Last Post
by jaypelle3
Today, 08:22 AM
|
Comment