本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KingDonut
//@version=6
// ═══════════════════════════════════════════════════════════════════════
// PIVOT BANDS by KingDonut
// The open-source successor to "Adaptive Pivots by KingDonut".
//
// WHAT IT DOES (in plain English):
// - Draws a slow Central Pivot (an EMA) through the middle of price.
// - Draws 7 pivots above it and 7 below it.
// - The gap between the pivots is based on volatility (ATR), so the
// pivots spread out when the market is fast and squeeze in when it
// is quiet.
// - Optional: the same pivots from two higher timeframes, drawn as
// flat lines across the chart (off by default).
// - Optional: a small BandWidth box showing the gap in points between
// one pivot and the next (off by default).
//
// SETTINGS ORDER:
// 1. Pivot Appearance 5-11. Pivot 1 to Pivot 7
// 2. Central Pivot 12-13. Extra Timeframe 1 and 2
// 3. Session 14. BandWidth Table
// 4. Shared Calculation
//
// Feel free to use it, pull it apart and change it however you like.
// ═══════════════════════════════════════════════════════════════════════
indicator(title="Pivot Bands by KingDonut", shorttitle="Pivot Bands by KD", overlay=true, max_bars_back=1100, max_labels_count=500, max_lines_count=100)
// ═══════════════════════════════════════════════════════════════════════
// 1. PIVOT APPEARANCE
// ═══════════════════════════════════════════════════════════════════════
var string gAPP = "1. Pivot Appearance"
pv_style = input.string("Dots", "Pivot appearance", options=["Dots","Lines"], group=gAPP, tooltip="Draw the main pivots as dots or as continuous lines.")
pv_sameOn = input.bool(true, "Same colour Pivots", group=gAPP, inline="same", tooltip="ON = every pivot and the Central Pivot use this one colour. OFF = each pivot uses its own colour (set in its own group).")
pv_sameCol = input.color(color.white, "", group=gAPP, inline="same")
// Simple true/false switches used by the plots further down
pv_lines = pv_style == "Lines"
pv_dots = not pv_lines
// ═══════════════════════════════════════════════════════════════════════
// 2. CENTRAL PIVOT
// ═══════════════════════════════════════════════════════════════════════
var string gCB_EPI = "2. Central Pivot"
cb_epi_show = input.bool(true, "Show Central Pivot", group=gCB_EPI, tooltip="Turn the Central Pivot (the middle line all pivots are built around) on or off.")
cb_epi_col = input.color(color.gray, "Colour", group=gCB_EPI, inline="epic", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_epi_width = input.int(3, "Width", group=gCB_EPI, inline="epic", minval=1, maxval=5)
cb_epi_len = input.int(987, "Central Pivot length (bars)", group=gCB_EPI, minval=2, tooltip="EMA length (counted in session bars) used for the Central Pivot. Higher = slower, smoother centre line.")
// ═══════════════════════════════════════════════════════════════════════
// 3. SESSION (which bars count in the calculation)
// ═══════════════════════════════════════════════════════════════════════
var string gSES = "3. Session (calculation window)"
cb_sess_on = input.bool(true, "Use session window (intraday charts only)", group=gSES, tooltip="ON = only bars inside the window below feed the calculation. OFF = all bars count.")
cb_sess_in = input.session("0100-2100", "Session hours", group=gSES, tooltip="Start-end time in the timezone below. Default 0100-2100.")
cb_tz = input.string("Europe/London", "Timezone", group=gSES, tooltip="Timezone the session hours are read in (e.g. Europe/London, America/New_York, Europe/Berlin, UTC).")
cb_d_sun = input.bool(true, "Sun", group=gSES, inline="d1")
cb_d_mon = input.bool(true, "Mon", group=gSES, inline="d1")
cb_d_tue = input.bool(true, "Tue", group=gSES, inline="d1")
cb_d_wed = input.bool(true, "Wed", group=gSES, inline="d1")
cb_d_thu = input.bool(true, "Thu", group=gSES, inline="d2")
cb_d_fri = input.bool(true, "Fri", group=gSES, inline="d2")
cb_d_sat = input.bool(true, "Sat", group=gSES, inline="d2")
// Build the session text TradingView expects, e.g. "0100-2100:1234567" (1 = Sunday ... 7 = Saturday).
// If every day is unticked we fall back to all days so the indicator never goes blank.
cb_days_raw = (cb_d_sun ? "1" : "") + (cb_d_mon ? "2" : "") + (cb_d_tue ? "3" : "") + (cb_d_wed ? "4" : "") + (cb_d_thu ? "5" : "") + (cb_d_fri ? "6" : "") + (cb_d_sat ? "7" : "")
cb_days = cb_days_raw == "" ? "1234567" : cb_days_raw
cb_sessStr = cb_sess_in + ":" + cb_days
// ═══════════════════════════════════════════════════════════════════════
// 4. SHARED CALCULATION (used by every pivot unless that pivot overrides)
// ═══════════════════════════════════════════════════════════════════════
var string gSH = "4. Shared Calculation (all pivots)"
cb_atr_short = input.int(13, "ATR short length", group=gSH, minval=1, tooltip="Fast ATR (current volatility). Used by all pivots unless a pivot has its own override ticked.")
cb_atr_long = input.int(144, "ATR long length", group=gSH, minval=1, tooltip="Slow ATR (baseline volatility). The long/short ATR ratio makes the pivots widen or tighten with volatility.")
cb_vol_min = input.float(0.5, "Volatility ratio MIN", group=gSH, minval=0.01, step=0.1, tooltip="Lowest value the long/short ATR ratio is allowed to reach (floor).")
cb_vol_max = input.float(2.0, "Volatility ratio MAX", group=gSH, minval=0.01, step=0.1, tooltip="Highest value the long/short ATR ratio is allowed to reach (cap).")
// ═══════════════════════════════════════════════════════════════════════
// 5. PIVOT 1
// ═══════════════════════════════════════════════════════════════════════
var string gCB_B1 = "5. PIVOT 1"
cb_b1_show = input.bool(true, "Show Pivot 1 (upper + lower)", group=gCB_B1, tooltip="Turn both Pivot 1 lines (above and below the Central Pivot) on or off.")
cb_b1_col = input.color(color.red, "Colour", group=gCB_B1, inline="b1c", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_b1_width = input.int(2, "Width", group=gCB_B1, inline="b1c", minval=1, maxval=5)
cb_b1_base_mult = input.float(5.0, "Distance multiplier", group=gCB_B1, minval=0.1, step=0.5, tooltip="How far Pivot 1 sits from the Central Pivot, in ATR steps. Bigger = further away.")
cb_b1_ovr = input.bool(false, "Override shared calculation", group=gCB_B1, tooltip="OFF = uses section 4 (Shared Calculation). ON = uses the 4 settings below for this pivot only.")
cb_b1_as_in = input.int(13, "ATR short length (override)", group=gCB_B1, minval=1)
cb_b1_al_in = input.int(100, "ATR long length (override)", group=gCB_B1, minval=1)
cb_b1_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gCB_B1, minval=0.01, step=0.1)
cb_b1_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gCB_B1, minval=0.01, step=0.1)
// Use this pivot's own override values if ticked, otherwise the shared ones from section 4
cb_b1_atr_short = cb_b1_ovr ? cb_b1_as_in : cb_atr_short
cb_b1_atr_long = cb_b1_ovr ? cb_b1_al_in : cb_atr_long
cb_b1_vol_min = cb_b1_ovr ? cb_b1_vmin_in : cb_vol_min
cb_b1_vol_max = cb_b1_ovr ? cb_b1_vmax_in : cb_vol_max
// ═══════════════════════════════════════════════════════════════════════
// 6. PIVOT 2
// ═══════════════════════════════════════════════════════════════════════
var string gCB_B2 = "6. PIVOT 2"
cb_b2_show = input.bool(true, "Show Pivot 2 (upper + lower)", group=gCB_B2, tooltip="Turn both Pivot 2 lines (above and below the Central Pivot) on or off.")
cb_b2_col = input.color(color.orange, "Colour", group=gCB_B2, inline="b2c", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_b2_width = input.int(2, "Width", group=gCB_B2, inline="b2c", minval=1, maxval=5)
cb_b2_base_mult = input.float(10.0, "Distance multiplier", group=gCB_B2, minval=0.1, step=0.5, tooltip="How far Pivot 2 sits from the Central Pivot, in ATR steps. Bigger = further away.")
cb_b2_ovr = input.bool(false, "Override shared calculation", group=gCB_B2, tooltip="OFF = uses section 4 (Shared Calculation). ON = uses the 4 settings below for this pivot only.")
cb_b2_as_in = input.int(13, "ATR short length (override)", group=gCB_B2, minval=1)
cb_b2_al_in = input.int(100, "ATR long length (override)", group=gCB_B2, minval=1)
cb_b2_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gCB_B2, minval=0.01, step=0.1)
cb_b2_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gCB_B2, minval=0.01, step=0.1)
// Use this pivot's own override values if ticked, otherwise the shared ones from section 4
cb_b2_atr_short = cb_b2_ovr ? cb_b2_as_in : cb_atr_short
cb_b2_atr_long = cb_b2_ovr ? cb_b2_al_in : cb_atr_long
cb_b2_vol_min = cb_b2_ovr ? cb_b2_vmin_in : cb_vol_min
cb_b2_vol_max = cb_b2_ovr ? cb_b2_vmax_in : cb_vol_max
// ═══════════════════════════════════════════════════════════════════════
// 7. PIVOT 3
// ═══════════════════════════════════════════════════════════════════════
var string gCB_B3 = "7. PIVOT 3"
cb_b3_show = input.bool(true, "Show Pivot 3 (upper + lower)", group=gCB_B3, tooltip="Turn both Pivot 3 lines (above and below the Central Pivot) on or off.")
cb_b3_col = input.color(color.yellow, "Colour", group=gCB_B3, inline="b3c", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_b3_width = input.int(2, "Width", group=gCB_B3, inline="b3c", minval=1, maxval=5)
cb_b3_base_mult = input.float(15.0, "Distance multiplier", group=gCB_B3, minval=0.1, step=0.5, tooltip="How far Pivot 3 sits from the Central Pivot, in ATR steps. Bigger = further away.")
cb_b3_ovr = input.bool(false, "Override shared calculation", group=gCB_B3, tooltip="OFF = uses section 4 (Shared Calculation). ON = uses the 4 settings below for this pivot only.")
cb_b3_as_in = input.int(13, "ATR short length (override)", group=gCB_B3, minval=1)
cb_b3_al_in = input.int(100, "ATR long length (override)", group=gCB_B3, minval=1)
cb_b3_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gCB_B3, minval=0.01, step=0.1)
cb_b3_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gCB_B3, minval=0.01, step=0.1)
// Use this pivot's own override values if ticked, otherwise the shared ones from section 4
cb_b3_atr_short = cb_b3_ovr ? cb_b3_as_in : cb_atr_short
cb_b3_atr_long = cb_b3_ovr ? cb_b3_al_in : cb_atr_long
cb_b3_vol_min = cb_b3_ovr ? cb_b3_vmin_in : cb_vol_min
cb_b3_vol_max = cb_b3_ovr ? cb_b3_vmax_in : cb_vol_max
// ═══════════════════════════════════════════════════════════════════════
// 8. PIVOT 4
// ═══════════════════════════════════════════════════════════════════════
var string gCB_B4 = "8. PIVOT 4"
cb_b4_show = input.bool(true, "Show Pivot 4 (upper + lower)", group=gCB_B4, tooltip="Turn both Pivot 4 lines (above and below the Central Pivot) on or off.")
cb_b4_col = input.color(color.green, "Colour", group=gCB_B4, inline="b4c", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_b4_width = input.int(2, "Width", group=gCB_B4, inline="b4c", minval=1, maxval=5)
cb_b4_base_mult = input.float(20.0, "Distance multiplier", group=gCB_B4, minval=0.1, step=0.5, tooltip="How far Pivot 4 sits from the Central Pivot, in ATR steps. Bigger = further away.")
cb_b4_ovr = input.bool(false, "Override shared calculation", group=gCB_B4, tooltip="OFF = uses section 4 (Shared Calculation). ON = uses the 4 settings below for this pivot only.")
cb_b4_as_in = input.int(13, "ATR short length (override)", group=gCB_B4, minval=1)
cb_b4_al_in = input.int(100, "ATR long length (override)", group=gCB_B4, minval=1)
cb_b4_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gCB_B4, minval=0.01, step=0.1)
cb_b4_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gCB_B4, minval=0.01, step=0.1)
// Use this pivot's own override values if ticked, otherwise the shared ones from section 4
cb_b4_atr_short = cb_b4_ovr ? cb_b4_as_in : cb_atr_short
cb_b4_atr_long = cb_b4_ovr ? cb_b4_al_in : cb_atr_long
cb_b4_vol_min = cb_b4_ovr ? cb_b4_vmin_in : cb_vol_min
cb_b4_vol_max = cb_b4_ovr ? cb_b4_vmax_in : cb_vol_max
// ═══════════════════════════════════════════════════════════════════════
// 9. PIVOT 5
// ═══════════════════════════════════════════════════════════════════════
var string gCB_B5 = "9. PIVOT 5"
cb_b5_show = input.bool(true, "Show Pivot 5 (upper + lower)", group=gCB_B5, tooltip="Turn both Pivot 5 lines (above and below the Central Pivot) on or off.")
cb_b5_col = input.color(#6A0DAD, "Colour", group=gCB_B5, inline="b5c", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_b5_width = input.int(2, "Width", group=gCB_B5, inline="b5c", minval=1, maxval=5)
cb_b5_base_mult = input.float(25.0, "Distance multiplier", group=gCB_B5, minval=0.1, step=0.5, tooltip="How far Pivot 5 sits from the Central Pivot, in ATR steps. Bigger = further away.")
cb_b5_ovr = input.bool(false, "Override shared calculation", group=gCB_B5, tooltip="OFF = uses section 4 (Shared Calculation). ON = uses the 4 settings below for this pivot only.")
cb_b5_as_in = input.int(13, "ATR short length (override)", group=gCB_B5, minval=1)
cb_b5_al_in = input.int(100, "ATR long length (override)", group=gCB_B5, minval=1)
cb_b5_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gCB_B5, minval=0.01, step=0.1)
cb_b5_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gCB_B5, minval=0.01, step=0.1)
// Use this pivot's own override values if ticked, otherwise the shared ones from section 4
cb_b5_atr_short = cb_b5_ovr ? cb_b5_as_in : cb_atr_short
cb_b5_atr_long = cb_b5_ovr ? cb_b5_al_in : cb_atr_long
cb_b5_vol_min = cb_b5_ovr ? cb_b5_vmin_in : cb_vol_min
cb_b5_vol_max = cb_b5_ovr ? cb_b5_vmax_in : cb_vol_max
// ═══════════════════════════════════════════════════════════════════════
// 10. PIVOT 6
// ═══════════════════════════════════════════════════════════════════════
var string gCB_B6 = "10. PIVOT 6"
cb_b6_show = input.bool(true, "Show Pivot 6 (upper + lower)", group=gCB_B6, tooltip="Turn both Pivot 6 lines (above and below the Central Pivot) on or off.")
cb_b6_col = input.color(#ADD8E6, "Colour", group=gCB_B6, inline="b6c", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_b6_width = input.int(2, "Width", group=gCB_B6, inline="b6c", minval=1, maxval=5)
cb_b6_base_mult = input.float(30.0, "Distance multiplier", group=gCB_B6, minval=0.1, step=0.5, tooltip="How far Pivot 6 sits from the Central Pivot, in ATR steps. Bigger = further away.")
cb_b6_ovr = input.bool(false, "Override shared calculation", group=gCB_B6, tooltip="OFF = uses section 4 (Shared Calculation). ON = uses the 4 settings below for this pivot only.")
cb_b6_as_in = input.int(13, "ATR short length (override)", group=gCB_B6, minval=1)
cb_b6_al_in = input.int(100, "ATR long length (override)", group=gCB_B6, minval=1)
cb_b6_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gCB_B6, minval=0.01, step=0.1)
cb_b6_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gCB_B6, minval=0.01, step=0.1)
// Use this pivot's own override values if ticked, otherwise the shared ones from section 4
cb_b6_atr_short = cb_b6_ovr ? cb_b6_as_in : cb_atr_short
cb_b6_atr_long = cb_b6_ovr ? cb_b6_al_in : cb_atr_long
cb_b6_vol_min = cb_b6_ovr ? cb_b6_vmin_in : cb_vol_min
cb_b6_vol_max = cb_b6_ovr ? cb_b6_vmax_in : cb_vol_max
// ═══════════════════════════════════════════════════════════════════════
// 11. PIVOT 7
// ═══════════════════════════════════════════════════════════════════════
var string gCB_B7 = "11. PIVOT 7"
cb_b7_show = input.bool(true, "Show Pivot 7 (upper + lower)", group=gCB_B7, tooltip="Turn both Pivot 7 lines (above and below the Central Pivot) on or off.")
cb_b7_col = input.color(#FF00FF, "Colour", group=gCB_B7, inline="b7c", tooltip="Only used when 'Same colour Pivots' is OFF.")
cb_b7_width = input.int(2, "Width", group=gCB_B7, inline="b7c", minval=1, maxval=5)
cb_b7_base_mult = input.float(35.0, "Distance multiplier", group=gCB_B7, minval=0.1, step=0.5, tooltip="How far Pivot 7 sits from the Central Pivot, in ATR steps. Bigger = further away.")
cb_b7_ovr = input.bool(false, "Override shared calculation", group=gCB_B7, tooltip="OFF = uses section 4 (Shared Calculation). ON = uses the 4 settings below for this pivot only.")
cb_b7_as_in = input.int(13, "ATR short length (override)", group=gCB_B7, minval=1)
cb_b7_al_in = input.int(100, "ATR long length (override)", group=gCB_B7, minval=1)
cb_b7_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gCB_B7, minval=0.01, step=0.1)
cb_b7_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gCB_B7, minval=0.01, step=0.1)
// Use this pivot's own override values if ticked, otherwise the shared ones from section 4
cb_b7_atr_short = cb_b7_ovr ? cb_b7_as_in : cb_atr_short
cb_b7_atr_long = cb_b7_ovr ? cb_b7_al_in : cb_atr_long
cb_b7_vol_min = cb_b7_ovr ? cb_b7_vmin_in : cb_vol_min
cb_b7_vol_max = cb_b7_ovr ? cb_b7_vmax_in : cb_vol_max
// ═══════════════════════════════════════════════════════════════════════
// 12. EXTRA TIMEFRAME 1 (full-width lines from a higher timeframe)
// ═══════════════════════════════════════════════════════════════════════
var string gTF1 = "12. Extra Timeframe 1"
tf1_show = input.bool(false, "Show", group=gTF1, tooltip="Turn this set of timeframe lines on or off.")
tf1_tf = input.timeframe("60", "Timeframe", group=gTF1, tooltip="Timeframe the pivots are calculated on (e.g. 60 = 1 hour, D = 1 day).")
tf1_col = input.color(color.yellow, "Colour", group=gTF1, inline="t1c")
tf1_width = input.int(3, "Width", group=gTF1, inline="t1c", minval=1, maxval=5)
tf1_ovr = input.bool(false, "Override main settings", group=gTF1, tooltip="OFF = uses the main pivots' Central Pivot length, Shared Calculation (section 4) and Distance multipliers. ON = uses the settings below for this timeframe only.")
tf1_len_in = input.int(987, "Central Pivot length (override)", group=gTF1, minval=2)
tf1_as_in = input.int(13, "ATR short length (override)", group=gTF1, minval=1)
tf1_al_in = input.int(100, "ATR long length (override)", group=gTF1, minval=1)
tf1_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gTF1, minval=0.01, step=0.1)
tf1_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gTF1, minval=0.01, step=0.1)
tf1_m1_in = input.float(5.0, "Pivot 1 distance multiplier (override)", group=gTF1, minval=0.1, step=0.5)
tf1_m2_in = input.float(10.0, "Pivot 2 distance multiplier (override)", group=gTF1, minval=0.1, step=0.5)
tf1_m3_in = input.float(15.0, "Pivot 3 distance multiplier (override)", group=gTF1, minval=0.1, step=0.5)
tf1_m4_in = input.float(20.0, "Pivot 4 distance multiplier (override)", group=gTF1, minval=0.1, step=0.5)
tf1_m5_in = input.float(25.0, "Pivot 5 distance multiplier (override)", group=gTF1, minval=0.1, step=0.5)
tf1_m6_in = input.float(30.0, "Pivot 6 distance multiplier (override)", group=gTF1, minval=0.1, step=0.5)
tf1_m7_in = input.float(35.0, "Pivot 7 distance multiplier (override)", group=gTF1, minval=0.1, step=0.5)
// Use this timeframe's own override values if ticked, otherwise copy the main pivot settings
tf1_len = tf1_ovr ? tf1_len_in : cb_epi_len
tf1_as = tf1_ovr ? tf1_as_in : cb_atr_short
tf1_al = tf1_ovr ? tf1_al_in : cb_atr_long
tf1_vmin = tf1_ovr ? tf1_vmin_in : cb_vol_min
tf1_vmax = tf1_ovr ? tf1_vmax_in : cb_vol_max
tf1_m1 = tf1_ovr ? tf1_m1_in : cb_b1_base_mult
tf1_m2 = tf1_ovr ? tf1_m2_in : cb_b2_base_mult
tf1_m3 = tf1_ovr ? tf1_m3_in : cb_b3_base_mult
tf1_m4 = tf1_ovr ? tf1_m4_in : cb_b4_base_mult
tf1_m5 = tf1_ovr ? tf1_m5_in : cb_b5_base_mult
tf1_m6 = tf1_ovr ? tf1_m6_in : cb_b6_base_mult
tf1_m7 = tf1_ovr ? tf1_m7_in : cb_b7_base_mult
// ═══════════════════════════════════════════════════════════════════════
// 13. EXTRA TIMEFRAME 2 (full-width lines from a higher timeframe)
// ═══════════════════════════════════════════════════════════════════════
var string gTF2 = "13. Extra Timeframe 2"
tf2_show = input.bool(false, "Show", group=gTF2, tooltip="Turn this set of timeframe lines on or off.")
tf2_tf = input.timeframe("D", "Timeframe", group=gTF2, tooltip="Timeframe the pivots are calculated on (e.g. 60 = 1 hour, D = 1 day).")
tf2_col = input.color(color.red, "Colour", group=gTF2, inline="t2c")
tf2_width = input.int(3, "Width", group=gTF2, inline="t2c", minval=1, maxval=5)
tf2_ovr = input.bool(false, "Override main settings", group=gTF2, tooltip="OFF = uses the main pivots' Central Pivot length, Shared Calculation (section 4) and Distance multipliers. ON = uses the settings below for this timeframe only.")
tf2_len_in = input.int(987, "Central Pivot length (override)", group=gTF2, minval=2)
tf2_as_in = input.int(13, "ATR short length (override)", group=gTF2, minval=1)
tf2_al_in = input.int(100, "ATR long length (override)", group=gTF2, minval=1)
tf2_vmin_in = input.float(0.5, "Volatility ratio MIN (override)", group=gTF2, minval=0.01, step=0.1)
tf2_vmax_in = input.float(2.0, "Volatility ratio MAX (override)", group=gTF2, minval=0.01, step=0.1)
tf2_m1_in = input.float(5.0, "Pivot 1 distance multiplier (override)", group=gTF2, minval=0.1, step=0.5)
tf2_m2_in = input.float(10.0, "Pivot 2 distance multiplier (override)", group=gTF2, minval=0.1, step=0.5)
tf2_m3_in = input.float(15.0, "Pivot 3 distance multiplier (override)", group=gTF2, minval=0.1, step=0.5)
tf2_m4_in = input.float(20.0, "Pivot 4 distance multiplier (override)", group=gTF2, minval=0.1, step=0.5)
tf2_m5_in = input.float(25.0, "Pivot 5 distance multiplier (override)", group=gTF2, minval=0.1, step=0.5)
tf2_m6_in = input.float(30.0, "Pivot 6 distance multiplier (override)", group=gTF2, minval=0.1, step=0.5)
tf2_m7_in = input.float(35.0, "Pivot 7 distance multiplier (override)", group=gTF2, minval=0.1, step=0.5)
// Use this timeframe's own override values if ticked, otherwise copy the main pivot settings
tf2_len = tf2_ovr ? tf2_len_in : cb_epi_len
tf2_as = tf2_ovr ? tf2_as_in : cb_atr_short
tf2_al = tf2_ovr ? tf2_al_in : cb_atr_long
tf2_vmin = tf2_ovr ? tf2_vmin_in : cb_vol_min
tf2_vmax = tf2_ovr ? tf2_vmax_in : cb_vol_max
tf2_m1 = tf2_ovr ? tf2_m1_in : cb_b1_base_mult
tf2_m2 = tf2_ovr ? tf2_m2_in : cb_b2_base_mult
tf2_m3 = tf2_ovr ? tf2_m3_in : cb_b3_base_mult
tf2_m4 = tf2_ovr ? tf2_m4_in : cb_b4_base_mult
tf2_m5 = tf2_ovr ? tf2_m5_in : cb_b5_base_mult
tf2_m6 = tf2_ovr ? tf2_m6_in : cb_b6_base_mult
tf2_m7 = tf2_ovr ? tf2_m7_in : cb_b7_base_mult
// ═══════════════════════════════════════════════════════════════════════
// 14. BANDWIDTH TABLE
// ═══════════════════════════════════════════════════════════════════════
var string gBW = "14. BandWidth Table"
bw_show = input.bool(false, "Show BandWidth", group=gBW, tooltip="Shows the gap in points between Pivot 1 and Pivot 2 (one step).")
bw_pos = input.string(position.bottom_right, "Position", options=[position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], group=gBW)
bw_size = input.string(size.normal, "Text size", options=[size.tiny, size.small, size.normal, size.large, size.huge], group=gBW)
bw_txt_col = input.color(color.white, "Text colour", group=gBW, inline="bwc")
bw_bg_col = input.color(color.new(color.black, 20), "Background", group=gBW, inline="bwc")
// ── Final plot colours (same colour override or original multi-colour) ──
// If 'Same colour Pivots' is ON, everything uses that one colour.
// If it is OFF, each pivot uses the colour chosen in its own group.
cb_epi_c = pv_sameOn ? pv_sameCol : cb_epi_col
cb_b1_c = pv_sameOn ? pv_sameCol : cb_b1_col
cb_b2_c = pv_sameOn ? pv_sameCol : cb_b2_col
cb_b3_c = pv_sameOn ? pv_sameCol : cb_b3_col
cb_b4_c = pv_sameOn ? pv_sameCol : cb_b4_col
cb_b5_c = pv_sameOn ? pv_sameCol : cb_b5_col
cb_b6_c = pv_sameOn ? pv_sameCol : cb_b6_col
cb_b7_c = pv_sameOn ? pv_sameCol : cb_b7_col
// ── Calculation window ──────────────────────────────────────────────────
// Works out whether the current bar is inside the chosen session hours.
// The session filter only applies on intraday charts. On daily and above, every bar counts.
cb_useFilter = cb_sess_on and timeframe.isintraday
cb_timeVal = time(timeframe.period, cb_sessStr, cb_tz)
cb_inSess = cb_useFilter ? not na(cb_timeVal) : true
// ── Calculation helpers ─────────────────────────────────────────────────
// Same maths as ta.ema and ta.rma (SMA-seeded), but the state only updates
// on counted bars. Other bars just hold the last value.
// Each helper is called once per bar at global scope, so history stays valid.
// f_ema = an EMA that only moves on bars inside the session
f_ema(src, len, active) =>
var float state = na
if active
alpha = 2.0 / (len + 1)
state := na(state) ? src : alpha * src + (1 - alpha) * state
state
// f_rma = the smoothing used inside ATR, also only moving on bars inside the session.
// It collects the first 'len' values, averages them to get started, then smooths from there.
f_rma(src, len, active) =>
var float state = na
var int n = 0
var float acc = 0.0
if active
n += 1
if n < len
acc += src
else if n == len
acc += src
state := acc / len
else
state := (state * (len - 1) + src) / len
state
// ── Bar counter and true range ──────────────────────────────────────────
// True range compares against the last counted close, so the first bar
// after the gap links straight back to the previous counted close.
var int cb_sessBars = 0
var float cb_prevSessClose = na
cb_tr = na(cb_prevSessClose) ? high - low : math.max(high - low, math.abs(high - cb_prevSessClose), math.abs(low - cb_prevSessClose))
if cb_inSess
cb_sessBars += 1
cb_prevSessClose := close
// ── Central Pivot Calculation ───────────────────────────────────────────────
// The Central Pivot is the session-only EMA of the close.
// cb_isReady stops anything being drawn until enough session bars have loaded.
cb_epiRaw = f_ema(close, cb_epi_len, cb_inSess)
cb_isReady = cb_sessBars > cb_epi_len
cb_epi = cb_isReady ? cb_epiRaw : cb_epiRaw[1]
// ── Band Calculations ───────────────────────────────────────────────────
// Each pivot is worked out the same way:
// 1. Short ATR = how much price is moving right now
// 2. Long ATR = how much price normally moves
// 3. Ratio = long ATR / short ATR, kept between the MIN and MAX settings
// 4. Distance = distance multiplier x ratio x short ATR
// 5. Upper pivot = Central Pivot + distance, lower pivot = Central Pivot - distance
cb_b1_atr = f_rma(cb_tr, cb_b1_atr_short, cb_inSess)
cb_b1_lAtr = f_rma(cb_tr, cb_b1_atr_long, cb_inSess)
cb_b1_atr_v = cb_isReady ? cb_b1_atr : cb_b1_atr[1]
cb_b1_lAtr_v = cb_isReady ? cb_b1_lAtr : cb_b1_lAtr[1]
cb_b1_ratio = math.min(math.max(cb_b1_lAtr_v / cb_b1_atr_v, cb_b1_vol_min), cb_b1_vol_max)
cb_b1_upper = cb_epi + cb_b1_base_mult * cb_b1_ratio * cb_b1_atr_v
cb_b1_lower = cb_epi - cb_b1_base_mult * cb_b1_ratio * cb_b1_atr_v
cb_b2_atr = f_rma(cb_tr, cb_b2_atr_short, cb_inSess)
cb_b2_lAtr = f_rma(cb_tr, cb_b2_atr_long, cb_inSess)
cb_b2_atr_v = cb_isReady ? cb_b2_atr : cb_b2_atr[1]
cb_b2_lAtr_v = cb_isReady ? cb_b2_lAtr : cb_b2_lAtr[1]
cb_b2_ratio = math.min(math.max(cb_b2_lAtr_v / cb_b2_atr_v, cb_b2_vol_min), cb_b2_vol_max)
cb_b2_upper = cb_epi + cb_b2_base_mult * cb_b2_ratio * cb_b2_atr_v
cb_b2_lower = cb_epi - cb_b2_base_mult * cb_b2_ratio * cb_b2_atr_v
cb_b3_atr = f_rma(cb_tr, cb_b3_atr_short, cb_inSess)
cb_b3_lAtr = f_rma(cb_tr, cb_b3_atr_long, cb_inSess)
cb_b3_atr_v = cb_isReady ? cb_b3_atr : cb_b3_atr[1]
cb_b3_lAtr_v = cb_isReady ? cb_b3_lAtr : cb_b3_lAtr[1]
cb_b3_ratio = math.min(math.max(cb_b3_lAtr_v / cb_b3_atr_v, cb_b3_vol_min), cb_b3_vol_max)
cb_b3_upper = cb_epi + cb_b3_base_mult * cb_b3_ratio * cb_b3_atr_v
cb_b3_lower = cb_epi - cb_b3_base_mult * cb_b3_ratio * cb_b3_atr_v
cb_b4_atr = f_rma(cb_tr, cb_b4_atr_short, cb_inSess)
cb_b4_lAtr = f_rma(cb_tr, cb_b4_atr_long, cb_inSess)
cb_b4_atr_v = cb_isReady ? cb_b4_atr : cb_b4_atr[1]
cb_b4_lAtr_v = cb_isReady ? cb_b4_lAtr : cb_b4_lAtr[1]
cb_b4_ratio = math.min(math.max(cb_b4_lAtr_v / cb_b4_atr_v, cb_b4_vol_min), cb_b4_vol_max)
cb_b4_upper = cb_epi + cb_b4_base_mult * cb_b4_ratio * cb_b4_atr_v
cb_b4_lower = cb_epi - cb_b4_base_mult * cb_b4_ratio * cb_b4_atr_v
cb_b5_atr = f_rma(cb_tr, cb_b5_atr_short, cb_inSess)
cb_b5_lAtr = f_rma(cb_tr, cb_b5_atr_long, cb_inSess)
cb_b5_atr_v = cb_isReady ? cb_b5_atr : cb_b5_atr[1]
cb_b5_lAtr_v = cb_isReady ? cb_b5_lAtr : cb_b5_lAtr[1]
cb_b5_ratio = math.min(math.max(cb_b5_lAtr_v / cb_b5_atr_v, cb_b5_vol_min), cb_b5_vol_max)
cb_b5_upper = cb_epi + cb_b5_base_mult * cb_b5_ratio * cb_b5_atr_v
cb_b5_lower = cb_epi - cb_b5_base_mult * cb_b5_ratio * cb_b5_atr_v
cb_b6_atr = f_rma(cb_tr, cb_b6_atr_short, cb_inSess)
cb_b6_lAtr = f_rma(cb_tr, cb_b6_atr_long, cb_inSess)
cb_b6_atr_v = cb_isReady ? cb_b6_atr : cb_b6_atr[1]
cb_b6_lAtr_v = cb_isReady ? cb_b6_lAtr : cb_b6_lAtr[1]
cb_b6_ratio = math.min(math.max(cb_b6_lAtr_v / cb_b6_atr_v, cb_b6_vol_min), cb_b6_vol_max)
cb_b6_upper = cb_epi + cb_b6_base_mult * cb_b6_ratio * cb_b6_atr_v
cb_b6_lower = cb_epi - cb_b6_base_mult * cb_b6_ratio * cb_b6_atr_v
cb_b7_atr = f_rma(cb_tr, cb_b7_atr_short, cb_inSess)
cb_b7_lAtr = f_rma(cb_tr, cb_b7_atr_long, cb_inSess)
cb_b7_atr_v = cb_isReady ? cb_b7_atr : cb_b7_atr[1]
cb_b7_lAtr_v = cb_isReady ? cb_b7_lAtr : cb_b7_lAtr[1]
cb_b7_ratio = math.min(math.max(cb_b7_lAtr_v / cb_b7_atr_v, cb_b7_vol_min), cb_b7_vol_max)
cb_b7_upper = cb_epi + cb_b7_base_mult * cb_b7_ratio * cb_b7_atr_v
cb_b7_lower = cb_epi - cb_b7_base_mult * cb_b7_ratio * cb_b7_atr_v
// ── Plots ────────────────────────────────────────────────────────────────
// Every level has two plots: one drawn as a line and one drawn as dots.
// Only the style picked in 'Pivot appearance' gets a value. The other is left empty (na) so it stays hidden.
plot(cb_epi_show and pv_lines ? cb_epi : na, "Central Pivot (Lines)", color=cb_epi_c, linewidth=cb_epi_width, style=plot.style_line)
plot(cb_epi_show and pv_dots ? cb_epi : na, "Central Pivot (Dots)", color=cb_epi_c, linewidth=cb_epi_width, style=plot.style_circles)
plot(cb_b1_show and pv_lines ? cb_b1_upper : na, "Pivot 1 Upper (Lines)", color=cb_b1_c, linewidth=cb_b1_width, style=plot.style_line)
plot(cb_b1_show and pv_dots ? cb_b1_upper : na, "Pivot 1 Upper (Dots)", color=cb_b1_c, linewidth=cb_b1_width, style=plot.style_circles)
plot(cb_b1_show and pv_lines ? cb_b1_lower : na, "Pivot 1 Lower (Lines)", color=cb_b1_c, linewidth=cb_b1_width, style=plot.style_line)
plot(cb_b1_show and pv_dots ? cb_b1_lower : na, "Pivot 1 Lower (Dots)", color=cb_b1_c, linewidth=cb_b1_width, style=plot.style_circles)
plot(cb_b2_show and pv_lines ? cb_b2_upper : na, "Pivot 2 Upper (Lines)", color=cb_b2_c, linewidth=cb_b2_width, style=plot.style_line)
plot(cb_b2_show and pv_dots ? cb_b2_upper : na, "Pivot 2 Upper (Dots)", color=cb_b2_c, linewidth=cb_b2_width, style=plot.style_circles)
plot(cb_b2_show and pv_lines ? cb_b2_lower : na, "Pivot 2 Lower (Lines)", color=cb_b2_c, linewidth=cb_b2_width, style=plot.style_line)
plot(cb_b2_show and pv_dots ? cb_b2_lower : na, "Pivot 2 Lower (Dots)", color=cb_b2_c, linewidth=cb_b2_width, style=plot.style_circles)
plot(cb_b3_show and pv_lines ? cb_b3_upper : na, "Pivot 3 Upper (Lines)", color=cb_b3_c, linewidth=cb_b3_width, style=plot.style_line)
plot(cb_b3_show and pv_dots ? cb_b3_upper : na, "Pivot 3 Upper (Dots)", color=cb_b3_c, linewidth=cb_b3_width, style=plot.style_circles)
plot(cb_b3_show and pv_lines ? cb_b3_lower : na, "Pivot 3 Lower (Lines)", color=cb_b3_c, linewidth=cb_b3_width, style=plot.style_line)
plot(cb_b3_show and pv_dots ? cb_b3_lower : na, "Pivot 3 Lower (Dots)", color=cb_b3_c, linewidth=cb_b3_width, style=plot.style_circles)
plot(cb_b4_show and pv_lines ? cb_b4_upper : na, "Pivot 4 Upper (Lines)", color=cb_b4_c, linewidth=cb_b4_width, style=plot.style_line)
plot(cb_b4_show and pv_dots ? cb_b4_upper : na, "Pivot 4 Upper (Dots)", color=cb_b4_c, linewidth=cb_b4_width, style=plot.style_circles)
plot(cb_b4_show and pv_lines ? cb_b4_lower : na, "Pivot 4 Lower (Lines)", color=cb_b4_c, linewidth=cb_b4_width, style=plot.style_line)
plot(cb_b4_show and pv_dots ? cb_b4_lower : na, "Pivot 4 Lower (Dots)", color=cb_b4_c, linewidth=cb_b4_width, style=plot.style_circles)
plot(cb_b5_show and pv_lines ? cb_b5_upper : na, "Pivot 5 Upper (Lines)", color=cb_b5_c, linewidth=cb_b5_width, style=plot.style_line)
plot(cb_b5_show and pv_dots ? cb_b5_upper : na, "Pivot 5 Upper (Dots)", color=cb_b5_c, linewidth=cb_b5_width, style=plot.style_circles)
plot(cb_b5_show and pv_lines ? cb_b5_lower : na, "Pivot 5 Lower (Lines)", color=cb_b5_c, linewidth=cb_b5_width, style=plot.style_line)
plot(cb_b5_show and pv_dots ? cb_b5_lower : na, "Pivot 5 Lower (Dots)", color=cb_b5_c, linewidth=cb_b5_width, style=plot.style_circles)
plot(cb_b6_show and pv_lines ? cb_b6_upper : na, "Pivot 6 Upper (Lines)", color=cb_b6_c, linewidth=cb_b6_width, style=plot.style_line)
plot(cb_b6_show and pv_dots ? cb_b6_upper : na, "Pivot 6 Upper (Dots)", color=cb_b6_c, linewidth=cb_b6_width, style=plot.style_circles)
plot(cb_b6_show and pv_lines ? cb_b6_lower : na, "Pivot 6 Lower (Lines)", color=cb_b6_c, linewidth=cb_b6_width, style=plot.style_line)
plot(cb_b6_show and pv_dots ? cb_b6_lower : na, "Pivot 6 Lower (Dots)", color=cb_b6_c, linewidth=cb_b6_width, style=plot.style_circles)
plot(cb_b7_show and pv_lines ? cb_b7_upper : na, "Pivot 7 Upper (Lines)", color=cb_b7_c, linewidth=cb_b7_width, style=plot.style_line)
plot(cb_b7_show and pv_dots ? cb_b7_upper : na, "Pivot 7 Upper (Dots)", color=cb_b7_c, linewidth=cb_b7_width, style=plot.style_circles)
plot(cb_b7_show and pv_lines ? cb_b7_lower : na, "Pivot 7 Lower (Lines)", color=cb_b7_c, linewidth=cb_b7_width, style=plot.style_line)
plot(cb_b7_show and pv_dots ? cb_b7_lower : na, "Pivot 7 Lower (Dots)", color=cb_b7_c, linewidth=cb_b7_width, style=plot.style_circles)
// ═══════════════════════════════════════════════════════════════════════
// HIGHER TIMEFRAME PIVOTS (extra timeframes, drawn as full-width lines)
// Same Pivot / Central Pivot maths as above, calculated on each chosen
// timeframe and drawn as live horizontal lines across the screen.
// Uses line objects (the chart is already near the 64 plot limit).
// ═══════════════════════════════════════════════════════════════════════
// f_mtf = the whole pivot calculation packed into one function, so it can be run on another timeframe.
// It returns the Central Pivot plus the upper and lower value of all 7 pivots (15 numbers in total).
f_mtf(int epiLen, int atrS, int atrL, float vMin, float vMax, bool useFilter, string sess, string tz, float m1, float m2, float m3, float m4, float m5, float m6, float m7) =>
mSess = useFilter ? not na(time(timeframe.period, sess, tz)) : true
var int mBars = 0
var float mPrevClose = na
mTr = na(mPrevClose) ? high - low : math.max(high - low, math.abs(high - mPrevClose), math.abs(low - mPrevClose))
if mSess
mBars += 1
mPrevClose := close
mEpiRaw = f_ema(close, epiLen, mSess)
mReady = mBars > epiLen
mEpi = mReady ? mEpiRaw : mEpiRaw[1]
mAtr = f_rma(mTr, atrS, mSess)
mLAtr = f_rma(mTr, atrL, mSess)
mAtrV = mReady ? mAtr : mAtr[1]
mLAtrV = mReady ? mLAtr : mLAtr[1]
mRatio = math.min(math.max(mLAtrV / mAtrV, vMin), vMax)
mStep = mRatio * mAtrV
[mEpi,
mEpi + m1 * mStep, mEpi - m1 * mStep,
mEpi + m2 * mStep, mEpi - m2 * mStep,
mEpi + m3 * mStep, mEpi - m3 * mStep,
mEpi + m4 * mStep, mEpi - m4 * mStep,
mEpi + m5 * mStep, mEpi - m5 * mStep,
mEpi + m6 * mStep, mEpi - m6 * mStep,
mEpi + m7 * mStep, mEpi - m7 * mStep]
// Ask TradingView to run f_mtf on each extra timeframe and hand back the 15 levels
[t1_e, t1_u1, t1_l1, t1_u2, t1_l2, t1_u3, t1_l3, t1_u4, t1_l4, t1_u5, t1_l5, t1_u6, t1_l6, t1_u7, t1_l7] = request.security(syminfo.tickerid, tf1_tf, f_mtf(tf1_len, tf1_as, tf1_al, tf1_vmin, tf1_vmax, cb_useFilter, cb_sessStr, cb_tz, tf1_m1, tf1_m2, tf1_m3, tf1_m4, tf1_m5, tf1_m6, tf1_m7))
[t2_e, t2_u1, t2_l1, t2_u2, t2_l2, t2_u3, t2_l3, t2_u4, t2_l4, t2_u5, t2_l5, t2_u6, t2_l6, t2_u7, t2_l7] = request.security(syminfo.tickerid, tf2_tf, f_mtf(tf2_len, tf2_as, tf2_al, tf2_vmin, tf2_vmax, cb_useFilter, cb_sessStr, cb_tz, tf2_m1, tf2_m2, tf2_m3, tf2_m4, tf2_m5, tf2_m6, tf2_m7))
// The line objects for each extra timeframe are stored here so they can be reused
var array<line> tf1_lines = array.new<line>()
var array<line> tf2_lines = array.new<line>()
// Creates the lines once, then just moves them to the latest value each tick.
f_drawLines(array<line> ln, array<float> vals, color c, int w, bool on) =>
if ln.size() == 0
for i = 0 to vals.size() - 1
ln.push(line.new(bar_index - 1, nz(vals.get(i)), bar_index, nz(vals.get(i)), extend=extend.both, color=c, width=w))
for i = 0 to vals.size() - 1
y = vals.get(i)
l = ln.get(i)
if on and not na(y)
line.set_xy1(l, bar_index - 1, y)
line.set_xy2(l, bar_index, y)
line.set_color(l, c)
line.set_width(l, w)
else
line.set_color(l, na)
// Only draw on the latest bar. Lines extend both ways so they run right across the chart.
if barstate.islast
f_drawLines(tf1_lines, array.from(t1_e, t1_u1, t1_l1, t1_u2, t1_l2, t1_u3, t1_l3, t1_u4, t1_l4, t1_u5, t1_l5, t1_u6, t1_l6, t1_u7, t1_l7), tf1_col, tf1_width, tf1_show)
f_drawLines(tf2_lines, array.from(t2_e, t2_u1, t2_l1, t2_u2, t2_l2, t2_u3, t2_l3, t2_u4, t2_l4, t2_u5, t2_l5, t2_u6, t2_l6, t2_u7, t2_l7), tf2_col, tf2_width, tf2_show)
// ═══════════════════════════════════════════════════════════════════════
// BANDWIDTH TABLE (display only, reads existing band values)
// Shows the gap in points between Pivot 1 and Pivot 2 (one step)
// ═══════════════════════════════════════════════════════════════════════
bw_value = cb_b2_upper - cb_b1_upper
// The table is created once. On the latest bar it is either filled in or left blank.
var table bw_table = table.new(bw_pos, 1, 1, border_width=1)
if barstate.islast
if bw_show
table.cell(bw_table, 0, 0, "BandWidth: " + (na(bw_value) ? "n/a" : str.tostring(bw_value, "#")), text_color=bw_txt_col, text_size=bw_size, bgcolor=bw_bg_col)
else
table.cell(bw_table, 0, 0, "", text_size=size.tiny, bgcolor=na)