Strategy · Pine Script v4 · On the chart

MACD + RSI Strategy [Alorse]

by Alorse· code credited to fredoorse

The MACD + RSI Strategy [Alorse] combines moving average convergence divergence (MACD) crossovers with recent Relative Strength Index (RSI) extreme levels to generate long trading signals overlaid directly on the price chart.

View on GitHub

GitHub stars330on the repository
Pine versionv4older, still runs
Lines of code53
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago25 Aug 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

53 lines of Pine Script v4 from strategies/momentum/MACD+RSI.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

MACD+RSI.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fredoorse

//@version=4
strategy(title="MACD + RSI Strategy [Alorse]", overlay=true, shorttitle="MACD+RSI STRTG", pyramiding=0, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

src = input(title="Source", type=input.source, defval=close)
// MACD
// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=12, group="MACD")
slow_length = input(title="Slow Length", type=input.integer, defval=26, group="MACD")
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9, group="MACD")
sma_source = input(title="Oscillator MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"], group="MACD")
sma_signal = input(title="Signal Line MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"], group="MACD")
// Calculating
fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? sma(macd, signal_length) : ema(macd, signal_length)

// RSI
len = input(14, minval=1, title="Length", group="RSI")
rsi = rsi(src, len)

// Strategy
SL = input(title="Stop Loss %", type=input.integer, defval=99, group="Strategy") * 0.01
TP = input(title="Take Profit %", type=input.integer, defval=10, group="Strategy")
rsiBack = input(title="Check RSI last # candles", type=input.integer, defval=5, group="Strategy")
rsiDown = input(title="RSI over sold level", type=input.integer, defval=30, group="Strategy")
rsiUp = input(title="RSI over bought level", type=input.integer, defval=70, group="Strategy")

wasRsiDown = false
for i = 1 to rsiBack
    if rsi[i] <= rsiDown
        wasRsiDown := true
        break

wasRsiUp = false
for i = 1 to rsiBack
    if rsi[i] >= rsiUp
        wasRsiUp := true
        break

bull_cross = crossover(macd, signal) and wasRsiDown
bear_cross = crossunder(macd, signal) and wasRsiUp
strategy.entry("Long", strategy.long, when=bull_cross)
// strategy.entry("Sell", strategy.short, when=bear_cross)
strategy.close("Long", when=bear_cross)

StopPrice  = strategy.position_avg_price * (1 - SL)

if (strategy.position_size > 0)
    strategy.exit(id="Exit", stop=StopPrice)

Overview

A long entry triggers when the MACD line crosses above its signal line while the RSI was in oversold territory within a specified lookback window. Positions close when the MACD crosses below the signal line following an overbought RSI condition or when reaching a percentage-based stop loss level. User inputs configure MACD lengths, smoothing methods, RSI thresholds, lookback duration, and stop loss percentage.

Details

Published by

Alorse on GitHub

Code credit

fredoorse, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Oscillators & momentum, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescriptpinescript-indicatorspinescript-strategiestrading-strategiestradingviewtradingview-alerttradingview-strategy

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sourcesourceclosesrc
Fast Lengthint12fast_length
Slow Lengthint26slow_length
Signal Smoothingint9signal_length
Oscillator MA TypestringEMAsma_source
Signal Line MA TypestringEMAsma_signal
Lengthint14len
Stop Loss %int99SL
Take Profit %int10TP
Check RSI last # candlesint5rsiBack
RSI over sold levelint30rsiDown
RSI over bought levelint70rsiUp
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy

This strategy combines Arnaud Legoux Moving Average percentage price change calculations with an adaptive Gaussian Moving Average to generate trend-following signals. Long and short entries are triggered when the ALMA change calculation crosses above or below

Strategy v5 Moving averagesOscillators & momentum
Updated 6 weeks ago · None stated GitHub repository of ESM2024 - ALMA Smoothed Gaussian Moving Average

MACD Strategy

by TradersPost
218 stars

This overlay strategy is built around MACD inputs for a fast average, a slow average, and a signal line. Comments say the script keeps MACD values available and can draw a histogram and zero line in a separate pane, with an optional overlay on price. Settings

Strategy v6 Oscillators & momentumMoving averages
Updated 3 months ago · None stated GitHub repository of MACD Strategy

255 EMA Strategy

by hasnocool
106 stars

255 EMA Strategy combines an exponential moving average with Average True Range bands and standard MACD crossovers to generate entry signals. Drawn directly on the price chart, it plots the central 255 EMA along with upper and lower volatility envelopes. Trade

Strategy v4 Moving averagesOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 255 EMA Strategy
104 stars

This overlay strategy combines a machine-learning distance model with a MACD oscillator to open trades on the price chart. It compares recent history using cosine similarity or Euclidean and Lorentzian distance, then filters direction with selectable moving-av

Strategy v5 AI / machine learningOscillators & momentum
Updated 3 years ago · MIT GitHub repository of MLCEL and MACD Strategy

SMA RSI Strategy

by PythonForForex
64 stars

This strategy evaluates simple moving averages and the Relative Strength Index to generate trading signals. It calculates a 10-period SMA, a 30-period SMA, and a 14-period RSI. Long positions are entered when the fast SMA crosses above the slow SMA while RSI i

Strategy v5 TrendOscillators & momentum
Updated 4 years ago · None stated GitHub repository of SMA RSI Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers