Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How Does OnBarUpdate() Interact With ForEach/While?

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

    How Does OnBarUpdate() Interact With ForEach/While?

    I have a foreach(var listRecord in listAtmStrategies) loop called from within OnBarUpdate(). The records in the list are added from an atmStrategyRecord class I have created, containing the desired fields/properties I wish to store.

    The loop iterates through running ATM Strategy IDs and associated properties I have stored within the list.

    At the termination of the ATM Strategy:
    Code:
    GetAtmStrategyMarketPosition(listRecord.AtmStrategyId) == Cbi.MarketPosition.Flat)
    I remove the record from my list using listAtmStrategies.RemoveAt(i) and adjust the indexer i so that it makes correct reference to any subsequent records as per the MSDN documentation:

    When you call RemoveAt to remove an item, the remaining items in the list are renumbered to replace the removed item. For example, if you remove the item at index 3, the item at index 4 is moved to the 3 position. In addition, the number of items in the list (as represented by the Count property) is reduced by 1.
    At runtime, I am producing the following error: Error on calling 'OnBarUpdate' method on bar 6370: Collection was modified; enumeration operation may not execute.

    I note there is a separate related error that can be raised, Collection was modified after the enumerator was instantiated, as per this thread: https://ninjatrader.com/support/foru...s-instantiated

    Couple of questions:
    1) What is causing the error in my case? This error is being raised when there is 1 record in the list...
    2) What other considerations should I be thinking about as it relates to running loops from within OnBarUpdate()?

    Thanks

    #2
    Answered my own question here, I cannot modify the List while looping through it... somewhat literally what the error message reads

    Comment


      #3
      Hello TheFil,

      To clarify that specifically happens with foreach loops. You can change to using a for loop if you need to modify the collection while iterating it. There are some nuances when using for loops in C# and modifying collections, you can search online for examples of modifying collections using a for loop depending on the need.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Thanks NinjaTrader_Jesse,

        What I actually ended up doing was maintaining my foreach() loop, assigning the string "DELETE" to the ID field of ListRecord as the final operation of the loop qualified by the requisite conditions and then immediately after the foreach() loop I call:
        Code:
        listAtmStrategies.RemoveAll(r => r.pAtmStrategyId == "DELETE");
        Not the most efficient from an execution perspective since I'm expending compute to write the new value and then iterating over the list once again, but record count is always small (less than 10-15 elements) so the performance loss is negligible.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        5 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        14 views
        0 likes
        Last Post AaronKoRn  
        Started by carnitron, Yesterday, 08:42 PM
        0 responses
        11 views
        0 likes
        Last Post carnitron  
        Started by strategist007, Yesterday, 07:51 PM
        0 responses
        13 views
        0 likes
        Last Post strategist007  
        Started by StockTrader88, 03-06-2021, 08:58 AM
        44 responses
        3,983 views
        3 likes
        Last Post jhudas88  
        Working...
        X