CME Commodity 1H & 4H High Low - IST

silvypeter · study · 263 行 · 点赞 17 · TradingView 原页

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

Pine Script

//@version=6
indicator("CME Commodity 1H & 4H High Low - IST", overlay=true, max_lines_count=500, max_labels_count=500)
 
//==================================================
// USER SETTINGS
//==================================================
 
show1H = input.bool(true, "Show 1H High/Low")
show4H = input.bool(true, "Show 4H High/Low")
 
h1Color = input.color(color.blue, "1H Colour")
h4Color = input.color(color.orange, "4H Colour")
 
lineWidth = input.int(2, "Line Width", minval=1, maxval=4)
 
string TZ = "America/Chicago"
 
//==================================================
// TIMEFRAME VALIDATION: 1 TO 15 MINUTES
//==================================================
 
if not timeframe.isintraday or timeframe.in_seconds() < 60 or timeframe.in_seconds() > 900
    runtime.error("Use an intraday chart from 1 minute to 15 minutes.")
 
//==================================================
// CME GLOBEX SESSION
// Daily opening: 5:00 PM Chicago time
// Automatically adjusts CST / CDT
//==================================================
 
int mins = hour(time, TZ) * 60 + minute(time, TZ)
 
int dow = dayofweek(time, TZ)
 
// Active trading sessions:
// Sunday: 5 PM onwards
// Monday to Thursday: 5 PM to 4 PM next day
// Friday: Until 4 PM
 
bool sessionActive =
     (dow == dayofweek.sunday and mins >= 1020) or
     (dow >= dayofweek.monday and dow <= dayofweek.thursday and (mins >= 1020 or mins < 960)) or
     (dow == dayofweek.friday and mins < 960)
 
// Calculate current session opening timestamp
int todayOpen = timestamp(
     TZ,
     year(time, TZ),
     month(time, TZ),
     dayofmonth(time, TZ),
     17, 0)
 
// If before 5 PM, use previous calendar day's 5 PM opening
int sessionStart = mins >= 1020 ? todayOpen :
     timestamp(TZ, year(time, TZ), month(time, TZ), dayofmonth(time, TZ), 0, 0) - 7 * 3600000
 
//==================================================
// SESSION STATE
//==================================================
 
var int activeSession = na
 
//==================================================
// 1H VARIABLES
//==================================================
 
var int h1Start = na
var float h1High = na
var float h1Low = na
 
var line h1HighLine = na
var line h1LowLine = na
 
var label h1HighLabel = na
var label h1LowLabel = na
 
//==================================================
// 4H VARIABLES
//==================================================
 
var int h4Start = na
var float h4High = na
var float h4Low = na
 
var line h4HighLine = na
var line h4LowLine = na
 
var label h4HighLabel = na
var label h4LowLabel = na
 
//==================================================
// RESET AT EVERY NEW CME DAILY SESSION
//==================================================
 
if sessionActive
 
    if na(activeSession) or sessionStart != activeSession
 
        activeSession := sessionStart
 
        h1Start := na
        h1High := na
        h1Low := na
 
        h1HighLine := na
        h1LowLine := na
        h1HighLabel := na
        h1LowLabel := na
 
        h4Start := na
        h4High := na
        h4Low := na
 
        h4HighLine := na
        h4LowLine := na
        h4HighLabel := na
        h4LowLabel := na
 
//==================================================
// 1 HOUR HIGH / LOW CALCULATION
//==================================================
 
if sessionActive
 
    int bucket1 = sessionStart +
         int(math.floor((time - sessionStart) / 3600000)) * 3600000
 
    if na(h1Start) or bucket1 != h1Start
 
        // Finalize previous 1H levels
        if not na(h1HighLabel)
            label.set_text(h1HighLabel, "1H H")
            label.set_text(h1LowLabel, "1H L")
 
        // Start new 1H period
        h1Start := bucket1
        h1High := high
        h1Low := low
 
        h1HighLine := na
        h1LowLine := na
        h1HighLabel := na
        h1LowLabel := na
 
        if show1H
 
            h1HighLine := line.new(
                 x1=h1Start, y1=h1High,
                 x2=time_close, y2=h1High,
                 xloc=xloc.bar_time,
                 color=h1Color,
                 width=lineWidth)
 
            h1LowLine := line.new(
                 x1=h1Start, y1=h1Low,
                 x2=time_close, y2=h1Low,
                 xloc=xloc.bar_time,
                 color=h1Color,
                 width=lineWidth)
 
            h1HighLabel := label.new(
                 time_close, h1High, "1H H (LIVE)",
                 xloc=xloc.bar_time,
                 style=label.style_none,
                 textcolor=h1Color,
                 size=size.tiny)
 
            h1LowLabel := label.new(
                 time_close, h1Low, "1H L (LIVE)",
                 xloc=xloc.bar_time,
                 style=label.style_none,
                 textcolor=h1Color,
                 size=size.tiny)
 
    else
 
        h1High := math.max(h1High, high)
        h1Low := math.min(h1Low, low)
 
    // Update horizontal 1H levels
    if show1H and not na(h1HighLine)
 
        line.set_xy1(h1HighLine, h1Start, h1High)
        line.set_xy2(h1HighLine, time_close, h1High)
 
        line.set_xy1(h1LowLine, h1Start, h1Low)
        line.set_xy2(h1LowLine, time_close, h1Low)
 
        label.set_xy(h1HighLabel, time_close, h1High)
        label.set_xy(h1LowLabel, time_close, h1Low)
 
//==================================================
// 4 HOUR HIGH / LOW CALCULATION
//==================================================
 
if sessionActive
 
    int bucket4 = sessionStart +
         int(math.floor((time - sessionStart) / 14400000)) * 14400000
 
    if na(h4Start) or bucket4 != h4Start
 
        // Finalize previous 4H levels
        if not na(h4HighLabel)
            label.set_text(h4HighLabel, "4H H")
            label.set_text(h4LowLabel, "4H L")
 
        // Start new 4H period
        h4Start := bucket4
        h4High := high
        h4Low := low
 
        h4HighLine := na
        h4LowLine := na
        h4HighLabel := na
        h4LowLabel := na
 
        if show4H
 
            h4HighLine := line.new(
                 x1=h4Start, y1=h4High,
                 x2=time_close, y2=h4High,
                 xloc=xloc.bar_time,
                 color=h4Color,
                 width=lineWidth)
 
            h4LowLine := line.new(
                 x1=h4Start, y1=h4Low,
                 x2=time_close, y2=h4Low,
                 xloc=xloc.bar_time,
                 color=h4Color,
                 width=lineWidth)
 
            h4HighLabel := label.new(
                 time_close, h4High, "4H H (LIVE)",
                 xloc=xloc.bar_time,
                 style=label.style_none,
                 textcolor=h4Color,
                 size=size.tiny)
 
            h4LowLabel := label.new(
                 time_close, h4Low, "4H L (LIVE)",
                 xloc=xloc.bar_time,
                 style=label.style_none,
                 textcolor=h4Color,
                 size=size.tiny)
 
    else
 
        h4High := math.max(h4High, high)
        h4Low := math.min(h4Low, low)
 
    // Update horizontal 4H levels
    if show4H and not na(h4HighLine)
 
        line.set_xy1(h4HighLine, h4Start, h4High)
        line.set_xy2(h4HighLine, time_close, h4High)
 
        line.set_xy1(h4LowLine, h4Start, h4Low)
        line.set_xy2(h4LowLine, time_close, h4Low)
 
        label.set_xy(h4HighLabel, time_close, h4High)
        label.set_xy(h4LowLabel, time_close, h4Low)

← 返回列表