Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problems with Math.Sign

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

    Problems with Math.Sign

    Hi guys

    I'm working on an indicator and I'm experiencing the following problem:

    The code that works (i.e. it appears on the chart) is:

    Code:
    A = Math.Sign(Close[[B]0[/B]] - Open[[B]0[/B]])
    but when I replace the [0]'s by [1]'s to get:

    Code:
    A = Math.Sign(Close[[B]1[/B]] - Open[[B]1[/B]])
    then nothing appears on the chart!

    I can't see why Math.Sign should fail to work just because the previous bar is being tested.

    Any advice on how to solve this problem will be much appreciated.

    #2
    Hello arbuthnot,

    Thank you for your post.

    Please go to the Log tab of the NinjaTrader Control Center after adding your indicator to your chart and advise if there are any errors listed in Yellow.

    If so, please detail these errors in your response.

    I look forward to your response.

    Comment


      #3
      Originally posted by arbuthnot View Post
      Hi guys

      I'm working on an indicator and I'm experiencing the following problem:

      The code that works (i.e. it appears on the chart) is:

      Code:
      A = Math.Sign(Close[[B]0[/B]] - Open[[B]0[/B]])
      but when I replace the [0]'s by [1]'s to get:

      Code:
      A = Math.Sign(Close[[B]1[/B]] - Open[[B]1[/B]])
      then nothing appears on the chart!

      I can't see why Math.Sign should fail to work just because the previous bar is being tested.

      Any advice on how to solve this problem will be much appreciated.
      ref: this post: http://www.ninjatrader.com/support/f...d.php?p=291077 in this thread: http://www.ninjatrader.com/support/f...d.php?p=291066

      Comment


        #4
        Thanks very much, Patrick.

        There are lots of errors in Yellow. They all read:

        Error on calling OBU method for indicator … on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on chart.
        My chart has hundreds of bars!

        Looking forward to your further advice.

        Comment


          #5
          Hello arbuthnot,

          Thank you for your response.

          You will need to make sure there are enough bars on your chart before calculating the values.

          On the first bar of a chart you would only have one bar and that bar would be considered Close[0], so Close[1] does not exist at that point.

          Try using the following or similiar at the beginning of the OnBarUpdate() method:

          Code:
          protected override void OnBarUpdate()
          {
          if (CurrentBar < 1) return;
          For information on making sure you have enough bars on your chart please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3170

          Please let me know if I may be of further assistance.

          Comment


            #6
            Thanks so much, Patrick. To say that I knew this and have kept it in my head every day but today is no excuse.

            It's as if you've spent days preparing and packing for a vacation. You get in the car to set off and find that you've mislaid the car keys...

            Putting
            Code:
            if (CurrentBar < 1) return;
            is the 'car keys' of coding any indicator!

            Much obliged.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            595 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            343 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
            556 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            554 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X