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!
See more
See less

Partner 728x90

Collapse

Logic for is CurrentBar last bar of Month?

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

    Logic for is CurrentBar last bar of Month?

    Hello.

    I have a desire to have my chart know if a bar is the last bar of the calendar month. This will be different depending on your PCs clock time of course.

    But for example, to find the weekly Open I've used:
    Code:
    			
    if(Time[0].DayOfWeek.ToString() == "Sunday" && Bars.FirstBarOfSession  ) 
      PlotSomething;

    To find the Weekly Close, I've used:
    Code:
    if(Time[0].DayOfWeek.ToString() == "Friday" && Bars.LastBarOfSession  ) 
      PlotSomething;

    To Find the Monthly Open, I've used:
    Code:
    if(Time[0].DayOfWeek.ToString() == "Sunday" && Bars.FirstBarOfSession && Time[0].Day <= 7)
      PlotSomething;
    All of the above work to find Weekly Opens/Closes and Monthly Open.

    For the Monthly Close, I can't think of the logic to work consistently? Just of note this is for FX data, using a 24/7 template. My timezone is Central Standard Time, or UTC-06:00.

    I was thinking to find the MOntly Close Bar, would be like:
    Code:
    if((Time[0].DayOfWeek.ToString() == "Friday" && Bars.LastBarOfSession && Time[0].Day <= 5)
    	[B]||[/B](Time[0].DayOfWeek.ToString() == "Friday" && Bars.LastBarOfSession && Time[0].Day >=27))
      PlotSomething;
    Where you plot if it's Friday and the day of month greater than 27 but less than the 5th day. As sometimes this bar occurs on the 27th,28th,29th,30th or 31st. Most times it occurs on the 30th or 31st and that is correct. But just those odd times it doesn't always work

    Or is there an easier way to accomplish what I'm trying to do?

    Thanks!
    Last edited by forrestang; 12-11-2015, 11:59 AM.

    #2
    Hello,

    If you want to compare if the Current TIme[0] is the Last date of the month, there is an easy way to do that in a couple of lines.

    First, get the end of the months DateTime:

    Code:
    int year = Time[0].Year;
    int month = Time[0].Month;
    DateTime endOfMonth = new DateTime (year, month,DateTime.DaysInMonth(year, month));
    Next compare the endOfMonth.Date with the Time[0].Date

    Code:
    if(Time[0].Date == endOfMonth.Date)
    {
         Print("End of month: " + Time[0]);
    }
    This should be true for any Time[0] that contains the Date, on intrabar this would be for all the bars on that day which if needed you could limit using Bars.FirstTickOfBar.

    Also keep in mind that the end of month may or may not have a bar depending on the session and instrument so if you see a lack of prints, try more data or a different instrument you know has a bar on the end of the month date.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by forrestang View Post
      Hello.

      I have a desire to have my chart know if a bar is the last bar of the calendar month. This will be different depending on your PCs clock time of course.

      But for example, to find the weekly Open I've used:
      Code:
      			
      if(Time[0].DayOfWeek.ToString() == "Sunday" && Bars.FirstBarOfSession  ) 
        PlotSomething;

      To find the Weekly Close, I've used:
      Code:
      if(Time[0].DayOfWeek.ToString() == "Friday" && Bars.LastBarOfSession  ) 
        PlotSomething;

      To Find the Monthly Open, I've used:
      Code:
      if(Time[0].DayOfWeek.ToString() == "Sunday" && Bars.FirstBarOfSession && Time[0].Day <= 7)
        PlotSomething;
      All of the above work to find Weekly Opens/Closes and Monthly Open.

      For the Monthly Close, I can't think of the logic to work consistently? Just of note this is for FX data, using a 24/7 template. My timezone is Central Standard Time, or UTC-06:00.

      I was thinking to find the MOntly Close Bar, would be like:
      Code:
      if((Time[0].DayOfWeek.ToString() == "Friday" && Bars.LastBarOfSession && Time[0].Day <= 5)
      	[B]||[/B](Time[0].DayOfWeek.ToString() == "Friday" && Bars.LastBarOfSession && Time[0].Day >=27))
        PlotSomething;
      Where you plot if it's Friday and the day of month greater than 27 but less than the 5th day. As sometimes this bar occurs on the 27th,28th,29th,30th or 31st. Most times it occurs on the 30th or 31st and that is correct. But just those odd times it doesn't always work

      Or is there an easier way to accomplish what I'm trying to do?

      Thanks!
      It may make more sense to find if the CurrentBar is the first day of the month, and just take the previous bar to be the last day of the previous month. I am assuming that you are using Daily bars of course. Is there any reason why you must know that the CurrentBar is the last day of the month. Does your code break if you use Input[1]/Time[1] for your check and action, rather than Input[0]/Time[0]?

      Comment


        #4
        Thanks for the help Jesse. Your solution does find the end of the month. One issue though is that the way i was capturing the beginning of the month, was whatever Sunday was the first to occur after a new month. In my timezone, this is the day the FX market opens.

        @koganam, thanks, I was going to do this if I coudln't figure out a simple way of doing it. It wouldn't be breaking anything by doing it that way, but it was just more coninenent for the way I have my script setup to calculate all the things at the end of my update block instead of at the beginning. Its just a bunch of calcs on some info I"m storing in arrays.

        Comment


          #5
          Originally posted by forrestang View Post
          Thanks for the help Jesse. Your solution does find the end of the month. One issue though is that the way i was capturing the beginning of the month, was whatever Sunday was the first to occur after a new month. In my timezone, this is the day the FX market opens.

          @koganam, thanks, I was going to do this if I coudln't figure out a simple way of doing it. It wouldn't be breaking anything by doing it that way, but it was just more coninenent for the way I have my script setup to calculate all the things at the end of my update block instead of at the beginning. Its just a bunch of calcs on some info I"m storing in arrays.
          In which case:
          Code:
          if (Time[0].Month != Time[1].Month)
          {
          //previous bar was last day of the previous month
          }

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by gravdigaz6, Today, 11:40 PM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by MarianApalaghiei, Today, 10:49 PM
          3 responses
          10 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by XXtrader, Today, 11:30 PM
          0 responses
          4 views
          0 likes
          Last Post XXtrader  
          Started by love2code2trade, Yesterday, 01:45 PM
          4 responses
          28 views
          0 likes
          Last Post love2code2trade  
          Started by funk10101, Today, 09:43 PM
          0 responses
          9 views
          0 likes
          Last Post funk10101  
          Working...
          X