Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Placing orders to inverse current position

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

    Placing orders to inverse current position

    How do I place an order that will inverse the current position? For example, the strategy is Long 1, and by some signal I want to sell 2 contracts to change the position to Short 1.

    Can this be achieved using the managed approach?

    Is there some sample code for this?

    Thanks,

    Boaz

    #2
    Hi Boaz,

    Yes, all enter methods with the manage approach will reverse your position. EnterShort() is one example:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks Ryan,
      I've build the following test code, and it works fine:

      Code:
      protected override void OnBarUpdate()
              {
                  if (CurrentBar < 20)
                      return;
      
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                      longOrder = EnterLongLimit(Close[0] - 2 * TickSize, "EnterLong");
                  }
                  else if (Position.MarketPosition == MarketPosition.Long)
                  {
                      shortOrder = EnterShortLimit(longOrder.AvgFillPrice + 5 * TickSize, "ReverseShort");
                  }
                  else
                  {   // Position.MarketPosition == MarketPosition.Short
                      longOrder = EnterLongLimit(shortOrder.AvgFillPrice - 5 * TickSize, "ReverseLong");
                  }
              }
      I've noticed that the 'Reverse' orders generated two actual limit orders - one tagged 'Close Position' (which I guess is auto-generated by NT), and one tagged 'ReverseShort' / 'ReverseLong', as specified by me.
      My question is what happens in real time:
      • NT will just send one order with quantity set to 2, right?
      • In case of a partial fill, will NT first mark the 'Close Position' order as filled?
      • Using the managed approach as above, will both orders (close, reverse) be cancled at the next bar if they were not filled? What happens in case of a partial fill?
      • Can I use this methods to reverse position in OnExecution(), as described here?
      with partial fills (which are not simulated)?

      Thanks,
      Boaz

      Comment


        #4
        NT will just send one order with quantity set to 2, right?
        In case of a partial fill, will NT first mark the 'Close Position' order as filled?
        The enter methods in managed approach take care of reversal logic, but is done with two transactions as you're seeing there. It works this way in both sim and live. Only when using an unmanaged strategy could you custom code your own reversal handling that submits it in only one order.

        This would not be considered a partial fill. Both close position order and the reversal order is quantity 1, so they cannot be filled less than that.

        Using the managed approach as above, will both orders (close, reverse) be cancled at the next bar if they were not filled? What happens in case of a partial fill?
        This is not a feature of managed approach necessarily, but the default behavior of orders unless you specify liveUntilCancelled = true using the advanced overload for orders.
        Can I use this methods to reverse position in OnExecution(), as described here?
        Yes, you can use this approach. It will not change the mechanics of reversal - that they are made with two separate orders. The sample offers a way of capturing the filled events from orders so that you can submit protective orders as soon as possible.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          The enter methods in managed approach take care of reversal logic, but is done with two transactions as you're seeing there.
          What would happen if the reversing order gets filled, but the closing one does not? Will there be a mix up of tags? Will Position.MarketPosition be flat?

          Comment


            #6
            The closing order is sent first, so this will be the first execution reported. It would be extremely rare that the reversal order is not filled at the same time. Yes, your position would be flat in the event only one order for your reversals is filled.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Stop Order with OnExecution

              Hallo, in this thread at #4 there is a reply stating that a position can be reversed placing an order with OnExecution. I am trying to do so, but it does not work.

              When a Long order is triggered by the strategy, no other order working, I want to place a reversal EnterShortStop order with OnExecution if the price falls below a certain limit. Unfortunately this does not work as the close order is kept alive while the sell order is immediately canceled automatically (see below the code).
              If I place the reversal in OnBarUpdate it works fine, but this is not my intention. How could I fix the issue? Should I rather use OnOrderUpdate or something else?
              Thank you

              protectedoverridevoid OnExecution(IExecution execution)
              {
              if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && Position.MarketPosition == MarketPosition.Long)

              {
              entryshort = EnterShortStop(
              1, MIN(Low, 5)[0]);

              }
              }

              Comment


                #8
                Hello marticora,

                Please see my response at the following link: http://ninjatrader.com/support/forum...637#post463637

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by GussJ, 03-04-2020, 03:11 PM
                16 responses
                3,281 views
                0 likes
                Last Post Leafcutter  
                Started by WHICKED, Today, 12:45 PM
                2 responses
                19 views
                0 likes
                Last Post WHICKED
                by WHICKED
                 
                Started by Tim-c, Today, 02:10 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by Taddypole, Today, 02:47 PM
                0 responses
                5 views
                0 likes
                Last Post Taddypole  
                Started by chbruno, 04-24-2024, 04:10 PM
                4 responses
                53 views
                0 likes
                Last Post chbruno
                by chbruno
                 
                Working...
                X