Basic Key Levels [Feng Futures]

ericfengg · study · 125 行 · 点赞 19 · TradingView 原页

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

Pine Script

//@version=6
indicator("Basic Key Levels [Feng Futures]", overlay=true, max_lines_count=50, max_labels_count=50)
 
// ============================================================================
// INPUTS
// ============================================================================
 
gLevel  = "⚙️ Levels"
gLabels = "🏷️ Labels"
 
showPDH  = input.bool(true, "PDH", group=gLevel, inline="pdh")
pdhColor = input.color(color.green, "", group=gLevel, inline="pdh")
 
showPDL  = input.bool(true, "PDL", group=gLevel, inline="pdl")
pdlColor = input.color(color.red, "", group=gLevel, inline="pdl")
 
showO    = input.bool(true, "Open (18:00)", group=gLevel, inline="o")
oColor   = input.color(color.blue, "", group=gLevel, inline="o")
 
linewidth = input.int(1, "Line Width", minval=1, maxval=4, group=gLevel)
 
showLabels   = input.bool(true, "Show Price Labels", group=gLabels)
labelOffset  = input.int(5, "Label X Offset", minval=-10, maxval=100, group=gLabels)
 
labelSizeOpt = input.string("Normal", "Size", options=["Tiny","Small","Normal","Large","Huge"], group=gLabels)
labelSize    = labelSizeOpt == "Tiny" ? size.tiny :
               labelSizeOpt == "Small" ? size.small :
               labelSizeOpt == "Normal" ? size.normal :
               labelSizeOpt == "Large" ? size.large : size.huge
 
// ============================================================================
// TIME + DATA
// ============================================================================
 
tz      = "America/New_York"
isIntra  = timeframe.isintraday
is1800NY = isIntra and (hour(time, tz) == 18 and minute(time, tz) == 0)
 
// "Futures day" boundary for most CME index futures is 18:00 NY.
// Using time("D") gives you session day on futures; for stocks it’s calendar daily.
newFutDay = isIntra ? ta.change(time("D")) : 0
 
// Prev day levels + session open (uses daily series)
pdh = request.security(syminfo.tickerid, "D", high[1], barmerge.gaps_off, barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1],  barmerge.gaps_off, barmerge.lookahead_on)
o   = request.security(syminfo.tickerid, "D", open,    barmerge.gaps_off, barmerge.lookahead_on)
 
// ============================================================================
// STATE
// ============================================================================
 
var line  pdhLine = na
var line  pdlLine = na
var line  oLine   = na
 
var label pdhLbl  = na
var label pdlLbl  = na
var label oLbl    = na
 
// Create at futures day start (18:00 bar) OR first load
shouldCreate = isIntra and (newFutDay != 0 or barstate.isfirst)
 
if shouldCreate
    // clean up old
    if not na(pdhLine)
        line.delete(pdhLine)
    if not na(pdlLine)
        line.delete(pdlLine)
    if not na(oLine)
        line.delete(oLine)
 
    if not na(pdhLbl)
        label.delete(pdhLbl)
    if not na(pdlLbl)
        label.delete(pdlLbl)
    if not na(oLbl)
        label.delete(oLbl)
 
    // create new starting at this bar
    if showPDH
        pdhLine := line.new(bar_index, pdh, bar_index, pdh, extend=extend.none, color=pdhColor, width=linewidth)
        if showLabels
            pdhLbl := label.new(bar_index + labelOffset, pdh, "PDH " + str.tostring(pdh, format.mintick),
                style=label.style_label_left, textcolor=color.white, color=pdhColor, size=labelSize)
 
    if showPDL
        pdlLine := line.new(bar_index, pdl, bar_index, pdl, extend=extend.none, color=pdlColor, width=linewidth)
        if showLabels
            pdlLbl := label.new(bar_index + labelOffset, pdl, "PDL " + str.tostring(pdl, format.mintick),
                style=label.style_label_left, textcolor=color.white, color=pdlColor, size=labelSize)
 
    if showO
        oLine := line.new(bar_index, o, bar_index, o, extend=extend.none, color=oColor, width=linewidth, style=line.style_dotted)
        if showLabels
            oLbl := label.new(bar_index + labelOffset, o, "O " + str.tostring(o, format.mintick),
                style=label.style_label_left, textcolor=color.white, color=oColor, size=labelSize)
 
// ============================================================================
// UPDATE (extend to the right + keep labels pinned)
// ============================================================================
 
if isIntra
    if showPDH and not na(pdhLine)
        line.set_x2(pdhLine, bar_index)
        line.set_y1(pdhLine, pdh)
        line.set_y2(pdhLine, pdh)
        if showLabels and not na(pdhLbl)
            label.set_xy(pdhLbl, bar_index + labelOffset, pdh)
            label.set_text(pdhLbl, "PDH " + str.tostring(pdh, format.mintick))
 
    if showPDL and not na(pdlLine)
        line.set_x2(pdlLine, bar_index)
        line.set_y1(pdlLine, pdl)
        line.set_y2(pdlLine, pdl)
        if showLabels and not na(pdlLbl)
            label.set_xy(pdlLbl, bar_index + labelOffset, pdl)
            label.set_text(pdlLbl, "PDL " + str.tostring(pdl, format.mintick))
 
    if showO and not na(oLine)
        line.set_x2(oLine, bar_index)
        line.set_y1(oLine, o)
        line.set_y2(oLine, o)
        if showLabels and not na(oLbl)
            label.set_xy(oLbl, bar_index + labelOffset, o)
            label.set_text(oLbl, "O " + str.tostring(o, format.mintick))

← 返回列表