Trend Following Trading Strategy Based on MACD and RSI

Author: ChaoZhang, Date: 2024-01-23 12:03:23



This strategy calculates the MACD and RSI indicators to identify trend directions and overbought/oversold situations for trend following trading. It is suitable for medium-to-long term trading, filtering out false breakouts effectively and establishing positions at early trend development, locking in profits later with trailing stop loss.


The strategy mainly utilizes the MACD and RSI indicators to generate trading signals.

MACD stands for Moving Average Convergence Divergence. It consists of the DIFF line, DEA line and histogram. In this strategy, DIFF is the difference between 5-day EMA and 13-day EMA of the closing price, while DEA is the 5-day EMA of DIFF. The buy and sell signals are generated when DIFF crosses above and below DEA respectively.

RSI stands for Relative Strength Index. It reflects overbought/oversold situations by comparing the average gains and losses over a period. This strategy sets the RSI period as 14. RSI above 70 suggests overbought conditions while below 30 oversold.

By combining the MACD trading signals and RSI filters, the strategy goes long when MACD gives buy signals and RSI is not overbought. It goes short when MACD sells and RSI not oversold.

In addition, the strategy checks if the current bar’s color differs from the previous one, skipping the signal if same color to avoid false breakout.

After entry, the strategy anticipates the next bar’s closing price to be higher/lower than open price to validate the trend, closing position for profit if the condition is met.


  • MACD signals and RSI filters effectively locate trend direction, avoiding unnecessary losses from false breakouts
  • The trailing stop loss design locks in profits, preventing pullbacks from erasing gains
  • The integration of trending and oscillating indicators realizes both trend following and reversal prevention

Risks & Solutions

The main risks of this strategy include:

  1. MACD may generate excessive noise and lead to over-trading. Solution: Optimize MACD parameters to smooth the curve.

  2. Improper RSI filter settings may cause missing trades. Solution: Test more appropriate RSI periods.

  3. Improper stop loss placement may stop out prematurely or too loosely. Solution: Adjust based on market volatility and personal risk preference.

  4. Extreme price swings may result in huge losses in short term. Solution: Hedge with options or other instruments.

Optimization Directions

The strategy can be improved in the following aspects:

  1. Optimize MACD parameters to reduce noisy signals

  2. Enhance RSI filter for better effectiveness

  3. Test other confirming indicators like KD, Bollinger Bands etc

  4. Implement dynamic trailing stop loss

  5. Utilize machine learning for parameter optimization

  6. Incorporate stock index futures, options for hedging


This strategy combines MACD and RSI for trend identification, overbought/oversold filtering and trailing stop loss, effectively controlling trading risks. Much room remains for improving performance by parameter tuning, new indicator adoption etc.

start: 2023-01-16 00:00:00
end: 2024-01-22 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

strategy("Al-Sat Sinyali ve Teyidi", overlay=true)

// MACD (Hareketli Ortalama Yakınsaklık Sapma)
[macdLine, signalLine, _] = ta.macd(close, 5, 13, 5)

// RSI (Göreceli Güç Endeksi)
rsiValue = ta.rsi(close, 14)

// RSI Filtresi
rsiOverbought = rsiValue > 70
rsiOversold = rsiValue < 30

// MACD Sinyalleri
buySignalMACD = ta.crossover(macdLine, signalLine) and not rsiOverbought
sellSignalMACD = ta.crossunder(macdLine, signalLine) and not rsiOversold

// Al-Sat Stratejisi
if (buySignalMACD and close[1] != close) // Al sinyali ve bir önceki mumdan farklı renkte ise
    strategy.entry("Buy", strategy.long)

if (sellSignalMACD and close[1] != close) // Sat sinyali ve bir önceki mumdan farklı renkte ise
    strategy.entry("Sell", strategy.short)

// Teyit için bir sonraki mumu bekleme
strategy.close("Buy", when=ta.crossover(close, open))
strategy.close("Sell", when=ta.crossunder(close, open))

// Varsayımsal bir sonraki mumun kapanış fiyatını hesapla
nextBarClose = close[1]
plot(nextBarClose,, linewidth=2, title="Tahmin Edilen Kapanış Fiyatı")

// Görselleştirmeyi devre dışı bırakma

// Al-Sat Etiketleri
plotshape(series=buySignalMACD, title="Al Sinyali",, style=shape.triangleup, location=location.belowbar, size=size.small, text="Al")
plotshape(series=sellSignalMACD, title="Sat Sinyali",, style=shape.triangledown, location=location.abovebar, size=size.small, text="Sat")