Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue with Indicator Script Reading Values from Secondary Bars Series

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

    Issue with Indicator Script Reading Values from Secondary Bars Series

    I am writing to seek assistance with an issue I encountered while working on an indicator.

    Recently, I developed an indicator that successfully reads all the indicators present on a chart. To achieve this, I store the indicators found in a collection variable and then utilize a for loop to print their current values. The indicator works flawlessly for indicators that use the primary bars series. However, it encounters problems when an indicator input series relies on another Bars series, resulting in the script either failing or returning a value of 0 as the current value.

    Since the indicators are added by the user and not my script, I am unsure how to determine if an indicator is based on the primary bars or some secondary bars series. I am using the following line to read the current value:

    Code:
    // process item 'i' in the collection of indicators
    _indicator = indicatorCollection[i];
    // find bar index last bar
    int _lastBar = (ChartBars.Count-1) - (_indicator.Calculate == Calculate.OnBarClose ? 1:0);
    // get current value of 1st plot
    double _curPlotVal = _indicator.Values[0].GetValueAt(_lastBar);​
    I suspect that the solution may involve using BarsArray[?], but I am uncertain about the correct approach. Is there a way to identify if an indicator input series is based on another bars series?

    I would greatly appreciate any insights or guidance you can provide to help me resolve this issue and make my indicator compatible with indicators based on secondary bars series.

    Thank you for your attention and support.

    #2
    Hello trendisyourfriend,

    Thanks for your post.

    The index being used for the indicators GetValueAt() method is likely causing the issue. Your lastBar variable uses the chart count which may not be the series that the indicator is using.

    Instead of lastBar, you could try using _indicator.Values[0].GetValueAt(_indicator.CurrentBar); if you want the most recent bar from the indicator using the indicators' series.

    GetValueAt(): https://ninjatrader.com/support/help...getvalueat.htm
    CurrentBar: https://ninjatrader.com/support/help...currentbar.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Using _indicator.Values[0].GetValueAt(_indicator.CurrentBar); works. However, how could i determine the bar index under the mouse pointer for an indicator that is based on another bar series than the primary Bars?

      I use the following code to find the price, bar index and time of the bar under the mouse location.

      For example, if the mouse pointer is in the middle of the chart, i can determine the bar index of the primary bars series at this _mousePointer.X location.
      I'd like to do the same when an indicator is based on another bars series. I need to determine the value of this indicator at the _mousePointer.X location.

      Using this code, the bar index will correspond to the primary bars series. I can't find a solution to this problem, Maybe i could try to use _indicator.CurrentBar - X but how to find the value of X ?

      Code:
      // get the mouse pointer x, y coordinates
      var _mousePointer = e.GetPosition(ChartControl as IInputElement);
      _mousePointer.X = ChartingExtensions.ConvertToHorizontalPixels(_mousePointer.X, ChartControl.PresentationSource);
      _mousePointer.Y = ChartingExtensions.ConvertToVerticalPixels(_mousePointer.Y, ChartControl.PresentationSource);
      
      // convert the Y coordinate into price
      _convertedPrice    = Instrument.MasterInstrument.RoundToTickSize(oChartScale.GetValueByY((float)_mousePointer.Y));
      
      // convert the X coordinate into a bar index
      _barIdx = ChartBars.GetBarIdxByX(ChartControl, (int)_mousePointer.X);
      
      // get the time value from the bar index
      _convertedTime = ChartBars.GetTimeByBarIdx(ChartControl, _barIdx);
      ​​

      Comment


        #4
        Hello trendisyourfriend,

        Thanks for your notes.

        This would not be possible to accomplish since secondary series are not visualized on the chart.

        That would only work if the user specifically has the secondary series added in the chart along with the primary series and then you would use the normal methods you noted to get the mouse location and convert it to a bar index.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          That's my case. There is a secondary series added to the chart along with the primary series. An indicator such as a SMA uses the secondary series as input. Trying to relate the mouse location to the bar idx of this secondary series to read the SMA value at that mouse location was not a success so far. Do i understand that it is not possible to do ? ChartBars.GetBarIdxByX apply only to the primary Bars series. So i can't use that.

          Originally posted by NinjaTrader_BrandonH View Post
          Hello trendisyourfriend,

          Thanks for your notes.

          This would not be possible to accomplish since secondary series are not visualized on the chart.

          That would only work if the user specifically has the secondary series added in the chart along with the primary series and then you would use the normal methods you noted to get the mouse location and convert it to a bar index.

          Comment


            #6
            Hello trendisyourfriend,

            Thanks for your notes.

            The secondary series added to the indicator script is not visualized on the chart window. The secondary series is only used for calculations within the indicator.

            You would only be able to determine the bar index of the primary bar series that the indicator is enabled on at _mousePointer.X location. I am not aware of any supported methods that would allow you to detect the bar index of the secondary added series being added in the indicator's code.

            This forum thread will be open for other community members to share their insights on the topic.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Originally posted by NinjaTrader_BrandonH View Post
              Hello trendisyourfriend,

              Thanks for your notes.

              The secondary series added to the indicator script is not visualized on the chart window. The secondary series is only used for calculations within the indicator.

              You would only be able to determine the bar index of the primary bar series that the indicator is enabled on at _mousePointer.X location. I am not aware of any supported methods that would allow you to detect the bar index of the secondary added series being added in the indicator's code.

              This forum thread will be open for other community members to share their insights on the topic.
              Just to make sure, I have attached a screen capture. The secondary series (1 Minute) is added by the user. The primary series is a 15 Second interval. (see Data Series window). The user adds 2 instances of an SMA(100) applied to the High and Low of the 1-minute Bars Series. All my script is trying to do is to determine the Bar Index of the secondary Bars Series if the user places the mouse pointer anywhere along the white vertical line. The idea is to read the value of any one of these SMA(100) at the mouse.X location. Is that clearer?
              Attached Files
              Last edited by trendisyourfriend; 07-27-2023, 11:27 AM.

              Comment


                #8
                Hello trendisyourfriend,

                Thanks for your notes.

                You would need to use the indicators ChartBars variable, just like how you had to use the indicators CurrentBar variable to know what current bar its running.

                An indicator only knows about the series it is applied to but in your case you are adding an indicator and looping over the other indicators in the chart. Each of those indicators has its own unique variables.

                If you want to do things based on the other indicator then you would need to get values from that indicator's properties or methods.

                Note that the bar id won't be relevant to anything inside the indicator looping over others but you could use that with its GetValueAt() method to get a value from that indicator similar to how you did with CurrentBar
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  @NinjaTrader_BrandonH

                  ChartBars only returns items related to the primery bars series. Is it possible to get the input series or bar interval used by an indicator ?

                  Comment


                    #10
                    Hello trendisyourfriend,

                    Unfortunately, you would only be able to translate the mouse coordinates to the primary series.

                    But once you have the primary bar series under the mouse, you could use BarsArray[barsInProgressIndex].GetBar() to get the closest bar to that time of the secondary series.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello trendisyourfriend,

                      Unfortunately, you would only be able to translate the mouse coordinates to the primary series.

                      But once you have the primary bar series under the mouse, you could use BarsArray[barsInProgressIndex].GetBar() to get the closest bar to that time of the secondary series.
                      https://ninjatrader.com/support/help...nt8/getbar.htm
                      Fantastic, i have not tried yet but i think your solution will get the job done. Thanks a lot.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      561 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      325 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      101 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      547 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      547 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X