Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order event subscription on multiple charts

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

    Order event subscription on multiple charts

    Dear Ninjatrader Support Team,

    Currently I'm facing with the following issue:
    I have created an indicator that has a couple of buttons that can open market orders and attach stoploss orders to them as well.
    For that I had to subscribe to the OnOrderUpdate event.
    It works fine (it opens the position with stoploss) if I use it for one chart. It even works if I open additional charts (for other instuments) and I attach this indicator to them.
    But:

    If I close and reopen Ninjatrader (or simply just disconnent and re-connect again) then all charts are loaded with the attached indicator.

    And here comes the issue: Only on the last loaded chart works the indicator (and opens the order by clicking on the button) all other charts throws an exception by click on the button (to open an order):
    "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    at System.ThrowHelper.ThrowArgumentOutOfRangeExceptio n(ExceptionArgument argument, ExceptionResource resource)
    at NinjaTrader.NinjaScript.Series`1.get_Item(Int32 barsAgo)
    at NinjaTrader.NinjaScript.NinjaScriptBase.get_Item(I nt32 barsAgo)
    at NinjaTrader.NinjaScript.Indicators.Profit.ABCOpenO rder.StopLossDistance(Boolean p_IsReverse, Boolean p_LongShort) in c:\Users\admin\Documents\NinjaTrader 8\bin\Custom\Indicators\Profit\ABCOpenOrder.cs:lin e 1407
    at NinjaTrader.NinjaScript.Indicators.Profit.ABCOpenO rder.OnOrderUpdate(Object sender, OrderEventArgs e) in c:\Users\admin\Documents\NinjaTrader 8\bin\Custom\Indicators\Profit\ABCOpenOrder.cs:lin e 347
    at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
    at NinjaTrader.Cbi.Account.RaiseOrderUpdate(OrderEven tArgs e)
    at NinjaTrader.Cbi.Account.CreateOrder(Instrument instrument, OrderAction action, OrderType orderType, OrderEntry orderEntry, TimeInForce timeInForce, Int32 quantity, Double limitPrice, Double stopPrice, Int32 filled, Double averageFillPrice, OrderState initialOrderState, String initialOrderId, DateTime time, String oco, String name, DateTime gtd, String comment, DateTime statementDate, CustomOrder customOrder, Boolean raiseOrderUpdate, Int64 id)
    at NinjaTrader.Cbi.Account.CreateOrder(Instrument instrument, OrderAction action, OrderType orderType, OrderEntry orderEntry, TimeInForce timeInForce, Int32 quantity, Double limitPrice, Double stopPrice, String oco, String name, DateTime gtd, CustomOrder customOrder)
    at NinjaTrader.NinjaScript.Indicators.Profit.ABCOpenO rder.<Button1Click>b__98_0(Object o) in c:\Users\admin\Documents\NinjaTrader 8\bin\Custom\Indicators\Profit\ABCOpenOrder.cs:lin e 571
    "

    If I switch the account on the account selector on the chart trader, then it works again and opens the orders as expected.

    My assumption is that somehow the event subscription does not work correctly if the OnOrderUpdate event subscription has to be executed on multiple charts.

    Can you please help me on this issue?

    Thanks in advance,
    Nintrader​

    #2
    Hello nintrader310,

    Below is a link to an example indicator that adds an event handler method to the <Account>.OrderUpdate event.


    Please close all open workspaces showing an icon in the Control Center > Workspaces menu.
    Save blank workspace with no charts in it.
    Then restart NinjaTrader.

    After restarting with no workspaces open, open a new chart and add the ChartOrderButtonsIndicatorExample to the chart.
    Open a second chart and add the example indicator.
    Then test for the behavior.

    Is the example producing the same behavior?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Dear ChelseaB,

      Thanks for the reply. Well this method is similar to mine, but in your example the account "Sim101" is actually hard coded at State.DataLoaded stage.

      I get the current account number from the chart trader form like this:

      chartTraderAccountSelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
      myAccount = chartTraderAccountSelector.SelectedAccount;
      myAccount.OrderUpdate += OnOrderUpdate;

      If I add my indicator to the charts as you suggested above, I'll get the exception that I mentioned in the initial post.

      BR,
      Nintrader

      Comment


        #4
        Hello Nintrader,

        Thank you for the additional details, and for confirming the example I have provided is not able to reproduce the behavior.

        Attached is an example that uses the Chart Trader Account Selector and adds an event handler to the account.OrderUpdate event.
        ChartTraderUIElementsFromAutomationIDsIndicatorExample_NT8.zip

        May I confirm you are able to reproduce using this example?

        (This script is hosted on the support site, however I found the code to add the event handler was not completed.​)
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Dear Chelsea B.,

          Thanks for the script. I cannot reproduce my issue with this, however I created a custom button for opening an order.
          Let me investigate this further..

          Thank you so much for your kind support!

          BR,
          Nintrader

          Comment


            #6
            Dear Chelsea,

            I extended the script you gave me with a button that creates a buy market order with 1 contract. Additionally creates the OrderUpdate process a Stop loss for that.
            What I experienced that a tons of orders are created by on click on this button and all of them are in initialized status.

            Can you please help?

            Thanks in advance,
            nintrader310
            Attached Files

            Comment


              #7
              Hello nintrader310,

              Tons of orders getting submitted sounds like an infinite recursive loop.

              Find all lines of code that submit orders and add prints above the branching commands that when true trigger an order to submit to understand why these are evaluating as true.

              Most likely you are submitting an order from an OrderUpdate event, and when that order updates and order is submitted, and when that order updates an order is submitted, etc.

              Double check if there an order being submitted from an OrderUpdate event and make sure that the order is only being submitted for the entry order only and not every order.

              Use prints to understand why the branching command that detects the first order is evaluating as true for all orders.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              556 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              324 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
              545 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              547 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X