ES/NQ RTH 5-Minute Bar Count + Previous Day Levels

BlueflowTrading · study · 137 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator(
     "ES/NQ RTH 5-Minute Bar Count + Previous Day Levels",
     shorttitle = "ES/NQ RTH Count + PD Levels",
     overlay = true,
     max_labels_count = 500,
     max_lines_count = 10)
 
// Designed for CME E-mini / Micro E-mini S&P 500 and Nasdaq-100 futures.
// Bar-count labels appear only during CME Regular Trading Hours on the
// 5-minute chart. Electronic Trading Hours bars are excluded from the count.
// Previous-day levels appear only on 5m, 15m, 30m, 1h, and 4h charts.
 
// ─────────────────────────────────────────────────────────────────────────────
// Display settings
// ─────────────────────────────────────────────────────────────────────────────
string GROUP_DISPLAY = "Display"
 
bool showBarCount = input.bool(true, "Show 5-minute bar count", group = GROUP_DISPLAY)
bool showPrevDayLevels = input.bool(true, "Show previous-day levels", group = GROUP_DISPLAY)
color mediumBlue = input.color(#42A5F5, "Number and line color", group = GROUP_DISPLAY)
 
// Exact chart timeframes allowed by this indicator.
bool isFiveMinute = timeframe.isminutes and timeframe.multiplier == 5
bool isLevelTimeframe = timeframe.isminutes and (
     timeframe.multiplier == 5 or
     timeframe.multiplier == 15 or
     timeframe.multiplier == 30 or
     timeframe.multiplier == 60 or
     timeframe.multiplier == 240)
 
// For ES/NQ futures, this follows the daily boundary supplied by the selected
// TradingView symbol/feed and therefore resets with each new futures trading day.
bool isNewTradingDay = barstate.isfirst or timeframe.change("1D")
 
// TradingView defines CME futures RTH as 08:30-15:15 Chicago time.
// Using the IANA time zone keeps the session correct through daylight saving.
string RTH_SESSION = "0830-1515:23456"
string RTH_TIMEZONE = "America/Chicago"
bool isInRTH = not na(time(timeframe.period, RTH_SESSION, RTH_TIMEZONE))
bool rthTradingDateChanged = barstate.isfirst or time_tradingday != time_tradingday[1]
bool isNewRTHSession = isInRTH and (barstate.isfirst or not isInRTH[1] or isNewTradingDay or rthTradingDateChanged)
 
// ─────────────────────────────────────────────────────────────────────────────
// RTH 5-minute bar count: 2, 4, 6, 8, 10... for the current session only
// ─────────────────────────────────────────────────────────────────────────────
var int rthBarNumber = 0
 
if isNewRTHSession
    rthBarNumber := 1
else if isInRTH
    rthBarNumber += 1
else
    rthBarNumber := 0
 
// Keep the current RTH session's labels in an array. Clear them when a new
// futures trading day or a new RTH session begins, leaving no prior-day count.
var array<label> currentDayCountLabels = array.new<label>()
 
if isNewTradingDay or isNewRTHSession
    int oldLabelCount = array.size(currentDayCountLabels)
    if oldLabelCount > 0
        for i = 0 to oldLabelCount - 1
            label.delete(array.get(currentDayCountLabels, i))
        array.clear(currentDayCountLabels)
 
bool shouldShowNumber = showBarCount and isFiveMinute and isInRTH and rthBarNumber >= 2 and rthBarNumber % 2 == 0
 
if shouldShowNumber
    label countLabel = label.new(
         x = bar_index,
         y = low,
         text = str.tostring(rthBarNumber),
         yloc = yloc.belowbar,
         style = label.style_none,
         textcolor = mediumBlue,
         size = size.small,
         textalign = text.align_center)
    array.push(currentDayCountLabels, countLabel)
 
// ─────────────────────────────────────────────────────────────────────────────
// Previous confirmed trading day's high, midpoint, and low
// ─────────────────────────────────────────────────────────────────────────────
// The [1] offset with lookahead_on returns the last completed daily candle and
// keeps the three levels fixed throughout the current session without repainting.
[previousDayHigh, previousDayLow] = request.security(
     syminfo.tickerid,
     "1D",
     [high[1], low[1]],
     gaps = barmerge.gaps_off,
     lookahead = barmerge.lookahead_on)
 
float previousDayMid = (previousDayHigh + previousDayLow) / 2.0
 
var line previousHighLine = na
var line previousMidLine = na
var line previousLowLine = na
 
// Replace the old set at every new futures trading day. The lines begin on the
// first bar of the current session and extend to the right as parallel levels.
if isNewTradingDay and showPrevDayLevels and isLevelTimeframe and not na(previousDayHigh) and not na(previousDayLow)
    line.delete(previousHighLine)
    line.delete(previousMidLine)
    line.delete(previousLowLine)
 
    previousHighLine := line.new(
         x1 = bar_index,
         y1 = previousDayHigh,
         x2 = bar_index + 1,
         y2 = previousDayHigh,
         xloc = xloc.bar_index,
         extend = extend.right,
         color = mediumBlue,
         style = line.style_solid,
         width = 1)
 
    previousMidLine := line.new(
         x1 = bar_index,
         y1 = previousDayMid,
         x2 = bar_index + 1,
         y2 = previousDayMid,
         xloc = xloc.bar_index,
         extend = extend.right,
         color = mediumBlue,
         style = line.style_solid,
         width = 1)
 
    previousLowLine := line.new(
         x1 = bar_index,
         y1 = previousDayLow,
         x2 = bar_index + 1,
         y2 = previousDayLow,
         xloc = xloc.bar_index,
         extend = extend.right,
         color = mediumBlue,
         style = line.style_solid,
         width = 1)

← 返回列表