Example output
Here is a Pine Script code that implements a multi-timeframe trend reversal indicator using RSI and MACD for TradingView:
```pinescript
//@version=5
indicator("Multi-Timeframe Trend Reversal", overlay=true, shorttitle="MTF Reversal", max_lines_count=500, max_labels_count=500)
// Input parameters
// RSI settings
rsiLength = input.int(14, title="RSI Length")
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
// MACD settings
macdFastLength = input.int(12, title="MACD Fast Length")
macdSlowLength = input.int(26, title="MACD Slow Length")
macdSignalSmoothing = input.int(9, title="MACD Signal Smoothing")
// Timeframes
tf1 = input.timeframe("1H", title="Lower Timeframe")
tf2 = input.timeframe("4H", title="Higher Timeframe")
// Backtesting range
backtestPeriod = input.time(timestamp("2023-01-01 00:00"), title="Backtest Start Date", type=input.time)
// Calculate RSI for both timeframes
rsi1 = ta.rsi(request.security(syminfo.tickerid, tf1, close), rsiLength)
rsi2 = ta.rsi(request.security(syminfo.tickerid, tf2, close), rsiLength)
// Calculate MACD for both timeframes
[macdLine1, signalLine1, _] = ta.macd(request.security(syminfo.tickerid, tf1, close), macdFastLength, macdSlowLength, macdSignalSmoothing)
[macdLine2, signalLine2, _] = ta.macd(request.security(syminfo.tickerid, tf2, close), macdFastLength, macdSlowLength, macdSignalSmoothing)
// Conditions for buy/sell signals
buyCondition = rsi1 < rsiOversold and rsi2 < rsiOversold and macdLine1 > signalLine1 and macdLine2 > signalLine2
sellCondition = rsi1 > rsiOverbought and rsi2 > rsiOverbought and macdLine1 < signalLine1 and macdLine2 < signalLine2
// Plotting signals
plotshape(series=buyCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", offset=-1)
plotshape(series=sellCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", offset=-1)
// Background color for conditions
bgcolor(buyCondition ? color.new(color.green, 85) : na, title="Buy Background", editable=true)
bgcolor(sellCondition ? color.new(color.red, 85) : na, title="Sell Background", editable=true)
// Alerts
alertcondition(buyCondition, title="Buy Alert", message="Buy signal on Multi-Timeframe Trend Reversal")
alertcondition(sellCondition, title="Sell Alert", message="Sell signal on Multi-Timeframe Trend Reversal")
// Overlay RSI and MACD for visualization
rsiPlot1 = plot(rsi1, title="RSI 1H", color=color.blue, linewidth=1)
rsiPlot2 = plot(rsi2, title="RSI 4H", color=color.orange, linewidth=1)
hline(rsiOverbought, "Overbought", color=color.red, linestyle=hline.style_dotted)
hline(rsiOversold, "Oversold", color=color.green, linestyle=hline.style_dotted)
macdPlot1 = plot(macdLine1, title="MACD Line 1H", color=color.blue, linewidth=2)
signalPlot1 = plot(signalLine1, title="Signal Line 1H", color=color.orange, linewidth=1)
macdPlot2 = plot(macdLine2, title="MACD Line 4H", color=color.red, linewidth=2)
signalPlot2 = plot(signalLine2, title="Signal Line 4H", color=color.green, linewidth=1)
fill(macdPlot1, signalPlot1, color=color.new(color.blue, 90), title="MACD Fill 1H")
fill(macdPlot2, signalPlot2, color=color.new(color.red, 90), title="MACD Fill 4H")
// Backtesting logic
var float account = na
if (time >= backtestPeriod)
if buyCondition
account := na(account) ? 1 : account * (1 + close[1] / close - 1)
if sellCondition
account := na(account) ? 1 : account * (1 - close[1] / close - 1)
// Plot backtesting result
plot(account, title="Backtest Account Growth", color=color.yellow, linewidth=2)
// Add comments/documentation
/*
Multi-Timeframe Trend Reversal Indicator
----------------------------------------
This script performs multi-timeframe analysis using RSI and MACD.
The script checks for overbought/oversold conditions on RSI and convergence/divergence on MACD
across two timeframes (1H and 4H by default). Buy/Sell signals are generated based on these
conditions being met on both timeframes simultaneously.
Customization:
- You can adjust the RSI, MACD, and timeframes used in the analysis via input parameters.
- The script provides alerts when buy/sell conditions are met.
- Visual signals and background colors are plotted on the main chart.
- The script includes a simple backtesting mechanism to review performance over the last year.
*/
```
### Explanation of the Code
1. **RSI and MACD Calculations:**
- The script calculates RSI and MACD for both the 1-hour (`tf1`) and 4-hour (`tf2`) timeframes.
- RSI levels are compared against user-defined overbought/oversold levels.
- MACD convergence/divergence is checked by comparing the MACD line to the signal line.
2. **Buy/Sell Conditions:**
- A buy signal is generated when both timeframes' RSI is below the oversold level, and MACD lines indicate a bullish crossover.
- A sell signal is generated when both timeframes' RSI is above the overbought level, and MACD lines indicate a bearish crossover.
3. **Visualization:**
- Signals are visually represented on the chart with green (buy) and red (sell) labels.
- Background colors change based on the conditions to provide a visual cue.
- The RSI and MACD values for both timeframes are plotted for reference.
4. **Alerts:**
- Alerts are set up for both buy and sell conditions so that users can be notified of potential trend reversals.
5. **Backtesting:**
- The script includes a simple backtesting mechanism that calculates hypothetical account growth based on past signals over the past year.
6. **Customization:**
- All key parameters, including RSI and MACD settings and timeframes, are adjustable.
7. **Documentation:**
- Comprehensive comments are included to explain the logic and usage of the script.
### Usage:
- Apply this script to your TradingView chart.
- Adjust parameters like timeframes, RSI/MACD settings, and backtest period to suit your trading strategy.
- Use the generated signals and alerts to assist in identifying potential trend reversals.
This script can be further expanded or customized depending on specific needs or additional indicators.