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 reverse a position

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

    How to reverse a position

    Hello,
    My strategy is working great using IOrder objects to place limit orders, then using OnExecution() to set stops and targets when filled. I have a scenario where I want to reverse a position when the target is hit for a filled order. My first attempt simply placed a limit order in the opposite direction, at the same price as the target for the original order. Looks like Ninja doesn't fill my limit when the target is hit. Reading the manual, seems I can't use that approach. What do you recommend to reverse a position, at the same price as the target, when the target is hit?
    Thanks.

    #2
    Correct TheWolf, the managed approach order handling rules would prevent that in your scenario - for a reverse the easiest would be submitting a market order then, but if you would like to use a limit order for specifying a price - then wait until the target is filled and submit it then. This could leave you unfilled however of course and you might need to chase the entry by modifying the limit price then. Outside the managed mode the direct reverse qty could be issued (Unmanaged = true).
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hello Bertrand,
      Still trying to get my reverse order to work. Again, I want to reverse an existing position, when the target it hit. I first tried setting a limit order in OnExecution() with the following code:

      if (targetOrder == execution.Order && reverseCondition)
      entryOrder = EnterLongLimit(0,true,NumContracts,revOrderPrice,m yLong);

      That didn't work, so I simply set a boolean var in OnExecution() then called EnterLongLimit in OnBarUpdate() when the var is true. Looking at the executions today, the limit will get placed, but several minutes after the fact of my initial target getting filled. I've got CalculateOnBarClose=false, so code gets executed on every tick. Two questions:

      1) Should I be able to place a limit reverse order in OnExecution upon fill of the initial target?

      2) If "No" to the above question, why is it taking so long for my limit to get placed in OnBarUpdate? It should be the next tick after the initial target is filled.

      Thanks!

      Comment


        #4
        Originally posted by TheWolf View Post
        Hello,
        My strategy is working great using IOrder objects to place limit orders, then using OnExecution() to set stops and targets when filled. I have a scenario where I want to reverse a position when the target is hit for a filled order. My first attempt simply placed a limit order in the opposite direction, at the same price as the target for the original order. Looks like Ninja doesn't fill my limit when the target is hit. Reading the manual, seems I can't use that approach. What do you recommend to reverse a position, at the same price as the target, when the target is hit?
        Thanks.
        Use 'Unmanaged' orders and enter a limit order in the opposite direction with twice the contract/share size.

        Comment


          #5
          Originally posted by TheWolf View Post
          Hello Bertrand,
          Still trying to get my reverse order to work. Again, I want to reverse an existing position, when the target it hit. I first tried setting a limit order in OnExecution() with the following code:

          if (targetOrder == execution.Order && reverseCondition)
          entryOrder = EnterLongLimit(0,true,NumContracts,revOrderPrice,m yLong);

          That didn't work, so I simply set a boolean var in OnExecution() then called EnterLongLimit in OnBarUpdate() when the var is true. Looking at the executions today, the limit will get placed, but several minutes after the fact of my initial target getting filled. I've got CalculateOnBarClose=false, so code gets executed on every tick. Two questions:

          1) Should I be able to place a limit reverse order in OnExecution upon fill of the initial target?

          2) If "No" to the above question, why is it taking so long for my limit to get placed in OnBarUpdate? It should be the next tick after the initial target is filled.

          Thanks!
          Hello,

          Yes, you can EnterLongLimit in OnExecution.
          MatthewNinjaTrader Product Management

          Comment


            #6
            Reverse order with OnExecution

            Hallo,
            after entering a position on the breakout of a MAX level, I want to reverse this position in case the trend is inverting and going below a MIN value.

            After entering the position, I use OnExecution to place the EnterShortStop order for the reversal in case the MIN level is hit.

            The problem I have is that OnExecution is entering correctly the Sell and SellShort orders at the Stop level, but the Sell Short order to reverse the position is immediately canceled (while the Sell keeps working): I do not understand why this is happening. Could you help me please?

            Here below the code.
            Thank you in advance
            Martin

            public class Reverse : Strategy
            {
            #region Variables
            private IOrder entrylong = null;
            private IOrder entryshort = null;
            #endregion

            protected override void Initialize()
            {

            ExitOnClose =
            true;

            }
            protected override void OnBarUpdate()
            {
            if (BarsInProgress == 0)
            {


            if (Position.MarketPosition == MarketPosition.Flat)
            {
            if (Close[0] >= MAX(High, 5)[1])
            {
            entrylong = EnterLong(
            1);
            }
            }
            }
            }

            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


              #7
              Hello marticora,
              Thank you for your post.
              If you are using exit orders(for example ExitLongLimit) for your position or are using the Set order methods (for example SetStopLoss) than the EnterShortStop order would be ignored as this breaks the internal order handling rules with managed orders. The rules that this would be going against would be Methods that generate orders to enter a position will be ignored if position is open and an order submitted by an exit method (ExitLongLimit() for example) is active and the order is used to open a position in the opposite direction or a position is open and an order submitted by a set method (SetStopLoss() for example) is active and the order is used to open a position in the opposite direction
              For more information on the internal order handling rules for managed orders please see the following link: http://ninjatrader.com/support/helpG...d_approach.htm

              To have a reverse strategy like this you would need to use Unmanaged Orders. For more on using Unmanaged Orders please see the following link: http://ninjatrader.com/support/helpG...d_approach.htm
              Cody B.NinjaTrader Customer Service

              Comment


                #8
                Hallo CodyB,
                I understand that I cannot have an exit or set order and an opposite stop order at the same time.

                If you look at my code, nevertheless, this is not the case.
                After the EnterLong order is triggered, I want to place with OnExecution a stop order for reverting to short if the price falls. No other order is working.

                This is not against the internal logic as it works perfectly putting the same code under OnBarUpdate: it is just not correctly working with OnExecution.

                Could I please ask you to have a look at my code below and see what is wrong with it.
                Thank you
                Martin

                Comment


                  #9
                  Hello,
                  The execution event is only called once and then the IOrder your reverse order is being cancelled. If using OnExecution in this manner the Close Order cancels the short order. The only way around this would be to submit the reverse order in OnBarUpdate like so:
                  if(Close[0] >= MAX(High, 5)[1] && Position.MarketPosition == MarketPosition.Flat)
                  myEntry = EnterLong("long");
                  else
                  myReverse = EnterShortStop(0, true, 1, MIN(Low, 5)[0], "short");
                  Cody B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi,
                    yes with OnBarUpdate it is working fine. The problem is that e.g. with 30 minutes bars, I have to wait for 30 minutes to get the position to be protected, what is not acceptable.
                    This is why I want to use OnExecution.

                    Could eventually another method like OnOrderUpdate or OnPositionUpdate solve the issue to place the reverse to short order immediately after the long entry is triggered?
                    Or could it make sense to call OnBarUpdate to place the ExitLongStop order and then OnExecution to set the EnterShort Order?
                    Thank you

                    Comment


                      #11
                      ... sorry, mistake in my last sentence, I wanted to write OnOrderUpdate and OnExecution

                      Comment


                        #12
                        Originally posted by marticora View Post
                        Or could it make sense to call OnBarUpdate to place the ExitLongStop order and then OnExecution to set the EnterShort Order?
                        I would run it this way as current the Close Position order comes through OnExecution and invalidates the Short entry placement and that is why it cancels.

                        Comment


                          #13
                          Can this be done using the Strategy Wizard?

                          GuppyDRV

                          Comment


                            #14
                            Hello,
                            You could check the position in the Strategy Wizard and submit your orders and submit new orders through the strategy wizard. All this would be processed within OnBarUpdate().

                            Please let me know if you have any further questions.
                            Cody B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by bmartz, 03-12-2024, 06:12 AM
                            5 responses
                            32 views
                            0 likes
                            Last Post NinjaTrader_Zachary  
                            Started by Aviram Y, Today, 05:29 AM
                            4 responses
                            12 views
                            0 likes
                            Last Post Aviram Y  
                            Started by algospoke, 04-17-2024, 06:40 PM
                            3 responses
                            28 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by gentlebenthebear, Today, 01:30 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by cls71, Today, 04:45 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X