OPEN-SOURCE SCRIPT

AMACD - All Moving Average Convergence Divergence

Updated
This indicator displays the Moving Average Convergane and Divergence ( MACD ) of individually configured Fast, Slow and Signal Moving Averages. Buy and sell alerts can be set based on moving average crossovers, consecutive convergence/divergence of the moving averages, and directional changes in the histogram moving averages.

The Fast, Slow and Signal Moving Averages can be set to:
Exponential Moving Average ( EMA )
Volume-Weighted Moving Average ( VWMA )
Simple Moving Average ( SMA )
Weighted Moving Average ( WMA )
Hull Moving Average ( HMA )
Exponentially Weighted Moving Average (RMA) ( SMMA )
Symmetrically Weighted Moving Average ( SWMA )
Arnaud Legoux Moving Average ( ALMA )
Double EMA ( DEMA )
Double SMA (DSMA)
Double WMA (DWMA)
Double RMA ( DRMA )
Triple EMA ( TEMA )
Triple SMA (TSMA)
Triple WMA (TWMA)
Triple RMA (TRMA)
Linear regression curve Moving Average ( LSMA )
Variable Index Dynamic Average ( VIDYA )
Fractal Adaptive Moving Average ( FRAMA )

If you have a strategy that can buy based on External Indicators use 'Backtest Signal' which returns a 1 for a Buy and a 2 for a sell.
'Backtest Signal' is plotted to display.none, so change the Style Settings for the chart if you need to see it for testing.
Release Notes
Added a case for no signal smoothing, when signal length = 1.
Re-wrote calcMA as a switch, much more pleasing on the eye.
Removed all the security fluff which I'd never even read, "terms of the Mozilla Public License 2.0" is more comprehensive anyway.
Added check boxes to quickly disable some of the default plots.
Release Notes
Version 3.00
• Added options to generate open and close for both long and short positions. So if you are in a long position and your criteria for a negative signal is met it will close your long, if another negative signal is generated it will open a short. If you're just trading the crossover this will keep you permanently trading short or long, as it's only when there are two or more consecutive signals are generated that will move you from longs to shorts.
• Plots a Green Triangle for Open Long, a Green Square for Close Long, a Red Triangle for Open Short and a Red Square for Close Short.
• To do this I needed to track the sate of open deals. Could have done this with variables but thought plotting it may help, so "Deal State" plots a 1 if it's in a long, 0 if no deals are open and -1 if it's in a short, to display.none. Enable it in Stlye Settings if you need it for testing.
• If you'd prefer that it just worked as it did before with simple Buy / Sell signals, which makes sense if you're just trading the crossover, disable the 'Generate Close Signals for Long / Short Positions' setting.
• Now that it can generate Open Long, Close Long, Open Short and Close Short signals I added a 'Long / Short Signals' section in settings, where you can specify what signals your backtester is expecting for each state.
• Moving averages are now plotted to display.none as well in case you want to see the price and the moving averages for testing. You're much better off running my other script 'Any Ribbon' on the top pane to show the moving averages ribbon, but these options enable you to show them here if you want to.
• Added the option to buy / sell based on the moving average crossing zero, thanks Bu Bader!
Release Notes
3.01
• Added filters for MACD above and below zero.
• Added the option to buy / sell based on divergences between the price and the MACD. Thanks to Trading View for the 'Divergence Indicator' and daviddtech for his idea to use that as the basis for a MACD Divergence script.
• REMEMBER you can't trigger a divergence before a local top or bottom is CONFIRMED by closing some bars after that local top or bottom. Replay some bars to see how far after the divergence the signal will actually fire.
• Added a bunch of tool tips, I bet there are spelling mistakes in some, tell me if so.
Release Notes
3.02 • When 'Generate Close Signals for Long / Short Positions' was disabled close long was plotting as open short (-1). Fixed the logic. Thanks for the testing Bu Bader!
Release Notes
3.03 • More signal weirdness, re-wrote the logic completely.
Release Notes
3.04
• I've had requests to filter based on a few different indicators, I think that the script is getting too complicated as it is and I don't want to add a bunch of filters that only one person may use. So I added the option to filter based on the output of an external indicator. You can enable or disable trading based any indicator hitting the levels that you set. I've tested a few indicators but you need to to confirm that your indicator is filtering as you expect it to. Enable the plot of 'Trades Enabled' in Style Settings to see if trades are enabled (1) or disabled (0). I expect that this was requested to only open longs in an uptrend etc. so it may not be helpful if you want to open longs and shorts. Thanks Jason5480 for hack_utils that made this easier.
• The external filter will stop new trades starting but it won't stop any existing trades closing.
• The MACD > 0 filter may not have stopped existing deals closing, changed that so that they can close.
• Added check to do nothing when buy and sell fire in the same bar.
• Corrected signal for consecutive short orders.
HMALeast Squares Moving Average (LSMA)Moving Average Convergence / Divergence (MACD)macd-bearish-divergencemacd-bullish-divergencermaSWMA

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?

Disclaimer