本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator('Price Levels: Master', 'PDH PDL PMH PML AH', overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// ============================================================================
// HELPER: LINE STYLES
// ============================================================================
getStyle(string option) =>
switch option
'dotted (┈)' => line.style_dotted
'dashed (╌)' => line.style_dashed
=> line.style_solid
// ============================================================================
// INPUTS: PRIOR DAY — RTH ONLY
// ============================================================================
pdhLevel = input.bool(true, 'Prev Day High', group = 'Prior Day', inline = 'pdh')
pdhColour = input.color(#f4980a, '', group = 'Prior Day', inline = 'pdh')
pdhWidth = input.int(1, 'Width', minval = 1, group = 'Prior Day', inline = 'pdh')
pdhStyleIn = input.string('solid (─)', '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)'], group = 'Prior Day', inline = 'pdh')
pdlLevel = input.bool(true, 'Prev Day Low', group = 'Prior Day', inline = 'pdl')
pdlColour = input.color(#bbbbbb, '', group = 'Prior Day', inline = 'pdl')
pdlWidth = input.int(1, 'Width', minval = 1, group = 'Prior Day', inline = 'pdl')
pdlStyleIn = input.string('solid (─)', '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)'], group = 'Prior Day', inline = 'pdl')
// ============================================================================
// INPUTS: PRE-MARKET
// ============================================================================
pmhLevel = input.bool(true, 'Pre-market High', group = 'Pre-Market', inline = 'pmh')
pmhColour = input.color(#3aff0e, '', group = 'Pre-Market', inline = 'pmh')
pmhWidth = input.int(1, 'Width', minval = 1, group = 'Pre-Market', inline = 'pmh')
pmhStyleIn = input.string('solid (─)', '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)'], group = 'Pre-Market', inline = 'pmh')
pmlLevel = input.bool(true, 'Pre-market Low', group = 'Pre-Market', inline = 'pml')
pmlColour = input.color(#990000, '', group = 'Pre-Market', inline = 'pml')
pmlWidth = input.int(1, 'Width', minval = 1, group = 'Pre-Market', inline = 'pml')
pmlStyleIn = input.string('solid (─)', '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)'], group = 'Pre-Market', inline = 'pml')
// ============================================================================
// INPUTS: PM RANGE HIGHLIGHT
// ============================================================================
showPmRange = input.bool(true, 'Highlight PM Range During RTH', group = 'PM Range Highlight')
pmRangeColour = input.color(color.new(color.blue, 85), 'Range Colour', group = 'PM Range Highlight')
pmRangeBorder = input.bool(true, 'Show Border', group = 'PM Range Highlight')
pmRangeBorderColour = input.color(color.new(color.blue, 40), 'Border Colour', group = 'PM Range Highlight')
// ============================================================================
// INPUTS: AFTER-HOURS
// ============================================================================
showAhLevels = input.bool(true, 'Show After-Hours High/Low', group = 'After-Hours')
ahHighColour = input.color(color.purple, 'AH High', group = 'After-Hours', inline = 'ahh')
ahHighWidth = input.int(1, 'Width', minval = 1, group = 'After-Hours', inline = 'ahh')
ahHighStyleIn = input.string('dashed (╌)', '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)'], group = 'After-Hours', inline = 'ahh')
ahLowColour = input.color(color.purple, 'AH Low', group = 'After-Hours', inline = 'ahl')
ahLowWidth = input.int(1, 'Width', minval = 1, group = 'After-Hours', inline = 'ahl')
ahLowStyleIn = input.string('dashed (╌)', '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)'], group = 'After-Hours', inline = 'ahl')
// ============================================================================
// INPUTS: LINE EXTENSION
// ============================================================================
lineExtensionMode = input.string(
'Bars ahead',
'Line Extension Mode',
options = ['Bars ahead', 'Extend right'],
tooltip = 'Bars ahead: lines end the selected number of bars beyond the current candle. Extend right: lines project indefinitely to the right, not just to the session close.',
group = 'Line Extension')
extendBars = input.int(
15,
'Bars Ahead',
minval = 1,
maxval = 500,
tooltip = 'Controls the line endpoint in Bars ahead mode. Also controls text positioning in both modes.',
group = 'Line Extension')
// ============================================================================
// INPUTS: TEXT DISPLAY
// ============================================================================
levelTextMode = input.string(
'Both',
'Display',
options = ['Name only', 'Price only', 'Both'],
group = 'Text Display')
textSizeChoice = input.int(
3,
'Text Size (1–10)',
minval = 1,
maxval = 10,
tooltip = '1 = 7 points, 2 = 9, 3 = 11, 4 = 13, 5 = 15, 6 = 17, 7 = 19, 8 = 21, 9 = 23, 10 = 25.',
group = 'Text Display')
labelInset = input.int(
3,
'Text Inset (Bars)',
minval = 0,
maxval = 500,
tooltip = 'Places text this many bars back from the Bars Ahead position. Text stays at or ahead of the current candle.',
group = 'Text Display')
// ============================================================================
// INPUTS: INDIVIDUAL TEXT VISIBILITY
// These switches hide text only. The corresponding lines remain visible.
// ============================================================================
showPdhText = input.bool(true, 'Show PDH Text', group = 'Individual Level Text')
showPdlText = input.bool(true, 'Show PDL Text', group = 'Individual Level Text')
showPmhText = input.bool(true, 'Show PMH Text', group = 'Individual Level Text')
showPmlText = input.bool(true, 'Show PML Text', group = 'Individual Level Text')
showAhHighText = input.bool(true, 'Show AH High Text', group = 'Individual Level Text')
showAhLowText = input.bool(true, 'Show AH Low Text', group = 'Individual Level Text')
labelSize = 5 + textSizeChoice * 2
labelOffset = math.max(0, extendBars - labelInset)
// ============================================================================
// HELPERS: TEXT AND DRAWING CREATION
// ============================================================================
getLevelText(string levelName, float levelPrice) =>
bool showText = switch levelName
'PDH' => showPdhText
'PDL' => showPdlText
'PMH' => showPmhText
'PML' => showPmlText
'AH High' => showAhHighText
'AH Low' => showAhLowText
=> true
string priceText = str.tostring(levelPrice, format.mintick)
string result = switch levelTextMode
'Name only' => levelName
'Price only' => priceText
=> levelName + ': ' + priceText
showText ? result : ''
getExtension(bool extendLeft) =>
if lineExtensionMode == 'Extend right'
extendLeft ? extend.both : extend.right
else
extendLeft ? extend.left : extend.none
makeLevelLine(float levelPrice, color levelColour, int levelWidth, string styleInput, bool extendLeft) =>
line.new(
x1 = bar_index,
y1 = levelPrice,
x2 = bar_index + extendBars,
y2 = levelPrice,
xloc = xloc.bar_index,
extend = getExtension(extendLeft),
color = levelColour,
style = getStyle(styleInput),
width = levelWidth)
makeLevelLabel(string levelName, float levelPrice, color levelColour) =>
label.new(
x = bar_index + labelOffset,
y = levelPrice,
text = getLevelText(levelName, levelPrice),
xloc = xloc.bar_index,
yloc = yloc.price,
color = color.new(color.black, 100),
style = label.style_label_down,
textcolor = levelColour,
size = labelSize,
textalign = text.align_center)
// ============================================================================
// INTERNAL VARIABLES
// ============================================================================
var float rthHighTracker = na
var float rthLowTracker = na
var float rthPrevHigh = na
var float rthPrevLow = na
var float pmHigh = na
var float pmLow = na
var float ahHigh = na
var float ahLow = na
var float ahPrevHigh = na
var float ahPrevLow = na
var line pdhLine = na
var label pdhLab = na
var line pdlLine = na
var label pdlLab = na
var line pmhLine = na
var label pmhLab = na
var line pmlLine = na
var label pmlLab = na
var line ahHighLine = na
var label ahHighLab = na
var line ahLowLine = na
var label ahLowLab = na
var box pmRangeBox = na
// ============================================================================
// SESSION STATES
// ============================================================================
isRTH = session.ismarket
isPM = session.ispremarket
isAH = session.ispostmarket
newRTH = isRTH and not isRTH[1]
newPM = isPM and not isPM[1]
newAH = isAH and not isAH[1]
// ============================================================================
// 1. RTH HIGH/LOW TRACKING
// Only regular-session candles contribute to PDH/PDL.
// ============================================================================
if isRTH
if newRTH
rthHighTracker := high
rthLowTracker := low
else
rthHighTracker := math.max(high, rthHighTracker)
rthLowTracker := math.min(low, rthLowTracker)
// ============================================================================
// 2. NEW PRE-MARKET SESSION
// Promote completed RTH and AH ranges at the next PM open.
// ============================================================================
if newPM
rthPrevHigh := rthHighTracker
rthPrevLow := rthLowTracker
ahPrevHigh := ahHigh
ahPrevLow := ahLow
pmHigh := high
pmLow := low
// Clear the previous day's PM-range shading.
box.delete(pmRangeBox)
pmRangeBox := na
// Prior-Day High
if pdhLevel and not na(rthPrevHigh)
line.delete(pdhLine)
label.delete(pdhLab)
pdhLine := makeLevelLine(rthPrevHigh, pdhColour, pdhWidth, pdhStyleIn, true)
pdhLab := makeLevelLabel('PDH', rthPrevHigh, pdhColour)
// Prior-Day Low
if pdlLevel and not na(rthPrevLow)
line.delete(pdlLine)
label.delete(pdlLab)
pdlLine := makeLevelLine(rthPrevLow, pdlColour, pdlWidth, pdlStyleIn, true)
pdlLab := makeLevelLabel('PDL', rthPrevLow, pdlColour)
// Previous After-Hours High
if showAhLevels and not na(ahPrevHigh)
line.delete(ahHighLine)
label.delete(ahHighLab)
ahHighLine := makeLevelLine(ahPrevHigh, ahHighColour, ahHighWidth, ahHighStyleIn, true)
ahHighLab := makeLevelLabel('AH High', ahPrevHigh, ahHighColour)
// Previous After-Hours Low
if showAhLevels and not na(ahPrevLow)
line.delete(ahLowLine)
label.delete(ahLowLab)
ahLowLine := makeLevelLine(ahPrevLow, ahLowColour, ahLowWidth, ahLowStyleIn, true)
ahLowLab := makeLevelLabel('AH Low', ahPrevLow, ahLowColour)
// Today's Pre-Market High
if pmhLevel
line.delete(pmhLine)
label.delete(pmhLab)
pmhLine := makeLevelLine(pmHigh, pmhColour, pmhWidth, pmhStyleIn, false)
pmhLab := makeLevelLabel('PMH', pmHigh, pmhColour)
// Today's Pre-Market Low
if pmlLevel
line.delete(pmlLine)
label.delete(pmlLab)
pmlLine := makeLevelLine(pmLow, pmlColour, pmlWidth, pmlStyleIn, false)
pmlLab := makeLevelLabel('PML', pmLow, pmlColour)
// ============================================================================
// 3. LIVE PRE-MARKET RANGE UPDATES
// PMH/PML freeze after pre-market ends.
// ============================================================================
if isPM and not newPM
pmHigh := math.max(high, pmHigh)
pmLow := math.min(low, pmLow)
if not na(pmhLine)
line.set_y1(pmhLine, pmHigh)
line.set_y2(pmhLine, pmHigh)
if not na(pmhLab)
label.set_y(pmhLab, pmHigh)
label.set_text(pmhLab, getLevelText('PMH', pmHigh))
if not na(pmlLine)
line.set_y1(pmlLine, pmLow)
line.set_y2(pmlLine, pmLow)
if not na(pmlLab)
label.set_y(pmlLab, pmLow)
label.set_text(pmlLab, getLevelText('PML', pmLow))
// ============================================================================
// 4. AFTER-HOURS HIGH/LOW TRACKING
// Displayed at the next pre-market open, matching the previous script.
// ============================================================================
if isAH
if newAH
ahHigh := high
ahLow := low
else
ahHigh := math.max(high, ahHigh)
ahLow := math.min(low, ahLow)
// ============================================================================
// 5. CURRENT DAY PM-RANGE SHADING — RTH ONLY
// No infinite right extension. Cleared at the next PM open.
// ============================================================================
if newRTH
box.delete(pmRangeBox)
pmRangeBox := na
if showPmRange and not na(pmHigh) and not na(pmLow)
pmRangeBox := box.new(
left = bar_index,
top = pmHigh,
right = bar_index + 1,
bottom = pmLow,
xloc = xloc.bar_index,
border_color = pmRangeBorder ? pmRangeBorderColour : na,
border_width = 1,
extend = extend.none,
bgcolor = pmRangeColour)
if isRTH and not na(pmRangeBox)
box.set_top(pmRangeBox, pmHigh)
box.set_bottom(pmRangeBox, pmLow)
box.set_right(pmRangeBox, bar_index + 1)
// ============================================================================
// 6. MAINTENANCE: LINE ENDPOINTS AND TEXT POSITIONING
// ============================================================================
if barstate.islast
int lineEndX = bar_index + extendBars
int textX = bar_index + labelOffset
levelLines = array.from(
pdhLine, pdlLine,
pmhLine, pmlLine,
ahHighLine, ahLowLine)
levelLabels = array.from(
pdhLab, pdlLab,
pmhLab, pmlLab,
ahHighLab, ahLowLab)
levelNames = array.from(
'PDH', 'PDL',
'PMH', 'PML',
'AH High', 'AH Low')
for i = 0 to array.size(levelLines) - 1
line currentLine = array.get(levelLines, i)
label currentLabel = array.get(levelLabels, i)
string currentName = array.get(levelNames, i)
if not na(currentLine)
bool hasLeftExtension = i < 2 or i >= 4
line.set_x2(currentLine, lineEndX)
line.set_extend(currentLine, getExtension(hasLeftExtension))
if not na(currentLabel)
float levelPrice = line.get_y1(currentLine)
label.set_xy(currentLabel, textX, levelPrice)
label.set_text(currentLabel, getLevelText(currentName, levelPrice))
label.set_style(currentLabel, label.style_label_down)
label.set_color(currentLabel, color.new(color.black, 100))
label.set_size(currentLabel, labelSize)