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

Getting DateTime data from additional data series

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

    Getting DateTime data from additional data series

    Hello,

    I would like to draw a price line at the start of the month, regardless of chart timeframe. To do this, I am using a method that is probably more complicated than it needs to be. I am checking if the first day of the month is Sunday or not Sunday, being that futures opens on Sunday. I have to do this because if the first or second day of the month is a Sunday, than I use Sunday open as month open. However, if the first day of the month is any weekday, it uses the day prior.

    This works well, unless there is a holiday, like for example Jan 1st, where the market was closed on Sunday Dec 31 and Monday Jan 1. The script than sees that the first day was Jan 1 on a Monday, but tries to use Sundays open, which the market was closed. This throws off the entire script as I use this price/time as a base value.

    Is there a way to just add an additional monthly data series, than pull the start DateTime from that additional data series bar? I didn't see any way to do this when looking through the library. Perhaps GetTime or TimeSeries, but this was unclear how it would work with an additional data series.

    Thanks in advance,
    Chris

    #2
    Hello ChrisR,

    Thank you for your post.

    You can add an additional data series via AddDataSeries(), however drawing objects can only be drawn on the primary data series.

    If you would just like to know if the date is first bar of the first trading day of the month, you can compare if Time[0].Month is not equal to Time[1].Month. If this is not true (i.e. the previous bar has a different month), then the bar is the first bar of a new month.

    Please let us know if you have any other questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      Hello ChrisR,

      Thank you for your post.

      You can add an additional data series via AddDataSeries(), however drawing objects can only be drawn on the primary data series.

      If you would just like to know if the date is first bar of the first trading day of the month, you can compare if Time[0].Month is not equal to Time[1].Month. If this is not true (i.e. the previous bar has a different month), then the bar is the first bar of a new month.

      Please let us know if you have any other questions.
      Code:
      Time[0].Month is not equal to Time[1].Month
      On equities this would work, however on futures, the prior day is the start of the session (6pm ET). So if its say Tuesday March 1st, than the monthly bar starts at Monday February 29th at 6pm ET. I'm trying to figure out an accurate way to grab this monthly bar start time and price that would account for holidays.

      Additionally, comparing Time[0].Month to Time[1].Month uses the bar end time, I need the bar start times.
      Last edited by ChrisR; 01-04-2024, 10:38 AM.

      Comment


        #4
        Hello,

        Please see the attached example script which demonstrates getting the first bar of first trading session of a new month. Is this what you are looking for?

        Unfortunately, bar times are marked by the end of the bar in NinjaTrader. The open time of the bar is not recorded.

        Additionally, please also note that trading hours templates and session iterators account for holidays, so checking the previous bar or session bar won't have bars on holidays.​

        If you need further assistance please let us know.
        Attached Files
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Gaby View Post
          Hello,

          Please see the attached example script which demonstrates getting the first bar of first trading session of a new month. Is this what you are looking for?

          Unfortunately, bar times are marked by the end of the bar in NinjaTrader. The open time of the bar is not recorded.

          Additionally, please also note that trading hours templates and session iterators account for holidays, so checking the previous bar or session bar won't have bars on holidays.​

          If you need further assistance please let us know.
          When applying the indicator you shared to a 5min chart on NQ 03-24, this indicator you shared returns the following:
          Code:
          11/5/2023 6:05:00 PM | First bar of first trading session of new month
          12/3/2023 6:05:00 PM | First bar of first trading session of new month​
          Both of the above DateTime references are wrong. For the November monthly bar, this bar would of started on October 31st at 6:05pm ET. For the December bar, this bar would of started on November 30th at 6:05pm ET.

          The opening of a new monthly futures bar is always 6pm on the last day of the prior month, unless the new month starts on Saturday or Sunday, than Sunday 6pm is used as the opening month. Holidays would affect this also. Using Bars.IsFirstBarOfSession would grab the first bar of the session, but I need to make sure that bar I am grabbing is the proper bar (starting month bar).

          Additionally, this is not showing the first bar of January for some reason.
          Last edited by ChrisR; 01-04-2024, 02:17 PM.

          Comment


            #6
            Hello,

            When I apply this to my own 5 minute NQ chart, I see the following:


            11/1/2023 5:05:00 PM | First bar of first trading session of new month

            12/3/2023 5:05:00 PM | First bar of first trading session of new month


            As mentioned, bars are marked by the end of the bar in NinjaTrader. The open time of the bar is not recorded. So the 11/1/2023 bar is stamped correctly, this is the session opening bar of the new month however it is timestamped with the close time.


            Please see the section of the Help Guide link below 'Understanding variables involved in building chart bars' below.

            How Bars Are Built - https://ninjatrader.com/support/help..._are_built.htm


            The December bar would not have started on November 30th. Thursday November 30th the session opened at 5PM CST, then closed at 4PM CST December 1st which was a Friday. The market is closed Saturday, it reopened 5PM Sunday December 3rd.

            For the January bar, you will have to reset the month after December because January won't be greater than December. You could use an 'or' statement as part of the condition, and check if the Time[0].Year is greater than the Time[1].Year for the January bar.

            Please let us know if we can assist further.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Gaby View Post
              Hello,

              The December bar would not have started on November 30th. Thursday November 30th the session opened at 5PM CST, then closed at 4PM CST December 1st which was a Friday. The market is closed Saturday, it reopened 5PM Sunday December 3rd.
              Gaby,

              I think you may be confused at what it is I am trying to do, apologies if I haven't conveyed it properly.

              Being that the new month (December) started on a Friday (December 1st), the actual futures session started Thursday, November 30th at 5PM CST, as you stated. Thus the Monthly opening bar for December has a start time of November 30th at 5pm CST. You can verify this by comparing the monthly bar open price to the open price of the November 30th 5:05PM CST bar, they will both be 15955 when viewing NQ 03-24.

              So your print of:
              12/3/2023 5:05:00 PM | First bar of first trading session of new month

              ....would be incorrect as 12/3/2023 5:05:00 PM is NOT the first bar of the month, 11/30/2023 5:05:00 PM would be the correct bar as the starting bar for December as that bar starts the session which rolls over to December 1st. This is the beginning of the monthly bar.

              Using your method does not account for the fact that futures sessions overlap two physical date values. Your method does not capture the opening price/time of a monthly bar because it is not factoring in that the session started the day prior once the month is logged as updated. You cant compare Time[0].Month to Time[1].Month and also use IsFirstBarOfSession because the first bar of the session will not be the monthly start bar if at the same moment Time[0].Month > Time[1].Month is true. Instead, this would use the DateTime one day late, which is what your recommendation is doing.

              I am seeking to capture the opening DateTime of the monthly bar, regardless of what timeframe the chart is set to. The method posted above does not solve this.
              Last edited by ChrisR; 01-04-2024, 02:57 PM.

              Comment


                #8
                Does anyone have any recommendation to capture the monthly open/time of a futures assets, specifically index futures that open 5pm CST? The beginning session for the new month often starts on the last day of the previous month, where that new month bar will begin once that 5pm CST session kicks off. Example of this would be December 2023 where the December monthly bar starts at November 30th @ 5:00pm CST.

                What I am trying to do is use the following logic;
                If Time[0].Month > Time[1].Month than grab the current session open time.

                However, the problem here is that if Time[0].Month > Time[1].Month is true than IsFirstBarOfSession can not be true if the month starts on a weekday because the month condition would be true at midnight, however the session started 5pm CST, 6hrs prior.

                The above would only work if the new month starts over the weekend, because once the new session starts at 5pm CST, both IsFirtBarOfSession and Time[0].Month > Time[1].Month would be true at the same time.


                Example One (New Month Begins Over Weekend):
                Code:
                Bar[1] = Friday, November 29 @ 4pm CST/5pm ET (session end, month ends)
                Bar[0] = Sunday, December 1 @ 5pm CST/6pm, ET (new session, new month)
                In example one, Time[0].Month > Time[1].Month && IsFirstBarOfSession would both be true.


                Example Two (New Month Begins During Weekday):
                Code:
                Bar[1] = Monday, November 30 @ 4pm CST/5pm ET (session end, month ends)
                Bar[0] = Monday, November 30 @ 5pm CST/6pm, ET (new session, December monthly bar starts)
                In example two, Time[0].Month > Time[1].Month && IsFirstBarOfSession can not be true at the same time.


                This is the issue I am trying to find a solution for, for when the month begins during a weekday.​
                Last edited by ChrisR; 01-05-2024, 11:07 AM.

                Comment


                  #9
                  Hello ChrisR,

                  I just want to simplify.

                  You are trying to find the first bar of the last session of the month and not the first session of the month, is this correct?

                  The example provided does account for the session overlapping multiple dates. This is why it is drawing a dot at 5:00 PM when the session starts and not at midnight when the session from the previous day overlaps.


                  If you want to add a 1 month bar with AddDataSeries() and code your own custom logic you are free to do so. I would recommend you open a 1 month chart to ensure the bar close of a 1 month bar is what you are expecting.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hello ChrisR,

                    I just want to simplify.

                    You are trying to find the first bar of the last session of the month and not the first session of the month, is this correct?

                    The example provided does account for the session overlapping multiple dates. This is why it is drawing a dot at 5:00 PM when the session starts and not at midnight when the session from the previous day overlaps.


                    If you want to add a 1 month bar with AddDataSeries() and code your own custom logic you are free to do so. I would recommend you open a 1 month chart to ensure the bar close of a 1 month bar is what you are expecting.
                    Chelsea,

                    I am simply trying to create a horizontal line starting at the open price/time of a new month, regardless of what timeframe the chart is set on, and have that line end when the month ends, the close time of the last session of that month. The anchor points is what I'm having issues with.

                    In the example provided:
                    Code:
                    12/3/2023 5:05:00 PM | First bar of first trading session of new month
                    ^^This is incorrect. The December monthly bar started on Thursday, November 30th at 5PM CST / 6pm ET when the new session started.

                    Futures sessions open and run through midnight into the next day. November 30th, 2023 @ 6pm ET started the new session which ran into December 1st. That session starting thus started the December monthly bar.

                    Just like this month, Wednesday January 31st at 5pm ET will end the January monthly bar. At 6pm that same day, the February bar starts because 01/31/2024 6:00:00pm ET --> 02/01/2024 5:00:00pm ET will be the first trading session of the February monthly bar. I would like to capture this start time and price.

                    Time[0].Month > Time[1].Month && IsFirstBarOfSession
                    ^^this logic will not work if the month changeover is a weekday because those two conditions will never be true at the same time on a weekday, only if the month change happens over the weekend will they be true at the same time because there is no trading overlap between new and old month.
                    Last edited by ChrisR; 01-05-2024, 05:52 PM.

                    Comment


                      #11
                      Does anyone from the NT community have a recommendation here?

                      Comment


                        #12
                        Hello,

                        12/3/2023 5:05:00 PM | First bar of first trading session of new month

                        Thursday November 30th the session opened at 5PM CST, this is the last session of November. Sunday December 3rd is the first session of December.

                        The script goes by the start DateTime of the session, not the end DateTime.

                        If you want to find the first bar of the last session of the month, you could tweak the logic of the code to instead return Time[1] once it has found the first bar of session of the new month.
                        Gaby V.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Gaby View Post

                          12/3/2023 5:05:00 PM | First bar of first trading session of new month
                          Thursday November 30th the session opened at 5PM CST, this is the last session of November. Sunday December 3rd is the first session of December.

                          Gaby, this is incorrect, as stated before. Lets compare opening price points please. I will be using NQ 03-24.

                          Code:
                          5min Bar - Thursday, November 30th at 5:05pm CST (New Session)
                          = 15955
                          
                          5min Bar - Sunday, December 3rd at 5:05pm CST (New Session)
                          = 16022
                          Now lets look at the December monthly bars opening price:
                          Code:
                          December Open = 15955
                          As you can see, November 30th at 5:05pm CST is the beginning of the December bar. This session ran into December, thus it is the first December session and starts the December bar.


                          Lets look at another one, October to November...
                          Code:
                          5min Bar - Tuesday, October 31st at 5:05pm CST (New Session)
                          = 14460 (Starts the November monthly bar)
                          Now lets look at the November monthly bars opening price:
                          Code:
                          November Open = 14460
                          As you can see, if the month starts on a weekend (Saturday or Sunday) than the first session of the month is Sunday because thats the first session with the new month, unless there is a holiday closure, like we had at the beginning of this month (Jan).

                          However, if the new month starts on a weekday, than the monthly bar start is that current session in play at midnight date changeover, which will always be the last day of the prior month when that new session started.

                          This is why Time[0].Month > Time[1].Month will not work...because that condition and IsFirstBarOfSession cant be true at the same time when the month changes on a weekday. Those two conditions will only be true at the same time if the month change happens on the weekend.
                          Last edited by ChrisR; 01-08-2024, 09:51 AM.

                          Comment


                            #14
                            Hello,

                            In my own testing, printing the open price of the new bar on a 5 minute chart:

                            12/3/2023 5:05:00 PM | First bar of first trading session of new month

                            Open[0]: 16231.75 of 12

                            On a monthly chart:

                            Open[0]: 16164.75 of 12


                            I am not sure how you are setting up the print statement, but that may account for why you are seeing a discrepancy. It is correct that the November 30th session opened at 5PM CST making it the last session of November, even though the session technically ended in December. Sunday December 3rd is the first session of December.
                            Gaby V.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Gaby View Post
                              Hello,

                              In my own testing, printing the open price of the new bar on a 5 minute chart:

                              12/3/2023 5:05:00 PM | First bar of first trading session of new month

                              Open[0]: 16231.75 of 12

                              On a monthly chart:

                              Open[0]: 16164.75 of 12


                              I am not sure how you are setting up the print statement, but that may account for why you are seeing a discrepancy. It is correct that the November 30th session opened at 5PM CST making it the last session of November, even though the session technically ended in December. Sunday December 3rd is the first session of December.
                              Gaby,

                              Im not using print statements, Im simply looking at the bar data on the chart itself. I would recommend you re-look at my last post and compare it to the chart, and not use any code/print statements so you can see the raw data and compare as I did, this ruling out any code issues.

                              If you can do me a favor, look at the following on your chart, do not use code/print, just look at the data box.

                              5m chart:
                              1) Novermber 30th, 5:05pm CST bar open price
                              2) December 3rd, 5:05pm CST bar open price

                              1 month chart:
                              3) December monthly bar open price

                              …and can you post those three data points here? You will see that 1) matches 3). This means November 30th @ 5:05pm CST is the first session of December monthly bar.
                              Last edited by ChrisR; 01-08-2024, 01:08 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,406 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              98 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              160 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X