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

New to ninja...problem with bar tracking

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

    New to ninja...problem with bar tracking

    As I said in the title, I am new to NinjaScript. New as in, I started learning yesterday. I am adept with mql4, and have some indicators I am working on transcoding. I believe I have all the issues resolved except one. Bar tracking.

    In mql4, the far right of the chart is bar 0, and I have found [in the ninja tutorial/reference material] that Ninja tracks with bar 0 on the far right side. I can write a routine to track this correctly, but the necessary info still escapes me at this time. You see, in mql4, I canuse the "Bars" command to tell me how many bars are on the chart. The closest I can find in NinjaScript is the "CurrentBar" command. If I understand it correctly though, this does not tell me how many bars are on the chart, but only if the position of the current bar being referenced based on the far left side count up from zero progressing toward the right.

    So, there is what I need to know to resolve my problem. How do I find out what the bar number is for the most current bar [far right side]? If I can at least figure out how to get this information, I can define the rest to make my current code compatible.

    Thank you for the help in advance!

    #2
    Antny,

    Thank you for your post and welcome to the forums!

    You would need to use [0] to reference the most current bar index.

    CurrentBar works from left to right and returns the number of bars starting at 0 count.

    The bar index that you are using starts at 0 as well but works from right to left.

    In short, [0] will the current bar from the right hand side of the chart.

    Additionally, this will link will come in handy -
    http://www.ninjatrader.com/support/h...currentbar.htm
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Cal View Post
      Antny,

      Thank you for your post and welcome to the forums!

      You would need to use [0] to reference the most current bar index.

      CurrentBar works from left to right and returns the number of bars starting at 0 count.

      The bar index that you are using starts at 0 as well but works from right to left.

      In short, [0] will the current bar from the right hand side of the chart.

      Additionally, this will link will come in handy -
      http://www.ninjatrader.com/support/h...currentbar.htm
      Hmmm. Maybe further explanation of what I am trying to do is required.

      Alright, so, I understand the direction and mechanism of CurrentBar. The issue is....the indicator starts scanning from the left side of the chart and moves right. This is done because the scan is very intensive and the logic requires it. In mql4 this is very simple. I grab the number of bars on the chart via the "Bars" command....check to make sure there are enough bars on the chart....then set my start bar [400 bars from the far right side], and begin the scan using a decrementing cycle.

      Now....IF..... I am understanding you correctly, all I need to do is test to see if "CurrentBar[400]" returns a value, and whatever that value is, will be the bar reference number I can begin the scan from. Is this correct? If this is correct....in NS does this also mean I would use an incrementing cycle [instead of decrementing as in mql4]?

      Sorry for the extra clarification. I just need to make sure I have the direction/orientation correct.

      Comment


        #4
        Antny,

        Thanks for the clarification.

        I think what you are looking for is
        if(CurrentBar < value)
        return;

        This kind if check ensures that there is enough data in the chart before calculating.
        http://www.ninjatrader.com/support/f...ead.php?t=3170
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Cal View Post
          Antny,

          Thanks for the clarification.

          I think what you are looking for is
          if(CurrentBar < value)
          return;

          This kind if check ensures that there is enough data in the chart before calculating.
          http://www.ninjatrader.com/support/f...ead.php?t=3170
          If I am understanding correctly, then this is what my code would be:

          //assuming indicator starts scanning /requires 400 bars from right side of chart
          if (CurrentBar[0] < 401) {return;}
          //set start reference bar (used in multiple locations)
          CurrentBar[401] = maxBarsInArray;
          //note-indie calcs on bar close

          Comment


            #6
            Yes, however CurrentBar is not indexed, but rather a count for the number of bars.

            So, you would just use-
            if(CurrentBar < 401) return;
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Cal View Post
              Yes, however CurrentBar is not indexed, but rather a count for the number of bars.

              So, you would just use-
              if(CurrentBar < 401) return;
              Ah! Okay, that is what I needed to know. The "CurrentBar" command in NS is the equivalent of the "Bars" command in mql4

              Thanks for the help!

              Comment


                #8
                Just wanted to post this in in case anyone else is having this same problem. If you need to figure out how to reference a specific bar index to start an indicator from, this is the NS code you need:

                DateTime timeNow = DateTime.Now;
                // gives DateTime reference for current date and time
                int mostRecentBar = Bars.GetBar(timeNow);
                //gives the index reference of current, not-yet-closed bar


                From mostRecentBar, you can subtract how many bars back you want to start a calculation from. This is what I was trying to figure out. So, in my instance, my maxBarsInArray variable would be set as such:

                int maxBarInArray = mostRecentBar - 401;

                I need to start my indicator 400 bars back from the most recent bar.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by alifarahani, Today, 09:40 AM
                6 responses
                33 views
                0 likes
                Last Post alifarahani  
                Started by Waxavi, Today, 02:10 AM
                1 response
                17 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Started by Kaledus, Today, 01:29 PM
                5 responses
                14 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by Waxavi, Today, 02:00 AM
                1 response
                12 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Started by gentlebenthebear, Today, 01:30 AM
                3 responses
                17 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X