Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with external indic with ChartBars.GetBarIdxByTime

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

    Problem with external indic with ChartBars.GetBarIdxByTime

    Hi,

    I'm reading signal from an external indicator (EI).
    I read the plot, with this there is no problem. I've donde this awith other indicators and I know how it works.

    But with this external indicator (EI) I'm having error: "Error on calling 'OnBarUpdate' method on bar 19: Object reference not set to an instance of an object."

    I've printed text and the problem is happening before this instruction:

    int barnumber=ChartBars.GetBarIdxByTime(ChartControl, Times[tf][n]);

    I'm using several TimFrames, but the error is happening with the actual TF.
    If I print just before this sentence the value "Times[tf][n]" I get a correct answer, and the time showing exists in the Chart (it is the bur #6 or 7).
    So the error happens when obtaining the Bar from this Time.
    Could it be an issue between the number of bars in the primary indicator and in the external indicator?
    It shouldn't be becaus it is the same chart....

    Thanks!

    #2
    Hello artson,

    Thank you for your post.

    Is this only occurring when you call the indicator from your script, or does it also happen when just using the indicator itself?

    Are you able to provide a reduced sample script to test?

    I look forward to assisting further.

    Comment


      #3
      Hi,

      If I load the External Indicator, I have no problem.
      When I load the main indicator, I get the error when calling this EI.

      Attached 2 easy indicators, one calling the EI, and in the EI the instruction with "ChartBars.GetBarIdxByTime"

      The Main indicator is " a0000Main.cs "
      Indside it calls the external: " a0000test.cs "

      The error I get is:
      Indicator 'a0000test': Error on calling 'OnBarUpdate' method on bar 20: Object reference not set to an instance of an object.

      Comment


        #4
        Hello artson,

        This is because the ChartControl is null when you are calling the hosted indicator within your main indicator script.

        From the Help Guide on ChartControl:

        "Note: The ChartControl object is ONLY guaranteed to be available when a NinjaScript type initiates from a Chart Window. There are situations where an indicator or strategy starts from another Windows (such as the Control Center's Strategies Grid, or from a Strategy Analyzer), where the ChartContol object is NOT accessible. Therefore, the ChartControl object should always be safely accessed (e.g., from within a try-catch, or conditionally using null reference checks)"



        The ChartControl isn't available because the indicator is not initiating from the chart window, it is initiating from the hosting (main) script.

        If you want to get use ChartBars.GetBarIdxByTime you'll need to do it from the main script, which is the script that is actually being applied to the chart (initiating from a chart window).

        Please let us know if you have any further questions.

        Comment


          #5
          Ok, thansk for the explanation.

          I try to keep main main script as small and clean as possible,
          My main script is only calling esternal indicators and geting their signals.


          So, how can I code my external indicator to get the bar number from a Time position, but without using this "ChartBars.GetBarIdxByTime", which gives me problem if not being used in the main script?
          Is there any way of avoiding the use of "ChartBars" but geting the same result?

          Thanks!!


          Comment


            #6
            Hello artson,

            You could try using GetBar(). This method returns the first bar that matches the time stamp of the "time" parameter provided.

            The Help Guide page has some code demonstrating:

            Comment

            Latest Posts

            Collapse

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