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

Last Tick of Range Bar

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

    Last Tick of Range Bar

    I'm building an indicator that needs to know when we reached the 'last tick' of the current Range bar. I can not use FirstTickOfBar, because at that point price has changed. I really need the calculation of my formula at the last tick of each bar (I also need CalculateOnBarClose = false).

    This was the tentative code I came up with, but it does not work. Has someone come up with such a code? Seems that it would be quite useful, and should be included in the library.

    Code:
    [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] tickValue = ([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]) [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Math[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Abs((High[0] - Low[0]) / TickSize);
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] (BarsPeriod.Value == tickValue ) 
    { 
    isLastTickOfBar = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2];
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
    }
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] isLastTickOfBar = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]; 
    [/SIZE][/FONT][/SIZE][/FONT]
    Any help?

    Thanks!

    #2
    ds1111, in NT's event based framework the open tick of a bar would also the one that closed the prior one - so if you're in FirstTickOfBar couldn't you just reference a bar back further to work with the price values you need?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks Bertrand. That was what I first tried to do, but by them I missed my precious entry, because of timing (I would be already at the opening of the current bar), and my signal would be late (I was putting the 'entry' arrow in the previous bar, which would be late for my entry).


      No, I need a code to tell me the last tick of the current bar. I feel I'm very close with the snipet above, it seems the problem to be something related to type casting. Someone with experience should be able to spot (i haven't programmed for a while).

      Also, if you could let me know the code for painting range bars, I could derive my code from that I think.

      Comment


        #4
        The code you presented would compile fine, but the main issue you face is this - in an event based framework the tick you seek to identify servers two functions, it close the bar and opens the next one, so once you determine the tick is seen to close the bar > you are already on the next bar. One potential way would be working with a lower series added here as an multi series script for looking inside the bigger Range bar you would work in.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          But I realized there are 2 calls into OnBarUpdate... when before the bar closes, and the other with the firt tick of the next bar. What would be a simple code to recognize the bar will close I guess is my question?

          Comment


            #6
            ds1111, the tick that closes the bar is seen and processed when the FirstTickOfBar is triggered - it's the same tick.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              wow... didn't seem like that to me. I've been testing this, and I have print statements in my code, it seemed to me that it would print at, for example, on CL at 106.50 close (OnBarUpdate call), and 106.51 at if FirstTickOfBar at the next call of OnBarUpdate .
              I will recheck that and come back to you.

              Thanks again...!

              Comment


                #8
                ok... did some more testing... you are absolutely right....

                In my code above, the fact that we achieved the range (e.g. 6 tick range) doesn't mean that it's the last tick of a bar, the last tick will be only when an extremity is reached and a new price beyond the 6 tick (FirstTickOfBar) in my example is reached.

                So, I guess what i can do is to print an arrow yellow once the range has been achieved (as a pre-alert), and once the new bar opens I will change it's color to red (or green) for short (long).

                Comment


                  #9
                  Originally posted by ds1111 View Post
                  Thanks Bertrand. That was what I first tried to do, but by them I missed my precious entry, because of timing (I would be already at the opening of the current bar), and my signal would be late (I was putting the 'entry' arrow in the previous bar, which would be late for my entry).


                  No, I need a code to tell me the last tick of the current bar. I feel I'm very close with the snipet above, it seems the problem to be something related to type casting. Someone with experience should be able to spot (i haven't programmed for a while).

                  Also, if you could let me know the code for painting range bars, I could derive my code from that I think.
                  If you look properly into the nature of Range Bars, because they are independent of time elapsed, you will see that the next bar always starts at least one tick beyond the confines of the previous bar. Therefore, you really only have 2 choices:
                  1. Use FirstTickofBar, and reference the Close[1], which means that the signal will always be 1 tick from the entry one way or another;
                  2. As the range of a Range Bar is exactly known, use High[0] - Low[0] = BarsPeriod.Value. Of course, this has the disadvantage that the bar may not tick beyond its range, so that your entry can be the last tick before reversal, putting you in at the very worst price.

                  Not exactly a Hobson;s choice, but pretty much what it is, is what it is.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Brevo, Today, 01:45 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post Brevo
                  by Brevo
                   
                  Started by aussugardefender, Today, 01:07 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post aussugardefender  
                  Started by pvincent, 06-23-2022, 12:53 PM
                  14 responses
                  242 views
                  0 likes
                  Last Post Nyman
                  by Nyman
                   
                  Started by TraderG23, 12-08-2023, 07:56 AM
                  9 responses
                  384 views
                  1 like
                  Last Post Gavini
                  by Gavini
                   
                  Started by oviejo, Today, 12:28 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post oviejo
                  by oviejo
                   
                  Working...
                  X