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 Hwop38, 05-04-2026, 07:02 PM
            0 responses
            169 views
            0 likes
            Last Post Hwop38
            by Hwop38
             
            Started by CaptainJack, 04-24-2026, 11:07 PM
            0 responses
            328 views
            0 likes
            Last Post CaptainJack  
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            252 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            353 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            181 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Working...
            X