Here's a TradingView strategy script that implements your specified trading strategy using Pine Script v5. This script uses 50-day and 200-day Simple Moving Averages (SMAs) for the golden cross and death cross signals. It also allows you to select either RSI or MACD as the momentum confirmation indicator. The strategy enters a long position when a golden cross occurs with bullish momentum confirmation and exits when a death cross occurs or bearish momentum is confirmed.
//@version=5
strategy("Golden Cross Strategy with Confirmation", overlay=true)
// User Inputs
confirmType = input.string(title="Confirmation Indicator", defval="RSI", options=["RSI", "MACD"])
// Moving Averages
fastLength = 50
slowLength = 200
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
// Calculate RSI and MACD
rsiValue = ta.rsi(close, 14)
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
// Confirmation conditions
bullishConfirm = (confirmType == "RSI" and rsiValue > 50) or (confirmType == "MACD" and ta.crossover(macdLine, signalLine))
bearishConfirm = (confirmType == "RSI" and rsiValue < 50) or (confirmType == "MACD" and ta.crossunder(macdLine, signalLine))
// Crossovers
goldenCross = ta.crossover(fastMA, slowMA)
deathCross = ta.crossunder(fastMA, slowMA)
// Buy and Sell conditions
buyCondition = goldenCross and bullishConfirm
sellCondition = deathCross or bearishConfirm
// Strategy Entry and Exit
if (buyCondition and strategy.position_size == 0)
strategy.entry("Long", strategy.long)
if (sellCondition and strategy.position_size > 0)
strategy.close("Long")
// Plot Moving Averages
plot(fastMA, color=color.blue, title="50-day SMA")
plot(slowMA, color=color.red, title="200-day SMA")
// Plot Buy and Sell Signals
plotshape(buyCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(sellCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
Explanation:
- Moving Averages: The script calculates the 50-day and 200-day SMAs using
ta.sma().
- Confirmation Indicator: You can select either "RSI" or "MACD" as the confirmation indicator using the
input.string() function.
- RSI Confirmation: Bullish when RSI > 50; bearish when RSI < 50.
- MACD Confirmation: Bullish when the MACD line crosses above the signal line; bearish when it crosses below.
- Buy Condition:
- A golden cross occurs (
ta.crossover(fastMA, slowMA)).
- Bullish momentum is confirmed (
bullishConfirm is true).
- Sell Condition:
- A death cross occurs (
ta.crossunder(fastMA, slowMA)).
- Bearish momentum is confirmed (
bearishConfirm is true).
- Strategy Execution:
- The strategy enters a long position when the buy condition is met and no current position exists.
- It exits the long position when the sell condition is met.
- Plotting:
- The 50-day and 200-day SMAs are plotted on the chart.
- Buy and sell signals are indicated with arrows on the chart.
How to Use:
- Add the Script to TradingView:
- Open TradingView and go to the Pine Editor.
- Paste the script into the editor and click "Add to Chart."
- Select Confirmation Indicator:
- In the script settings, choose either "RSI" or "MACD" as your confirmation indicator.
- Analyze Signals:
- The chart will display the moving averages and signal arrows.
- Use the backtesting feature to assess the strategy's performance.
Notes:
- This strategy only takes long positions as per your specification.
- You can modify the moving average lengths or the RSI length by adding additional input variables if needed.
- Always test the strategy over different time frames and market conditions to evaluate its effectiveness.