Key Levels | PDH PDL PMH PML

jnguyen311 · study · 289 行 · 点赞 16 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Key Levels | PDH PDL PMH PML", overlay=true, max_lines_count=500, max_labels_count=500)
 
// ======================================================
// 1. PREVIOUS DAY HIGH / LOW SETTINGS
// ======================================================
 
grpGeneral = "PDH / PDL — General"
 
daysBack = input.int(1, "Number of past days to show", minval=1, maxval=200, group=grpGeneral)
 
leadFrac = input.float(0.05, "Lead ahead of price (fraction of day)", minval=0.0, maxval=0.5, step=0.01, group=grpGeneral)
 
// PDH settings
grpH = "PDH Line"
 
colH = input.color(color.red, "Color", group=grpH, inline="h1")
widthH = input.int(1, "Width", minval=1, maxval=5, group=grpH, inline="h1")
 
styleHi = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group=grpH)
 
showLblH = input.bool(true, "Show label", group=grpH, inline="h2")
txtH = input.string("PDH", "Label text", group=grpH, inline="h2")
 
// PDL settings
grpL = "PDL Line"
 
colL = input.color(color.green, "Color", group=grpL, inline="l1")
widthL = input.int(1, "Width", minval=1, maxval=5, group=grpL, inline="l1")
 
styleLo = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group=grpL)
 
showLblL = input.bool(true, "Show label", group=grpL, inline="l2")
txtL = input.string("PDL", "Label text", group=grpL, inline="l2")
 
// PDH / PDL label appearance
grpLbl = "PDH / PDL — Label Appearance"
 
lblCol = input.color(color.white, "Text Color", group=grpLbl)
 
lblSize = input.string("Normal", "Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=grpLbl)
 
// Helpers
f_style(s) =>
    s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
 
f_size(s) =>
    s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Large" ? size.large : s == "Huge" ? size.huge : size.normal
 
// ======================================================
// 2. PREVIOUS DAY HIGH / LOW CALCULATIONS
// ======================================================
 
var float curHigh = na
var float curLow = na
var int curHighT = na
var int curLowT = na
var int dayOpenT = na
 
bool newDay = ta.change(time("D")) != 0
int dayMs = 86400000
 
var array<line> hLines = array.new_line()
var array<line> lLines = array.new_line()
 
var array<label> hLbls = array.new_label()
var array<label> lLbls = array.new_label()
 
if newDay
    if not na(curHigh)
        line lh = line.new(curHighT, curHigh, time, curHigh, xloc=xloc.bar_time, color=colH, width=widthH, style=f_style(styleHi))
 
        line ll = line.new(curLowT, curLow, time, curLow, xloc=xloc.bar_time, color=colL, width=widthL, style=f_style(styleLo))
 
        array.push(hLines, lh)
        array.push(lLines, ll)
 
        if showLblH
            label lbH = label.new(time, curHigh, txtH, xloc=xloc.bar_time, style=label.style_label_left, color=color.new(color.black, 100), textcolor=lblCol, size=f_size(lblSize))
 
            array.push(hLbls, lbH)
 
        if showLblL
            label lbL = label.new(time, curLow, txtL, xloc=xloc.bar_time, style=label.style_label_left, color=color.new(color.black, 100), textcolor=lblCol, size=f_size(lblSize))
 
            array.push(lLbls, lbL)
 
        while array.size(hLines) > daysBack
            line.delete(array.shift(hLines))
            line.delete(array.shift(lLines))
 
        while array.size(hLbls) > daysBack
            label.delete(array.shift(hLbls))
 
        while array.size(lLbls) > daysBack
            label.delete(array.shift(lLbls))
 
    curHigh := high
    curLow := low
    curHighT := time
    curLowT := time
    dayOpenT := time
 
else
    if na(curHigh) or high > curHigh
        curHigh := high
        curHighT := time
 
    if na(curLow) or low < curLow
        curLow := low
        curLowT := time
 
// Extend active lines
if array.size(hLines) > 0 and not na(dayOpenT)
    int endOfDay = dayOpenT + dayMs
    int rightT = math.min(time + int(dayMs * leadFrac), endOfDay)
 
    line activeH = array.get(hLines, array.size(hLines) - 1)
    line activeL = array.get(lLines, array.size(lLines) - 1)
 
    line.set_x2(activeH, rightT)
    line.set_x2(activeL, rightT)
 
    if showLblH and array.size(hLbls) > 0
        label.set_x(array.get(hLbls, array.size(hLbls) - 1), rightT)
 
    if showLblL and array.size(lLbls) > 0
        label.set_x(array.get(lLbls, array.size(lLbls) - 1), rightT)
 
 
// ======================================================
// 3. PREMARKET HIGH / LOW SETTINGS
// ======================================================
 
string tz = "America/Los_Angeles"
string pmSession = "0100-0630:23456"
string rthSession = "0630-1300:23456"
 
grpPM = "PMH / PML — General"
 
showHistory = input.bool(false, "Show Previous Days PMH/PML", group=grpPM)
 
showForming = input.bool(true, "Show During Premarket", group=grpPM)
 
showLabels = input.bool(true, "Show Labels", group=grpPM)
 
labelOffset = input.int(5, "Label Offset Bars", minval=1, maxval=100, group=grpPM)
 
// PMH appearance
grpPMH = "PMH Line"
 
pmhColor = input.color(color.yellow, "Color", group=grpPMH, inline="pmh1")
 
pmhWidth = input.int(1, "Width", minval=1, maxval=5, group=grpPMH, inline="pmh1")
 
pmhStyle = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group=grpPMH)
 
