Example output
Here's a custom Pine Script indicator designed for swing trading on TradingView. This script integrates the RSI, Moving Average (MA), and MACD indicators, with custom conditions to generate buy and sell signals. The script also includes visualization enhancements, adjustable parameters, an alert system, and backtesting capability.
```pinescript
//@version=5
indicator("Swing Trading Enhanced Indicator", overlay=true)
// Input Parameters
maLength = input.int(50, title="MA Length", minval=1)
rsiLength = input.int(14, title="RSI Length", minval=1)
rsiOverbought = input.int(70, title="RSI Overbought Level", minval=50)
rsiOversold = input.int(30, title="RSI Oversold Level", minval=0)
macdFastLength = input.int(12, title="MACD Fast Length", minval=1)
macdSlowLength = input.int(26, title="MACD Slow Length", minval=1)
macdSignalSmoothing = input.int(9, title="MACD Signal Smoothing", minval=1)
// Moving Average (MA)
ma = ta.sma(close, maLength)
plot(ma, color=color.blue, title="Moving Average")
// RSI
rsi = ta.rsi(close, rsiLength)
hline(rsiOverbought, "RSI Overbought", color=color.red, linestyle=hline.style_dashed)
hline(rsiOversold, "RSI Oversold", color=color.green, linestyle=hline.style_dashed)
plot(rsi, color=color.purple, title="RSI", linewidth=2)
// MACD
[macdLine, signalLine, _] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalSmoothing)
histogram = macdLine - signalLine
plot(macdLine, color=color.orange, title="MACD Line")
plot(signalLine, color=color.blue, title="Signal Line")
plot(histogram, color=color.red, style=plot.style_histogram, title="MACD Histogram")
// Custom Conditions for Buy and Sell Signals
buyCondition = ta.crossover(rsi, rsiOversold) and ta.crossover(macdLine, signalLine) and close > ma
sellCondition = ta.crossunder(rsi, rsiOverbought) and ta.crossunder(macdLine, signalLine) and close < ma
// Plot Buy and Sell Signals on Chart
plotshape(series=buyCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", size=size.small)
plotshape(series=sellCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", size=size.small)
// Alerts
alertcondition(buyCondition, title="Buy Alert", message="Buy Signal Triggered")
alertcondition(sellCondition, title="Sell Alert", message="Sell Signal Triggered")
// Backtesting
var float starting_balance = 10000
var float current_balance = starting_balance
var int position_size = 0
var float trade_price = na
if (buyCondition and position_size == 0)
trade_price := close
position_size := current_balance / close
current_balance := 0
if (sellCondition and position_size > 0)
current_balance := position_size * close
position_size := 0
plot(current_balance, title="Equity Curve", color=color.yellow, linewidth=2, style=plot.style_line)
// Tooltips and Comments
// This indicator is designed for swing trading by combining RSI, MA, and MACD.
// Buy signals are triggered when RSI crosses above the oversold level, MACD crosses above its signal line, and price is above the MA.
// Sell signals are triggered when RSI crosses below the overbought level, MACD crosses below its signal line, and price is below the MA.
// The indicator includes alerts for buy and sell signals, dynamic plots for easy visualization, and backtesting to evaluate performance over time.
```
### Key Features:
1. **Integration of Indicators**:
- **Moving Average (MA)**: A simple moving average plotted on the price chart.
- **RSI**: Plotted with overbought and oversold levels.
- **MACD**: Includes the MACD line, Signal line, and Histogram.
2. **Custom Conditions**:
- **Buy Signal**: Triggered when the RSI crosses above the oversold level, MACD line crosses above the Signal line, and the price is above the MA.
- **Sell Signal**: Triggered when the RSI crosses below the overbought level, MACD line crosses below the Signal line, and the price is below the MA.
3. **Visualization Enhancements**:
- **Color-Coded Signals**: Green "BUY" labels below bars for buy signals and red "SELL" labels above bars for sell signals.
- **Dynamic Plots**: Plots for MA, RSI, and MACD with clear color differentiation.
4. **Adjustable Parameters**:
- Parameters for MA, RSI, and MACD are adjustable, allowing fine-tuning of the indicator to fit different trading strategies.
5. **Alert System**:
- Alerts can be set up to notify when a buy or sell condition is met.
6. **Backtesting Capability**:
- A simple equity curve is plotted based on buy and sell signals to visualize the performance of the indicator over time.
7. **Tooltips and Comments**:
- The script includes comments explaining the purpose of each section and functionality, aiding in understanding and customization.
This script provides a comprehensive approach to swing trading, allowing traders to combine multiple indicators into a single, easy-to-visualize and customizable tool.