Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why CS0021 error?

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

    Why CS0021 error?

    Why am I getting an error CS0021 "cannot apply indexing with [] to an expression of type 'double'" on this line of code below?

    if (myclose > myclose[1])

    Thanks

    /////////////////////////////////////////////////////////////////////////////////

    protected override void OnBarUpdate()
    {

    if (CurrentBar < 1)
    return;
    double myclose = 0;

    myclose = Close[0];

    if (myclose > myclose[1])
    {

    Value[0] = myclose;

    }
    }

    #2
    Your myclose is a double variable which can hold only one value. By adding an index [1] you are attempting to look at previous value that is not there. You need to make myclose a data series so there is a previous index value to compare to.
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Hello Ninat,

      Thanks for your post and welcome to the forums!

      The error message is advising that myclose is a double and you have declared it as a double with the statement: double myclose = 0;

      A double is a single container, not an array (or data series) so using an index [1] to point to a prior value is invalid.

      If all you are doing is storing the current close and want to test the prior close then you can use the close series directly like:

      if (Close[0] > Close[1]) // if current close is greater than the previous close
      {
      Value[0] = Close[0]; // plot the current close
      }


      If you want to use myclose then you would need to declare it as a double series: http://ninjatrader.com/support/helpG.../?iseriest.htm

      Comment


        #4
        Declaring as series fixed it.
        Thank you Danny

        //////////////////////////////////////////////////


        public class myexample : Indicator
        {
        private Series<double> myclose;
        }

        Comment

        Latest Posts

        Collapse

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