OPEN-SOURCE SCRIPT

Moving Average Crossover Swing Strategy

Updated
Moving Average Crossover Swing Strategy

**Overview:**
The basic concept of this strategy is to generate a signal when a faster/shorter length moving average crosses over (for Longs) or crosses under (for Shorts) a medium/longer length moving average. All of which are customizable. This strategy can work on any timeframe, however the daily is the timeframe used for the default settings and screenshots, as it was designed to be a multi-day swing strategy. Once a signal has been confirmed with a candle close, based on user options, the strategy will enter the trade on the open of the next candle.

The crossover strategy is nothing new to trading, but what can make this strategy unique and helpful, is the addition of further confirmation points, ATR based stop loss and take profit targets, optional early exit criteria, customizable to your needs and style, and just about everything visual can be toggled on/off. This strategy is based on a Trend (MA) indicator and a Momentum (MACD) indicator. While a Volume-based indicator is not shown here, one could consider using their favorite from that category to further compliment the signal idea.

It should be noted that depending on the time frame, direction(s) chosen, the signal options, confirmation options, and exit options selected, that a ticker may not produce more than 100 trades on the back test. Depending on your style and frequency, one could consider adjusting options and/or testing multiple tickers. It should also be noted that this strategy simply tests the underlying stock prices, not options contracts. And of course, testing this strategy against historical data does not assume that the same results will occur in future price action.


Shoutout given to Ripster's Clouds Indicator as pieces of that code were taken and modified to create both the Cloud visualization effects, and the Moving Average Pair Plots that are implemented in this strategy.


BASIC DEFAULTS
All can be changed as normal
  • Initial capital = 10,000
  • Order Sizing = 25% of equity (use the "Inputs" tab to modify this)
  • Pyramiding = 0
  • Commission = 0.65 USD per order
  • Price Verification = 1 tick
  • Slippage = 1 tick


RISK MANAGMENT
You will notice two different percentage options and ATR multipliers. This strategy will adjust position sizing by not exceeding either one of those % values based on the ATR (Average True Range) of the symbol and the multipliers selected, should the stock hit the stop loss price.

For Example, lets assume these values are true:
  • Account size = $10,000,
  • Max Risk = 1% of account size
  • Max Position Size = 25% of the account size
  • Stock Price = 23.45
  • ATR = 3.5
  • ATR Stop Loss Multiplier = 1.4


Then the formulas would be:


The minimum value of each of those options is then used, which in this case would be to purchase 20 shares so as not to exceed the max dollar risk should the stock reach the stop loss target. Likewise, if the ATR were to be much lower, say 0.48 cents, and all else the same, then the strategy would purchase the 106 shares based on Max Equity Allocation because the Max Cash Risk would require 149.25 shares.

MOVING AVERAGE OPTIONS
  • Select between and change the length & type of up to 5 pairs (10 total) of moving averages
  • The "Show Cloud-x" option will display a fill color between the "a" and "b" pairs
  • All moving averages lines can be toggled on/off in the "Style" tab, as well as adjusting their colors.
  • Visualization features do not affect calculations, meaning you could have all or nothing on the chart and the strategy will still produce results


SIGNAL CHOICES
  • Choose the fast/shorter length MA and the medium/longer length MA to determine the entry signal


CONFIRMATION OPTIONS
Both of these have customizable values and can be toggled on/off
  • A candle close over a slower/much longer length moving average
  • An additional cross-over (cross-under for Shorts) on the MACD indicator using default MACD values. While the MACD indicator is not necessary to have on the chart, it can help to add that for visualization. The calculations will perform whether the indicator is on the chart or not.


EARLY EXIT CRITERIA
  • Both can be toggled on/off with customizable values
  • MA Cross Exit will exit the trade early if the select moving averages cross-under (for longs) or cross-over (for shorts), indicating a potential reversal.
  • Max Bars in Trades will act as a last-resort exit by simply calculating the amount of full bars the trade has been open, and exiting on the opening of the next bar. For example: the default value is 8 bars, so after 8 full bars in the trade, if no other exit has been triggered (Stop Loss, Take Profit, or MA Cross(if enabled)), then the trade will exit at the opening of the 9th bar.
  • Finally, there is a table displaying the amount of trades taken for each side, and the amount & percent of both early exits. This table can be turned off in the "Style" tab


ADDITIONAL PLOTS
MACD (Moving Average Convergence/Divergence):
- The MACD is an optional confirmation indicator for this strategy.
- Plotting the indicator is not necessary for the strategy to work, but it can be helpful to visually see the status and position of the MACD if this feature is enabled in the strategy
- This helps to identify if there is also momentum behind the entry signal



snapshot
Release Notes
Moving Average Crossover Swing Strategy v2

