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 prevent unwanted order - stop fills while reversal is ordered

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

    How to prevent unwanted order - stop fills while reversal is ordered

    Hi, I'm using a managed approach, and right now the code will prevent new orders from even being fired off until a previous order (entry or exit is complete).

    However, we are finding that sometimes this will still happen:

    A "Close position" is fired off from a reversal, (IE: we were long, then a EnterShort fires off) and stop fills just as this order is fired. This is happening on realtime data, with the NT simulator.

    Description of the problem (see screen shot)

    In (line 2) a reversal is ordered (EnterShort), so NT attempts to close the open Long position (line 1). Profit target and stop loss are working.

    The Stop fills (lines 4) the net position is now flat (lines 7), but the Close Position order gets accepted and is eventually filled (lines 8-10).

    The net position is now Short 1, when it should be flat at this point (lines 12).

    The "Bar Direction Short" order now executes (lines 13-15), and the result is a net short position of 2 (lines 17).

    Is there a way to fix this? (ie: using OCO on the Close Position?)
    Attached Files
    Jeremytang
    NinjaTrader Ecosystem Vendor - Shark Indicators

    #2
    This is known as an in-flight execution where an order is is sent to manage a position, however an existing order was filled to before the other order was sent.

    The only way to avoid this completely us to avoid using market orders/reversal orders when you have a working stop/target.

    If you expect the position to be flat, you should consider perhaps using ExitLong rather than EnterShort and confirming the position before sending a new order to reverse.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Originally posted by Jeremytang View Post
      Hi, I'm using a managed approach, and right now the code will prevent new orders from even being fired off until a previous order (entry or exit is complete).

      However, we are finding that sometimes this will still happen:

      A "Close position" is fired off from a reversal, (IE: we were long, then a EnterShort fires off) and stop fills just as this order is fired. This is happening on realtime data, with the NT simulator.

      Description of the problem (see screen shot)

      In (line 2) a reversal is ordered (EnterShort), so NT attempts to close the open Long position (line 1). Profit target and stop loss are working.

      The Stop fills (lines 4) the net position is now flat (lines 7), but the Close Position order gets accepted and is eventually filled (lines 8-10).

      The net position is now Short 1, when it should be flat at this point (lines 12).

      The "Bar Direction Short" order now executes (lines 13-15), and the result is a net short position of 2 (lines 17).

      Is there a way to fix this? (ie: using OCO on the Close Position?)
      What is the CalculateOnBarClose setting?

      Comment


        #4
        It is set to TRUE, but I need something that will work in both cases.

        Update: I now have it coded to first clean out all outstanding working/accepted orders before placing a new trade.

        The next hurdle I'm facing is that when a reversal is needed, I first cancel all the stops/profit targets, then execute the reversal (via: EnterLong or EnterShort) only after confirmation has been received that the stops/profit targets are either filled or cancelled.

        The problem now is that CancelOrder() doesn't seem to induce them to cancel immediately. In fact they don't cancel at all (in Sim), sometimes they sit in the PendingCancel State, other times there is no cancellation.

        Sample output:

        5/22/2013 10:38:43 AM 7) Def[10409] Queue EnterShort

        5/22/2013 10:38:43 AM 14) Def[10409] - Cancelling Order: Stop loss Sell - Stop : Accepted

        5/22/2013 10:38:43 AM 14) Def[10409] - Cancelling Order: Profit target Sell - Limit : Working

        5/22/2013 10:38:43 AM 10) Def[10409] Active Orders:
        Stop loss] Sell - Stop : Accepted
        Profit target] Sell - Limit : Working


        I have gotten around this by using CancellAllOrders, but I'd rather use CancelOrder directly, since the former requires that the Position.MarketPosition state be correct at the time of the check.

        Jeremy
        Jeremytang
        NinjaTrader Ecosystem Vendor - Shark Indicators

        Comment


          #5
          Hello Jeremytang,

          Thank you for your note.

          Are you setting the Profit Target and Stop Loss using SetProfitTarget and SetStopLoss?

          If so, these orders cannot be cancelled. Instead you will need to make an exit order that is a ExitLongLimit or ExitShortLimit or ExitLongStopLimit or ExitShortStopLimit in place of the profit target and stop loss. Then, using IOrder, these can be cancelled using CancelOrder(orderHandle).


          Please let me know if this does not resolve your inquiry.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Ok Thanks, I understand.

            Is there a way to do a Trailing stop without using SetTrailStop?

            Jeremy
            Jeremytang
            NinjaTrader Ecosystem Vendor - Shark Indicators

            Comment


              #7
              Yes,

              To this, after the position is entered set an exit order.

              In OnMarketData check to see that the exit order is working and that the price (+ or - however many ticks) is greater than the order.LimitPrice. If it is, use the same handle and call the same exit order again using a new price.

              For example:

              private IOrder trailStop = null;

              private override void OnMarketData(MarketDataEventArgs e)
              {
              if (trailStop != null && trailStop.OrderState == OrderState.Working && e.MarketDataType == MarketDataType.Last && e.Price > trailStop.LimitPrice)
              trailStop = ExitLongLimit(e.Price + 5 * TickSize, "entryOrderSignal");
              }

              This is just a rough example that may not do what you want, but it should point you in the right direction.

              Below is a link to the help guide on OnMarketData().
              http://www.ninjatrader.com/support/h...arketdata2.htm


              Please let me know if I can be of further assistance.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by Jeremytang View Post
                Ok Thanks, I understand.

                Is there a way to do a Trailing stop without using SetTrailStop?

                Jeremy
                SeeChelseaB's response, but remember that there is no way to BackTest/Optimize using OnMarketData(), other than manually, by using MarketReplay.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bc24fl, 08-30-2019, 01:58 PM
                4 responses
                257 views
                0 likes
                Last Post PaulMohn  
                Started by sugalt, Today, 04:02 AM
                0 responses
                3 views
                0 likes
                Last Post sugalt
                by sugalt
                 
                Started by tradingnasdaqprueba, 04-09-2024, 09:52 AM
                6 responses
                29 views
                0 likes
                Last Post tradingnasdaqprueba  
                Started by PaulMohn, Today, 02:06 AM
                1 response
                3 views
                0 likes
                Last Post PaulMohn  
                Started by Mindset, Today, 01:27 AM
                0 responses
                5 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Working...
                X