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.
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
MonthOfYear and WeekOfMonth
Collapse
X
-
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.Tags: None
-
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.
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
-
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; }
Comment
-
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; }
Kate W.NinjaTrader Customer Service
- Likes 1
Comment
-
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
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Is it possible to have a drawing, such as a line, cycle through different colors?
by gentlebenthebear
Started by gentlebenthebear, Today, 01:30 AM
|
1 response
8 views
0 likes
|
Last Post Today, 07:30 AM | ||
Started by Aviram Y, Today, 05:29 AM
|
1 response
6 views
0 likes
|
Last Post Today, 07:28 AM | ||
Started by cls71, Today, 04:45 AM
|
1 response
7 views
0 likes
|
Last Post Today, 07:25 AM | ||
Started by TradeForge, Today, 02:09 AM
|
1 response
22 views
0 likes
|
Last Post Today, 07:24 AM | ||
Started by elirion, Today, 01:36 AM
|
2 responses
14 views
0 likes
|
Last Post
by elirion
Today, 07:21 AM
|
Comment