Example output
Here is a complete Pine Script to generate buy and sell signals based on the alignment of MACD signals across 1-hour, 4-hour, and daily timeframes in TradingView. The script includes visualizations, alerts, adjustable parameters, and backtesting functionality.
```pinescript
//@version=5
indicator("Multi-Timeframe MACD Signal Generator", overlay=true)
// Adjustable parameters for MACD
[fastLength, slowLength, signalSmoothing, src] = input.int(12, title="MACD Fast Length"), input.int(26, title="MACD Slow Length"), input.int(9, title="MACD Signal Smoothing"), input.source(close, title="Source")
// Adjustable timeframes
timeframe1 = input.timeframe("60", title="Timeframe 1 (1H)", group="Timeframes")
timeframe2 = input.timeframe("240", title="Timeframe 2 (4H)", group="Timeframes")
timeframe3 = input.timeframe("D", title="Timeframe 3 (1D)", group="Timeframes")
// MACD Calculation
macdLine(source, fast, slow) =>
fastMA = ta.ema(source, fast)
slowMA = ta.ema(source, slow)
macd = fastMA - slowMA
macd
signalLine(macd, signalSmoothing) =>
ta.ema(macd, signalSmoothing)
// MACD on different timeframes
[macd1, signal1, hist1] = request.security(timeframe1, macdLine(src, fastLength, slowLength), lookahead=barmerge.lookahead_on), request.security(timeframe1, signalLine(macdLine(src, fastLength, slowLength), signalSmoothing), lookahead=barmerge.lookahead_on), macd1 - signal1
[macd2, signal2, hist2] = request.security(timeframe2, macdLine(src, fastLength, slowLength), lookahead=barmerge.lookahead_on), request.security(timeframe2, signalLine(macdLine(src, fastLength, slowLength), signalSmoothing), lookahead=barmerge.lookahead_on), macd2 - signal2
[macd3, signal3, hist3] = request.security(timeframe3, macdLine(src, fastLength, slowLength), lookahead=barmerge.lookahead_on), request.security(timeframe3, signalLine(macdLine(src, fastLength, slowLength), signalSmoothing), lookahead=barmerge.lookahead_on), macd3 - signal3
// Signal conditions for each timeframe
buySignal1 = hist1 > 0
buySignal2 = hist2 > 0
buySignal3 = hist3 > 0
sellSignal1 = hist1 < 0
sellSignal2 = hist2 < 0
sellSignal3 = hist3 < 0
// Combined buy and sell signals across timeframes
multiBuySignal = buySignal1 and buySignal2 and buySignal3
multiSellSignal = sellSignal1 and sellSignal2 and sellSignal3
// Visualization of buy/sell signals on the chart
plotshape(series=multiBuySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=multiSellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// Background color for timeframe alignment
bgcolor(multiBuySignal ? color.new(color.green, 90) : na)
bgcolor(multiSellSignal ? color.new(color.red, 90) : na)
// Real-time alerts when signals align across all selected timeframes
alertcondition(multiBuySignal, title="Multi-Timeframe Buy Signal", message="MACD Buy Signal across all timeframes!")
alertcondition(multiSellSignal, title="Multi-Timeframe Sell Signal", message="MACD Sell Signal across all timeframes!")
// Backtesting section
var float entryPrice = na
var float exitPrice = na
strategy.entry("Buy", strategy.long, when=multiBuySignal)
if (multiSellSignal)
strategy.close("Buy")
// Strategy performance metrics
longProfit = exitPrice - entryPrice
longProfitPts = strategy.position_size > 0 ? (close - strategy.position_avg_price) : na
plot(longProfitPts, title="Long Profit", color=color.green)
```
### Key Features:
1. **MACD Calculation for Multiple Timeframes**: MACD is calculated for 1-hour, 4-hour, and daily timeframes using the `request.security()` function. You can adjust the MACD's fast, slow, and signal lengths.
2. **Signal Generation**: Buy and sell signals are generated when MACD's histogram crosses above or below zero, respectively, for all the selected timeframes.
3. **Signal Visualization**: Buy signals are displayed as green markers below the bar, while sell signals are red markers above the bar. The background color also changes when buy (green) or sell (red) signals align across all timeframes.
4. **Real-Time Alerts**: Alerts are generated when buy or sell signals align across all timeframes.
5. **Backtesting**: The strategy is set to backtest using `strategy.entry()` for buy signals and `strategy.close()` for sell signals. Performance metrics are shown on the chart for better evaluation.
6. **Adjustable Parameters**: You can change the MACD parameters (fast, slow, signal length) and timeframes to refine the accuracy of signals.
### How It Works:
- The MACD line and signal line are calculated for each timeframe (1-hour, 4-hour, daily).
- Buy signals occur when the histogram (difference between MACD and signal line) is greater than zero for all timeframes.
- Sell signals occur when the histogram is less than zero for all timeframes.
- Visual markers and alerts trigger when signals align across timeframes.
### Backtesting:
You can view the historical performance of the strategy on your chart by running it over the last year or any desired period.