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

Strategy Does Not Reverse When Using StopLoss/TrailStop

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

    Strategy Does Not Reverse When Using StopLoss/TrailStop

    Hello.

    Why does my strategy not reverse positions as soon as I put in StopLoss or TrailStop methods? The active position has to be closed by either two methods before a new entry can take place.
    How do I solve this?

    #2
    Hello otislauwaert,

    When using the managed approach with SetProfitTarget() / SetStopLoss() NinjaTrader will not reverse the position. Instead this is an exit.

    If you want to reverse the position, call an entry order in the opposite direction of the position without calling an exit method first. For example if the position is long then call EnterShort(). NinjaTrader will automatically send a Close position order and then send another order as the entry order to enter the opposite direction.

    As a heads up, calling an exit order and an entry order on the same pass of OnBarUpdate will result in either the strategy position entering the opposite position with double the quantity or will result in an Overfill.

    From the help guide:
    "An overfill is categorized as when an order returns a "Filled" or "PartFilled" state after the order was already marked for cancellation. The cancel request could have been induced by an explicit CancelOrder() call, from more implicit cancellations like those that occur when another order sharing the same OCO ID is filled, or from things like order expirations."
    http://www.ninjatrader.com/support/h...reoverfill.htm
    This means that if you are attempting to cancel an order that filled, part filled, or already cancelled you will get this message.

    This includes situations where this can occur as well. The most common reason this error appears is because an exit and entry are both submitted on the same bar.
    When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.
    If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.
    The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.

    Another common reason for overfills is using multiple exits on a single entry, for example a stop loss and an exit order and both exits fill.
    When the exit order fills, NinjaTrader will attempt to close the position and cancel any orders attached to the entry. This will cause the stop loss to be attempted to be cancelled. If the stop loss fills before it is cancelled, an overfill will occur. The opposite is also true. If the stop loss fills, this will attempt to cancel exit order. If the exit order fills before it is cancelled an overfill will occur.

    This is not elaborated on in the help guide but the help guide does say "Entry methods are used to submit orders that create a market position if none exists or to reverse an existing position". A hint that calling an Entry method when there is already a position, will reverse the position. This is in the Entry Methods section in the link below.
    http://www.ninjatrader.com/support/h...d_approach.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your detailed reply.

      Does the same logic from NT7 take place in NT8, or is there a solution for this problem in the new version?

      Comment


        #4
        Hello otislauwaert,

        The behavior in NinjaTrader 8 with stop losses and profit targets is the same as NinjaTrader 7.

        A stop loss is not intended to reverse a position. A stop loss is intended to exit a position.

        If you want to reverse using the managed approach, place an entry order in the opposite direction without using a stop loss as advised in post #2.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          I am not trying to use a StopLoss to reverse a position. I am trying to use a StopLoss to set a BreakEven trigger. My code will place a StopLoss on the Position.AvgPrice + 1 * TickSize when the Close[0] > Position.AvgPrice + 10 * TickSize. But what happens is that the StopLoss is active even before the condition is met to place the StopLoss and this causes any other reverse condition to be ignored for some reason.

          Comment


            #6
            Hello otislauwaert,

            Is an order being placed in the opposite direction while a stop loss is working?
            If yes, this violates the internal order handling rules.
            Below is a public link to the help guide.


            When using the managed approach, it would be needed to either use an exit order and cancel it then place the entry order in the opposite direction to reverse the position, or continue using the stop loss and wait until it fills before placing an order to reverse the position.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea, thank you for your reply.

              How do I reference a SetStopLoss order with the CancelOrder() method? Do I assign an IOrder to the SetStopLoss? Or how?

              Comment


                #8
                Hello otislauwaert,

                The Set methods are not intended to be cancelled. If you need to cancel an order please use an exit method such as ExitLongStopMarket() / ExitShortStopMarket() and in OnOrderUpdate assign the order object to an Order variable. Then supply the order variable to CancelOrder().

                Below are public links to the help guide.


                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chelsea,

                  Thank you for your assistance. All the problems are now solved. I hope this thread will be useful for others as well.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by funk10101, Today, 12:02 AM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_LuisH  
                  Started by GLFX005, Today, 03:23 AM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by nandhumca, Yesterday, 03:41 PM
                  1 response
                  12 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by The_Sec, Yesterday, 03:37 PM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by vecnopus, Today, 06:15 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post vecnopus  
                  Working...
                  X