**Overview:**
The basic concept of this strategy is to generate a signal when a faster/shorter length moving average crosses over (for Longs) or crosses under (for Shorts) a medium/longer length moving average. All of which are customizable. This strategy can work on any timeframe, however the daily is the timeframe used for the default settings and screenshots, as it was designed to be a multi-day swing strategy. Once a signal has been confirmed with a candle close, based on user options, the strategy will enter the trade on the open of the next candle.

The crossover strategy is nothing new to trading, but what can make this strategy unique and helpful, is the addition of further confirmation points, ATR based stop loss and take profit targets, optional early exit criteria, customizable to your needs and style, and just about everything visual can be toggled on/off. This strategy is based on a Trend (MA) indicator and a Momentum (MACD) indicator. While a Volume-based indicator is not shown here, one could consider using their favorite from that category to further compliment the signal idea.

It should be noted that depending on the time frame, direction(s) chosen, the signal options, confirmation options, and exit options selected, that a ticker may not produce more than 100 trades on the back test. Depending on your style and frequency, one could consider adjusting options and/or testing multiple tickers. It should also be noted that this strategy simply tests the underlying stock prices, not options contracts. And of course, testing this strategy against historical data does not assume that the same results will occur in future price action.


Shoutout given to Ripster's Clouds Indicator as pieces of that code were taken and modified to create both the Cloud visualization effects, and the Moving Average Pair Plots that are implemented in this strategy.


BASIC DEFAULTS
All can be changed as normal
  • Initial capital = 10,000
  • Order Sizing = 25% of equity (use the "Inputs" tab to modify this)
  • Pyramiding = 0
  • Commission = 0.65 USD per order
  • Price Verification = 1 tick
  • Slippage = 1 tick


RISK MANAGMENT
You will notice two different percentage options and ATR multipliers. This strategy will adjust position sizing by not exceeding either one of those % values based on the ATR (Average True Range) of the symbol and the multipliers selected, should the stock hit the stop loss price.

For Example, lets assume these values are true:
  • Account size = $10,000,
  • Max Risk = 1% of account size
  • Max Position Size = 25% of the account size
  • Stock Price = 23.45
  • ATR = 3.5
  • ATR Stop Loss Multiplier = 1.4


Then the formulas would be:


The minimum value of each of those options is then used, which in this case would be to purchase 20 shares so as not to exceed the max dollar risk should the stock reach the stop loss target. Likewise, if the ATR were to be much lower, say 0.48 cents, and all else the same, then the strategy would purchase the 106 shares based on Max Equity Allocation because the Max Cash Risk would require 149.25 shares.

MOVING AVERAGE OPTIONS
  • Select between and change the length & type of up to 5 pairs (10 total) of moving averages
  • The "Show Cloud-x" option will display a fill color between the "a" and "b" pairs
  • All moving averages lines can be toggled on/off in the "Style" tab, as well as adjusting their colors.
  • Visualization features do not affect calculations, meaning you could have all or nothing on the chart and the strategy will still produce results


SIGNAL CHOICES
  • Choose the fast/shorter length MA and the medium/longer length MA to determine the entry signal


CONFIRMATION OPTIONS
Both of these have customizable values and can be toggled on/off
  • A candle close over a slower/much longer length moving average
  • An additional cross-over (cross-under for Shorts) on the MACD indicator using default MACD values. While the MACD indicator is not necessary to have on the chart, it can help to add that for visualization. The calculations will perform whether the indicator is on the chart or not.


EARLY EXIT CRITERIA
  • Both can be toggled on/off with customizable values
  • MA Cross Exit will exit the trade early if the select moving averages cross-under (for longs) or cross-over (for shorts), indicating a potential reversal.
  • Max Bars in Trades will act as a last-resort exit by simply calculating the amount of full bars the trade has been open, and exiting on the opening of the next bar. For example: the default value is 8 bars, so after 8 full bars in the trade, if no other exit has been triggered (Stop Loss, Take Profit, or MA Cross(if enabled)), then the trade will exit at the opening of the 9th bar.
  • Finally, there is a table displaying the amount of trades taken for each side, and the amount & percent of both early exits. This table can be turned off in the "Style" tab


ADDITIONAL PLOTS
MACD (Moving Average Convergence/Divergence):
- The MACD is an optional confirmation indicator for this strategy.
- Plotting the indicator is not necessary for the strategy to work, but it can be helpful to visually see the status and position of the MACD if this feature is enabled in the strategy
- This helps to identify if there is also momentum behind the entry signal



Release Notes
Minor code update: The fast and medium signals would not work correctly if using the "MA 3b".
Release Notes
A minor visual update
Release Notes
-Updated color schemes and changed name
-Misc Fixes
bearcrossbullcrosscrossovercrossunderExponential Moving Average (EMA)Moving Average Convergence / Divergence (MACD)Simple Moving Average (SMA)swing

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Also on:

Disclaimer