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 Mindset, 04-21-2026, 06:46 AM
        0 responses
        52 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        71 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        38 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        99 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        60 views
        0 likes
        Last Post PaulMohn  
        Working...
        X