Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Search for a bar by its time

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

    Search for a bar by its time

    Hello!


    I am looking at a bar by its time. So I go through a loop as followed:

    int timme = 0;

    for (int i = CurrentBar; i > 0; i--)
    {
    time = ToTime(Time[CurrentBar-i]);
    }

    I assume that CurrentBar represents the total number of the bars on the chart.
    I am failling to get the time of a given bar at the time "i". What am I making wrong?

    Best regrads.

    #2
    Hello Stanfillirenfro,

    Thanks for your post.

    CurrentBar is the systems bar number that is being processed. When your script is loaded the first bar which is zero-based, would be 0, the next bar loaded would 1, then 2, etc, etc. Eventually, as all of the historical bars are processed CurrentBar would point to either the just completed bar or the currently forming bar (this is based on which Calculate setting the script is running in and if the chart is connected to real time data).

    The ToTime() method will return an integer value representing time.

    You may want to add a Print statement inside your for loop to help see what values are being returned.

    Print (ToTime(Time[CurrentBar-i]));

    A method you may want to review is Bars.GetBar(): https://ninjatrader.com/support/help...t8/?getbar.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Many tanks Paul for your valable reply.
      OK which function should return the total number of the bars? I though I could use "CurrentBars" (with 's"), but what is the didéférence with CurrentBar (without s)?
      ​​​I also though I could us Bars.getBars(), but I muss give as argument a defined time. In my case I am in a loop and I do not want to restrict thé code to this time.
      I also failed to get anything printed on output. I do not know how to fix this problem. The window is displayed wothout anything printed on it.
      I would appreciate any help.

      Comment


        #4
        Hello Stanfillirenfro,

        Thanks for your reply.

        If you mean how many bars are in the data series, you could use Bars.Count.

        If you mean how many bars are displayed on the chart, you would have to work in OnRender() to determine that using these properties:
        https://ninjatrader.com/support/help..._fromindex.htm
        https://ninjatrader.com/support/help...rs_toindex.htm

        CurrentBar is used when there is no additional data series. If you have added a data series then you would need to use CurrentBars[] where [] would specify the bars array \(added data series) to use.
        Reference:
        https://ninjatrader.com/support/help...currentbar.htm
        https://ninjatrader.com/support/help...urrentbars.htm

        To get a print of the bar times, try this:

        if (State == State.Historical) // waits until real time data!
        return;

        for (int i = CurrentBar; i > 0; i--)
        {
        Print (ToTime(Time[CurrentBar-i]));
        }
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Many thanks Paul for your reply.

          Regarding the output,
          I have placed the code you gave me just above protected override void OnBarUpdate() and compiled. Nothing appeared on the output.
          I brought it in protected override void OnBarUpdate() and after compilation, still nothing. Either the emplacement is wrong or my code is wrong. Right?
          I am checking other issues.

          Thanks in advance.

          Comment


            #6
            Hello Stanfillirenfro,

            Thanks for your reply.

            The code in Post#4 should only be inside OnBarUpdate.

            You would have to be connected to real time data.

            If that does not resolve the question, please attach your code file.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Many thanks Paul!
              I have attached the code.
              Attached Files

              Comment


                #8
                Hello Stanfillirenfro,

                Thanks for your reply.

                It looks like you incorporated my example incorrectly into your existing code.

                I've attached a complete example you can test with. Please note that to use it you must be connected to realtime (or playback) data and wait until the first live bar closes before you will see an output.

                TestForStan.zip

                As far as your code, I would suggest debugging with print statements to find out where your code is not working as you expect it to.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Many thanks Paul for your help.
                  I have compiled the code/indicator you posted in #8.went to the platform and attached it. After opening the output, the window ffliped very fast between white and came back to it setting color. Nothing was printed unfortunatly.

                  Following up my code, could you please have a look on the lines 80 and 82? Is the line 82 enough to get the time of a given bar?

                  Comment


                    #10
                    Hello Stanfillirenfro,

                    Thanks for your reply.

                    Something is not correct here. The example I provided in the zipped file will print the bar times.

                    Did you apply the indicator to a chart that is connected to real time (or playback) data?

                    Did you observe the output window after a bar has closed on the chart?

                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Paul for your reply.

                      Yes, I have applied the indicator t a chart cinnnected to real tiime data.
                      Yes, the output window was observed after a bar has closed on the chart.

                      Comment


                        #12
                        Hello Stanfillirenfro,

                        Thanks for your reply.

                        To test on my end I delete the indicator, download and imported the indicator I posted in Post#8. I created a chart of NQ 06-21 1 minute bars connected to Real time data and applied the indicator to the chart. After the first real time bar close, the new>Ninjascript output window, on Output 1 tab showed the prints.

                        I think it is important that you be able to replicate this. Please delete the indicator TestForStan and use Tools>ImportNinjascript add-on.. and import the zipped folder. Create the same chart and test again.

                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Paul for your help.
                          I failed again to have something printed on output tab.
                          It really does not work in my case.
                          Is there any setting I should look for before proceeding?

                          Comment


                            #14
                            Hello Stanfillirenfro,

                            Thanks for your reply.

                            Sorry, you are having such difficulty!

                            No setting would be needed.

                            Here is another script to import that should just print the time of every bar, please import, test and advise. It should print immediately.

                            TestsforStan2.zip

                            If this script does not print then please advise what version of NinjaTrader8 you are using (found under help>about).
                            Also please make sure you are checking the "Log" tab of the control center for any related error messages.

                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Paul for your help.

                              It is printing now every minute on output 1.

                              What was happening with TestForStan?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by gbajaj, Today, 03:35 PM
                              2 responses
                              5 views
                              0 likes
                              Last Post gbajaj
                              by gbajaj
                               
                              Started by space_trader, Today, 03:19 PM
                              2 responses
                              8 views
                              0 likes
                              Last Post space_trader  
                              Started by max1ci6, Today, 02:02 AM
                              4 responses
                              27 views
                              0 likes
                              Last Post joromero  
                              Started by Johnny, Today, 02:38 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Tyler  
                              Started by M_ichel, Today, 02:26 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Tyler  
                              Working...
                              X