Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exception when compiling a strategy

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

    Exception when compiling a strategy

    Suppose I have the following code snippet:

    LinkedList<int> a = new LinkedList<int>();
    a.AddLast(1);
    foreach(int i in a){
    }

    A window pops up at compilation because of the foreach statement. But then even the foreach is removed, NinjaTrader cannot compile the strategy. Only a restart can solve this problem.

    Here's what I don't understand:

    1) foreach works well in .NET on LinkedList, why not in NinjaTrader?

    2) why I have to restart NinjaTrader because of a bug or mis-use of keywords in my program? (If Visual Studio .NET does this, I'd short MSFT!)

    #2
    Perhaps I was wrong. NinjaTrader's exception report might not be relevant to foreach. Since the following code snippet produces the exception as well,


    // iSL is a linkedlist
    IEnumerator<int> e = iSL.GetEnumerator();
    while(e.MoveNext()){
    int i = e.Current;
    Print(" " + this.pl(i2j(i)));
    }

    The problem is not consistent.

    The following procedure solved the problem sometimes:

    1) Close the strategy editor
    2) Open the strategy editor
    3) compile EXACTLY the same code that previously leads to error works sometimes!

    Comment


      #3
      I don't believe .NET has a linked list by default. http://www.codeproject.com/csharp/doubly-linkedlist.asp

      foreach should work fine. I used it in this indicator sample with no problems. I'm not sure which error window pop up you are referring to, but if you are seeing the same one I think you are seeing you can do this instead of restarting NinjaTrader.
      1. Hit cancel on the pop up
      2. Close NinjaScript editor
      3. Reopen NinjaScript editor
      4. F5 to compile
      5. You will see meaningful error messages this time
      The error messages you should see because .NET doesn't have linked list are these:
      • The type or namespace name 'LinkedList' could not be found (are you missing a using directive or an assembly reference?)
      • foreach statement cannot operate on variables of type 'LinkedList<int>' because 'LinkedList<int>' does not contain a public definition for 'GetEnumerator'
      Edit: I see you've noticed the same solution to the NinjaScript editor bug. This is a persistent bug, but is very erratic and I have not been able to make any discernible pattern of what causes it yet. If you notice anything we would be glad to hear it.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Thx for the prompt reply, Josh.

        Though I'm not an expert, .NET does have a LinkedList implementation in the namespace System.Collections.Generic. I tried it in studio.net, it works well.

        I got those error reporting under VMWare Fusion hosted on an iMac. I just rebooted my computer (an iMac) into windows XP. It seems I can't reproduce that problem.

        Really confused!

        Comment


          #5
          Ahh. Generic does the trick. Thanks for the info. The NinjaScript editor issue happens once in awhile. Majority of the times you won't experience it.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by shliang View Post
            Suppose I have the following code snippet:

            LinkedList<int> a = new LinkedList<int>();
            a.AddLast(1);
            foreach(int i in a){
            }

            A window pops up at compilation because of the foreach statement. But then even the foreach is removed, NinjaTrader cannot compile the strategy. Only a restart can solve this problem.

            Here's what I don't understand:

            1) foreach works well in .NET on LinkedList, why not in NinjaTrader?

            2) why I have to restart NinjaTrader because of a bug or mis-use of keywords in my program? (If Visual Studio .NET does this, I'd short MSFT!)
            How about "including" the appropriate namespace before using the generics?

            Please try adding
            Code:
            using System.Collections.Generic;
            on top of your strategy file.

            Comment


              #7
              Borrowing this thread for a related question;

              foreach loop gives error even though I´ve been adding the System.Collections.Generic namespace.

              Code;

              foreach (int bars in HighestbarsDS)
              {
              if (bars != 0)
              {
              PeaksDS.Set(bars);
              }
              }

              Error message;

              foreach statement cannot operate on variables of type 'NinjaTrader.Data.DataSeries' because 'NinjaTrader.Data.DataSeries' does not contain a public definition for 'GetEnumerator'

              Why can´t the foreach loop access the HighestbarsDS dataseries array? Any suggestion on how to resolve this?
              Last edited by FREEN; 02-11-2011, 04:10 AM.

              Comment


                #8
                FREEN, the dataseries do not implement an IEnumerable interface...try looping trough them the regular way.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                574 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                333 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X