If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Strategy Does Not Reverse When Using StopLoss/TrailStop

10-21-2018, 01:02 PM

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?

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

Comment

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.

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.

Comment

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().

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates.

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.