本页源码来自 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/
// © Mo Alqaq
//
// Pro Trader Pivot Points — v1.0
//
// Standard (floor) pivot points for:
// • Trading sessions (Tokyo / London / New York) — previous completed session
// • Daily — previous day
// • Weekly — previous week
//
// PP = (High + Low + Close) / 3
// R1 = 2·PP − Low S1 = 2·PP − High
// R2 = PP + (High − Low) S2 = PP − (High − Low)
// R3 = High + 2·(PP − Low) S3 = Low − 2·(High − PP)
//
// All levels are drawn as short projected lines to the right of the last bar.
// Session pivots follow a lifecycle: they appear when a session ends and are removed
// when the last candle of the following session starts. London and New York start
// times adjust automatically for daylight saving (optional).
// Daily scenario markers (▲ above / ▼ below / ● ranging) sit next to the daily and weekly Pivot.
//
// Educational tool only. Pivot levels are reference points, not trade signals.
//@version=6
indicator("Pro Trader Pivot Points", shorttitle = "Pro Trader Pivots", overlay = true, max_lines_count = 100, max_labels_count = 100)
// ═════════════════════════════════════════════════════════════════════════════
// INPUTS
// ═════════════════════════════════════════════════════════════════════════════
// ── Session pivots ─────────────────────────────────────────────────────────
G_SES = "SESSION PIVOTS · appear when a session ENDS · disappear when the NEXT session's LAST candle starts"
TT_SES = "Session pivots use the High, Low and Close of the session that just ended.\n\n" +
"LIFECYCLE (only one session set is on the chart at a time):\n" +
"• Tokyo ends → Tokyo pivots appear (for London analysis). They DISAPPEAR when the last candle of London starts.\n" +
"• London ends → London pivots appear (for New York analysis). They DISAPPEAR when the last candle of New York starts.\n" +
"• New York ends → New York pivots appear (for the next Tokyo session). They DISAPPEAR when the last candle of Tokyo starts.\n\n" +
"Each session runs from its start time until the next session's start time. Session pivots are available on intraday charts only (most accurate on 1H and below)."
showSes = input.bool(true, "Show session pivots", group = G_SES, tooltip = TT_SES)
sesTz = input.string("UTC+3", "Start times are in time zone", options = ["UTC-10", "UTC-9", "UTC-8", "UTC-7", "UTC-6", "UTC-5", "UTC-4", "UTC-3", "UTC-2", "UTC-1", "UTC+0", "UTC+1", "UTC+2", "UTC+3", "UTC+3:30", "UTC+4", "UTC+5", "UTC+5:30", "UTC+6", "UTC+7", "UTC+8", "UTC+9", "UTC+10", "UTC+11", "UTC+12"], group = G_SES,
tooltip = "Time zone of the session start times below. Default UTC+3 (Amman / Riyadh / Istanbul). Pick your own time zone from the list and enter the start times in that time zone.")
autoDst = input.bool(true, "Auto-adjust London & New York for daylight saving", group = G_SES,
tooltip = "ON (recommended): enter the London and New York start times for SUMMER time. When the UK switches to winter time (GMT, late October to late March), London automatically starts 1 hour later. When the US switches to winter time (EST, early November to mid March), New York automatically starts 1 hour later. Each market switches on its own date, so you never need to change the times by hand.\n\nWith the defaults (UTC+3): London 10:00 in summer → 11:00 in winter; New York 16:00 in summer → 17:00 in winter. Tokyo has no daylight saving and never shifts.\n\nOFF: the start times are used exactly as entered all year round.")
tkOn = input.bool(true, "Tokyo ", inline = "tk", group = G_SES)
tkH = input.int(3, "Start", minval = 0, maxval = 23, inline = "tk", group = G_SES)
tkM = input.int(0, ":", minval = 0, maxval = 59, inline = "tk", group = G_SES,
tooltip = "Tokyo session start (hour : minute). Tokyo pivots appear at the END of Tokyo (= London start) and DISAPPEAR when the LAST candle of the London session starts.")
lnOn = input.bool(true, "London ", inline = "ln", group = G_SES)
lnH = input.int(10, "Start", minval = 0, maxval = 23, inline = "ln", group = G_SES)
lnM = input.int(0, ":", minval = 0, maxval = 59, inline = "ln", group = G_SES,
tooltip = "London session start (hour : minute), SUMMER time if auto-adjust is on. London pivots appear at the END of London (= New York start) and DISAPPEAR when the LAST candle of the New York session starts.")
nyOn = input.bool(true, "New York ", inline = "ny", group = G_SES)
nyH = input.int(16, "Start", minval = 0, maxval = 23, inline = "ny", group = G_SES)
nyM = input.int(0, ":", minval = 0, maxval = 59, inline = "ny", group = G_SES,
tooltip = "New York session start (hour : minute), SUMMER time if auto-adjust is on. New York pivots appear at the END of New York (= Tokyo start) and DISAPPEAR when the LAST candle of the Tokyo session starts.")
sesPPLine = input.color(#9E9E9E, "Pivot line", inline = "sc1", group = G_SES)
sesPPText = input.color(#9E9E9E, "price", inline = "sc1", group = G_SES)
sesLvLine = input.color(#BDBDBD, "R/S line", inline = "sc2", group = G_SES)
sesLvText = input.color(#BDBDBD, "price", inline = "sc2", group = G_SES)
sesDist = input.int(20, "Distance from last bar", minval = 0, maxval = 400, inline = "sx", group = G_SES)
sesLen = input.int(18, "Length", minval = 1, maxval = 100, inline = "sx", group = G_SES,
tooltip = "Lines start this many bars to the right of the current bar and run for 'Length' bars.")
// ── Daily pivots ───────────────────────────────────────────────────────────
G_D = "DAILY PIVOTS (previous day)"
showDaily = input.bool(true, "Show daily pivots", group = G_D, tooltip = "Calculated from the previous day's High, Low and Close. Shown on charts up to the 1D timeframe.")
dPPLine = input.color(#FFD600, "Pivot line", inline = "dc1", group = G_D)
dPPText = input.color(#FFD600, "price", inline = "dc1", group = G_D)
dLvLine = input.color(#FFF59D, "R/S line", inline = "dc2", group = G_D)
dLvText = input.color(#FFF59D, "price", inline = "dc2", group = G_D)
dDist = input.int(50, "Distance from last bar", minval = 0, maxval = 400, inline = "dx", group = G_D)
dLen = input.int(18, "Length", minval = 1, maxval = 100, inline = "dx", group = G_D)
// ── Weekly pivots ──────────────────────────────────────────────────────────
G_W = "WEEKLY PIVOTS (previous week)"
showWeekly = input.bool(true, "Show weekly pivots", group = G_W, tooltip = "Calculated from the previous full week's (Monday → Sunday) High, Low and Close, so weekend trading on crypto and other 7-day markets is included. Shown on charts up to the 1W timeframe.")
wPPLine = input.color(#4FC3F7, "Pivot line", inline = "wc1", group = G_W)
wPPText = input.color(#4FC3F7, "price", inline = "wc1", group = G_W)
wLvLine = input.color(#B3E5FC, "R/S line", inline = "wc2", group = G_W)
wLvText = input.color(#B3E5FC, "price", inline = "wc2", group = G_W)
wDist = input.int(70, "Distance from last bar", minval = 0, maxval = 400, inline = "wx", group = G_W,
tooltip = "Used when daily pivots are also shown. If daily pivots are off, weekly pivots move into the daily position.")
wLen = input.int(18, "Length", minval = 1, maxval = 100, inline = "wx", group = G_W)
// ── Pivot scenarios ────────────────────────────────────────────────────────
G_SC = "PIVOT SCENARIOS (markers next to the daily & weekly Pivot price)"
showBull = input.bool(true, "A · Above PP (bullish) ▲", inline = "a", group = G_SC)
bullCol = input.color(#00C853, "", inline = "a", group = G_SC)
showBear = input.bool(true, "B · Below PP (bearish) ▼", inline = "b", group = G_SC)
bearCol = input.color(#FF1744, "", inline = "b", group = G_SC)
showRange = input.bool(true, "C · Crossing PP (ranging) ●", inline = "c", group = G_SC)
rangeCol = input.color(#FFEB3B, "", inline = "c", group = G_SC)
holdBars = input.int(3, "Bars needed to confirm 'holding' above/below PP", minval = 1, maxval = 50, group = G_SC,
tooltip = "Scenario A / B: price must CLOSE above (or below) the daily Pivot for this many consecutive bars before the ▲ / ▼ is shown.")
rangeX = input.int(3, "PP crosses that mean 'ranging'", minval = 2, maxval = 20, inline = "rg", group = G_SC)
rangeLb = input.int(20, "within last N bars", minval = 2, maxval = 500, inline = "rg", group = G_SC,
tooltip = "Scenario C: if price closes across the daily Pivot at least this many times within the last N bars (today only), the market is treated as ranging/indecisive and ● is shown. Scenario C takes priority over A and B.")
markSize = input.string(size.large, "Marker size (▲ ▼ ●)", options = [size.small, size.normal, size.large, size.huge], group = G_SC, tooltip = "Size of the scenario arrow / circle next to the daily and weekly Pivot price, independent of the price-text size.\n\nThe colours, on/off switches and marker size above apply to BOTH daily and weekly pivots. The two settings for holding bars and crosses apply to the DAILY pivot only.\n\nWEEKLY scenarios use a fixed rule based on the full current week (no settings): each daily close this week (Monday → Sunday), plus the current price, is compared with the weekly Pivot. 2 or more switches between above and below this week = ranging ●. Otherwise current price above the weekly Pivot = ▲, below = ▼.")
// ── Style ──────────────────────────────────────────────────────────────────
G_ST = "LINE & TEXT STYLE"
styleStr = input.string("Dashed", "Line style", options = ["Dashed", "Dotted", "Solid"], inline = "ls", group = G_ST)
lineWidth = input.int(1, "Width", minval = 1, maxval = 4, inline = "ls", group = G_ST)
showLabels = input.bool(true, "Show price text", inline = "tx", group = G_ST)
textSize = input.string(size.small, "Size", options = [size.tiny, size.small, size.normal, size.large, size.huge], inline = "tx", group = G_ST)
showPrefix = input.bool(false, "Prefix text with set name (e.g. 'D Pivot: ', 'London R1 (')", group = G_ST)
// ═════════════════════════════════════════════════════════════════════════════
// HELPERS
// ═════════════════════════════════════════════════════════════════════════════
lineStyle = styleStr == "Dashed" ? line.style_dashed : styleStr == "Dotted" ? line.style_dotted : line.style_solid
NO_BG = color.new(color.black, 100)
var array<line> gLines = array.new<line>()
var array<label> gLabels = array.new<label>()
clearDrawings() =>
for l in gLines
line.delete(l)
for lb in gLabels
label.delete(lb)
gLines.clear()
gLabels.clear()
// Draw PP + R1..R3 + S1..S3 from a High/Low/Close, starting at bar x1 for len bars.
drawSet(float h, float l, float c, int x1, int len, color ppLine, color ppText, color lvLine, color lvText, string pfx, string sym, color symCol) =>
float pp = (h + l + c) / 3.0
float rng = h - l
array<float> vals = array.from(pp, 2 * pp - l, pp + rng, h + 2 * (pp - l), 2 * pp - h, pp - rng, l - 2 * (h - pp))
array<string> names = array.from("Pivot", "R1", "R2", "R3", "S1", "S2", "S3")
int x2 = x1 + len
for i = 0 to 6
float v = vals.get(i)
bool isPP = i == 0
gLines.push(line.new(x1, v, x2, v, xloc = xloc.bar_index, color = isPP ? ppLine : lvLine, style = lineStyle, width = lineWidth))
if showLabels
string px = str.tostring(v, format.mintick)
string txt = pfx + (isPP ? "Pivot: " + px : names.get(i) + " (" + px + ")")
gLabels.push(label.new(x1, v, txt, xloc = xloc.bar_index, style = label.style_label_lower_left, color = NO_BG, textcolor = isPP ? ppText : lvText, size = textSize))
if sym != ""
gLabels.push(label.new(x1, pp, sym, xloc = xloc.bar_index, style = label.style_label_right, color = NO_BG, textcolor = symCol, size = markSize))
posMod(int x) => ((x % 1440) + 1440) % 1440
// ═════════════════════════════════════════════════════════════════════════════
// SESSION PIVOTS — track H/L/C of each session instance
// ═════════════════════════════════════════════════════════════════════════════
// UTC offset (minutes) of a time zone at the current bar — detects daylight saving.
tzOffsetMin(string tz) =>
int d = (hour(time, tz) * 60 + minute(time, tz)) - (hour(time, "UTC") * 60 + minute(time, "UTC"))
d > 720 ? d - 1440 : d < -720 ? d + 1440 : d
// Auto DST: start times are entered for summer; add 60 min while each market is on winter time.
int ukShift = autoDst and tzOffsetMin("Europe/London") == 0 ? 60 : 0 // GMT (winter) vs BST +60
int usShift = autoDst and tzOffsetMin("America/New_York") == -300 ? 60 : 0 // EST (winter) vs EDT -240
array<int> sStart = array.from(tkH * 60 + tkM, posMod(lnH * 60 + lnM + ukShift), posMod(nyH * 60 + nyM + usShift))
array<bool> sOn = array.from(tkOn, lnOn, nyOn)
array<string> sName = array.from("Tokyo", "London", "New York")
// Session i runs from its start until the next session's start.
durOf(int i) =>
int d = posMod(sStart.get((i + 1) % 3) - sStart.get(i))
d == 0 ? 1440 : d
int modNow = hour(time, sesTz) * 60 + minute(time, sesTz)
int sesType = -1
for i = 0 to 2
if sesType == -1 and posMod(modNow - sStart.get(i)) < durOf(i)
sesType := i
int barStartT = na
int barEndT = na
int sesDur = 0
if sesType >= 0
sesDur := durOf(sesType)
barStartT := time - posMod(modNow - sStart.get(sesType)) * 60000 - second(time, sesTz) * 1000
barEndT := barStartT + sesDur * 60000
var int curType = -1 // session currently being built
var int curStart = na
var float cH = na
var float cL = na
var float cC = na
var int shownType = -1 // last completed session (the one on the chart)
var float shH = na
var float shL = na
var float shC = na
var bool shownVis = false
if sesType >= 0
bool typeChange = sesType != curType
bool startDiff = barStartT != curStart
// A bar arriving late in its session after a gap (e.g. FX Sunday open) is merged
// into the previous session of the same type instead of becoming a tiny "stub" session.
bool isStub = (time - barStartT) > sesDur * 60000 / 2
bool newInst = curType == -1 or typeChange or (startDiff and not isStub)
if newInst
// previous session just ended → publish its pivots
if curType != -1 and sOn.get(curType)
shH := cH
shL := cL
shC := cC
shownType := curType
shownVis := true
curType := sesType
curStart := barStartT
cH := high
cL := low
cC := close
else
cH := math.max(cH, high)
cL := math.min(cL, low)
cC := close
curStart := barStartT
// Last candle of the current session has started → remove the previous session's pivots
if not na(time_close) and time_close >= barEndT and shownVis and shownType == (sesType + 2) % 3
shownVis := false
// ═════════════════════════════════════════════════════════════════════════════
// DAILY & WEEKLY PIVOTS (non-repainting: previous completed period)
// ═════════════════════════════════════════════════════════════════════════════
[dH, dL, dC] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
// Weekly pivot = previous FULL week (Monday → Sunday) High / Low / Close.
// TradingView's weekly candle starts Monday and includes every trading day of the week:
// Saturday & Sunday for crypto and other 7-day markets; Mon–Fri (with the Sunday-evening
// FX open counted as Monday) for forex, stocks and futures. Previous completed week only → no repainting.
[wH, wL, wC] = request.security(syminfo.tickerid, "W", [high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
float dPP = (dH + dL + dC) / 3.0
tfSec = timeframe.in_seconds()
sesActive = showSes and timeframe.isintraday
dailyActive = showDaily and tfSec <= 86400 and not na(dPP)
weeklyActive = showWeekly and tfSec <= 604800 and not na(wH)
// ═════════════════════════════════════════════════════════════════════════════
// DAILY SCENARIOS A / B / C
// ═════════════════════════════════════════════════════════════════════════════
var array<int> crossBars = array.new<int>()
var int aboveRun = 0
var int belowRun = 0
bool pivChanged = na(dPP[1]) or dPP != dPP[1]
if pivChanged
crossBars.clear()
aboveRun := 0
belowRun := 0
bool crossedPP = not pivChanged and not na(dPP) and ((close > dPP and close[1] <= dPP) or (close < dPP and close[1] >= dPP))
if crossedPP
crossBars.push(bar_index)
while crossBars.size() > 0 and bar_index - crossBars.first() >= rangeLb
crossBars.shift()
aboveRun := close > dPP ? aboveRun + 1 : 0
belowRun := close < dPP ? belowRun + 1 : 0
// 0 = none, 1 = bullish (A), 2 = bearish (B), 3 = ranging (C)
int scenario = crossBars.size() >= rangeX ? 3 : aboveRun >= holdBars ? 1 : belowRun >= holdBars ? 2 : 0
string dSym = ""
color dSymC = na
if scenario == 3 and showRange
dSym := "●"
dSymC := rangeCol
else if scenario == 1 and showBull
dSym := "▲"
dSymC := bullCol
else if scenario == 2 and showBear
dSym := "▼"
dSymC := bearCol
// ═════════════════════════════════════════════════════════════════════════════
// WEEKLY SCENARIOS A / B / C — fixed rule over the full current week
// Sequence = side (above/below weekly PP) of every completed daily close this
// week + the current price. 2+ side switches → ranging; otherwise the side of
// the current price decides bullish / bearish. Timeframe-independent.
// ═════════════════════════════════════════════════════════════════════════════
float wPP = (wH + wL + wC) / 3.0
var array<int> wDaySides = array.new<int>()
bool wPivChanged = na(wPP[1]) or wPP != wPP[1]
bool newDay = timeframe.change("D")
if wPivChanged
wDaySides.clear()
else if newDay and not na(wPP)
float prevDayClose = close[1]
if prevDayClose != wPP
wDaySides.push(prevDayClose > wPP ? 1 : -1)
int wCurSide = na(wPP) or close == wPP ? 0 : close > wPP ? 1 : -1
int wSwitches = 0
int wLast = 0
for sd in wDaySides
if wLast != 0 and sd != wLast
wSwitches += 1
wLast := sd
if wCurSide != 0 and wLast != 0 and wCurSide != wLast
wSwitches += 1
int wScenario = na(wPP) ? 0 : wSwitches >= 2 ? 3 : wCurSide == 1 ? 1 : wCurSide == -1 ? 2 : 0
string wSym = ""
color wSymC = na
if wScenario == 3 and showRange
wSym := "●"
wSymC := rangeCol
else if wScenario == 1 and showBull
wSym := "▲"
wSymC := bullCol
else if wScenario == 2 and showBear
wSym := "▼"
wSymC := bearCol
// ═════════════════════════════════════════════════════════════════════════════
// DRAW (once, on the last bar — lines are projected to the right)
// ═════════════════════════════════════════════════════════════════════════════
if barstate.islast
clearDrawings()
if sesActive and shownVis and shownType >= 0
drawSet(shH, shL, shC, bar_index + sesDist, sesLen, sesPPLine, sesPPText, sesLvLine, sesLvText,
showPrefix ? sName.get(shownType) + " " : "", "", na)
if dailyActive
drawSet(dH, dL, dC, bar_index + dDist, dLen, dPPLine, dPPText, dLvLine, dLvText,
showPrefix ? "D " : "", dSym, dSymC)
if weeklyActive
int wx = dailyActive ? wDist : dDist
drawSet(wH, wL, wC, bar_index + wx, wLen, wPPLine, wPPText, wLvLine, wLvText,
showPrefix ? "W " : "", wSym, wSymC)
// ═════════════════════════════════════════════════════════════════════════════
// ALERTS (create them from the TradingView "Create alert" dialog)
// ═════════════════════════════════════════════════════════════════════════════
bool dCrossUp = ta.crossover(close, dPP)
bool dCrossDown = ta.crossunder(close, dPP)
bool wCrossUp = ta.crossover(close, wPP)
bool wCrossDown = ta.crossunder(close, wPP)
bool sesNewSet = sesActive and shownVis and not shownVis[1]
alertcondition(dCrossUp, "Price crossed ABOVE daily Pivot", "{{ticker}} crossed above the daily Pivot")
alertcondition(dCrossDown, "Price crossed BELOW daily Pivot", "{{ticker}} crossed below the daily Pivot")
alertcondition(wCrossUp, "Price crossed ABOVE weekly Pivot", "{{ticker}} crossed above the weekly Pivot")
alertcondition(wCrossDown, "Price crossed BELOW weekly Pivot", "{{ticker}} crossed below the weekly Pivot")
alertcondition(sesNewSet, "New session pivots plotted", "{{ticker}}: a session has ended and its pivot levels are now on the chart")