OPEN-SOURCE SCRIPT

Auto Pivot Levels CPR

1️⃣ Inputs for User Preferences

pinescript
Copy
Edit
show_daily = input(true, title="Show Daily Pivots")
show_weekly = input(true, title="Show Weekly Pivots")
show_monthly = input(true, title="Show Monthly Pivots")
Users can turn ON/OFF pivot levels for different timeframes.

2️⃣ Detects Timeframe (Auto-Switching)
pinescript
Copy
Edit
is_daily = timeframe.isintraday and timeframe.multiplier <= 15
is_weekly = timeframe.isintraday and timeframe.multiplier > 15 and timeframe.multiplier <= 75
is_monthly = not timeframe.isintraday or timeframe.multiplier > 75
Determines if the chart is intraday, weekly, or monthly.
This helps the script automatically switch between daily, weekly, and monthly pivot levels.
3️⃣ Function to Calculate Pivot Levels
pinescript
Copy
Edit
calculate_pivots(high, low, close) =>
pivot = (high + low + close) / 3
bc = (high + low) / 2
tc = (pivot - bc) + pivot
r1 = (2 * pivot) - low
s1 = (2 * pivot) - high
r2 = pivot + (high - low)
s2 = pivot - (high - low)
r3 = high + 2 * (pivot - low)
s3 = low - 2 * (high - pivot)
r4 = high + 3 * (pivot - low)
s4 = low - 3 * (high - pivot)
[pivot, bc, tc, r1, r2, r3, r4, s1, s2, s3, s4]
Uses high, low, and close prices from the previous period (day, week, month) to compute pivot levels.
Returns Pivot Point (PP), Central Pivot Range (BC, TC), 4 Resistance (R1-R4), and 4 Support (S1-S4) levels.
4️⃣ Fetching Previous Period Data
pinescript
Copy
Edit
daily_high = request.security(syminfo.tickerid, "D", high[1])
daily_low = request.security(syminfo.tickerid, "D", low[1])
daily_close = request.security(syminfo.tickerid, "D", close[1])
Uses request.security() to get previous day/week/month high, low, and close prices.
5️⃣ Auto-Switching Logic for Pivot Levels
pinescript
Copy
Edit
pivot = is_daily ? daily_pivot : is_weekly ? weekly_pivot : is_monthly ? monthly_pivot : na
r1 = is_daily ? daily_r1 : is_weekly ? weekly_r1 : is_monthly ? monthly_r1 : na
s1 = is_daily ? daily_s1 : is_weekly ? weekly_s1 : is_monthly ? monthly_s1 : na
Dynamically assigns the correct pivot levels based on current timeframe.
6️⃣ Plots Pivot Levels on Chart
pinescript
Copy
Edit
plot(pivot, color=color.blue, title="Pivot", linewidth=2)
plot(r1, color=color.red, title="R1", linewidth=1)
plot(s1, color=color.green, title="S1", linewidth=1)
Uses plot() to draw pivot levels, support, and resistance levels.
Color coding:
Blue → Pivot
Red → Resistance (R1–R4)
Green → Support (S1–S4)
7️⃣ Plots Previous Highs & Lows (Circles on Chart)
pinescript
Copy
Edit
plot(daily_high_plot, color=color.red, title="Previous Daily High", linewidth=2, style=plot.style_circles)
Circles mark previous high and low for daily, weekly, and monthly periods.
🎯 Final Output on TradingView Chart
✅ Auto-adjusts pivot levels based on chart timeframe
✅ Displays support & resistance levels dynamically
✅ Marks previous period highs & lows with circles
✅ Works with any intraday or higher timeframe chart

🔥 Why Use This Indicator?
✔ Automatically selects the correct pivot timeframe
✔ Saves time by avoiding manual calculations
✔ Helps traders identify key price levels for entries/exits
✔ Great for intraday, swing, and positional trading

📈 How to Find Buying & Selling Opportunities Using This Auto Pivot Levels Indicator
This Auto Pivot Levels indicator helps traders identify support & resistance zones, which can be used for buying and selling decisions.

1️⃣ Buying Opportunities (Long Trades)
✅ Buy when price is near support levels (S1, S2, S3, S4)
If the price approaches S1, S2, or S3 and shows a reversal, it’s a potential buying opportunity.
Look for bullish candlestick patterns (e.g., Hammer, Bullish Engulfing) or oscillator confirmation (e.g., RSI oversold).
✅ Buy on a breakout above pivot or resistance (Pivot, R1, R2)
If the price breaks above the Pivot with strong volume, it indicates bullish momentum.
A breakout above R1 can push the price toward R2 and R3.
🔹 Example Buy Strategy:

Buy when the price bounces from S1 or S2 with a bullish candlestick pattern.
Buy on a breakout above Pivot (PP) or R1 with strong volume.
Stop-loss (SL): Below the previous swing low or S2.
Target (TP): Next resistance level (R1, R2, R3).
2️⃣ Selling Opportunities (Short Trades)
❌ Sell when price is near resistance levels (R1, R2, R3, R4)
If the price approaches R1, R2, or R3 and fails to break, it’s a selling opportunity.
Look for bearish candlestick patterns (e.g., Shooting Star, Bearish Engulfing) or oscillator confirmation (e.g., RSI overbought).
❌ Sell on a breakdown below pivot or support
If the price breaks below the Pivot with strong volume, it indicates bearish momentum.
A breakdown below S1 can push the price toward S2 and S3.
🔹 Example Sell Strategy:

Sell when the price gets rejected from R1 or R2 with a bearish candlestick pattern.
Sell on a breakdown below Pivot (PP) or S1 with strong volume.
Stop-loss (SL): Above the previous swing high or R2.
Target (TP): Next support level (S1, S2, S3).
3️⃣ Example Trade Setups
Scenario Entry Point Stop-Loss (SL) Target (TP)
Buy at Support Bounce from S1 or S2 Below S2 R1 or Pivot
Buy Breakout Break above Pivot or R1 Below Pivot R2 or R3
Sell at Resistance Rejection at R1 or R2 Above R2 S1 or Pivot
Sell Breakdown Break below Pivot or S1 Above Pivot S2 or S3
📌 Additional Confirmation for High-Accuracy Trades
✔ RSI (Relative Strength Index):

Buy when RSI < 30 (oversold) near support.
Sell when RSI > 70 (overbought) near resistance.
✔ MACD (Moving Average Convergence Divergence):

Buy when MACD crosses above Signal Line near support.
Sell when MACD crosses below Signal Line near resistance.
✔ Volume Confirmation:

Strong buying volume at support increases the chance of reversal.
Strong selling volume at resistance confirms a potential drop.
🎯 Conclusion
Support levels (S1, S2, S3) → Potential Buy Zones
Resistance levels (R1, R2, R3) → Potential Sell Zones
Pivot Breakout → Bullish
Pivot Breakdown → Bearish
Confirm with RSI, MACD, and Volume for best results.
🚀 Using this strategy, you can trade BankNIFTY or any market effectively! Let me know if you need more custom rules. 🎯

Disclaimer