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

New day and day of the week detection

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

    New day and day of the week detection

    Hi,

    Can somebody recommend an elegant way to detect if we are at new day and if it is not monday ?

    I am writing code for strategy and it is running on 15-min chart.

    Thanks

    #2
    Originally posted by ssg10 View Post
    Hi,

    Can somebody recommend an elegant way to detect if we are at new day and if it is not monday ?

    I am writing code for strategy and it is running on 15-min chart.

    Thanks
    if (Time[1].Date != Time[0].Date && Time[0].DayOfWeek != DayOfWeek.Monday) ...

    Comment


      #3
      Originally posted by koganam View Post
      if (Time[1].Date != Time[0].Date && Time[0].DayOfWeek != DayOfWeek.Monday) ...
      You should first convert the local time to exchange time, before you apply that logic.

      Comment


        #4
        How do you convert to server time? Any API available?

        Comment


          #5
          Hello ssg10,

          Thank you for your post.

          koganam's logic would work here. Comparing the current bar's timestamp (Time[0]) against the previous (Time[1]) and using the Day allows us to see if they are the same. Then we can check the DayOfWeek and confirm the day.

          What Harry is pointing out is that the exchange may use a different time then your PC and the bar's timestamp used for the calculations will be the PC's time zone and not the exchange time zone.

          Please let me know if you have any questions.

          Comment


            #6
            Originally posted by ssg10 View Post
            How do you convert to server time? Any API available?
            You would need to know your PC's time zone versus your own and make the difference in your calculation.

            Let's say the instrument I am trading is on the NYSE and I am in California and my PC is set to Pacific Time, and the instrument's session begins at midnight. However, midnight would be Eastern and I am 3 hours behind, so Time[0].DayOfWeek for me is going to equal the day before the session began. That means my DayOfWeek could report Sunday but the new session is actually Monday.

            Comment


              #7
              Thanks. That makes sense. I am at pacific time as well. I would assume I will need to add 3 hours in some intelligent way. How to add this 3 hours into the Time[..] collection, so that the API Time[0].Date can report the right answer based on exchange time?

              The robust way is perhaps:
              Find PC time and exchange time difference
              Adjust the time and save it to a variable
              Feed the time into some kinds of internal variable for the Time[0].Date and .DayofWeek to report the right data for exchange time.

              The only problem i can see here is if I am running a 24/7 time, instead of US market RTH only. Is that right?

              Comment


                #8
                It is never a good idea to use a session template 24/7. This would be always false, as it inserts session breaks in the wrong places.

                For example, if you write a strategy for ES, then you should make sure that your trading day matches the contractual definition of the trading day for ES. The contractual trading times are 5:00 PM Central Time to 4:15 PM Central Time. In that sense a new trading day starts at 5:00 PM CT.

                Your PC uses Pacific Time, which is comparatively easy, as the are no different daylight saving times. However, many traders who trade ES are located in Europe or Asia where the daylight saving schedules are different or non existant. Time conversion therefore needs to be handled via the .NET framework, and not by adding or subtracting a few hours.

                In case that you have selected the proper template for an instrument (always use a template in exchange time), NinjaTrader can detect the trading day automatically.

                Code:
                DayOfWeek  currentTradingDay  = Bars.GetTradingDayFromLocal(time).DayOfWeek;
                gets you the current trading day. To detect the first bar of a trading day, all you need to do is to store the prior value in a variable and check whether the current value is different from the prior value.

                Comment


                  #9
                  Originally posted by Harry View Post
                  It is never a good idea to use a session template 24/7. This would be always false, as it inserts session breaks in the wrong places.

                  For example, if you write a strategy for ES, then you should make sure that your trading day matches the contractual definition of the trading day for ES. The contractual trading times are 5:00 PM Central Time to 4:15 PM Central Time. In that sense a new trading day starts at 5:00 PM CT.

                  Your PC uses Pacific Time, which is comparatively easy, as the are no different daylight saving times. However, many traders who trade ES are located in Europe or Asia where the daylight saving schedules are different or non existant. Time conversion therefore needs to be handled via the .NET framework, and not by adding or subtracting a few hours.

                  In case that you have selected the proper template for an instrument (always use a template in exchange time), NinjaTrader can detect the trading day automatically.

                  Code:
                  DayOfWeek  currentTradingDay  = Bars.GetTradingDayFromLocal(time).DayOfWeek;
                  gets you the current trading day. To detect the first bar of a trading day, all you need to do is to store the prior value in a variable and check whether the current value is different from the prior value.
                  So, to make kosher what I first gave you, you can do this.
                  Code:
                  int currentDay = Bars.GetTradingDayFromLocal(Time[0]).Day;
                  int previousDay = Bars.GetTradingDayFromLocal(Time[1]).Day;
                  if (currentDay != previousDay && Bars.GetTradingDayFromLocal(Time[0]).DayOfWeek != DayOfWeek.Monday) ...

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by ms52390, 06-20-2024, 03:48 PM
                  2 responses
                  14 views
                  0 likes
                  Last Post ms52390
                  by ms52390
                   
                  Started by ccrone2024, Today, 05:13 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post ccrone2024  
                  Started by ttodua, Today, 03:40 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post ttodua
                  by ttodua
                   
                  Started by ttodua, Today, 03:18 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post ttodua
                  by ttodua
                   
                  Started by TraderKnG, Today, 01:30 AM
                  0 responses
                  13 views
                  0 likes
                  Last Post TraderKnG  
                  Working...
                  X