Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SubmitOrderUnmanaged from an Indicator

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

    SubmitOrderUnmanaged from an Indicator

    Hi,

    Is it possible to have SubmitOrderUnmanaged methods for OCO orders from indicator instead of from Strategy.
    I found below best example on the forum on how to place SubmitOrderUnmanaged orders. but they got invoked from Strategy.




    Wondering is it allowed to perform same actions from Indicator as well ? Thanks in advance.

    #2
    Hello greenhollow803,

    Yes, this is possible through the Addon approach by placing orders directly to an account.

    See the 'ProfitChaseStopTrailIndicatorExample' example linked here.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      You are awesome. Thanks for the quick response. Will go through it and get back if I got struck anywhere

      Comment


        #4
        Hi,

        I am trying to submit a entry signal with limit price instead of market price. Some how my limit price not getting triggered. Instead of that fill happening with market price. Can you please check the below snippet and advice.


        entryShortLimit=0;
        entryShortLimit =Low[0] -(4*TickSize);
        Print("Candle Low :"+Low[0] +" :entryShortLimit Price: "+entryShortLimit);
        currentShortSlPrice = High[0] + StopLossDistance * TickSize;
        Print("StopLoss SHORT price: "+currentShortSlPrice +": High[0]: "+High[0]+" :StopLossDistance: "+StopLossDistance+" :TickSize: "+TickSize);


        if (IsFirstTickOfBar && (accountPosition == null || accountPosition.MarketPosition == MarketPosition.Flat))
        {
        // the name of the order must be Entry or the order will get stuck in the intialize state
        entrySellLimitOrder = submissionAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated, TimeInForce.Day, 1, entryShortLimit, 0, string.Empty, "Entry", Core.Globals.MaxDate, null);
        submissionAccount.Submit(new[] { entrySellLimitOrder });
        }





        Comment


          #5
          Hello greenhollow803,

          You are using OrderType.Market. Use OrderType.Limit.

          Also, provide a limit price.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the quick response. Initially I have tried with the options you suggested. But it didn't work for me.
            In below I am passing entryShortLimit class varibale as limit price which calculated as shown below.

            entryShortLimit=0;
            entryShortLimit =Low[0] -(4*TickSize);​

            entrySellLimitOrder = submissionAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, 1, entryShortLimit, 0, string.Empty, "Entry", Core.Globals.MaxDate, null);

            Do you see anything wrong with my syntax.

            Comment


              #7
              Hello greenhollow803,

              To confirm, the order is still showing as a market order on the Orders tab of the Control Center?

              Have you compiled and re-run the script?

              Is there other code in the script submitting orders?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8

                Just now one order got filled.
                In the log it showed the my expected Limit price 1984.1. But after that again the limit price got changed to 1984.5 .

                I used the code which you shared yesterday.
                10/24/2023 12:50 Order Order='da22a2b62c614d70949c73f87551513c/Sim101' Name='Short Stop Loss' New state='Accepted' Instrument='GC 12-23' Action='Buy' Limit price=0 Stop price=1988.4 Quantity=1 Type='Stop Market' Time in force=DAY Oco='f9c8c365199c4776a89389e58ab39d5f' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:50 Order Order='734795855e904fb2b3c06d98c25f2763/Sim101' Name='Short Profit Target' New state='Working' Instrument='GC 12-23' Action='Buy' Limit price=1983.5 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='f9c8c365199c4776a89389e58ab39d5f' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:50 Order Order='734795855e904fb2b3c06d98c25f2763/Sim101' Name='Short Profit Target' New state='Accepted' Instrument='GC 12-23' Action='Buy' Limit price=1983.5 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='f9c8c365199c4776a89389e58ab39d5f' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:50 Position Instrument='GC 12-23' Account='Sim101' Average price=1984.5 Quantity=1 Market position=Short Operation=Operation_Add
                10/24/2023 12:50 Execution Execution='7ee02a288f2342889d5c50df71dd7462' Instrument='GC 12-23' Account='Sim101' Exchange=Default Price=1984.5 Quantity=1 Market position=Short Operation=Operation_Add Order='a2efdbb653a24706872bdb4723fcf02b' Time='10/24/2023 12:50 PM'
                10/24/2023 12:50 Order Order='da22a2b62c614d70949c73f87551513c/Sim101' Name='Short Stop Loss' New state='Submitted' Instrument='GC 12-23' Action='Buy' Limit price=0 Stop price=1988.4 Quantity=1 Type='Stop Market' Time in force=DAY Oco='f9c8c365199c4776a89389e58ab39d5f' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:50 Order Order='734795855e904fb2b3c06d98c25f2763/Sim101' Name='Short Profit Target' New state='Submitted' Instrument='GC 12-23' Action='Buy' Limit price=1983.5 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='f9c8c365199c4776a89389e58ab39d5f' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:50 Order Order='a2efdbb653a24706872bdb4723fcf02b/Sim101' Name='Entry' New state='Filled' Instrument='GC 12-23' Action='Sell' Limit price=1984.1 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=1 Fill price=1984.5 Error='No error' Native error=''
                10/24/2023 12:50 Order Order='a2efdbb653a24706872bdb4723fcf02b/Sim101' Name='Entry' New state='Working' Instrument='GC 12-23' Action='Sell' Limit price=1984.1 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:50 Order Order='a2efdbb653a24706872bdb4723fcf02b/Sim101' Name='Entry' New state='Accepted' Instrument='GC 12-23' Action='Sell' Limit price=1984.1 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:50 Order Order='a2efdbb653a24706872bdb4723fcf02b/Sim101' Name='Entry' New state='Submitted' Instrument='GC 12-23' Action='Sell' Limit price=1984.1 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:15 Order Order='bf4c4843b6c1464893233f0db5fd22df/Sim101' Name='Short Stop Loss' New state='Cancelled' Instrument='YM 12-23' Action='Buy' Limit price=0 Stop price=33284 Quantity=1 Type='Stop Market' Time in force=DAY Oco='d1faa41b77ed4dada37ced9f13a6a5f8' Filled=0 Fill price=0 Error='No error' Native error=''
                10/24/2023 12:15 Order Order='bf4c4843b6c1464893233f0db5fd22df/Sim101' Name='Short Stop Loss' New state='Cancel submitted' Instrument='YM 12-23' Action='Buy' Limit price=0 Stop price=33284 Quantity=1 Type='Stop Market' Time in force=DAY Oco='d1faa41b77ed4dada37ced9f13a6a5f8' Filled=0 Fill price=0 Error='No error' Native error=''

                Posting the again the Account_Update method which I took from the example and added Selll Orders.



                private void Account_OrderUpdate(object sender, OrderEventArgs orderUpdateArgs)
                {

                Print("Inside Account_OrderUpdate");
                if (entryBuyMar****rder != null && entryBuyMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
                {
                string oco = Guid.NewGuid().ToString("N");
                submitOrdersArray = new List<Order>();

                if (UseProfitTarget)
                {
                Print("UseProfitTarget currentPtPrice"+currentPtPrice +"rderUpdateArgs.AverageFillPrice:"+orderUpdateArgs. AverageFillPrice+":ProfitTargetDistance:"+ProfitTa rgetDistance+":TickSize:"+TickSize);
                currentPtPrice = orderUpdateArgs.AverageFillPrice + ProfitTargetDistance * TickSize;
                Print("UseProfitTarget Inside Account_OrderUpdate currentPtPrice"+currentPtPrice);
                // if (PrintDetails)
                // Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentPtPrice: {1}", orderUpdateArgs.Time, currentPtPrice));

                profitTargetOrder = submissionAccount.CreateOrder(orderUpdateArgs.Orde r.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
                submitOrdersArray.Add(profitTargetOrder);
                }

                if (UseStopLoss)
                {
                //currentSlPrice = orderUpdateArgs.AverageFillPrice - StopLossDistance * TickSize;
                Print("UseStopLoss currentSlPrice"+currentSlPrice);
                //currentSlPrice = Low[0] - StopLossDistance * TickSize;
                // if (PrintDetails)
                // Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentSlPrice: {1}", orderUpdateArgs.Time, currentSlPrice));

                stopLossOrder = submissionAccount.CreateOrder(orderUpdateArgs.Orde r.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
                //Print("UseStopLoss Stop loss order details for Long:"+currentSlPrice +"Low[0]:"+Low[0]+":StopLossDistance:"+StopLossDistance+":TickSi ze: "+TickSize);
                submitOrdersArray.Add(stopLossOrder);
                }

                submissionAccount.Submit(submitOrdersArray);
                }else if (entrySellLimitOrder != null && entrySellLimitOrder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
                {
                string oco = Guid.NewGuid().ToString("N");
                submitOrdersArray = new List<Order>();

                if (UseProfitTarget)
                {
                Print("UseProfitTarget Short Order currentShortPtPrice"+currentShortPtPrice +"rderUpdateArgs.AverageFillPrice:"+orderUpdateArgs. AverageFillPrice+":ProfitTargetDistance:"+ProfitTa rgetDistance+":TickSize:"+TickSize);
                currentShortPtPrice = orderUpdateArgs.AverageFillPrice - (ProfitTargetDistance * TickSize);
                Print("UseProfitTarget currentShortPtPrice"+currentShortPtPrice);
                // if (PrintDetails)
                // Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentShortPtPrice: {1}", orderUpdateArgs.Time, currentShortPtPrice));

                profitTargetShortOrder = submissionAccount.CreateOrder(orderUpdateArgs.Orde r.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, currentShortPtPrice, 0, oco, "Short Profit Target", Core.Globals.MaxDate, null);
                submitOrdersArray.Add(profitTargetShortOrder);
                }

                if (UseStopLoss)
                {
                //currentSlPrice = orderUpdateArgs.AverageFillPrice - StopLossDistance * TickSize;
                Print("UseStopLoss currentShortSlPrice"+currentShortSlPrice);
                //currentShortSlPrice = High[0] + StopLossDistance * TickSize;

                // if (PrintDetails)
                // Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentShortSlPrice: {1}", orderUpdateArgs.Time, currentShortSlPrice));

                stopLossShortOrder = submissionAccount.CreateOrder(orderUpdateArgs.Orde r.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, 0, currentShortSlPrice, oco, "Short Stop Loss", Core.Globals.MaxDate, null);
                Print("Stop loss order details for Short:"+currentShortSlPrice +"High[0]:"+High[0]+":StopLossDistance:"+StopLossDistance+":TickSi ze: "+TickSize);
                submitOrdersArray.Add(stopLossShortOrder);
                }

                submissionAccount.Submit(submitOrdersArray);
                }

                // once the exit orders are closed, reset for a new entry.
                else if (entryBuyMar****rder != null && (profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled || profitTargetOrder.OrderState == OrderState.Rejected || profitTargetOrder.OrderState == OrderState.Cancelled)) || (stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled || stopLossOrder.OrderState == OrderState.Rejected || stopLossOrder.OrderState == OrderState.Cancelled)))
                {
                entryBuyMar****rder = null;
                profitTargetOrder = null;
                stopLossOrder = null;
                }else if (entrySellLimitOrder != null && (profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled || profitTargetOrder.OrderState == OrderState.Rejected || profitTargetOrder.OrderState == OrderState.Cancelled)) || (stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled || stopLossOrder.OrderState == OrderState.Rejected || stopLossOrder.OrderState == OrderState.Cancelled)))
                {
                entrySellLimitOrder = null;
                profitTargetShortOrder = null;
                stopLossShortOrder = null;
                }

                }​

                Comment


                  #9
                  Hello greenhollow803,

                  To confirm, the order in question is not showing as a market order and is showing as a limit order?

                  Which order was changed?

                  I'm only seeing that the entry order order 'a2efdbb653a24706872bdb4723fcf02b' filled at 1984.5 and I am not seeing any other orders with that price.

                  Order='a2efdbb653a24706872bdb4723fcf02b/Sim101' Name='Entry' New state='Filled' Instrument='GC 12-23' Action='Sell' Limit price=1984.1 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=1 Fill price=1984.5 Error='No error' Native error=''

                  This order was submitted with a price of 1984.1 and was not modified.

                  A limit order fills at the specified price or better. If 1984.5 was the market price, this is a better price than 1984.1 for a sell limit order.

                  A sell limit order should be placed above the current bid. A buy limit order should be placed below the current ask.
                  If a limit is placed on the wrong side of the market it will either be filled at the market price (with some brokerages) or will be rejected (with some other brokerages).
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Understood now. But I would like to place Short order only when the price reaches to 1984.1 instead of filling at 1984.5 as its a kind of confirmation for me that price may go down further. Any suggestions how to handle it?

                    Comment


                      #11
                      Hello greenhollow803,

                      A stop (or stop limit) would be used to place a sell order below the bid.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Thank so much for helping me. Thats fixed my entry problem. However it broke other orders. After the Limit order fill when it try to open the Target and StopLoss orders ran into below error. Looks like its not able to find the object to open new orders.

                        Anything you advise to fix the error? Thank you much in advance, You are the best.


                        Time Category Message
                        10/24/2023 2:04:06 PM
                        Default Error in realtime market data handling:
                        System.NullReferenceException: Object reference not set to an instance of an object.
                        at NinjaTrader.NinjaScript.Indicators.Siva.SivaBuySel l.Account_OrderUpdate(Object sender, OrderEventArgs orderUpdateArgs) in c:\Users\balas\Documents\NinjaTrader 8\bin\Custom\Indicators\Siva\SivaBuySell.cs:line 287
                        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.Siva.SivaBuySel l.Account_OrderUpdate(Object sender, OrderEventArgs orderUpdateArgs) in c:\Users\balas\Documents\NinjaTrader 8\bin\Custom\Indicators\Siva\SivaBuySell.cs:line 273
                        at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
                        at NinjaTrader.Cbi.Account.RaiseOrderUpdate(OrderEven tArgs e)
                        at NinjaTrader.Cbi.Account.OrderUpdateCallback(Order order, String orderId, Double limitPrice, Double stopPrice, Int32 quantity, Double averageFillPrice, Int32 filled, OrderState orderState, DateTime time, ErrorCode error, String comment, DateTime statementDate)
                        at NinjaTrader.Cbi.Simulator.Fill(Int32 maxFillQuantity, Double price)
                        at NinjaTrader.Cbi.Simulator.OnMarketData(Object sender, MarketDataEventArgs e)
                        at NinjaTrader.Cbi.Instrument.<>c__DisplayClass153_0. <OnRealtimeDataTimerElapsed>b__1(SubscribedThrea d[] threads, Int32 i, RealtimeEvents evts)

                        Comment


                          #13
                          Hello greenhollow803,

                          System.NullReferenceException: Object reference not set to an instance of an object.

                          The error is stating a variable you are using is null. Are you checking all nullable type variables are not null before attempting to use them?

                          Below is a link to a forum post that discusses.
                          Hello, I keep running into an error of &quot;Object reference not set to an instance of an object.&quot; when I set the entryOrder to Null after it has been cancelled. Below is the block of code that I found in several of the example strategies. protected override void OnOrderUpdate(Order order, double limitPrice,


                          Which variable is null?

                          If you are unsure, add prints above each branching command, assignment, and method call to identify which line is causing the error.


                          Note, trying to use a series (Low[0]) from a non-data-driven method, such as the OrderUpdate event handler, requires calling TriggerCustomEvent().


                          Or alternatively using <Series>.GetValueAt() would not require using TriggerCustomEvent().
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi,

                            I am still struggling with my Entry Limit Order code.

                            Need help to understand the Account_OrderUpdate method ProfitChaseStopTrailIndicatorExample_NT8 in example.

                            How this method gets invoked? Can you please help me to understand it.

                            Comment


                              #15
                              Hello greenhollow803,

                              The Account.OrderUpdate method is invoked by NinjaTrader when the orders on the account are changing states. This is similar to when OnOrderUpdate() is invoked in a Strategy.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              59 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              37 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              41 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              78 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X