// PML appearance
grpPML = "PML Line"
 
pmlColor = input.color(color.yellow, "Color", group=grpPML, inline="pml1")
 
pmlWidth = input.int(1, "Width", minval=1, maxval=5, group=grpPML, inline="pml1")
 
pmlStyle = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group=grpPML)
 
// ======================================================
// 4. PREMARKET SESSION TRACKING
// ======================================================
 
bool inPM = not na(time(timeframe.period, pmSession, tz))
bool inRTH = not na(time(timeframe.period, rthSession, tz))
 
bool newPM = inPM and not inPM[1]
 
var float pmHigh = na
var float pmLow = na
 
var line pmHighLine = na
var line pmLowLine = na
 
var label pmHighLabel = na
var label pmLowLabel = na
 
var array<line> pmHighLines = array.new_line()
var array<line> pmLowLines = array.new_line()
 
var array<label> pmHighLabels = array.new_label()
var array<label> pmLowLabels = array.new_label()
 
// ======================================================
// 5. NEW PREMARKET SESSION
// ======================================================
 
if newPM
    if not showHistory
        if array.size(pmHighLines) > 0
            for i = 0 to array.size(pmHighLines) - 1
                line.delete(array.get(pmHighLines, i))
 
        if array.size(pmLowLines) > 0
            for i = 0 to array.size(pmLowLines) - 1
                line.delete(array.get(pmLowLines, i))
 
        if array.size(pmHighLabels) > 0
            for i = 0 to array.size(pmHighLabels) - 1
                label.delete(array.get(pmHighLabels, i))
 
        if array.size(pmLowLabels) > 0
            for i = 0 to array.size(pmLowLabels) - 1
                label.delete(array.get(pmLowLabels, i))
 
        array.clear(pmHighLines)
        array.clear(pmLowLines)
        array.clear(pmHighLabels)
        array.clear(pmLowLabels)
 
    pmHigh := na
    pmLow := na
 
    pmHighLine := na
    pmLowLine := na
 
    pmHighLabel := na
    pmLowLabel := na
 
// ======================================================
// 6. CALCULATE PMH / PML
// ======================================================
 
if inPM
    pmHigh := na(pmHigh) ? high : math.max(pmHigh, high)
 
    pmLow := na(pmLow) ? low : math.min(pmLow, low)
 
bool pmValid = not na(pmHigh) and not na(pmLow)
 
bool pmVisible = pmValid and (inRTH or (inPM and showForming))
 
// ======================================================
// 7. DRAW PREMARKET LEVELS
// ======================================================
 
if pmVisible
    if na(pmHighLine)
        pmHighLine := line.new(bar_index, pmHigh, bar_index, pmHigh, color=pmhColor, width=pmhWidth, style=f_style(pmhStyle))
 
        pmLowLine := line.new(bar_index, pmLow, bar_index, pmLow, color=pmlColor, width=pmlWidth, style=f_style(pmlStyle))
 
        array.push(pmHighLines, pmHighLine)
        array.push(pmLowLines, pmLowLine)
 
    line.set_x2(pmHighLine, bar_index)
    line.set_y1(pmHighLine, pmHigh)
    line.set_y2(pmHighLine, pmHigh)
 
    line.set_x2(pmLowLine, bar_index)
    line.set_y1(pmLowLine, pmLow)
    line.set_y2(pmLowLine, pmLow)
 
    if showLabels
        string highText = "PMH: " + str.tostring(pmHigh, format.mintick)
 
        string lowText = "PML: " + str.tostring(pmLow, format.mintick)
 
        if na(pmHighLabel)
            pmHighLabel := label.new(bar_index + labelOffset, pmHigh, highText, style=label.style_label_left, color=color.new(color.black, 15), textcolor=pmhColor, size=size.small)
 
            pmLowLabel := label.new(bar_index + labelOffset, pmLow, lowText, style=label.style_label_left, color=color.new(color.black, 15), textcolor=pmlColor, size=size.small)
 
            array.push(pmHighLabels, pmHighLabel)
            array.push(pmLowLabels, pmLowLabel)
 
        label.set_xy(pmHighLabel, bar_index + labelOffset, pmHigh)
        label.set_text(pmHighLabel, highText)
 
        label.set_xy(pmLowLabel, bar_index + labelOffset, pmLow)
        label.set_text(pmLowLabel, lowText)
 
// ======================================================
// 8. PREMARKET ALERTS
// ======================================================
 
bool crossHigh = ta.crossover(close, pmHigh)
bool crossLow = ta.crossunder(close, pmLow)
 
alertcondition(inRTH and crossHigh, "Cross Above PMH", "Price crossed above premarket high")
 
alertcondition(inRTH and crossLow, "Cross Below PML", "Price crossed below premarket low")

← 返回列表