Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error Message in OnBarUpdate Event

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

    Error Message in OnBarUpdate Event

    Hi,

    I developed a custom strategy and have some errors in it.

    Code:
    Error on calling 'EventHandlerBarsUpdate' method: More than 100 subsequent user events
    This is the error message.

    I think this error is displayed because of OnBarUpdate event, but I can't find the solution.

    Code:
    protected override void OnBarUpdate()
    {
        if (currentState == ButtonState.ON)
        {
            lastPrice = Close[0];
            double newBuyStopPrice = 0;
            double newSellStopPrice = 0;
    
             newBuyStopPrice = Close[0] + Distance * TickSize;
             newSellStopPrice = Close[0] - Distance * TickSize;
    
            // Check if the order is still working and not filled
            if (buyStopOrder != null && sellStopOrder != null && buyStopOrder.OrderState == OrderState.Accepted && buyStopOrder.OrderState == OrderState.Accepted)
            {
                ChangeOrder(buyStopOrder, buyStopOrder.Quantity, buyStopOrder.LimitPrice, newBuyStopPrice);
                ChangeOrder(sellStopOrder, sellStopOrder.Quantity, sellStopOrder.LimitPrice, newSellStopPrice);
            }
        }​
    }
    Clicking on Run Button will place a buy stop order and a sell stop order at x pts distance from last price.
    When price move, orders will also move keeping the input distance.

    It works well in a short time, but the error message is displayed.

    Calculation event is OnPriceChange.

    Please help me in this problem.

    Regards.​

    #2
    I'm not sure if you are doing this in Visual Studios or not, but if you aren't then you probably should. In the NinjaTrader Editor, right click and make sure Debug Mode is checked. Click the Visual Studio icon at the top of the same window to open it in Visual Studio. You can now select Debug in Visual Studios and Attach to Process. Select Ninja Trader to attach it to. When you run it again, it'll break where it's causing the issue.

    Comment


      #3
      Hello Rogers101,

      The error: More than 100 subsequent user events

      Means that your logic is doing something repetitive that was blocked as the platform deemed that it was in error. An example would be trying to submit over 100 orders in a loop or doing 100 change orders in a loop.

      Are you using any type of looping logic?

      That could also be if you tried to do 100 change orders in a short time because your condition remained true for 100 ticks. Did you toggle currentState to a different state after doing the change order?


      Comment


        #4
        Thanks for your reply.
        I want to change stop orders based on real-time price movements. For example, if I place a buy stop and a sell stop order at the same distance from the current price, they should move with the real-time price and maintain their distance based on real-time price movements. Do you understand me?
        How many changes can be made per second in a NinjaScript loop?

        Comment


          #5
          Hello Rogers101,

          If you want to maintain a distance based on realtime movement you would want to incorporate a way to rate limit the frequency that you call change order. If your script is using OnEachTick or OnPriceChange you could be submitting hundreds of change events which will eventually cause either the platform to show an error or a rejection from the broker. Most logic that makes an order trail only does a change once the price has moved X in profit so it is limiting the amount of change orders sent. To confirm that part of your logic is the problem you can add a print into that section to see if it is called very frequently before you get the user events error.

          Comment


            #6
            But I want to place both buy stop and sell stop order at the X distance of current price and move these orders if the price change with maintain the distance. This isn't stop loss order. These are Stop Market order. I wanna show the video but I can't upload the video here.

            Comment


              #7
              And one more question : Is there any function or event similar with OnTimer()? I use OnPriceChange() Method but if the price doesn't change, the event can't be triggered. I want real time trigger.

              Comment


                #8
                Hello Rogers101,

                If you are not using loops then with whatever you are doing you will need to do it less because you are getting the error about user events. You will need to implement logic so that the orders are only updated so often instead of on every tick or price change.

                There is not a timer method, NinjaScript is driven by the active market so you would need to wait for a price change, tick or bar close event to drive your logic.

                Comment


                  #9
                  Originally posted by NinjaTrader_Jesse View Post

                  If you are not using loops then with whatever you are doing you will need to do it less because you are getting the error about user events. You will need to implement logic so that the orders are only updated so often instead of on every tick or price change.
                  How can I implement logic so that the orders are only update so often instead of on every tick or price change?

                  My strategy is like this :

                  I need a button (ON/RUN/OFF) on NT8 chart (upper right corner).

                  ON state ("ON" label with "ON background color" ) :
                  Clicking on it will place a buy stop order and a sell stop order at x pts distance from last price.
                  When price move, orders will also move keeping the input distance.
                  If we move manually an order to another distance from last price, it will move the other order to the same distance from last price

                  RUN state ("RUN" label with "RUN background color" ) :
                  Clicking again on it will leave pending orders but they will not move with price, they will stay at the same price level.

                  OFF state ("OFF" label with "OFF background color" ) :
                  Clicking again on it will delete pending orders.

                  During RUN state, if buy stop or sell stop is converted in running order, the other one will be deleted.
                  No target level.
                  ​Trailing stop using input settings

                  Button is still in "RUN" state.

                  If we click again on button during "RUN" state with running position, it will close orders and reach the "OFF" state.​

                  Inputs :
                  -Lots
                  -Distance
                  - SL distance
                  - Trailing starting distance ( if = 5. then when order will earn 5 points, the trailing will start)
                  - Trailing distance
                  -OFF color button
                  -ON color button
                  -RUN color button​

                  Can you help me to implement this logic?

                  Thanks a lot.

                  Comment


                    #10
                    Hello Rogers101,

                    Please see the trailing sample from the following post, that shows how to trail an order only when the price moves in X amount of profit, you could do something similar to limit the amount of changes. Once you confirm that changing the order is causing the error you could try to implement logic like that as a resolution.




                    Regarding the button, I can point to an example of how to add buttons. Beyond that you would need to code whatever you needed the strategy to do by using variables and logic. You can find a sample that shows how to control strategy properties using buttons here: https://ninjatraderecosystem.com/use...bar-buttons-2/

                    Comment


                      #11
                      If I should run the strategy on selected account of ChartTrader, should I develop AddOn?

                      Comment


                        #12
                        Hello Rogers101,

                        Yes if you plan to use the chart trader to select an account you would want to use an indicator and the addon framework. Strategies are intended to use the account you select when configuring the strategy.

                        Comment


                          #13
                          Thanks for your help.

                          Comment


                            #14
                            Can you provide AddOn example which implement custom strategy logic using selected account feature?

                            Comment


                              #15
                              Hello Rogers101,

                              An addon window would not have access to a specific chart, addons are their own tools similar to how the superdom is its own tool. If you want to add buttons to a chart but have a strategy place orders the best solution would be the link I had provided in post 10. If you wanted to use the account selector from the chart you could change to using an indicator instead of a strategy. There is a sample of finding the account selector here: https://ninjatrader.com/support/foru...51&postcount=2

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, 03-13-2026, 05:17 AM
                              0 responses
                              89 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              152 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              80 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              53 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              63 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X