Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Looking to calculate CashHours of Futures contract

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

    Looking to calculate CashHours of Futures contract

    NT Forum,

    With the addition of TradingHours (or SessionIterator) in NT8 I'm looking to programmatically calculate whether a futures contract is trading within CashHours. Where "CashHours" is defined as occurring within the Regular Trading Hours (RTH) of the "instrument" the futures contract derives from. (applying the RTH of the derived from "instrument" to the futures contract is not a suitable solution)

    For example, where the futures contract is ES, the "instrument" derived from is ^SP500. Thus CashHours would occur within the ^SP500 RTH.

    Requirements:
    - Return boolean isCashHours
    - Chart is in local time (i.e. not the timezone of the ES or ^SP500)
    - Function appropriately with Historical and Live data (which may be an issue as found by @NJA_MC in Bars.TradingHours.Sessions[0].Begin/EndTime Documentation

    Given the work of @GrumpyTrader AddDataSeries Question, I imagine others have partial solutions to the problem.

    Short of an existing solution, I would expect the code would require:
    - Add the appropriate DataSeries
    - Based on BarsInProgress[1] determine CashHours (possibly referencing IsInSession() noting the performance hit noted in SessionIterator)

    Any guidance would be appreciated.

    Regards
    Shannon

    #2
    There are multiple ways to handle this task. I've attached a sample indicator that demonstrates one way to achieve this for you.

    We use BarsArray[1].SessionIterator.GetNextSession() method for each new IsFirstBarsOfSession on that series to recalculate the actual trading hour times and save your start/end times in a private variable. This should conserve PC performance rather than checking IsInSession() on each bar.

    The attached indicator should change the chart's background color to a dark green when isCashHours= true

    Let me know if you have any questions/issues
    Attached Files
    Last edited by NinjaTrader_Matthew; 07-13-2015, 09:11 AM.
    MatthewNinjaTrader Product Management

    Comment


      #3
      NinjaTrader_Matthew,

      Thank you for concise and clear solution.

      Shannon

      Comment


        #4
        NinjaTrader_Matthew,

        Following up on the SampleGetCashHours indicator, I changed your code slightly. Specifically, changing the code to determine if the current time is within cash hours from:
        Code:
        // check if the currentbar time is within cash hours
        if(Time[0] >[B][COLOR="Red"]=[/COLOR][/B] startTime && Time[0] <= endTime)
        to:
        Code:
        if(Time[0] > startTime && Time[0] <= endTime)
        This extends from NinjaTrader stamping a bar with the closing time of the bar. Thus, the bar equal to the start time would refer to the time leading up to the start time. This change appears to be confirmed with the attached screenshot.

        The attached screenshot shows the ES 09-15 (30Min) and ^SP500 (30Min). The SampleGetCashHours works as expected throughout history (until 11 July) as can be seen by the PaleGreen BackBrush coinciding with the first ^SP500 bar.

        However, an issue appears to occur in recent history. In recent history (11 July onwards) the PaleGreen BackBrush begins one bar before ^SP500 (highlighted with yellow arrows). The 11 July date is not special. The issue appears to consistently affect the most recent bars (i.e. with more data and a fresh download of historical data, the issue on 11 July would resolve)

        Any ideas would be appreciated.

        Regards
        Shannon
        Attached Files

        Comment


          #5
          I cannot reproduce, but can you clarify this comment:

          Originally posted by Shansen View Post
          The 11 July date is not special. The issue appears to consistently affect the most recent bars (i.e. with more data and a fresh download of historical data, the issue on 11 July would resolve)
          Are you saying that this happens in real-time, and just by reloading the data the issue corrects itself? Are you sure redownloading the data is correcting the issue? Can you resolve this just by pressing F5 to reload the NinjaScript?

          It might be that I had that hard coded to Calculate.OnBarClose which would not paint that first bar in real-time. I've attached another version which contains a few bug fixes:

          - your > time check
          - a BarsInProgress check I previously omitted,
          - defaulted to Calculate.OnPriceChange.

          Please test again with this version and let me know if you see issues.
          Attached Files
          MatthewNinjaTrader Product Management

          Comment


            #6
            Mathew,

            Thanks for the quick reply and revised indicator.

            Please find attached three screenshots which I hope demonstrates the issue:
            1. Before connecting to IB (Interactive Brokers)
            2. After connecting to IB
            3. After manual ES 09-15 historical data load
            In 1, you will see the indicator works as expected for 10 July (circled green) but not for 11 July onwards (circled red).
            In 2, you will see the indicator works for 11 July (circled green) by not for the next trading day, 14 July, and onwards (circled red).
            The issue appears to consistently affect the four most recent bars.

            I have not had the opportunity to check the behaviour of this indicator real-time.

            Regarding your changes, if my understanding is correct, Calculation.OnBarClose will only present a problem for the first bar after startTime where the frequency the first DataSeries (BarsArray[0]) is less than or equal to the added DataSeries (BarsArray[1]). The issue would present real-time or historical. As the attached screenshots have BarsArray[0] frequency of 30 minutes no problem should present.

            As an unrelated matter, in 2, there is the missing data block (circled orange). In 3, this data block is filled (circled orange) with a manual historical load.

            Again, thanks for your continued efforts.

            Regards
            Shannon
            Attached Files

            Comment


              #7
              I see. I can't be certain since I'm not able to reproduce on my workstation - but this may be an artifact of the time/bar scale compression. I can definitely see how that is misleading if that's the case and will look into that.

              Just to make sure this is a render issue and not and issue with the chart indicator logic / session iterator, can you please decompress the chart to zoom into that first bar with issues as much as possible and take a screenshot so I can look at that further? I attached an example of the resolution level I'm looking for.

              Click image for larger version

Name:	2015-07-17_1553.png
Views:	1
Size:	76.4 KB
ID:	874049

              Thanks!
              MatthewNinjaTrader Product Management

              Comment


                #8
                Mathew,

                Please find attached the requested screenshot.

                Adding the below code yielded some unexpected results.
                Code:
                if (isCashHours)
                  Print(Time[0].ToString("g") + " isCashHours");
                The output window shows 30min data from the first bar (as expected). However, at 14/07/2015 1:11 AM the data appears to change to 1min data (below) which persists to the last bar.
                Code:
                11/07/2015	5:30	AM
                11/07/2015	6:00	AM
                14/07/2015	12:00 AM
                14/07/2015	12:30 AM
                14/07/2015	1:00	AM
                [B]14/07/2015	1:11	AM[/B]
                14/07/2015	1:12	AM
                14/07/2015	1:13	AM
                14/07/2015	1:14	AM
                The timestamp of final bar of the 14/07/2015 session, 15/07/2015 6:00 AM, occurs twice (below).
                The subsequent trading day appears to start at 11:31 PM (below) which is in contrast to the start of prior trading days 12:00 AM (above).
                Code:
                15/07/2015	5:58	AM
                15/07/2015	5:59	AM
                [B]15/07/2015	6:00	AM[/B]
                [B]15/07/2015	6:00	AM[/B]
                [B]15/07/2015	11:31 PM[/B]
                15/07/2015	11:32 PM
                15/07/2015	11:33 PM
                15/07/2015	11:34 PM
                Thanks again for your assistance.
                Regards
                Shannon
                Attached Files

                Comment


                  #9
                  Hello,

                  I added the print to the existing example but I am unable to see this on my end.

                  Was the only change adding the print into the condition? if so can you provide the script you are testing with that is displaying the issue?

                  Also are you running the script as it defaults to or have you changed any of the indicator properties like CalculateOnBarClose? If so can you provide details on anything else that may be different between our two tests?

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

                  Comment


                    #10
                    Jesse,

                    Thank you for the contact.

                    Apologies. I have reloaded the SampleGetCashHours as provided by Mathew (07-18-2015 07:55 AM). All issues have resolved.

                    Again apologies for for the hassle.

                    Regards
                    Shannon

                    Comment


                      #11
                      Hello,

                      Thank you for the update,

                      Not a problem, good to see this is resolved and can be closed.

                      Happy Trading!
                      JesseNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by arvidvanstaey, Today, 02:19 PM
                      4 responses
                      10 views
                      0 likes
                      Last Post arvidvanstaey  
                      Started by samish18, 04-17-2024, 08:57 AM
                      16 responses
                      56 views
                      0 likes
                      Last Post samish18  
                      Started by jordanq2, Today, 03:10 PM
                      2 responses
                      8 views
                      0 likes
                      Last Post jordanq2  
                      Started by traderqz, Today, 12:06 AM
                      10 responses
                      18 views
                      0 likes
                      Last Post traderqz  
                      Started by algospoke, 04-17-2024, 06:40 PM
                      5 responses
                      47 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X