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

Daily Percent Change on a 5-min Chart?

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

    Daily Percent Change on a 5-min Chart?

    Has anyone been able to program an indicator that will display the DAILY percent chagne on a lower time-frame chart. For instance, I am trying to place it on a 5 minute chart. I've read the material on Multi-Time Frame & Instruments, and I came up with the following code. Unfortunately, it does not work as planned.

    When I add my indicator to a 5-minute chart, the indicator shows -0.00 0.0% (basically, it is plotting the indicator, just not getting the values)--see attached screenshot. When I change the time frame to DAILY, the indicator works fine.

    Any thoughts as to why my code will not work on a lower time-frame?
    I have included my code as an attachment.



    Any help would be greatly appreciated. Thanks.

    fosch
    Attached Files

    #2
    fosch, are you using NT6.5? If so, multi-timeframe indicators are unsupported. If you are using NT7, please try using Closes[1][0] for the most recent close of the secondary timeframe (daily in your case) outside of a BarsInProgress check.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Austin,

      I'm using NT 7.

      I tried what you suggested. Namely,

      PrevBarClose = Close[1][1];
      CurrBarClose = Close[1][0];

      Unfortunately, I get the following error message, "Cannot apply indexing with [] to an expression of type 'double'". (CS0021)

      But that makes no sense, because the all price values are of type "double"?

      Comment


        #4
        fosch, it is Closes[x][y], not Close[x][y].
        AustinNinjaTrader Customer Service

        Comment


          #5
          I was so hoping that would have fixed things, but now I get the following error message when I try to add the indicator to my chart. And all I did was change Close[x][y] to Closes[x][y]??

          "Error on calling 'OnBarUpdate' method for indicator 'aaaTSWJDailyChange'on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

          Comment


            #6
            That error message occurs when you try to access bars that don't exist. For example, on the very first bar of the chart, when CurrentBar == 0, and you try to access the previous bar (Closes[1][1]), you will get the error message. To solve the issue, please add this line of code to the very beginning of the OnBarUpdate() section:
            Code:
            if (CurrentBar < 2)
               return;
            AustinNinjaTrader Customer Service

            Comment


              #7
              Austin,

              Almost there, sorta.

              I added the CurrentBar > 2 line of code you suggested, and now the indicator DOES plot a daily value on the intra-day chart. Excellent!

              However, that value is not updating? I copied my code into this post below. What I an noticing in the Output window (since I am plotting values with the Print method) is that the code inside the BarsInProgress ==1 routine is only getting processed ONCE. Why is that? Shouldn't it be processed every time a BarsInProgress 1 event happens?

              With that result, I tried to simply remove the BarsInProgress ==1 if statement, thus moving the following code

              PrevBarClose = Closes[1][1];
              CurrBarClose = Closes[1][0];

              to directly under the CurrentBar < 2 statement.

              I then I got the following error again:

              Error on calling 'OnBarUpdate' method for indicator 'aaaTSWJDailyChange' on bar 2: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

              Here's the code:

              protectedoverridevoid Initialize()
              {

              CalculateOnBarClose =
              false;
              Add(PeriodType.Day,
              1);
              }
              protectedoverridevoid OnBarUpdate()
              {
              if (CurrentBar < 2) return;

              if (BarsInProgress == 1) //Processing for Daily bars
              {
              PrevBarClose = Closes[
              1][1];
              CurrBarClose = Closes[
              1][0];

              //Verify values are updating INSIDE of BarsInProgress == 1
              Print("Inside: " + PrevBarClose);
              Print(
              "Inside: " + CurrBarClose);
              }
              //Verify values are updating OUTSIDE of BarsInProgress == 1
              Print("Outside: " + PrevBarClose);
              Print(
              "Outside: " + CurrBarClose);
              }

              Comment


                #8
                You can try changing the CurrentBar check to include more bars:
                Code:
                if (CurrentBar < 5) return;
                Since you are running with CalculateOnBarClose = false, OnBarUpdate() should fire for each BarsInProgress for every tick.

                Go ahead and try the code with the Prev, Curr = Closes[1][x] right under the are-there-enough-bars check (with additional bars) and see if it works.
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  CurrentBar < 5 did not do the trick either, same error message.

                  However, when I used CurrentBar < 156, it worked. Could that have something to do with the fact that there are 78 x 2 = 156 5-min bars in a day. And the 5-minute is my primary series, with Daily as my secondary series? I would conclude that that was the case, except that when I make a 10-minute my primary with 156 bars (which should be twice the number of bars needed), then I get nothing printing again.

                  Unless you see something here, I feel that we have come to a point where it works on a 5-minute, but we just do not know why? What do you think?

                  Comment


                    #10
                    fosch, that sounds like a very plausible explanation. We've had people who need >1000 bars for certain scripts to function correctly (like 1 min primary daily secondary), so just setting the bars check to a high enough value is a great solution.
                    AustinNinjaTrader Customer Service

                    Comment


                      #11
                      Austin,

                      One other comment on the behavior of NT v7 64-bit when I add this indicator to one of my charts... the indicator properties window goes blank for a second (like it's thinking hard). I have never seen this behavior when adding an indicator to a chart, and it makes me wonder if the "minimal amount of code I do have written" is violated some sort of NT coding standard? All I'm really doing is adding the 2nd instrument, Add(PeriodType.Day, 1) in Initialize() and then calling the Closes[x][y] from within OnBarUpdate()... nothing crazy, right?

                      Oh, and by the way, once when I was adding the indicator, I got a red X on my chart, then it crashed. Not good.

                      Comment


                        #12
                        It could simply be loading up all the necessary data to run the calculations. Once the indicator is on the chart hit the F5 key to refresh all NinjaScripts on the chart (both indicators and strategies) and then the reload time is determined solely by the amount of calculating done by the indicator/strategy. You can use this test to see if a script is computationally intensive.

                        If you can reproduce the red X, please let us know with the exact steps leading up to the crash.
                        AustinNinjaTrader Customer Service

                        Comment


                          #13
                          Is there a way to display the change/percent change from the previous day's close on a chart window as part of the basic package without needing to add/write an indicator? I've looked and couldn't find anything like it, although most charting packages I've seen do allow this in the heading of the chart window, or as part of the chart window title. It would be nice to have this feature on the SuperDOM window as well. Forgive me if this is a proverbial "dumb question".

                          Comment


                            #14
                            gwoolum, unfortunately this would need to be coded out in an indicator, but there is definitely example code of this somewhere on the forum.
                            AustinNinjaTrader Customer Service

                            Comment


                              #15
                              Gwoolum,
                              Here is the code (see attached) that I ended up writing to accomlish this. Unfortunately, it seems to be a little gludgey, like the need for 156 previous bars, and the fact that it only works on a 5-minute chart and higher. Cannot seem to get it to work on a 1-minute. Also, sometimes I get erros loading the indictor. That said, feel free to us it as is, or to make any needed treaks. If you see something glaringly wrong, please let me know.
                              -fosch
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jxs_xrj, 01-12-2020, 09:49 AM
                              6 responses
                              3,290 views
                              1 like
                              Last Post jgualdronc  
                              Started by Touch-Ups, Today, 10:36 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post Touch-Ups  
                              Started by geddyisodin, 04-25-2024, 05:20 AM
                              11 responses
                              62 views
                              0 likes
                              Last Post halgo_boulder  
                              Started by Option Whisperer, Today, 09:55 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post Option Whisperer  
                              Started by halgo_boulder, 04-20-2024, 08:44 AM
                              2 responses
                              25 views
                              0 likes
                              Last Post halgo_boulder  
                              Working...
                              X