TradingView Purple Cloud PineScript Indicator Code Explanation


Detailed Explanation of the Pine Script and Its Buy/Sell Rules

This script is written in Pine Script v5, which is used for developing custom technical indicators and strategies in TradingView. The script, named “Purple Cloud [MMD]”, is designed to display buy and sell signals using a combination of Supertrend, VWMA (Volume-Weighted Moving Average), ATR (Average True Range), and EMA (Exponential Moving Average).

purple cloud

1. Script Structure Overview

The script follows this structure:

  1. Indicator Initialization: Defines the script’s properties, like name and overlay.
  2. Supertrend Calculation: Uses ATR to create the Supertrend indicator.
  3. ATR-Based Bands Calculation: Computes upper and lower bands based on ATR and a smoothing factor.
  4. VWMA-Based Calculation: Uses volume-weighted moving averages for price trend filtering.
  5. Buy and Sell Logic: Compares calculated levels to define buy and sell conditions.
  6. Signal Plotting: Displays buy/sell labels on the chart.
  7. Optional EMAs: Provides optional EMA indicators (20, 50, 200-period).
  8. Alert Conditions: Creates alerts for buy/sell signals.

2. Detailed Breakdown of Each Section

2.1 Indicator Initialization

//@version=5
indicator("Purple Cloud [MMD]", overlay=true, timeframe="", timeframe_gaps=true)
  • The script runs on Pine Script v5.
  • overlay=true ensures that the indicator is plotted on the price chart instead of in a separate pane.

2.2 Supertrend Calculation

atrPeriod = input(10, "Supertrend ATR Length")
factor = input.float(3.0, "Supertrend Factor", step = 0.01)
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
  • atrPeriod (default 10) is the length of ATR used in Supertrend calculations.
  • factor (default 3.0) defines the multiplier for ATR.
  • The built-in ta.supertrend function computes the Supertrend line and its direction:
    • direction > 0 ? Uptrend
    • direction < 0 ? Downtrend

2.3 ATR-Based Bands Calculation

x1 = input(14, "Period")
alpha = input.float(0.7, "Alpha", step = 0.1)
bpt = input.float(1.4,"Buying Pressure Threshold %", step = 0.1)
spt = input.float(1.4,"Selling Pressure Threshold %", step = 0.1)
x2 = ta.atr(x1) * alpha
xh = close + x2
xl = close – x2
  • ATR Bands:
    • x2 = ta.atr(x1) * alpha ? ATR is smoothed using an alpha factor (0.7).
    • xh = close + x2 ? Upper Band.
    • xl = close - x2 ? Lower Band.
  • Buy/Sell Thresholds:
    • bpt = 1.4%, spt = 1.4% ? Define thresholds for price deviation.

2.4 VWMA-Based Calculation

a1 = ta.vwma(hl2*volume,math.ceil(x1/4))/ta.vwma(volume,math.ceil(x1/4))
a2 = ta.vwma(hl2*volume,math.ceil(x1/2))/ta.vwma(volume,math.ceil(x1/2))
a3 = 2*a1 - a2
a4 = ta.vwma(a3, x1)
  • VWMA (Volume-Weighted Moving Average) Calculations:
    • a1 ? VWMA using ¼ of x1 period.
    • a2 ? VWMA using ½ of x1 period.
    • a3 ? Difference-based adjustment.
    • a4 ? Final VWMA smoothing over x1 period.

2.5 Adaptive Moving Average Calculation

b1 = 0.0
b1 := na(b1[1]) ? ta.sma(close, x1) : (b1[1] * (x1 - 1) + close) / x1
a5 = 2 * a4 * b1 / (a4 + b1)
  • b1 is a Simple Moving Average (SMA) initialized at 0.0.
  • The final VWMA-weighted adaptive moving average is a5.

3. Buy and Sell Rules

buy = a5 <= xl and close > b1 * (1 + bpt * 0.01)
sell = a5 >= xh and close < b1 * (1 - spt * 0.01)

Buy Condition

A buy signal occurs when:

  • a5 <= xl ? Price is near or below the lower ATR band.
  • close > b1 * (1 + bpt * 0.01) ? Price is rising above the SMA by at least 1.4%.

Sell Condition

A sell signal occurs when:

  • a5 >= xh ? Price is near or above the upper ATR band.
  • close < b1 * (1 - spt * 0.01) ? Price is falling below the SMA by at least 1.4%.

3.1 Trend State Calculation

xs = 0
xs := buy ? 1 : sell ? -1 : xs[1]
  • The xs variable tracks trend states:
    • 1 ? Buy condition.
    • -1 ? Sell condition.
    • Otherwise, retains previous state.

4. Signal Display

barcolor( color = xs == 1 ? color.green : xs == -1 ? color.red : na)
  • Green bars for buy signals.
  • Red bars for sell signals.

4.1 Buy/Sell Labels

plotshape(buy and xs != xs[1], title = "BUY", text = 'B', style = shape.labelup, location = location.belowbar, color = color.green, textcolor = color.white, size = size.tiny)
plotshape(sell and xs != xs[1], title = "SELL", text = 'S', style = shape.labeldown, location = location.abovebar, color = color.red, textcolor = color.white, size = size.tiny)
  • Labels are plotted only when the trend changes.
  • "B" for buy signals.
  • "S" for sell signals.

4.2 Strong Buy/Sell Labels (Using Supertrend)

plotshape(buy and xs != xs[1] and direction < 0, title = "Strong BUY", text = '', style = shape.labelup, location = location.belowbar, color= color.green, textcolor = color.white, size = size.tiny)
plotshape(sell and xs != xs[1] and direction > 0, title = "Strong SELL", text = '', style = shape.labeldown, location = location.abovebar, color= color.red, textcolor = color.white, size = size.tiny)
  • Strong Buy occurs when:
    • Buy signal + Supertrend is in a downtrend.
  • Strong Sell occurs when:
    • Sell signal + Supertrend is in an uptrend.

5. Optional EMA Plots

plot(ta.ema(close,200), color=ema200 ? color.black : na, title="Ema 200", linewidth = 4)
plot(ta.ema(close,50), color=ema50 ? color.blue : na, title="EMA 50", linewidth = 3)
plot(ta.ema(close,20), color=ema20 ? color.orange : na, title="EMA 20", linewidth = 2)
  • The script optionally plots EMA (200, 50, 20).

6. Alerts for Buy/Sell

alertcondition(buy and xs != xs[1], "PC Long", "PC Long")
alertcondition(sell and xs != xs[1], "PC Short", "PC Short")
  • Alerts trigger when trend shifts.

Conclusion

This script combines Supertrend, ATR bands, VWMA-based trend filtering, and EMAs to generate buy and sell signals with trend confirmation.

Fxigor

Fxigor

Igor has been a trader since 2007. Currently, Igor works for several prop trading companies. He is an expert in financial niche, long-term trading, and weekly technical levels. The primary field of Igor's research is the application of machine learning in algorithmic trading. Education: Computer Engineering and Ph.D. in machine learning. Igor regularly publishes trading-related videos on the Fxigor Youtube channel. To contact Igor write on: igor@forex.in.rs

Trade gold and silver. Visit the broker's page and start trading high liquidity spot metals - the most traded instruments in the world.

Trade Gold & Silver

GET FREE MEAN REVERSION STRATEGY

Recent Posts