Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot apply indexing with [] to an expression of type "double"

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

    Cannot apply indexing with [] to an expression of type "double"

    Hey guys,

    In my trading strategy, Im running multiple time frames and instruments on "On each tick" settings. Im trying to compare previous opens and closes referring to only my primary time frame. Originally I had Open[3] < Close[2] but I was getting values from the other instrument. I used information from the link below and changed it to Open[0][3] < Close[0][2] but now im getting the error, Cannot apply indexing with [] to an expression of type "double". What can i do to get this to work?

    #2
    Originally posted by Tmaninc View Post
    Hey guys,

    In my trading strategy, Im running multiple time frames and instruments on "On each tick" settings. Im trying to compare previous opens and closes referring to only my primary time frame. Originally I had Open[3] < Close[2] but I was getting values from the other instrument. I used information from the link below and changed it to Open[0][3] < Close[0][2] but now im getting the error, Cannot apply indexing with [] to an expression of type "double". What can i do to get this to work?
    That should be,

    Code:
    Opens[0][3] < Closes[0][2]

    The 's' is missing in your code.


    Comment


      #3
      Btw, that error message is correct, and deserves deeper attention.

      You coded 'Open[0][3]' and got an error.

      Why?
      The compiler knows 'Open[0]' resolves to a double, so applying '[3]' to this
      double is illegal code -- so, the error message is spot on and well-worded.

      When you use 'Opens[0][3]', it works.

      Why?
      The compiler knows 'Opens[0]' resolves to a DataSeries, so applying '[3]' to
      this DataSeries is correct code.

      All error messages should be analyzed and understood, learn to trust them
      as completely correct -- they are worthy of being deciphered -- study them
      vigorously until you can 'see' why the compiler is complaining.

      What about compiler error messages you've never encountered?
      Copy & paste the entire message into Google ... trust me, it will be found.

      Why is this useful?
      Solo programming (aka, alone, esp after midnight) requires a deeper set
      of programmer/detective skills -- 'knowing' your compiler is extremely key.

      Just some extra 2˘.
      Last edited by bltdavid; 08-11-2020, 03:46 AM.

      Comment


        #4
        Thanks for your help and appreciate the extra tip, anything helps. I just started learning how to code so I can edit my strategy outside of NinjaTraders Strategy Builder. Thanks again

        Comment


          #5
          I have two data series added in my strategy. I wan to add a condition if the volume on the current bar is greater than the Max Volume in the last 5 bars. How would I code this working with multiple time frames or multiple insruemnts.

          My current code works when I apply it to instrument in the chart window: (Volume[0] > Max(Volume, 5)[1].
          I am getting error message: "Cannot Apply indexing with ][ to an expression of type double" when I enter the following code: (Volumes[1][0] > Max(Volume, 5)[1][0]

          Comment


            #6
            Try this,

            Volumes[1][0] > Max(Volumes[1], 5)[0]

            Comment


              #7
              Thanks, that works.

              Comment


                #8
                Hi David,

                I am still getting a few errors: I am hoping if more guidance and I am hoping you or someone can me with this.
                My objective is to read from a file containing list of symbols then I want an output which prints a list of stocks that are gaping.

                Output will be something like this:
                "Symbol | NetChange | Price | Volume"

                My current code is able to read symbols from a file but it is giving me an error that "Object Reference not set to an instance of an object".
                This error is coming from line: double NetChange = CDOHL.Opens[1][0] - PDOHLC.Closes[1][0]; // Get Net Change fom Prev Day.

                Here is the code for method I am writing:
                private void SymsList()
                {
                string symbolName = null;
                if(!File.Exists(filepath1))
                {
                Print("Symbols List was not found. Creating a New Symbol List");
                }
                else
                {
                string[] symbolsList= File.ReadAllLines(filepath1);
                int i = 0;
                foreach(string symbol in symbolsList)
                {
                symbolName = symbolsList[i];
                Print("Adding Data Series for this symbol "+symbolName.ToString());
                AddDataSeries(symbolName, BarsPeriodType.Minute, 1);
                AddDataSeries(symbolName, BarsPeriodType.Day, 1);
                double NetChange = CDOHL.Opens[1][0] - PDOHLC.Closes[1][0]; // Get Net Change fom Prev Day.
                if((NetChange > 1 || NetChange < -1)&& (Volumes[1][0] > MAX(Volumes[1], 60)[0]))
                {
                string GappingUp = symbolName + "|" + NetChange + "|" + Volumes[1][0] + "|" + Environment.NewLine;
                Print(GappingUp);
                }
                i++;
                }
                }


                Thanks,

                Comment


                  #9
                  Hello bjunaid,

                  Thank you for your post.

                  When receiving the error message "Object Reference not set to an instance of an object" for a line of code, each individual object should be checked within that line to make sure those objects are not returning null. You could check if an object is null or not by using a Print to print out the values for your method to a NinjaScript Output window.

                  Also, I see that you are using AddDataSeries for that method in your script. Are you calling that method within State.SetDefaults or State.Configured? If this is the case, values would return null since the script has not started processing data at this point. Please note that accessing an indexed series should not be done before the script reaches a data processing state. You could, for example, print Close[0] in State.Realtime.

                  Here is a help guide link with more information about AddDataSeries - https://ninjatrader.com/support/help...dataseries.htm

                  And, here is a help guide link about OnStateChange - https://ninjatrader.com/support/help...tatechange.htm

                  Let us know if we may further assist
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    I think I am getting the error because AddDataSeries is not loading. How do I check if the symbol was added to the DataSeries and DataSeries was loaded.

                    I having tried running this code from State: Configured, Default, Historical, DataLoaded, Realtime.

                    I have tried changing the code to:
                    private void SymsList()
                    {
                    string symbolName = null;
                    if(!File.Exists(filepath1))
                    {
                    Print("Symbols List was not found. Creating a New Symbol List");
                    }
                    else
                    {
                    string[] symbolsList= File.ReadAllLines(filepath1);
                    int i = 0;
                    foreach(string symbol in symbolsList)
                    {
                    symbolName = symbolsList[i];

                    if(State == State.Configure)
                    {
                    AddDataSeries(symbolName, BarsPeriodType.Minute, 1);
                    //AddDataSeries(symbolName, BarsPeriodType.Day, 1);
                    Print("Adding Data Series for: "+symbolName.ToString() + " | " + Close[1].ToString("$#,###.##"));
                    if(State == State.DataLoaded)
                    {

                    double NetChange = CDOHL.Opens[1][0] - PDOHLC.Closes[1][0]; // Get Net Change fom Prev Day.
                    if(NetChange > 1 && (Volumes[1][0] > MAX(Volumes[1], 60)[0]))
                    {
                    string GappingUp = symbolName + "|" + NetChange + "|" + Volumes[1][0] + "|" + Environment.NewLine;
                    Print(GappingUp);
                    }
                    }
                    }
                    i++;
                    }
                    }
                    }



                    Comment


                      #11
                      Hello bjunaid,

                      Thank you for that information.

                      I see in your code that you are calling your method in OnStateChange(). The way Multi-TimeFrame/Multi-Instrument scripts work is as follows:

                      AddDataSeries is used in OnStateChange when the State reaches State.Configure. Then in OnBarUpdate, you would reference the added series to get information from that series. Please see the example script I have attached that demonstrates how to add a data series for a Multi-TimeFrame/Multi-Instrument script. The script also demonstrates how to reference the added data series to get volume and max volume at 60 bars for the added series.

                      To understand why these Multi-TimeFrame/Multi-Instrument errors are occurring in your script, please review the help guide documentation linked below. The SampleMultiTimeFrame and SampleMultiInstrument strategies that come with NinjaTrader may be used alongside the help guide link for learning about Multi-Timeframe/Instrument scripts.

                      Multi-Timeframe and Instruments - https://ninjatrader.com/support/help...nstruments.htm

                      Please let us know if we may further assist.
                      Attached Files
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        HI Brandon,
                        Thank you for your response.
                        I was able to tweak the code and get the strategy to work earlier today however I changed something in my code later today and it is not working anymore.
                        It doesn't give me any output and connection column under the strategy tab changes to Loading status and doesn't let me disable the strategy, starts freezing the control center. Also the log doesn't recognize when I enable or disable the strategy.
                        I have had to kill the NT8 application multiple times to retest the strategy but I am not having any luck...


                        Here is my code:

                        private DateTime currDateTime = new DateTime(2020, 09, 18, 09, 30, 00); //


                        protected override void OnBarUpdate()
                        {
                        if(State == State.Historical)//
                        {
                        int di = 1; //Data Sereies Index
                        int i = 0; //symbole index
                        while(i < symbolsList.Length)
                        {
                        if(BarsInProgress == di && CurrentBars[di] >= 1 && Times[di][0] == currDateTime)
                        {
                        currdayOpen = Opens[di][0];
                        currdayHigh = Highs[di][0];
                        currdayLow = Lows[di][0];
                        currdayClose = Closes[di][0];
                        currdayVolume = Volumes[di][0];

                        string GappingUp = symbolsList[i].ToString()
                        + " | " + currdayVolume.ToString("#,###")
                        + " | " + currdayOpen.ToString()
                        + " | " + prevdayClose.ToString();
                        Print(GappingUp);
                        i++;
                        di++;
                        }

                        }
                        }
                        Last edited by bjunaid; Today, 01:23 AM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by kschuble, Today, 01:16 PM
                        1 response
                        9 views
                        0 likes
                        Last Post omololu
                        by omololu
                         
                        Started by wooofad, Today, 11:34 AM
                        0 responses
                        15 views
                        0 likes
                        Last Post wooofad
                        by wooofad
                         
                        Started by tonynt, Today, 10:06 AM
                        1 response
                        19 views
                        0 likes
                        Last Post bltdavid  
                        Started by Kediil, Yesterday, 09:26 AM
                        2 responses
                        35 views
                        0 likes
                        Last Post Kediil
                        by Kediil
                         
                        Started by mr_trade, Today, 08:41 AM
                        0 responses
                        18 views
                        0 likes
                        Last Post mr_trade  
                        Working...
                        X