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

Correct/accurate way to specify a Trading Time that includes before and after 12:00am

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

    Correct/accurate way to specify a Trading Time that includes before and after 12:00am

    Hello

    I need to know what would be the precise form to work in the situations where a strategy trades during a specific time range along the whole trading session, covering from the evening of the present calendar day to the next calendar day, i.e. the key point here is we are talking about 2 different calendar days even being in the same session.

    For example, when you need a strategy works only during a delimited time range in the same calendar day, let’s say from 8:00 am to 1:00 pm, you can specify that as:


    if (ToTime(Time[0]) >= 080000 && ToTime(Time[0]) <= 130000)
    {
    // do something
    }


    And in this way, you clearly delimit and enclosure the trading time range without doubts.

    But what about when a strategy has the trading time, for example, from 10:00 pm to 7:00 am? i.e., when the strategy works during the same trading session but that is a time range that covers time of 2 calendar days (from this night to the next morning).

    I tried with different if-statements like the next:


    // This takes trades, but it is like if the strategy doesn’t see a continuous time range
    // and instead of that it sees a kind of a segmented time range.

    if (ToTime(Time[0]) >= 220000 && ToTime(Time[0]) <= 235959) || (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) <= 070000)
    {
    // do something
    }




    // This doesn’t take any trades.

    if (ToTime(Time[0]) >= 220000 && ToTime(Time[0]) <= 235959) && (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) <= 070000)
    {
    // do something
    }


    I also tried with the next one, and it seems to work (I only tried it with backtests), but it works with 2 issues. The first issue is I don’t feel it enclosures a time range because it uses ‘||’ instead of ‘&&’ that is what you would use if you were working during the same calendar day as the first if-statement example at the beginning of this post. The second and most important issue is I tested using the London time in NinjaTrader 8 (changing the time zone into the NT8 platform, restarting NT8,… ) to see what would be the strategy behavior and its performance if you were using a trading time range that is entirely between hours into the same calendar day, and the problem is that with the next if-statement the strategy doesn’t have the exact same performance in the Strategy Analyzer (backtest) in comparison to when using the London time, and with this if-statement the backtest summary shows the strategy takes some extra Total # of trades that of course produces lightly different Total net profit and lightly different other metrics. All this of course testing the strategy with the exact same settings and only converting the time range from US E.T. to London time.


    if (ToTime(Time[0]) >= 220000 || ToTime(Time[0]) <= 070000)
    {
    // do something
    }


    I tried to find other posts that covers this kind of situations, but I couldn’t find anything that talks about this kind of time situations.

    I would like to know what would be the way to work this topic with accuracy in a way that the NT8 platform produces the exact same results as if you were working with a time range in the same calendar day.

    Thank you​

    #2
    I would like to add some extra notes that maybe could be relevant for the answer, that are, I haven't tried with other ways to work the time because I need the time can be Optimizable, and about to use the London time zone, that could be a kind of "patch", the problem is the strategy would be working with other scripts that are already in US time, so it is not feasible to adapt everything into the platform to the London time zone.

    I also thought about to maybe use a custom Trading hours template with the time range, but again, I need the time can be Optimizable, and I think this must be solved simply via strategy code.

    Thank you

    Comment


      #3
      Hello futurenow,

      Code:
      if (ToTime(Time[0]) > 230000 && ToTime(Time[0]) < 10000)
      This would mean the time is after 11:00 PM and before 1:00 AM. This does not look at the calendar day and could be during any session that is trading during that time.

      But what about when a strategy has the trading time, for example, from 10:00 pm to 7:00 am? i.e., when the strategy works during the same trading session but that is a time range that covers time of 2 calendar days (from this night to the next morning).
      What is the issue with this?
      It's any bar on any day where the time is after 10 PM and before 7 AM.

      What do you specifically need?
      Are you trying to find 10 PM and 7 AM on a specific date or day of the week?
      For example if I wanted to know its after 10 PM Monday and before 7 AM Tuesday then I would break this into two conditions.
      Code:
      if ( (Time[0].DayOfWeek == DayOfWeek.Monday && ToTime(Time[0]) > 220000 && ToTime(Time[0]) < 235959) || (Time[0].DayOfWeek == DayOfWeek.Tuesday && ToTime(Time[0]) > 0 && ToTime(Time[0]) < 70000) )
      Below is a link to a forum thread about optimizing the time.
      Hi Struggling a bit with this, I'm trying to optimize on time - so the system buys @ 9:00am or 10:00am etc The strategy works fine with default values but once I
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Below is a link to a forum thread about optimizing the time.
        Yes, thank you for the reference the to the example “OptimizeTimeExample_NT8.zip”. Since a time ago I already use that script when I need to optimize time, specifically Hours and also Minutes.​

        However, actually the problem doesn't come from the time optimization and I will explain it again the problem with more details, in a way where you can read specific code and even test it in your side to better check the situation I describe.


        if (ToTime(Time[0]) > 230000 && ToTime(Time[0]) < 10000)

        This would mean the time is after 11:00 PM and before 1:00 AM. This does not look at the calendar day and could be during any session that is trading during that time.
        This kind of if-statement using '&&' when the time range is between 2 calendar days but in the same trading session, this is exactly what is not working for me (please check the attached strategy) because it makes the strategy doesn't take any trades, i.e. 0 trades as you can see in attached picture below.

        Based on what I've tested, this statement works perfectly when you need a strategy takes trades during a delimited time range but in the same calendar day, let’s say from 8:00 am to 1:00 pm, but when I test this kind of statements for a time range that is between 2 calendar days, then the strategy doesn't take a single trade, and this is the problem I'm talking about since the first post.


        What do you specifically need?
        Are you trying to find 10 PM and 7 AM on a specific date or day of the week?
        For example if I wanted to know its after 10 PM Monday and before 7 AM Tuesday then I would break this into two conditions.
        Code:
        if ( (Time[0].DayOfWeek == DayOfWeek.Monday && ToTime(Time[0]) > 220000 && ToTime(Time[0]) < 235959) || (Time[0].DayOfWeek == DayOfWeek.Tuesday && ToTime(Time[0]) > 0 && ToTime(Time[0]) < 70000) )
        What I specifically need is not about a specific date or day/s of the week, it is easier than that, what I specifically need is just about a strategy works in any trading session of the week (without to specify the day of the week) but that the strategy works during a time range that is before and after the midnight, i.e. including the time before and after a calendar day change takes place, seeing this as a continuous time range.

        I will try to explain it with simpler words. What I specifically need is a strategy works during a specific time range between 2 calendar days in a continuous way, exactly as if the time range would be during a same calendar day, i.e. that the strategy works exactly in the same way and with the exact same backtest performance,

        either if you are using:
        Code:
        if (ToTime(Time[0]) >= 220000 || ToTime(Time[0]) <= 130000)
        {
            ...
        }​
        
        // with NT8 in US E.T. time (UTC-05:00), where in this case the time range is during 2 different
        // days but in the same trading session.

        or either if you are using:
        Code:
        if (ToTime(Time[0]) >= 030000 && ToTime(Time[0]) <= 180000)
        {
           ...
        }​
        
        // with NT8 in London time (UTC+00:00), knowing that here the London time zone is just a time
        // conversion example, only to show an equivalent time range that is during the same calendar
        // day, continuously without day change.


        About:
        if (ToTime(Time[0]) > 230000 && ToTime(Time[0]) < 10000)

        This would mean the time is after 11:00 PM and before 1:00 AM. This does not look at the calendar day and could be during any session that is trading during that time.
        I've attached a strategy example and its Summary backtest performance when working with 2 variations of the if-statement you mention, all that showing that the if-statement you mention is not working in my side. I tested everything in 2 different NinjaTrader 8 installations, in 2 different PCs, to make sure that the strategy doesn't take trades when using an if-statement as the one you show using '&&' (And operator) in the specific scenario where this kind of statement is using a time range of 2 days (the evening/night of one day and the rest of the session during the next calendar day).

        Please check the "SampleMACrossOverWithTimeRangeBeforeAndAfter12am.c s" code where you can try the 2 variations of the statements you mention and where you can see that one variation works and the other variation doesn't work.


        In short, as I explained in the first post, a way to see this problem is that at least in my side working with 2 different NinjaTrader 8 installations, that NinjaTrader 8 is not producing the exact same backtest performance when using a time range that is between 2 days compared to when using the exact same time range BUT converted to a time zone where the same time range is in the same day, and what I need is the way to specify this (the time range) where a strategy have the same performance (in Backtests, in Optimizations, in Live) in this 2 situations, i.e. in E.T. time zone and in London time zone.

        In case you are still not sure about what is the exact problem I'm describing, you just need to run a strategy in your side (as the example I attached here), and test what I'm describing comparing the Strategy Analyzer results where using a time range that is between 2 days, and where using the converted time range with a time zone where the time range be in the same calendar day. In my case I did the tests in the Strategy Analyzer with 1 tick resolution.

        Thank you

        Click image for larger version  Name:	SampleMACrossOverWithTimeRangeBeforeAndAfter12am - NOT Taking trades when using 'AND'.png Views:	0 Size:	114.9 KB ID:	1224842
        Click image for larger version  Name:	SampleMACrossOverWithTimeRangeBeforeAndAfter12am - Taking trades when using 'OR'.png Views:	0 Size:	132.0 KB ID:	1224843






        Last edited by futurenow; 11-23-2022, 11:13 AM.

        Comment


          #5
          Hello futurenow,

          I am still not understanding.

          If its any calendar days, why would it matter what session the bar is in?

          If its specific calendar days, then you can specify the date or the day of the week.

          The time conditions are going to work the same in real-time as backtest. This won't be different.


          Can you provide output from prints, showing the time of the bar, and the values compared in the conditions?



          Though I'm not understanding what you are trying to achieve, you could check if the session opened on the same date as the bar and the time of the bar is after the session open using a SessionIterator.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello futurenow,

            I am still not understanding.

            If its any calendar days, why would it matter what session the bar is in?

            If its specific calendar days, then you can specify the date or the day of the week.

            The time conditions are going to work the same in real-time as backtest. This won't be different.


            Can you provide output from prints, showing the time of the bar, and the values compared in the conditions?
            https://ninjatrader.com/support/foru...121#post791121


            Though I'm not understanding what you are trying to achieve, you could check if the session opened on the same date as the bar and the time of the bar is after the session open using a SessionIterator.

            Hello

            Thank you for your fast response ChelseaB,

            I am still not understanding.
            If you try in your side what I'm describing, then I think you will instantly understand the situation. In the attached strategy code are some guides about what is half-working and what is not working at all.


            The concept of the situation I'm having is easier than the scenarios you describe, this is not about to check any specific calendar day, or specific dates, or specific days of the week. I don't know if the problem is that when you use a statement like 'if (ToTime(Time[0]) >= 220000 && ToTime(Time[0]) <= 130000)' that maybe NT8 doesn't see this time period as a continuous period. What I can notice that could be the problem is when you try to work with a time range where includes time before a day change and after a day change, what I can understand about this is the platform maybe doesn't see in the same way a time period like from 10pm to 11pm that is clearly a continuous time being in the same calendar day, than to specify a time period from 10pm to 3am that in this other case you are talking about a time period that covers 2 days that is before the 12am and after the 12am.

            You just could test the strategy sample I attached in the previous post, and you will see what I'm talking about. Please just try this, because more than about explanations I think this could be the way you see in your screen what I'm describing:

            "In case you are still not sure about what is the exact problem I'm describing, you just need to run a strategy in your side (as the example I attached here), and test what I'm describing comparing the Strategy Analyzer results where using a time range that is between 2 days, and where using the converted time range with a time zone where the time range be in the same calendar day. In my case I did the tests in the Strategy Analyzer with 1 tick resolution."​

            If you don't test the next with the attached strategy sample (or this in any other strategy you want) then it will be more difficult to understand the situation because the results will show the problem:

            What I specifically need is a strategy works during a specific time range between 2 calendar days in a continuous way, exactly as if the time range would be during a same calendar day, i.e. that the strategy works exactly in the same way and with the exact same backtest performance,

            either if you are using:
            Code:
            if (ToTime(Time[0]) >= 220000 || ToTime(Time[0]) <= 130000)
            {
                ...
            }​
            
            // with NT8 in US E.T. time (UTC-05:00), where in this case the time range is during 2 different
            // days but in the same trading session.

            or either if you are using:
            Code:
            if (ToTime(Time[0]) >= 030000 && ToTime(Time[0]) <= 180000)
            {
               ...
            }​
            
            // with NT8 in London time (UTC+00:00), knowing that here the London time zone is just a time
            // conversion example, only to show an equivalent time range that is during the same calendar
            // day, continuously without day change.


            Thank you
            Last edited by futurenow; 11-23-2022, 11:13 AM.

            Comment


              #7
              Please just try in your side with a time period that goes from the evening/night to the next day, without to specify the date or the day of the week (any of this), i.e. an entry logic that work in general for any day of week with to specify any more that the time period.


              For example:
              Code:
              if ( (ToTime(Time[0]) >= 220000 || ToTime(Time[0]) <= 130000) && Close[5] < Close[0] )
                  EnterLong();


              and then test the same converted time period in Europe time zone where the converted time period goes in the same day

              For example:
              Code:
              if ( (ToTime(Time[0]) >= 030000 && ToTime(Time[0]) <= 180000) && Close[5] < Close[0] )
                  ​EnterLong();


              Please note that when working with a time period that is between 2 days here I'm using '||' because if I use '&&' then the strategy doesn't take trades.

              Thank you

              Comment


                #8
                I believe Chelsea was not advocating the use of the non working example with the &&. He made reference to it and the next statement had nothing more to do with it but was referring to your 10:00 PM to 7:00 example.
                eDanny
                NinjaTrader Ecosystem Vendor - Integrity Traders

                Comment


                  #9
                  Hello futurenow,

                  I get it now. You were right I had to test before it dawned on me. The time can't be both after 10 and before 2 at the same time and the OR has to be used.

                  However, I am not seeing any differences in historical than real-time playback with a test script.

                  It still prints the same bars to the output window. The time of the bars does not change in historical. The condition does not change in historical. Its exactly the same.

                  Below is a link to a video of the test.
                  https://drive.google.com/file/d/1tiq...w?usp=drivesdk

                  Attached are the output text files.
                  Playback_realtime - NinjaScript Output 11_23_2022 11_09 AM.txt
                  Historical - NinjaScript Output 11_23_2022 11_10 AM.txt
                  TimeConditionTest_NT8.zip
                  Attached Files
                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by futtrader, 04-21-2024, 01:50 AM
                  5 responses
                  56 views
                  0 likes
                  Last Post NinjaTrader_Eduardo  
                  Started by PeakTry, Today, 10:49 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post PeakTry
                  by PeakTry
                   
                  Started by llanqui, Today, 10:32 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post llanqui
                  by llanqui
                   
                  Started by StockTrader88, 03-06-2021, 08:58 AM
                  45 responses
                  3,992 views
                  3 likes
                  Last Post johntraderuser2  
                  Started by TAJTrades, Today, 09:46 AM
                  0 responses
                  8 views
                  0 likes
                  Last Post TAJTrades  
                  Working...
                  X