Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Error solved if I update the chart 2 times...?

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

    Error solved if I update the chart 2 times...?

    Hello NT support,

    Sometimes, when I update (with F5) my indicators chart, its return an error and the indicator not load. The chart is white.
    The error in the output window is similar to this:

    Code:
    Indicator '': Error on calling 'OnBarUpdate' method on bar 2: Collection was modified; enumeration operation may not execute.

    If I update again (with F5) the indicator load correctly.
    I cant understand why is happening it. I am sure is not a problem of my indicators because if I update a second time everything works correctly again.

    its a NT8 chart bug?
    Its posible to detect this error via code? I could update the second time automatically at least...

    Thank you!

    #2
    Hello ninjo,

    From the error, the problem resides in your scripts OnBarUpdate code. This would not be a bug, you likely have used syntax in a way which is generating this error.

    The type of error you are seeing can happen if you modify a collection while it is being looped over. Are you currently using any collections in your code? this would include items like the Account or Drawing Objects collections.

    You will very likely need to further isolate what syntax you are using which generates the error to proceed further here. You can comment out logic and reload the chart until you see this no longer happens, once you locate that syntax we could look into that further.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse, thanks for the suggestion

      I am not using Draw objects collections but Controls buttons and labels, and public class structures.

      My code have 3000 lines. I have no idea where it can happen Do you any idea how could I isolate the syntax for catch this error?

      Thank you

      Comment


        #4
        Hello ninjo,

        I would suggest to start with looking up the C# definition for this error (google) and common reason you would see it. As I noted this can occur when looping over a collection while it is changing. If you use any loops or collections that would be a good place to start.

        If your code is large, you may need to do some debugging and dig into your code. You can use comments or really any debugging technique you see fit, if you are developing the code and it reaches an error this is something you will have to diagnose by exploring your code and testing to see what causes that outcome.

        A common reason for this error is using a foreach loop or also Linq expressions which equate to foreach. You would need to use a for loop with indexing if you are also modifying the collection which is causing the error. If you are unsure if a collection may be modified when you use it, a good rule is to only use for loops always. Alternatively use the .ToList() extension where applicable to generate a new list of the collection which is not a direct reference, this can be iterated and will not change.

        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi jesse, I am also getting the same error code, but I am using a Dictionary and in OnBarUpdate() when condition is made, I am removing a key. error is exactly same and when I refresh like him, it works fine. Any advise?

          please note, I am using foreach loop for the dictionary.
          Last edited by asmmbillah; 09-26-2019, 11:39 AM.

          Comment


            #6
            Hello asmmbillah,

            I would have the same response for your problem as well. This is not specific to NinjaTrader and you can use a search engine like google to search for C# resolutions to this based on the code you are using. foreach loops can cause this message specifically if you make changes to the collection while looping over it. The type of collection generally wouldn't matter, if you change the collection while it is being used you will get this error. You could try using a for loop instead as one solution.

            I look forward to being of further assistance.


            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by techgetgame, Yesterday, 11:42 PM
            0 responses
            8 views
            0 likes
            Last Post techgetgame  
            Started by sephichapdson, Yesterday, 11:36 PM
            0 responses
            2 views
            0 likes
            Last Post sephichapdson  
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,613 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Yesterday, 05:56 PM
            0 responses
            10 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            20 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Working...
            X