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 on calling 'OnBarUpdate' method on bar 5100

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

    Error on calling 'OnBarUpdate' method on bar 5100

    Dear NT8 team,
    I hope you are doing well.

    This morning i got the following error in an automatic strategy that I have running and made the strategy to disconnect:
    Error on calling 'OnBarUpdate' method on bar 5100: Collection was modified; enumeration operation may not execute.

    I've never had this issue before and i was wondering if you could provide some guidelines on what creates this issue and how to avoid it next time.

    Below the Log of this morning. Error is in time 10:00:53 (highlighted in red):
    05/03/2024 10:00:53 Order Order='162379006/PRACTICEFeb193454011' Name='MMDOS2E' New state='Cancelled' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18057.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162378533/PRACTICEFeb193454011' Name='MMDOS1E' New state='Cancelled' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18059.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162376909/PRACTICEFeb193454011' Name='MMDOS5' New state='Cancelled' Instrument='NQ MAR24' Action='Sell short' Limit price=18073.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162376906/PRACTICEFeb193454011' Name='MMDOS4' New state='Cancelled' Instrument='NQ MAR24' Action='Sell short' Limit price=18071.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162379006/PRACTICEFeb193454011' Name='MMDOS2E' New state='Cancel submitted' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18057.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162378533/PRACTICEFeb193454011' Name='MMDOS1E' New state='Cancel submitted' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18059.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162376909/PRACTICEFeb193454011' Name='MMDOS5' New state='Cancel submitted' Instrument='NQ MAR24' Action='Sell short' Limit price=18073.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162376906/PRACTICEFeb193454011' Name='MMDOS4' New state='Cancel submitted' Instrument='NQ MAR24' Action='Sell short' Limit price=18071.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162379083/PRACTICEFeb193454011' Name='MMDOS3E' New state='Working' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18059.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='162379083/PRACTICEFeb193454011' Name='MMDOS3E' New state='Accepted' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18059.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Order Order='2c967158d63a4ccead7b39c2801d57d9/PRACTICEFeb193454011' Name='MMDOS3E' New state='Submitted' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18059.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:53 Default Disabling NinjaScript strategy 'Prod_DonchianMMOverextALLv4/320688240'
    05/03/2024 10:00:53 Default Strategy 'Prod_DonchianMMOverextALLv4': Error on calling 'OnBarUpdate' method on bar 5100: Collection was modified; enumeration operation may not execute.
    05/03/2024 10:00:53 NinjaScript NinjaScript strategy 'Prod_DonchianMMOverextALLv4/320688240' submitting order
    05/03/2024 10:00:53 Execution Execution='1205759|162376905|162376905' Instrument='NQ MAR24' Account='PRACTICEFeb193454011' Exchange=Globex Price=18069.25 Quantity=1 Market position=Short Operation=Operation_Add Order='162376905' Time='3/5/2024 10:00 AM'
    05/03/2024 10:00:53 Order Order='162376905/PRACTICEFeb193454011' Name='MMDOS3' New state='Filled' Instrument='NQ MAR24' Action='Sell short' Limit price=18069.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=1 Fill price=18069.25 Error='No error' Native error=''
    05/03/2024 10:00:53 Position Instrument='NQ MAR24' Account='PRACTICEFeb193454011' Average price=18066.58333333 Quantity=3 Market position=Short Operation=Update
    05/03/2024 10:00:52 Order Order='162379006/PRACTICEFeb193454011' Name='MMDOS2E' New state='Working' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18057.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:52 Order Order='162379006/PRACTICEFeb193454011' Name='MMDOS2E' New state='Accepted' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18057.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:52 Order Order='12d4f239628f4cfeb0cc062c9ad06b1c/PRACTICEFeb193454011' Name='MMDOS2E' New state='Submitted' Instrument='NQ MAR24' Action='Buy to cover' Limit price=18057.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    05/03/2024 10:00:52 Order Order='162376903/PRACTICEFeb193454011' Name='MMDOS2' New state='Filled' Instrument='NQ MAR24' Action='Sell short' Limit price=18067.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='' Filled=1 Fill price=18067.25 Error='No error' Native error=''
    05/03/2024 10:00:52 Position Instrument='NQ MAR24' Account='PRACTICEFeb193454011' Average price=18065.25 Quantity=2 Market position=Short Operation=Update
    05/03/2024 10:00:52 NinjaScript NinjaScript strategy 'Prod_DonchianMMOverextALLv4/320688240' submitting order
    05/03/2024 10:00:52 Execution Execution='1217973|162376903|162376903' Instrument='NQ MAR24' Account='PRACTICEFeb193454011' Exchange=Globex Price=18067.25 Quantity=1 Market position=Short Operation=Operation_Add Order='162376903' Time='3/5/2024 10:00 AM'

    Please let me also know if you need to check the strategy code or something else.

    Thank you in advance!
    Attached Files

    #2
    Hello linkcou,

    Thank you for your post.

    Is Prod_DonchianMMOverextALLv4 a strategy that you developed or one that was imported from a third party?
    If it is from a third party, please reach out to the developer for assistance. If it is a script written by you, you should add print statements to help debug and understand the cause of the error. This type of error is typically caused by values in a collection being changed while that collection is being looped through. If the script contains any loops, please focus on adding prints both outside of and inside of the loop to determine if the collection that is being iterated on is being modified to cause this error. For more information about using prints to debug unexpected behavior:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,
      Thanks for the quick reply.

      I wrote the script myself. Interestingly i only have a loop in the script to cancel orders that is set for IsFirstTickOfBar and if 2 conditions are not met during the bar duration. See below the script.

      Do you see anything wrong that should be reviewed? What type of prints should I add to understand the issue?

      Code:
      if (IsFirstTickOfBar)
                  {
                      foreach (Order order in Account.Orders) //this will cancel all orders on the account.
                      {
                          if (order.Name == "MMDOL1" || order.Name == "MMDOL2" || order.Name == "MMDOL3" || order.Name == "MMDOL4" || order.Name == "MMDOL5"
                              || order.Name == "MMDOS1" || order.Name == "MMDOS2" || order.Name == "MMDOS3" || order.Name == "MMDOS4" || order.Name == "MMDOS5"
                              )
                          {
                              if(order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted || order.OrderState == OrderState.CancelPending
                              )
                              {
                                  CancelOrder(order);    
                              }
                          }
                      }
                  }
                  
                  if (Close[0] < Open[0] - 2
                      && (ToTime(Time[0]) >= 090500 && ToTime(Time[0]) <= 160500)
                      && (CurrentBarSec <= LimitSecL)
                      )
                  {                                    
                      
                      if (entryOrder1 == null
                          && BarsSinceEntryExecution(0, "MMDOL1", 0) != 0
                          )
                      {
                          EnterLongLimit(0, true, Qty, (LLL1 - EntryFactorL), "MMDOL1");    
                      }
                      
                      if (entryOrder2 == null
                          && BarsSinceEntryExecution(0, "MMDOL2", 0) != 0
                          )
                      {
                          EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL1), "MMDOL2");    
                      }
                      
                      if (entryOrder3 == null
                          && BarsSinceEntryExecution(0, "MMDOL3", 0) != 0
                          )
                      {
                          EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL2), "MMDOL3");    
                      }
                      
                      if (entryOrder4 == null
                          && BarsSinceEntryExecution(0, "MMDOL4", 0) != 0
                          )
                      {
                          EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL3), "MMDOL4");    
                      }
                      
                      if (entryOrder5 == null
                          && BarsSinceEntryExecution(0, "MMDOL5", 0) != 0
                          )
                      {
                          EnterLongLimit(0, true, Qty, (LLL1 - EntryAggressiveL4), "MMDOL5");    
                      }
                  }
                  else if (Close[0] > Open[0] + 2
                      && (ToTime(Time[0]) >= 090500 && ToTime(Time[0]) <= 160500)
                      && (CurrentBarSec <= LimitSecS)
                      )
                  {    
                      
                      if (entryOrder1 == null
                          && BarsSinceEntryExecution(0, "MMDOS1", 0) != 0
                          )
                      {
                          EnterShortLimit(0, true, Qty, (HHH1 + EntryFactorS), "MMDOS1");
                      }
                      
                      if (entryOrder2 == null
                          && BarsSinceEntryExecution(0, "MMDOS2", 0) != 0
                          )
                      {
                          EnterShortLimit(0, true, Qty, (HHH1 + EntryAggressiveS1), "MMDOS2");
                      }
                      
                      if (entryOrder3 == null
                          && BarsSinceEntryExecution(0, "MMDOS3", 0) != 0
                          )
                      {
                          EnterShortLimit(0, true, Qty, (HHH1 + EntryAggressiveS2), "MMDOS3");
                      }
                      
                      if (entryOrder4 == null
                          && BarsSinceEntryExecution(0, "MMDOS4", 0) != 0
                          )
                      {
                          EnterShortLimit(0, true, Qty, (HHH1 + EntryAggressiveS3), "MMDOS4");
                      }
                      
                      if (entryOrder5 == null
                          && BarsSinceEntryExecution(0, "MMDOS5", 0) != 0
                          )
                      {
                          EnterShortLimit(0, true, Qty, (HHH1 + EntryAggressiveS4), "MMDOS5");
                      }
                  }
                  else
                  {        
                      foreach (Order order in Account.Orders) //this will cancel all orders on the account.
                      {
                          if (order.Name == "MMDOL1" || order.Name == "MMDOL2" || order.Name == "MMDOL3" || order.Name == "MMDOL4" || order.Name == "MMDOL5"
                              || order.Name == "MMDOS1" || order.Name == "MMDOS2" || order.Name == "MMDOS3" || order.Name == "MMDOS4" || order.Name == "MMDOS5"
                              )
                          {
                              if(order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted || order.OrderState == OrderState.CancelPending
                              )
                              {
                                  CancelOrder(order);    
                              }
                          }
                      }
                  }​

      Comment


        #4
        Hello linkcou,

        Thank you for your reply.

        I suspect this error is related to your loop through the Account.Orders collection. If orders are being added or removed while looping through, that will throw this type of error. Instead, you could use ToList() to prevent this error by looping through the list of Orders as follows:

        Code:
        foreach (Order order in Account.Orders.ToList())
        More info about the ToLis() method may be found at the following publicly available link:


        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you so much, Emily!
          Do I need to add anything else in the script to initialize, reset (or something else) the list()?

          I implemented the change in the script and it looks like it compiled well but just wondering if something else is needed to avoid other errors when putting in prod.

          Thanks in advance!
          Best,

          Comment


            #6
            Hello linkcou,

            Thank you for your reply.

            No, nothing else is needed for any kind of setup or cleanup. Now that it has been compiled, please test it out (even on a simulated account) and don't hesitate to reach out with any other questions or concerns.

            Thank you for using NinjaTrader.
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by StockTrader88, 03-06-2021, 08:58 AM
            45 responses
            3,992 views
            3 likes
            Last Post johntraderuser2  
            Started by TAJTrades, Today, 09:46 AM
            0 responses
            7 views
            0 likes
            Last Post TAJTrades  
            Started by rhyminkevin, Yesterday, 04:58 PM
            5 responses
            62 views
            0 likes
            Last Post dp8282
            by dp8282
             
            Started by realblubb, Today, 09:28 AM
            0 responses
            8 views
            0 likes
            Last Post realblubb  
            Started by AaronKoRn, Yesterday, 09:49 PM
            1 response
            19 views
            0 likes
            Last Post Rikazkhan007  
            Working...
            X