Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem accessing Close[] from OnMouseMove()

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

    Problem accessing Close[] from OnMouseMove()

    I do not understand what is going on when I try to access Close[] from OnMouseMove(...). Consider the following two lines of code:
    Code:
                double close = Close[0];
                double close1 = Close[1];
    • CurrentBar = 16507
    • Count = 16508
    • The first line does execute, but to my surprise it returns information for the oldest bar in the array (the one you see when the chart is scrolled all the way to the left)
    • The second line throws an ArgumentOutOfRangeException that says " Index was out of range. Must be non-negative and less than the size of the collection."
    • The same thing happens with the other standard arrays (Open, High, Low, Volume)
    • The same thing happens if I address it as Close[0][1]

    It is as if NT was trying to index back from zero, rather than from CurrentBar. That would explain the error message -- subtracting the BarsAgo value from zero, rather than from CurrentBar would certainly cause a negative index.

    I then tried an explicitly negative index to see whether that would index forward (zero minus a negative would give a positive index value) and stay in the array. That got a legitimate exception, evidently from NT array access error checking that said " 'barsAgo' needed to be between 0 and 16507 but was -1"

    This looks to me like an NT bug. Accessing the standard arrays from OnMouseMove() worked in NT7 -- I used it to display some information helpful to a developer. So far, though, it appears to fail in NT8.

    --EV

    #2
    Here is a very simple indicator that shows the problem. It is a trimmed-down version of that the Wizard generates, with OnMouseMove() added. The comments should be self-explanatory. It appears that Close[] (etc) are not usable from OnMouseMove in NT8 (they were usable in NT7). I look forward to someone else taking a look to confirm/deny the issue.

    --EV

    Code:
    using System.Windows.Input;
    
    //This namespace holds Indicators in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class VsaNew : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    IsOverlay                    = true;
    
                } else if (State == State.DataLoaded) {
                    if (this.ChartPanel != null)
                        this.ChartPanel.MouseMove += new MouseEventHandler(OnMouseMove);
    
                }
            }
    
            protected override void OnBarUpdate()
            {
                //Add your custom indicator logic here.
            }
            
            internal void OnMouseMove(object sender, MouseEventArgs e)
            {
                // Close[0] gets the wrong value -- it gets the data from the oldest bar
                double d0 = Close[0];
                
                // If you uncomment the following line you get an out of bounds exception
                //double d1 = Close[1];
            }
        }
    }
    Last edited by ETFVoyageur; 08-22-2015, 02:03 AM.

    Comment


      #3
      Originally posted by ETFVoyageur View Post
      Here is a very simple indicator that shows the problem. It is a trimmed-down version of that the Wizard generates, with OnMouseMove() added. The comments should be self-explanatory. It appears that Close[] (etc) are not usable from OnMouseMove in NT8 (they were usable in NT7). I look forward to someone else taking a look to confirm/deny the issue.

      --EV

      Code:
      using System.Windows.Input;
      
      //This namespace holds Indicators in this folder and is required. Do not change it. 
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class VsaNew : Indicator
          {
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      IsOverlay                    = true;
      
                  } else if (State == State.DataLoaded) {
                      if (this.ChartPanel != null)
                          this.ChartPanel.MouseMove += new MouseEventHandler(OnMouseMove);
      
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  //Add your custom indicator logic here.
              }
              
              internal void OnMouseMove(object sender, MouseEventArgs e)
              {
                  // Close[0] gets the wrong value -- it gets the data from the oldest bar
                  double d0 = Close[0];
                  
                  // If you uncomment the following line you get an out of bounds exception
                  //double d1 = Close[1];
              }
          }
      }
      Try using the BarsArray instead.

      Comment


        #4
        Another thing to consider is the TriggerCustomEvent(Action<object> customEvent, object state) function. You are doing something that accesses the Bars data, NT8 might need to make sure things are setup properly before accessing that data. So you may need to run the mouse event through the Custom Event handler.

        Comment


          #5
          Originally posted by NJA_MC View Post
          Another thing to consider is the TriggerCustomEvent(Action<object> customEvent, object state) function. You are doing something that accesses the Bars data, NT8 might need to make sure things are setup properly before accessing that data. So you may need to run the mouse event through the Custom Event handler.

          http://ninjatrader.com/support/helpG...ustomevent.htm
          Interesting thought. Also interesting design. You would think that since it is dealing with events there would be an overload that would pass along the two standard event arguments. (I know, you could bundle both into a single object yourself, but why not have an overload that just accommodates the standard event arguments?)

          --EV

          Comment


            #6
            Originally posted by koganam View Post
            Try using the BarsArray instead.
            Thanks for the suggestion. It seems to be working fine. I was surprised to see that the index is interpreted as an absolute index, not as a BarsAgo index. I gather from the documentation that is the way it is intended to work, though.
            Code:
                        // Returns close of the oldest bar in the array
                        double ba0 = BarsArray[0].GetClose(0);
            
                        // Returns close of the next-to-oldest bar in the array
                        double ba1 = BarsArray[0].GetClose(1);
            --EV
            Last edited by ETFVoyageur; 08-22-2015, 05:19 PM.

            Comment


              #7
              Originally posted by ETFVoyageur View Post
              Thanks for the suggestion. It seems to be working fine. I was surprised to see that the index is interpreted as an absolute index, not as a BarsAgo index. I gather from the documentation that is the way it is intended to work, though.
              Code:
                          // Returns close of the oldest bar in the array
                          double ba0 = BarsArray[0].GetClose(0);
              
                          // Returns close of the next-to-oldest bar in the array
                          double ba1 = BarsArray[0].GetClose(1);
              --EV
              Yes, the BarsArray.Get() methods are indexed from left to right.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              41 views
              0 likes
              Last Post futtrader  
              Started by Option Whisperer, Today, 09:55 AM
              1 response
              12 views
              0 likes
              Last Post bltdavid  
              Started by port119, Today, 02:43 PM
              0 responses
              8 views
              0 likes
              Last Post port119
              by port119
               
              Started by Philippe56140, Today, 02:35 PM
              0 responses
              7 views
              0 likes
              Last Post Philippe56140  
              Started by 00nevest, Today, 02:27 PM
              0 responses
              7 views
              0 likes
              Last Post 00nevest  
              Working...
              X