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

ChartBars.FromIndex and ChartBars.ToIndex

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

    ChartBars.FromIndex and ChartBars.ToIndex

    Hi,

    Does the concept of ChartBars.ToIndex and ChartBars.FromIndex can be used in OnBarUpdate? If not how do we replace them?

    I am looking for a certain amount of volume in the past using a for loop.


    for(int barIndex = index; barIndex <= ChartBars.FromIndex; barIndex--)
    {
    sumOfVolumes4 += Bars.GetVolume(barIndex);
    if (sumOfVolumes4 >= sumvolfibo)
    {
    foundIndex = barIndex;
    break;

    Print("FI" + foundIndex);


    }
    }

    index is the lowest bar on the chart. I am looking for the bar with the amount of volume equal to
    sumvolfibo from index to ChartBars.FromIndex (left side of the chart).
    The loop is working OnRender but not OBU.

    Also cant Print CurrentBar or Close[0] it return the wrong number.

    Any pointers?

    Ty

    #2
    Hello frankduc,

    Yes, you can use the ChartBars.FromIndex and ChartBars.ToIndex and this will return the bar numbers of the first bar and last bar at the moment the bar updates (not as the chart renders when you are moving the chart).

    CurrentBar is the current processing bar number, even if it is not in view of the chart.

    As historical data is processed the ChartBars.Count will be the total number of bars even if those bars have not been processed in OnBarUpdate() by the script yet.
    This means you would only want to loop through the visible chart bars after the historical data is processed, (either on the last historical bar or on the first real-time bar).

    Close[barsAgo index] uses a bars ago index. If you want the close from the Close series the barsAgo value would be Close[CurrentBar - barIndex] and can only be used if CurrentBar . Alternatively, use Bars.GetClose(barIndex).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello ChelseaB,

      So what about my code. What do i need to change to make it work? Because replacing
      ChartBars.FromIndex for
      ChartBars.Count dont solve the problem.

      As for the close of CurrentBar, i tried
      Print("CVCR" + Bars.GetClose(CurrentBar)); and it still returning the wrong answer.

      ty

      Comment


        #4
        Hello frankduc,

        What is incorrect?

        Are you looping from whatever index is initially set to to ChartBars.FromIndex which is the first visible bar and Bars.GetTime(barIndex) is not returning the time of each bar?

        What is index set to?

        Why does the loop end on the first visible bar instead of the last visible bar?

        Are you waiting until real-time to use series?

        Are you using CurrentBar - barIndex for barsAgo values?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Well the problem is my code, it is not returning an answer. It is returning index but nothing for foundIndex. Its returning the volume at index in the loop but not searching for the barindex that the sum is equal to sumvolfibo.
          If sumvolfibo = 10 000 than foundIndex should return the bar between index and
          ChartBars.FromIndex that the sum equal 10 000.

          It works with OnRender why its not working with OnBarUpdate?


          index is the lowest bar on the chart.

          That loop should be passing through the bars from index (lowest price in the chart) and
          ChartBars.FromIndex
          (all the visible bars going to the left side of the chart). Is it not?

          Code:
          [LEFT][COLOR=#252C2F][FONT=inherit][I]for(int barIndex = index; barIndex <= ChartBars.FromIndex; barIndex--)[/I][/FONT][/COLOR][/LEFT]

          As for the close price of the CurrentBar, does it need to be in a loop of bars to obtain it? In OnRender you just use Close.GetValueAt(CurrentBar) to get the close price of that bar. Cause if you need a loop that means the close price will only update when there is a bar update?

          ty

          Comment


            #6
            Hello frankduc,

            When you mention "It works with OnRender why its not working with OnBarUpdate?"
            This is incorrect. CharBars.FromIndex does work in OnBarUpdate() when used as I have advised in my previous post.

            Below is a link to a video that demonstrates CharBars.FromIndex can be used in OnBarUpdate() in real-time.



            When you mention "index is the lowest bar on the chart", are you certain about this?

            If this is true, then this loop will not loop more than once. If the index is set to ChartBars.FromIndex, then this loops from ChartBars.FromIndex to ChartBars.FromIndex and will only loop once.
            Have you used prints to ensure the correct bar numbers are printing?
            Are you printing the time of the bars to ensure they are the bars represented on the chart?

            I am not able to write the logic for you, but I can look at the output of your prints and help you understand what is being looped through and when, if you would like to provide a text file with the output.

            Below is a link to a forum post that demonstrates how to use print to understand behavior.



            When you mention: "As for the close price of the CurrentBar, does it need to be in a loop of bars to obtain it?"

            CurrentBar is the current processing bar number in OnBarUpdate.

            In each pass of OnBarUpdate() this is a constant value. Its the bar that OnBarUpdate() is working on.


            No, this does not need to be in a loop to obtain. Just call CurrentBar in OnBarUpdate().
            The bars will be painted on the chart and the full amount of bars will exist in the ChartBars collection before they are processed in OnBarUpdate() in an script.

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea,

              OK this is the code for lowest Price on chart: everything is fine it return 3029.5

              Code:
              double lowPrice0 = Int32.MaxValue;
              
                  for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
                   {
                      if (Bars.GetLow(barIndex) < lowPrice0)
                      {
                          lowPrice0 = Bars.GetLow(barIndex);
                          index = barIndex;
              
              
                      }
                   } 
              
                  Print(lowPrice0);
              I dont understand what you mean by calling
              CurrentBar in OnBarUpdate(). In OnRender you just :

              Code:
              [COLOR=#007000]Close.GetValueAt(CurrentBar)  //[/COLOR]Print(Close.GetValueAt(CurrentBar));//
              [LEFT][COLOR=#252C2F][FONT=inherit][/FONT][/COLOR][/LEFT]

              But its not returning the live close price of the bar in progress it always returns 3033 in OBU.

              I did print everything. All seems fine except for CurrentBar, i cant get the close price of the bar in progress and i dont understand how. It is not clear, where is it in the Tutorial? Why its not written somewhere how to get close price of bar in progress for OBU. Are we suppose to guess?

              Comment


                #8
                Hello frankduc,

                The code you have posted has changed.

                I also have not seen your output.

                Close.GetValueAt(CurrentBar) will return the close of the CurrentBar each time this is called in either OnRender() or OnBarUpdate().

                This will change each time a bar closes.

                Attached is an exported script that demonstrates Close.GetValueAt(CurrentBar) will return the close of the CurrentBar in OnBarUpdate().
                Attached Files
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9

                  This is the return in the OutPut Window. Is it what you requested? As you can see Printing :

                  Code:
                  Print(string.Format("{0} | Close.GetValueAt(CurrentBar: {1}): {2}", Time[0], CurrentBar, Close.GetValueAt(CurrentBar)));
                  will return 3033 and not the close price of the bar in progress at eachtick.

                  2019-10-28 09:20:00 | Close.GetValueAt(CurrentBar: 0): 3033
                  3029,5
                  sov328444
                  svolfib-284291742,285714
                  fibo-0,00115530615613137
                  close3033
                  cma3031,5
                  Indicator 'NEWSDBA': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array.

                  As you can see svofib, fibo, cma wont return the right answer because they are all dependent from a live Close price i can get in OR but not OBU.

                  If i try your file GetValueAtTest alone it will return a close value but only at bar closes. My indicator is oneachtick.

                  Is it something i am doing wrong or my NT go rogue?

                  Here's state defautl:

                  Code:
                  protected override void OnStateChange()
                    {
                     if (State == State.SetDefaults)
                     {
                      Description         = @"Enter the description for your new custom Indicator here.";
                      Name          = "NEWSDBA";
                      Calculate         = Calculate.OnEachTick;
                      IsOverlay         = true;
                      DisplayInDataBox       = true;
                      DrawOnPricePanel       = true;
                      DrawHorizontalGridLines      = true;
                      DrawVerticalGridLines      = true;
                      PaintPriceMarkers       = true;
                      ScaleJustification       = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive     = true;
                     }
                     else if (State == State.Configure)
                     {
                     }
                    }
                  In OR no problem
                  Close.GetValueAt(CurrentBar) is returning live close price of the current bar in progress but in OBU it return 3033.

                  thanks

                  Comment


                    #10
                    Hello frankduc,

                    Below is a link to a forum post that demonstrates how to use prints to understand behavior.



                    The CurrentBar value does not change on each tick because the bar has not closed.
                    On each tick the price may or may not change. If the price changes, the value for Close.GetValueAt(CurrentBar) will also change.

                    Below is a link to a video that demonstrates with Calculate set to On each tick the value of Close.GetValueAt(CurrentBar) changes for each tick.


                    Notice the print on each tick matches the last price marker as this is the current close price of that bar.


                    svofib, fibo, cma are not defined and I have no idea what these are. Are you no longer wanting to address your inquiry about using Close.GetValueAt(CurrentBar) as demonstrated in the script I have provided you?
                    Or are you wanting to change your inquiry to something about these variables?

                    In your output, you are seeing the close of the 9:20 as 3033, is this correct?
                    What is the close of this bar with that time on the chart?

                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      If i copy/paste that code in my indicator in OBU
                      Calculate.OnEachTick
                      :

                      Code:
                      Print(string.Format("{0} | Close.GetValueAt(CurrentBar: {1}): {2}", Time[0], CurrentBar, Close.GetValueAt(CurrentBar)));
                      I should get a live update of the close at CurrentBar like in your indicator GetValueAtTest.

                      But its not the case you saw the return in OW it returns:

                      2019-10-28 10:02:00 | Close.GetValueAt(CurrentBar: 0): 3039
                      3032,25
                      sov479990
                      svolfib-215622174,444444
                      fibo-0,00222606975018551
                      close3039
                      cma3032,25
                      Indicator 'NEWSDBA': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array.


                      In fact at 10:02 the close price is 3036.75.

                      It should return the same as
                      GetValueAtTest the list of close at CurrentBar as the transactions are coming in.

                      Lets solve one thing at a time. Why
                      Close.GetValueAt(CurrentBar) is not working in my indicator?

                      Comment


                        #12
                        Hello frankduc,

                        I am not seeing what is outputting on your end. There is no video. There is no text file of the output.

                        I am seeing that you are saying it returns some values, but I cannot see if those match the chart or not.

                        Setting values, in State.SetDefaults means you have to remove the instance of the script and add a new instance as the defaults are only pulled when a new instance is added.

                        In the video I have provided you, I have set the Calculate setting in the parameters.


                        I cannot say why Close.GetValueAt(CurrentBar) is not working in your indicator.
                        You have not confirmed my previous questions to help identify the issue.
                        I do not have the script.
                        The test script I have provided you is a working example that works without any issues and is showing the close price of the currently updating (or most recently updated) bar as demonstrated in the video I have provided you. This test script is showing how to use Close.GetValueAt() in OnBarUpdate.

                        The most likely reason is that the custom logic in your script is not correct.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          i sent a copy of the script code to your attn

                          if you got a chance to look at it.

                          thanks
                          Frank

                          Comment


                            #14
                            Hello frankduc,

                            May I confirm the test script I have provided you is not functioning as expected?

                            I will not be able to debug a script on your behalf, which means to assist you, you will still need to add prints and provide output.

                            I am happy to assist you in analyzing output.

                            Printing the value of the indexes and the value of current bar and the value of State would let us know what is being looped through.
                            This is what I am demonstrating in the videos I have provided you which clearly show that CurrentBar can be used in OnBarUpdate, and Close.GetValueAt(CurrentBar) works in OnBarUpdate().
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              May I confirm the test script I have provided you is not functioning as expected?
                              If you are talking about GetValueAtTest it is working. I sent you the script but Jim replied instead of you.

                              It does not explain why in my script
                              Code:
                               Print(string.Format("{0} | Close.GetValueAt(CurrentBar: {1}): {2}", Time[0], CurrentBar, Close.GetValueAt(CurrentBar)));
                              wont work.

                              In fact if i use
                              Close.GetValueAt(CurrentBar)
                              in another indicator i created it is working.

                              About ChartBars.FromIndex can it be used in OnBarUpdate or not. Cause Jim says no.

                              thanks



                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by f.saeidi, Yesterday, 02:09 PM
                              3 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Jltarrau, Today, 05:57 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by kujista, Today, 06:23 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post kujista
                              by kujista
                               
                              Started by traderqz, Yesterday, 04:32 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by f.saeidi, Today, 05:56 AM
                              1 response
                              5 views
                              0 likes
                              Last Post Jltarrau  
                              Working...
                              X