Moving Average Crossover Long-Short Reversal with Breakeven and ATR-Based Stop Loss, Trailing Stop, & Profit Target (W Exit Order Fxns) [Algo File Only] (Point-and-Click NinjaTrader® Strategy)
Original Trading Strategy
Peer Reviews, Modifications & Responses
Thanks for Sharing!
Trading Strategy Summary
This strategy is a moving average crossover strategy that goes long when a fast moving average crosses above a slow moving average and goes short when the fast moving average crosses below the slow moving average.
If the strategy is currently long and the fast moving average crosses below the slow moving average, it will reverse its position from long to short by exiting the long position and entering a short position.
If the strategy is currently short and the fast moving average crosses above the slow moving average, it will reverse its position from short to long by exiting the short position and entering a long position.
The strategy also exits its long (or short) position via a profit target at a specified number of 14-Period ATRs above (or below if short) the average fill price of the position.
The strategy also exits its long (or short) position via a stop loss at a specified number of 14-Period ATRs below (or above if short) the average fill price of the position.
The strategy also exits its long or short position via a breakeven stop at a specified number of ticks above or below the average fill price of the position.
The strategy also exits its long (or short) position via a trailing stop at a specified number of 14-Period ATRs below (or above if short) the highest (or lowest if short) close price achieved since entry of the position. Note that the highest (or lowest if short) close price achieved depends on the value of the strategy’s “Calculate” field in the NinjaTrader® user interface. If the value of the strategy’s “Calculate” field is “On Bar Close”, the highest (or lowest if short) close price achieved is the highest (or lowest if short) close price of completed bars since entry. If the strategy’s “Calculate” field is something other than “On Bar Close”, such as “On Each Tick” or “On Price Change”, then the highest (or lowest) close price achieved is the highest (or lowest if short) close price encountered by all executions of the trading logic in the “When Bar Updates” section, which makes it possible to determine the highest high (or lowest low if short) price since entry. Note that the strategy’s “Calculate” field is always equal to “On Bar Close” in the NinjaTrader® Strategy Analyzer, but on a live chart the strategy’s “Calculate” field can be set to something other than “On Bar Close” from the drop-down list in the NinjaTrader® user interface. The default value for this drop-down list can be set from the “Calculate” drop-down list in this Quagensia N Edition Strategy’s “Strategy Details” section.
When a position is opened by this strategy, the strategy runs in one of three protective stop “states”:
1. When a position is opened, it starts in the “stop loss” state, during which time a stop loss is placed. This state lasts for at least one “When Bar Updates” execution, i.e. it will not be skipped.
2. When a position is in the “stop loss” state and the close price reaches a value that is greater than or equal to a “breakeven trigger price” (or less than or equal to a “breakeven trigger price” if short), the strategy enters the “breakeven” state, during which time a breakeven stop is placed a specified number of ticks above or below breakeven. You can set the “Breakeven Stop Price Offset (Nbr of Ticks)” input parameter to 0 to cause the breakeven stop to be placed at breakeven, i.e. the average fill price for the position. If this state is entered at all, it lasts for at least one “When Bar Updates” execution, i.e. it will not be skipped.
3. When a position is in the “breakeven” state and the close price reaches a value such that the trailing stop price is greater than the breakeven stop price (or less than the breakeven stop price if short), the strategy enters the “trailing stop” state, during which time a trailing stop is placed a specified number of 14-Period ATRs below (or above if short) the current close price, but not less than (or not greater than if short) any previous trailing stop price for the same position.
This strategy’s money management logic, namely the profit target, stop loss, breakeven stop, and trailing stop logic, are done via NinjaTrader® “Exit Entire Long Position Using A Limit Order (By Entry Signal Name)” and “Exit Entire Long Position Using A Stop Order (By Entry Signal Name)” functions for long positions, and “Exit Entire Short Position Using A Limit Order (By Entry Signal Name)” and “Exit Entire Short Position Using A Stop Order (By Entry Signal Name)” functions for short positions. These functions are found in the “Submit an Order > Exit Long” and “Submit an Order > Exit Short” subfolders of the Action context menu. Such “Exit Order” syntax for setting protective stops via stop orders and setting profit targets via limit orders is incompatible with the money management functions in the “Money Management” subfolder of the Action context menu because these two methods of placing stop and limit orders from a NinjaTrader® strategy are incompatible with each other.
You can learn more about the incompatibility of these two methods of setting protective stops via stop orders and setting profit targets via limit orders on the “Managed Approach” documentation page in NinjaTrader®’s help guide at https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?managed_approach.htm under the heading “Internal Order Handling Rules that Reduce Unwanted Positions”.
WARNING: If you set the input parameters of this strategy to values that cause your profit target’s limit order or protective stop’s stop order to be submitted to the market at prices that are very close to the last known market prices (last, bid, and ask), or a stop order for a trailing stop is submitted or re-submitted to the market at prices that are very close to the last known market prices, the orders may be submitted at prices that are not valid by the time the orders are received by the broker, gateway, or exchange. This may cause your strategy to send cancel requests, attempt to close the position, and terminate itself. Since this strategy uses actual stop orders and limit orders, this situation will arise regardless of what input parameters are used. If this is unacceptable to you, please do not use this strategy or its logic, and instead reproduce the logic in this strategy using market orders that simulate stop orders and limit orders. This strategy is provided as-is as an educational example only. If you encounter problems due to these expected order rejections, be it in a backtest or in actual trading, Quagensia support will not be able to help you troubleshoot this known, documented behavior. Here is an example of an unavoidable warning message you may get when you use this strategy: “Strategy ‘Q_MvgAvgCrossLSRevWBEATRSLTraiLSPT_WExitOrders/-1’: An order has been ignored since the stop price ‘177.12’ near the bar stamped ‘11/08/2013 15:00:00’ is invalid based on the price range of the bar. This is an invalid order and subsequent orders may also be ignored.”
WARNING: Your positions are not protected with a protective stop the moment they are entered. The soonest a stop loss could possibly be submitted by this strategy is when a “When Bar Updates” event occurs, i.e. the NinjaTrader® strategy’s “OnBarUpdate()” function is called, at a point where the strategy believes its current market position is long or short for the first time for the new position, after an entry order is sent to the market, and no earlier than the “When Bar Updates” event directly after the “When Bar Updates” event that submits the entry order. If the value of the strategy’s “Calculate” field in the NinjaTrader® user interface is “On Bar Close”, this delay could even be for an entire bar, i.e. your position may not be protected for the entirety of the entry bar. The preceding discussion goes for profit targets as well, i.e. the soonest a limit order could possibly be submitted is as was described for the initial protective stop order. If you encounter problems due to this expected delay of submitting a protective stop or profit target’s limit order, be it in a backtest or in actual trading, Quagensia support will not be able to help you troubleshoot this known, documented behavior.
WARNING: The red diamonds representing the stop losses, the orange diamonds representing the breakeven stops, the yellow diamonds representing the trailing stops, and the green diamonds representing profit targets are drawn on the bar upon which they are calculated and submitted via exit orders, which will always be at least one bar earlier than the bar upon which the submitted stop and limit exit orders are actually working in the market when the strategy’s “Calculate” field in the NinjaTrader® user interface is “On Bar Close”. In the NinjaTrader® Strategy Analyzer, this will always be the case, and the diamonds will be drawn exactly one bar prior to the bar upon which the submitted stop and limit exit orders are actually working in the simulated market (assuming the simulated market accepts a stop or limit order rather than rejecting it for reasons including the reason described in a warning above). If you use this strategy or its trading logic on a live chart, the diamonds could be drawn on the same bar as the bar upon which the submitted stop and limit exit orders are actually working in the market if the “Calculate” field in the NinjaTrader® user interface is something other than “On Bar Close”, such as “On Each Tick” or “On Price Change”, and there is one more “On Each Tick” or “On Price Change” event occurring after the “On Each Tick” or “On Price Change” event that caused the strategy’s protective stop and limit order exit logic to be executed and the corresponding diamonds to be drawn. Note also that the diamonds will be drawn even if the stop or limit order is rejected. The diamonds represent the calculated values for profit targets, stop losses, breakeven stops, and trailing stops as calculated on a given bar and are not guaranteed to represent a corresponding stop or limit order in the market on the bar upon which the diamonds are drawn.
Copyright © 2023 Quagensia, Inc. All rights reserved.
How to Get the Quagensia Strategies
This point-and-click trading strategy is built into Quagensia N Edition (for NinjaTrader®).
Customize Quagensia Strategies with Your Own Ideas
Tweaking Quagensia Trading Strategies using point-and-click with the Quagensia Desktop Application is easy for non-programmers. If you get stuck you can usually find the answer you need in our online help documentation or you can ask a question in our friendly Discord community.
If you don’t want to tweak a Quagensia Trading Strategy with point-and-click to add your own proprietary trading logic, you can still download a Quagensia Trading Strategy file, open it up in the Quagensia Desktop Application, and generate its code, then backtest and optimize the trading strategy on different instruments, different bar periods (weekly bars, daily bars, hourly bars, 15-minute bars, etc.), or different bar types (time-based bars, volume-based bars, tick-based bars, etc.), and use different start and end dates.
Some tweaks you can make to the Quagensia Trading Strategies you download include:
Modify the entry & exit logic.
- Add more conditions, remove conditions, or change them by choosing from among a very large number of components, including many exotic indicators.
- Add or modify stop losses, trailing stops, and profit targets. Make them tighter, less tight, or based on an entirely different calculation.
- Add or modify time stops. For example, exit after a certain number of bars either unconditionally or only if the post-entry price action did or did not exhibit certain characteristics.
Enhance the output of the strategy to go beyond simply placing orders.
- Draw lines, shapes, and text on the chart. For instance, you can mark times or prices where each entry or exit condition of a multi-condition entry or exit was true, even if all the necessary entry or exit conditions were not true at the same time so an entry or exit did not occur.
- Write information to NinjaTrader®’s NinjaScript® Output window or TradeStation®’s EasyLanguage® Print Log window.
- Write information to a file. You can even output a report that can be opened in Microsoft Excel or consumed by another application that reads comma-delimited, semicolon-delimited, or otherwise character-delimited tabular data files.
![Moving Average Crossover Long-Short Reversal with Breakeven and ATR-Based Stop Loss, Trailing Stop, & Profit Target (W Exit Order Fxns) [Algo File Only] (Point-and-Click NinjaTrader® Strategy) Moving Average Crossover Long-Short Reversal with Breakeven and ATR-Based Stop Loss, Trailing Stop, & Profit Target (W Exit Order Fxns) [Algo File Only] (Point-and-Click NinjaTrader® Strategy)](https://eadn-wc02-5937971.nxedge.io/images/trading-idea-images/2023/11/02/quagensia/ma-crossover-long-short-reversal-with-breakeven-atr-stop-loss-trailing-stop-profit-target-efxns-n.png)

![Screenshot for point-and-click trading strategy "Moving Average Crossover Long-Short Reversal with Breakeven and ATR-Based Stop Loss, Trailing Stop, & Profit Target (W Exit Order Fxns) [Algo File Only]" Screenshot for point-and-click trading strategy "Moving Average Crossover Long-Short Reversal with Breakeven and ATR-Based Stop Loss, Trailing Stop, & Profit Target (W Exit Order Fxns) [Algo File Only]"](https://eadn-wc02-5937971.nxedge.io/images/trading-idea-images/2023/11/02/quagensia/ma-crossover-long-short-reversal-with-breakeven-atr-stop-loss-trailing-stop-profit-target-efxns-n-full-1.png)