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

Retrieving data from specific bars from the previous day

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

    Retrieving data from specific bars from the previous day

    I am trying to retrieve data from the FIRST bar and LAST bar from the previous day.

    The data needed: RSI(BarsArray[3],14, 1)

    How could I retrieve this data that could be required at any point during the current live trading day? What construct would I use to count back the correct number of bars?

    Thanks in advance.
    Last edited by ArmKnuckle; 03-02-2020, 05:24 AM.

    #2
    Hello ArmKnuckle,

    Thanks for your post.

    You can use the Bars.IsFirstBarOfSession and Bars.IsLastBarOfSession as triggers to store the previous days values at those points into variables that you can pull when needed.



    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response.

      I am fuzzy on how to use this for a previous session/day. Don't Bars.IsFirstBarOfSession and Bars.IsLastBarOfSession refer to the if the current bar is the first/last bar of the current session only?


      Relevant data:

      Indicator needed:
      RSI(BarsArray[3],14, 1)

      Time frame:
      BarsArray[3] = 3 minutes

      Calculation:
      Calculate = Calculate.OnEachTick



      For example:

      At any point during the current trading session/day, how do I retrieve the specific RSI(BarsArray[3],14, 1) reading for the first and last bar from the session/day before?

      Thanks again.
      Last edited by ArmKnuckle; 03-03-2020, 10:55 PM.

      Comment


        #4
        Hello ArmKnuckle,

        Thanks for your reply.

        When you load a script it will process the bars sequentially from the beginning, starting at Current bar 0 all the way through to the current live bar. As each session is processed you have the opportunity to store that sessions first/last bar data of interest into variables that you can pull as needed.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Understood.

          The bars I am looking to retrieve data from have a fixed bar number. The current bar that triggers the ENTRY signal is variable, as it can be any bar during the current live trading day.

          Could NinjaTrader Support specifically tell me how count back to a specific bar numbers that occur on a previous day from any bar during the live trading day using the RSI sample above?

          Thanks.

          Comment


            #6
            Hello ArmKnuckle,

            Thanks for your reply.

            Sure, historically when the bool Bars.IsFirstBarOfSession is true save the CurrentBar into an int variable. When the bool Bars.IsLastbarOfSession is true, save the CurrentBar into another int type variable.

            Then whenever you want to retrieve the values from your indicators you would use for example RSI(14, 3)[CurrentBar-nameofVariable you saved first or last bar of session in] This provides the bars ago to look back at the RSI value.

            The only thing you will have to figure out is how to not save the bar number of the first bar of the current session.

            Another alternative, would be to use GetBar(); https://ninjatrader.com/support/help...t8/?getbar.htm For this you would need to specify the date/time of the bar to retrieve, see the example in the help guide.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Great! Thank you!

              Comment


                #8
                There has been a tweak to my original question from March.

                I am now trying to get specific indicator data from:
                - the CLOSE one day ago,
                and
                - the CLOSE from two days ago.

                Here are the items I need clarification on:
                1. The initial plan was to note when the last historical Bars.IsLastbarOfSession occurrence was and assign the "CurrentBar" to an int variable. I now need to store the last two occurrences of Bars.IsLastbarOfSession. How can I accomplish this? I am not sure how to setup int variables for the last two items that are true. Please be specific.
                2. I am working in a “Multi-Time Frame” strategy, as well. I will need to store the "CurrentBar" information for 4 different time frames. I am unclear how to make sure I am utilizing the correct data series when saving the int variables.
                When calculating in OnBarUpdate, do I utilize:
                if (BarsInProgress == 0)
                if (BarsInProgress == 1)
                if (BarsInProgress == 2)
                if (BarsInProgress == 3)
                around each block of code that is storing the last two occurrences of Bars.IsLastbarOfSession "CurrentBar" data?


                I am trying to make sure I am using the correct [CurrentBar-nameofVariable) BarsArray when extracting the needed indicator data for each specific time frame.

                Your help in solving this will be a HUGE help to me. Thanks in advance!

                P.S. GetBar() seems interesting, but I am very unclear how to specifically call out the date of the previous two days to extract the "CurrentBar" information. Weekends, holidays, and changing of the calendar month/year make it confusing.
                Last edited by ArmKnuckle; 10-18-2020, 11:50 PM.

                Comment


                  #9
                  Hello ArmKnuckle,

                  Thanks for your post.

                  "The initial plan was to note when the last historical Bars.IsLastbarOfSession occurrence was and assign the "CurrentBar" to an int variable. I now need to store the last two occurrences of Bars.IsLastbarOfSession. How can I accomplish this? I am not sure how to setup int variables for the last two items that are true. Please be specific.

                  I am working in a “Multi-Time Frame” strategy, as well. I will need to store the "CurrentBar" information for 4 different time frames. I am unclear how to make sure I am utilizing the correct data series when saving the int variables."

                  Our goal here is not to write specific code for you but to help you reach your goal through your own efforts with examples where needed. To that end in this case you would need two variables, for each barsArray and you may want to name them so that they are clear in their intent to you. For this discussion, I will call them A & B where A will be the prior day and B will be the data before. To continue that I will use a1 and B1 for the next barsarray to help you see the MTF side. You would continue that example for the other 2 barsarrays.

                  Continuing on with the concept that when you load your script that it will process the data sequentially from the beginning, you would so something like:

                  if (BarsInProgress == 0 && Bars.IsLastbarOfSession)
                  {
                  B = A; // assign the existing A to B
                  A = CurrentBar; // assign the newest current bar to A
                  }
                  if (BarsInProgress == 1 && Bars.IsLastbarOfSession)
                  {
                  B1 = A1; // assign the existing A1 to B1
                  A1 = CurrentBar; // assign the newest current bar to A1
                  }
                  etc. etc.

                  In the above sequence, on the first day A ( and A1) will not have anything so it will not assign anything to B (or B1) and then the current bar will be assigned to A (and A1). On the next day, A (and A1) will again be assigned to B (and B1) and A (and a1) will hold the new days currentbar, and so on and so forth. So in the end A (and A1) will hold the latest and B (and B1) the days before that, for their respective bars array.



                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    I am having trouble getting this to print out the correct results for the assigned variables.

                    Being that I am looking to assign variables from data derived before the current trading day, do I have to include

                    "&& State == State.Historical"
                    within the parentheses of
                    if (BarsInProgress == 1 && Bars.IsLastbarOfSession)

                    to get the variable assigned before live trading commences on the current day?

                    Thanks

                    Comment


                      #11
                      Hello ArmKnuckle,

                      Thanks for your reply.

                      Adding State == State.Historical to the condition would not change the values pulled prior to real time data and would prevent access during real time data.

                      You would not need to add that for pulling historical values.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        While reviewing Help Guide, I came across the following note in "IsLastBarOfSession" that seems to point to my troubles.


                        When running Calculate.OnEachTick (with "IsLastBarOfSession"), this property will always return TRUE on the most current real-time bar since it is the last bar that is updating in the trading session. If you need to find a bar which coincides with the session end time, please use the SessionIterator.ActualSessionEnd.

                        https://ninjatrader.com/support/help...rofsession.htm


                        Being that the current bar is always TRUE, I am not storing the proper last two occurrences my needed indicator.

                        What is the proper code for SessionIterator.ActualSessionEnd that I could use to store the indicators from the final bar of the previous two sessions, when using Calculate.OnEachTick?

                        Thanks in advance.
                        Last edited by ArmKnuckle; 11-09-2020, 01:15 AM.

                        Comment


                          #13
                          Hello ArmKnuckle,

                          Thanks for your reply.

                          Correct, when processing the real time bars, AND you are using calculate.OnEachTick or CalculateOnPriceChange, the bool IsLastBarOfSession will always be true.

                          To work around this you would need to add a condition to check that the current bar is not equal to the Bars.Count -1.

                          I've attached an example that you can test with, it will draw a dot on the first and last bar of the session and will also print out on the Ninjascript output window what the current and previous first and last bars of the session were. A cyan-colored dot will show on the chart for the last bar of the session, as you can see on the chart there is no dot on the right edge bar which is the current real time bar and the printed text advises the last bar of the session is correctly 11/6/2020 3:00:00 PM (on my chart MT timezone)

                          Click image for larger version

Name:	ArmKnuckle-2.PNG
Views:	695
Size:	59.3 KB
ID:	1126786

                          [ATTACH]n1126787[/ATTACH]

                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            Could you re-attach the example? It is not being shown as something I can download. Thanks!

                            Comment


                              #15
                              Hello ArmKnuckle,

                              Thanks for your reply.

                              Sure, here it is again: ExampleFirstAndLastBarOfSession.zip
                              Paul H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bmartz, 03-12-2024, 06:12 AM
                              4 responses
                              31 views
                              0 likes
                              Last Post bmartz
                              by bmartz
                               
                              Started by Aviram Y, Today, 05:29 AM
                              4 responses
                              12 views
                              0 likes
                              Last Post Aviram Y  
                              Started by algospoke, 04-17-2024, 06:40 PM
                              3 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by gentlebenthebear, Today, 01:30 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by cls71, Today, 04:45 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X