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

How to get order status

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

    How to get order status

    Hello, I'm in the middle of developing my strategy and I got stuck at the part where I need to close all orders on opposite signal. Orders could be in pending stage or already triggered, and I'm using multiple orders with different TPs. Is there a way to query an order to see whether it exists or not ? i.e. if its on the market or it hasn't been triggered yet ? Because as soon as stop order on market it get Filled OrderState, and if TP hits for that order its the same state. So depending on whether it was triggered or not I need to use cancel order function or exitlong for example please correct me if I'm wrong. Anyone can suggest please on this topic anything ? maybe some sample of the code where there is some decision making login based on if the order was triggered or not ?or some logic to close all long orders (pending and currently running ones).

    #2
    Hello vitaliys,
    Welcome to our forum and I am happy to assist you.

    While submitting the order you can add an IOrder object to it and check it later on for the order state

    Code:
    IOrder order = EnterLongLimit(limitPrice);
    Code:
    if (order.OrderState != OrderState.Filled)
    {
      //do something
    }


    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply. I was trying to use iOrder object, but i'm getting stuck on the part where OrderState.Filled is true. So my order entered market and its currently open lets say the second order in same direction (buy stop) is not triggered yet, (i have two iOrder objects). To close both of those orders I need to apply ExitLong to the first order and CancelOrder to the second one right ? So if I have the third order which reached TP and has been closed it will have OrderState.Filled state untill I clear iOrder object. So at the point where one order has reached TP and closed and second still working they both will have OrderState.Filled. How can I distinguish between currently open and running order vs the one that hit TP ? Lets say I need to close 3 orders (1-pending buy stop, 2- currently active market order, 3 - Order that reached TP and requires iOrder object to be nulled )?

      P.S. I null iOrder objects in execution function if orderstate.Filled or cancelled. But then I can't refer to open order to close it.
      Last edited by vitaliys; 04-27-2012, 11:08 AM.

      Comment


        #4
        Hello vitaliys,
        To clarify further, the third order closed which entry order. the market order or the buystop entry order?

        I look forward to assisting you further.
        .
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Sorry for little messy explanation, third order is buystop order which has been triggered and hit TP so its a closed order. so it looks like this:

          1) Order1 - buystop - pending to be triggered
          2) Order2 - buystop - that has been triggered and currently open
          3) Order3 - buystop - TP was hit and order closed.

          so basically I need to cancel first order & null iOrder, exit second order & null iOrder, and null iOrder third one.

          Comment


            #6
            Hello vitaliys,
            Thanks for the clarification.

            I will use unique names for the entry and exit codes and will nullify the objects in the OnExecution event when the exit order gets filled.

            for example, if the entry is defined by
            Code:
            IOrder long3 = EnterLong("long3")
            and the exit order is defined by
            Code:
            ExitLong("exitlong3", "long3")
            then nullify the entry order object in the OnExecution event when it is squared off:

            Code:
             
            protected override void OnExecution(IExecution execution)
            {
             
               if (execution.Order != null && execution.Order.Name == "exitlong3" && execution.order.OrderState == OrderState.Filled)
               {
                   long3 = null;
               }
            }
            This way you now have the 2 orders to take care off as the 3rd order is already set to null.

            http://www.ninjatrader.com/support/helpGuides/nt7/index.html?onexecution.htm

            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Thank you for the example,
              What about if order hit TP which was set by SetTargetPrice ? there wouldn't be ExitLong, is there a way to do something as you mentioned below for order that hit TP ?
              Last edited by vitaliys; 04-27-2012, 01:10 PM.

              Comment


                #8
                Hello vitaliys,
                Please use the below overload for the target orders
                SetProfitTarget(stringfromEntrySignal, CalculationModemode, doublevalue)
                http://www.ninjatrader.com/support/helpGuides/nt7/index.html?setprofittarget.htm

                Now you can monitor the targets in the OnExecution event.

                Code:
                 
                if (execution.Order != null && execution.Order.Name = "Profit target" && execution.Order.FromEntrySignal = "long3" && execution.Order.OrderState == Filled)
                {
                   //do something
                }
                Please refer to this sample code for further reference http://ninjatrader.com/support/forum/showthread.php?t=5790

                Please let me know if I can assist you any further.

                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Hello Joydeep,

                  I got now confused by the use of SetProfitTarget by you.

                  SetProfitTarget(stringfromEntrySignal, CalculationModemode, doublevalue)

                  stringfromEntrySignal - argument would be the be signal Name that would be assigned to the entry unique name right ? so how would I refer to that target price or SL in OnExecution function ?

                  My entry code is:

                  Order1 = EnterLongStop(0,true,LotSize1, ePrice, "Order1");
                  Order2 = EnterLongStop(0,true,LotSize2, ePrice, "Order2");
                  Order3 = EnterLongStop(0,true,LotSize3, ePrice, "Order3");

                  so each order has its own Signal name,

                  then there is
                  protected override void OnExecution(IExecution execution)
                  {
                  if(Order1!=null && Order1.OrderState == OrderState.Filled)
                  {
                  //***********************TARGET PRICE FOR ORDER1*****************************

                  if (tpType == 1 || tpType == 0)
                  {
                  SetProfitTarget("Order1", CalculationMode.Ticks, TP1); // I Assign TP for Order1, I do almost same same for the next two orders based on different conditions
                  }


                  // I add the following code to clear Order1,2,3 objects after TP is hit

                  if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order1" && execution.Order.OrderState == OrderState.Filled)
                  {
                  Print ("Order1 hit TP and iOrder was nulled");
                  Order1 = null;
                  }
                  if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order2" && execution.Order.OrderState == OrderState.Filled)
                  {
                  Print ("Order2 hit TP and iOrder was nulled");
                  Order2 = null;
                  }
                  if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order3" && execution.Order.OrderState == OrderState.Filled)
                  {
                  Print ("Order3 hit TP and iOrder was nulled");
                  Order3 = null;
                  }



                  }

                  But execution.Order.Name is always either Sell or Order1,2,3. So there is some error in how do I attach TP(assuming all orders might have different entry price) to the order itself could you please clarify this part or let me know if you need all my code (it kind of lengthy and messy so I got only relevant code in here).
                  Last edited by vitaliys; 04-29-2012, 08:56 PM.

                  Comment


                    #10
                    Hello vitaliys,
                    An order submitted via SetProfitTarget() will have a name of "Profit target".
                    Please refer to this sample code for more details http://ninjatrader.com/support/forum...ead.php?t=5790

                    Since you are submitting the target orders in the OnExecution event, and it is filtered by more conditions please make sure your target orders are being placed.

                    You can also add the code TraceOrders in the Initialize section of the strategy to find out which order is getting filled etc


                    Please let me know if I can assist you any further.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      So Joydeep how do I relate SetTargetPrice to the particular order ? Or if I set it in execution event for Order1 it will be automatically assigned to Order1 ?

                      Comment


                        #12
                        Hello vitaliys,
                        You have already done so via the code
                        Code:
                        if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order1" && execution.Order.OrderState == OrderState.Filled)
                        Have you set the SetProfitTarget for all the orders. From your code snippets I could only see you have only set the profit target for the order "Order1". Please make sure you have set the target order for all the orders (Order2 and Order3).

                        Further you code filters like
                        Code:
                        if(Order1!=null && Order1.OrderState == OrderState.Filled)
                        Now if Order1 is null then it fails to get the print and set the other orders to null (order2 and order3).

                        Please remove that filter or make case-by case filter for each order.

                        Please let me know if I can assist you any further.
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          Thank your for clarifying that, so TP is not attached to the entry order directly but through the entry price. If I resubmit SetTargetPrice with same name but different TP price, will it move TP or create a new one ? Also is there a good logic to deal with Partial Fills ? As I understand order with partial fill may not get filled completely at all so it creates a little bit tricky conditions.

                          Comment


                            #14
                            Hello,
                            Yes, if you reassign the SetProfitTarget() then it will change the target price for that particular order.

                            You have to custom code any part filled logic. It depends entirely on you how you want to handle such situations.

                            You may also like to look into this sample coded http://ninjatrader.com/support/forum...ead.php?t=7499

                            Please let me know if I can assist you any further.
                            JoydeepNinjaTrader Customer Service

                            Comment


                              #15
                              Thanks a lot! I have looked at the sample of the code that you provided and got a few question, what is the difference between SetStopLoss and ExitLongStop for example in terms of how the order will be closed? I understand that those two functions have slightly different properties in it but result looks the same. Also second question is I set my stop loss as

                              SetStopLoss("Order1", CalculationMode.Ticks, slSize, false);

                              Will the SetStopLoss stay at the same level of entry after entry signal "Order1" order is closed ? Do i have to reset the SetStopLoss function every time to avoid lets say stop loss being higher then TP situations ? if that's the case can ExitLongStop for example be used as a SL and what is the pros and concerns of using ExitLongStop instead on SetStopLoss function ?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by martin70, 03-24-2023, 04:58 AM
                              14 responses
                              105 views
                              0 likes
                              Last Post martin70  
                              Started by TraderBCL, Today, 04:38 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post TraderBCL  
                              Started by Radano, 06-10-2021, 01:40 AM
                              19 responses
                              606 views
                              0 likes
                              Last Post Radano
                              by Radano
                               
                              Started by KenneGaray, Today, 03:48 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post KenneGaray  
                              Started by thanajo, 05-04-2021, 02:11 AM
                              4 responses
                              470 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Working...
                              X