This is one trend following systems that's fairly easy to use, it will allow you to avoid ranging markets and only take a trade when a strong trend has been confirmed. Expect the system to give you lagging buy and sell signals since momentum and trend can only be confirmed after it has run up/down a bit. Also expect trading signals to be fewer in frequency but should be of higher quality, meaning there are fewer false signals.
Trading Rules
Buy - if MACD cross is above the zero line and Stochastics is moving up. Stochastics is only used for confirmation.
Sell - if MACD crosses down, regardless of other factors. For some volatile stocks, I would usually use a stop loss limit based on ATR to protect positions (I did not include the ATR in the script below).
As always, back test trading strategies before using them live and understand all the trading risks involved.
// MACD Trending Strategy
// Set variables
Set MACD_line = MACD(12,26,9, exponential)
Set MACD_signal = MACDSIGNAL(12,26,9, exponential)
Set Hist = MACD_line - MACD_signal
Set StochD = SOPD(14,3,3, simple)
Set StochK = SOPK(14,3,3, simple)
// Buy if MACD cross and both MACD and signal >0 and Stochd> Stochk
Set BuySignal = Crossover( MACD_line, MACD_signal) and MACD_line > 0 and MACD_signal > 0 and (StochD > StochK)
// Sell if MACD cross down, regardless of Stoch
Set SellSignal = Crossover(MACD_signal, MACD_line)
//Plot MACD
Set MACDhistup = if(hist > 0, hist ,0)
Set MACDhistdown = if(hist < 0, hist, 0)
plot( MACD_line, line, any, MACD)
plot(MACD_signal, line, any, MACD)
plot(MACDhistup, stackedbar, green, hist)
plot(MACDhistdown, stackedbar, red, hist)
//Plot Buy and Sell Signals
plot(Buy, Buy, Green, Main)
plot(Sell, Sell, Red, Main)
//Set signals for backtesting and screener
signals(buy,sell)
//end