Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit orders

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

    Exit orders

    Hi,

    What is the strategy code that would send out an exit order to close a trade labeled "a" only if that trade is currently open, but would not even try to close out "a" if it is not open?

    The strategy that I have now sends out the exit order for "a" whenever the conditions for the exit are met despite the fact that "a" was not even opened to begin with. This doesn't really cause any major problems since NT7 just ignores the exit order if it isn't applicable. But how would I prevent this attempted exit order execution from occurring in the first place?

    What I've been using so far is:

    if (Position.MarketPosition == MarketPosition.Long)

    and this does prevent the exit order from trying to execute when I have no orders open. But this line of code doesn't help when I have order "b" open long, because this line of code just says to send the exit order when there is ANY position open long.

    How do I limit it even further so that the condition is that -- only if "a" is long, then and only then send out the exit order: ExitLong("Exit a", "a") ?



    Thank you very much.

    #2
    Hello,

    Not sure that I understand what your trying to achieve here. As you would need to let the trade close itself off before you are able to start a new trade. Therefor if you never exited the trade your Positions would be off and a new trade would never be taken as you are limited by Entried Per Direction = 1 for example if this is what you have it set too.


    Also, your NinjaScript strategy it sounds like you have the settings set to Wait until falt before executing live. In this case the automated strategy running on historical data would need to go flat before the entry is submitted live. Therefor in this case also you would still need the exit signal code.

    I look forward to assisting you further.

    Comment


      #3
      Hi Brett,

      What I'm trying to do is put a condition on the exit order so that it only executes if in fact the entry order associated with that exit order is live and the position is in play.
      Right now my code is something like:

      if (............)
      {
      EnterLong(200000, "a");
      }

      if (Position.MarketPosition == MarketPosition.Long
      &&...................., 1))
      {
      ExitLong("Exit a", "a");
      }


      The problem is that the exit order will try to execute when the rule (..............) is met and when there is currently a live Long position. It will execute the order to "Exit a" even if "a" is not an open position. As long as there is any Long position that is live, this exit order will execute. Now, NT7 won't close an order in this case and will just ignore "Exit a" if "a" is not live. But is there a way to put a condition on the exit order to only execute "Exit a" when "a" is live?
      The condition I put: if (Position.MarketPosition == MarketPosition.Long works to stop the exit order when there is NO live Long position, but it will still execute the exit order (although not close anything) when there is ANY long position that is live.
      How do I set a condition for the exit to only execute if "a" is live?

      Thank you.

      Comment


        #4
        Hello,

        In this case, I would just not run on historical data is this on option?



        On the top of OnBarUpdate()
        {

        if( Historical )
        return;

        }

        Otherwise you would be unable to do this as there is no way for the strategy to tell the difference from what occured historically and what occured live. It simply sees the entries and the exits, this is explicit design.

        Let me know if I can be of further assistance.

        Comment


          #5
          Executing the exit based on the strategy applied to the chart with historical data is ok for my purposes so long as "a" is in play from the strategy (not necessarily live from having been executed by the strategy in real time). But is there any way to set a condition for the exit order to only execute when the entry order associated with that exit order actually needs to be closed as per the strategy?
          The reason this is important is that I also have a signal set up to signal me when the exit order is triggered and I would only like that signal to show when that exit order is indeed relevant (i.e. "a" is needs to be closed) as opposed to just when the conditions of the exit order are met despite "a" not even being live (from historical or not, from live execution or from the historical data). I don't want a signal telling me to "exit a" when "a" doesn't need to be closed because it was never open from the strategy (live or not).
          Isn't there a way to connect the exit order to the entry order in a better way so that the exit order will only go through if it's entry order was called for by the strategy?
          Is there something like:
          if (Position.MarketPosition == MarketPosition.a)
          where the important thing is that the strategy checks to make sure that the order labeled "a" is in play (i.e. open position from the strategy), and not just check to make sure that there is ANY order that is currently Long from the strategy (which will show as Long in MarketPosition when "immediatley submit live working historical orders" is checked like I have it now).
          Is there a code line that says " if 'a' is open" ......?

          Comment


            #6
            Hello,

            Yes, you can check to see if an entry order has been executed. With an IOrder object which gets into some more complex unmanaged order code. However this will not assist you as the IOrder object will still show a filled status on historical. So that when you switch to live it will still consider the order executed and will still need to issue the exit order as NinjaTrader will see no difference vs an order executed in Simulation/Historical or live they are both the same in NinjaTraders eye.

            What you need here is the information on things to watch out for the strategy position vs the account position when this does occur, also to make sure you account is in sync.


            Strategy Position vs. Account Position:
            <http://www.ninjatrader-support2.com/...ead.php?t=4033>
            1st solution:
            You can include the following statement in your code to avoid the strategy to be calculated on historical data.
            // Only run on real-time data
            if (Historical)
            return;
            Add the statement to the top of OnBarUpdate()
            2nd solution:
            You can set 'On starting a real-time strategy' to 'Wait until flat before executing live'.
            This option can be found at Tools-->Options-->Strategies-->NinjaScript-tab.
            3rd solution, would be submitting manual orders to sync the strategy and account positions described in the link


            Let me know if I can be of further assistance.

            Comment


              #7
              Hey Brett,

              I'm not trying to make NT distinguish between live orders and historical orders from the strategy. This is not my question at all. I'm perfectly ok with "NinjaTrader will see no difference vs an order executed in Simulation/Historical or live they are both the same in NinjaTraders eye". This is not a problem for me. In fact, this is good for my purposes.
              What is a problem is completely different. The problem is that NT looks at conditions in the code and if those conditions are met, then it will do what its told.....and I'm looking for the code that sets the condition "if 'a' is open" (historical or live) so that I can include that in my exit strategy so that the strategy first checks to see if "a" is open according to the strategy it's self, and only then attempt to close "a" if other conditions are met. I need the code in the strategy to first see if the strategy it's self has even called for "a" to open. I don't want the strategy to try to close "a" if the strategy has never been called to open "a" in the first place (historical or live). I only want the exit to be ready to close if "a" has been opened by it's own doing.
              Is there a way to make the exit strategy part of the entry strategy so that the exit strategy only begins to get ready to close once the open strategy has been met and is indeed in play, and then completes the order execution to exit the order when it's own conditions are met?
              Is there a way to at least make the strategy check to see if "a" is even open or was supposed to be open based on it's own code, and only if "a" is open as per it's self && the other conditions are met on that bar, only then will it proceed to "Exit a".
              The way my code is now, it's a little better than it was. I used to get signaled to "exit a" whenever the conditions in the code were met, even if there was no orders open (historical or live). Then I put in the condition that it should also have a market position open, and this made the signal more accurate because now I get the signal to "exit a" when there is an open position and the conditions to exit "a" are met.
              But the problem is that I'm not just getting the "exit a" signal when "a" is open and the conditions are met. I'm getting the signal when ANY trade is open and the conditions are met.
              How can I get the signal to be more accurate and only signal me to "exit a" when "a" is open (historical or live) and the conditions are met?

              Thank you very much.

              Comment


                #8
                Hello,

                Just so that i understand fully before my next response here, also want to take a step back here as your last response only left my more confused on what your tryin to to do.

                Scenario 1: You have 2 EnterLongs. EnterLong("a"); and EnterLong("b");

                You want to close EnterLong("a") position only, instead EnterLong("b");'s position is also getting closed which is not wanted.

                Scenario 2: You have 2 EnterLongs. EnterLong("a"); and EnterLong("b");

                You want to close EnterLong('b"); only if EnterLong("a"); is filled.

                Also, I went back and reviewed the ticket: You stated the below:

                if (............)
                {
                EnterLong(200000, "a");
                }

                if (Position.MarketPosition == MarketPosition.Long
                &&...................., 1))
                {
                ExitLong("Exit a", "a");
                }


                The problem is that the exit order will try to execute when the rule (..............) is met and when there is currently a live Long position. It will execute the order to "Exit a" even if "a" is not an open position.


                The bolded statement is incorrect however. As if you specify ExitLong("Exit a", "a"); this will only exit the long position is a was currently long. Otherwise it will not do anything this may be what your missing?

                I look forward to assisting you further.

                Comment


                  #9
                  my scenario is:
                  "b" is currently long. The conditions in the strategy call for it to "exit a". "b" does not close. This is good. I don't want "b" to close. The problem is, the strategy still tries to close "a" even if only "b" is long. This doesn't create any problem in terms of auto-trading because NT7 correctly ignores the "exit a" order since "a" is not the one currently long.
                  However, I also have the strategy send me a signal when it tries to exit "a" and I don't want to get the signal unless it is really exiting "a" and not just trying to exit. I would like to find a way to get the strategy to not even send out the "exit a" order unless "a" is really long.

                  Comment


                    #10
                    Hello,

                    Ok your correct NinjaTrader ignores Exit for A if A does not exist and does not close out of B this is working as you want.


                    In this case the question is not in the area of the actual orders doing anything this is working well. It is in the notification of the exit of A(Believe this is where we had some miscommunication).

                    Ok so heres what I do. For the order part I would leave it as is and still call ExitLong('a"); As you currently are.

                    However for your notification I would change this to a check that checks to see if ExitLong() was filled.

                    I would put the notification code into the following sample, this is more advanced programming as you are starting to delve into the work of unmanaged code. This gives you the ability to monitor the fill of a stop loss or profit target. This can also be used your exit signal. Montitor for the fill of your Exit Signal name. Then you can take some action if it is order.OrderState == OrderState.Filled such as your notification.

                    Therefor you would actually do this in OnOrderUpdate and not in OnBarUpdate().







                    Let me know if I can be of further assistance.

                    Comment


                      #11
                      Hi Brett,

                      Yes, precisely. That is what I'm trying to do. I'm not quite sure how to program it though and I'm still trying to figure out how to use the OnOrderUpdate and order.OrderState == OrderState.Filled as a condition for notification of a specific exit order and not just globally. The specific entry and exit orders are all within OnBarUpdate so how would I use order.OrderState == OrderState.Filled to send out a specific notification about WHICH order was filled? I can say if order.OrderState == OrderState.Filled then notify me, but it won't know what was filled.
                      In the process of trying to figure this out, I noticed that information is written to a container when the order is filled, and then order.OrderState == OrderState.Filled just checks to make sure that this container has "Filled" in it.

                      So I was wondering, is there a way to write "b" to a container when the strategy opens "b", and then have NT call that container to check if there is a "b" in it? Then I could make a condition in the exit strategy that if the container has b then it should exit b and then erase b from the container? So when the entry order executes EnterLong(200000, "b") i would also write something that will make NT put b in a container. And then when the exit strategy's conditions are met, I would include the condition that b is in the container along with the other conditions for the strategy to exit.
                      Is there any way to do this?

                      Thank you very much.

                      Comment


                        #12
                        werido, the IOrders would allow you to see / check the FromEntrySignal string as well, so you can monitor a) a fillstate change to filled and b) if this was produced for an order with your needed entrySignal string, then you could sent yourself a notification.
                        BertrandNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by andrewtrades, Today, 04:57 PM
                        1 response
                        5 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by chbruno, Today, 04:10 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post chbruno
                        by chbruno
                         
                        Started by josh18955, 03-25-2023, 11:16 AM
                        6 responses
                        436 views
                        0 likes
                        Last Post Delerium  
                        Started by FAQtrader, Today, 03:35 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post FAQtrader  
                        Started by rocketman7, Today, 09:41 AM
                        5 responses
                        19 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X