MonoIntel Session Levels: ETH and RTH Highs and Lows

monobosh · study · 246 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("MonoIntel Session Levels: ETH and RTH Highs and Lows", shorttitle="MI Session Levels", overlay=true,
     max_lines_count=500, max_labels_count=500)
 
// Built for CME futures on charts from 1 minute to 4 hours. All times are New York.
// ETH = 16:00 to 09:30. RTH = 09:30 to 16:00.
// Monday's ETH starts at the Sunday 18:00 open, so Friday's 16:00 to 17:00 hour is left out.
// Levels are measured from 30-minute or 1-minute data underneath the chart, so they are the true
// session high and low on any intraday timeframe, including ones that don't line up with 09:30 or 16:00.
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 1 — INPUTS
// ══════════════════════════════════════════════════════════════════════════════
 
// ── ETH Session ──────────────────────────────────────────────────────────────
i_showETH      = input.bool(true,    "Show ETH Levels",  group="ETH Session (4pm – 9:30am)",
     tooltip="Draws the high and low of the 16:00 to 09:30 session once 09:30 prints.")
i_ethColor     = input.color(#000000,"Color",             group="ETH Session (4pm – 9:30am)",
     tooltip="Colour of the ETH lines and labels.")
i_ethWidth     = input.int(2,        "Line Width",        group="ETH Session (4pm – 9:30am)",
     minval=1, maxval=4, tooltip="Thickness of the ETH lines.")
i_ethStyle     = input.string("Solid","Line Style",       group="ETH Session (4pm – 9:30am)",
     options=["Solid","Dashed","Dotted"], tooltip="Style of the ETH lines.")
i_showETHHigh  = input.bool(true,    "Show High",         group="ETH Session (4pm – 9:30am)",
     tooltip="Draws the ETH high.")
i_showETHLow   = input.bool(true,    "Show Low",          group="ETH Session (4pm – 9:30am)",
     tooltip="Draws the ETH low.")
 
// ── RTH Session ──────────────────────────────────────────────────────────────
i_showRTH      = input.bool(true,    "Show RTH Levels",  group="RTH Session (9:30am – 4pm)",
     tooltip="Draws the high and low of the 09:30 to 16:00 session once 16:00 prints.")
i_rthColor     = input.color(#000000,"Color",             group="RTH Session (9:30am – 4pm)",
     tooltip="Colour of the RTH lines and labels.")
i_rthWidth     = input.int(2,        "Line Width",        group="RTH Session (9:30am – 4pm)",
     minval=1, maxval=4, tooltip="Thickness of the RTH lines.")
i_rthStyle     = input.string("Dashed","Line Style",      group="RTH Session (9:30am – 4pm)",
     options=["Solid","Dashed","Dotted"], tooltip="Style of the RTH lines.")
i_showRTHHigh  = input.bool(true,    "Show High",         group="RTH Session (9:30am – 4pm)",
     tooltip="Draws the RTH high.")
i_showRTHLow   = input.bool(true,    "Show Low",          group="RTH Session (9:30am – 4pm)",
     tooltip="Draws the RTH low.")
 
// ── Display ───────────────────────────────────────────────────────────────────
i_histDays   = input.int(10, "Levels kept",           group="Display",
     minval=1, maxval=20,
     tooltip="How many of each level type (ETH High, ETH Low, RTH High, RTH Low) stay on the chart. The oldest is removed when a new one is added.")
i_mitigation = input.bool(true, "Remove mitigated levels", group="Display",
     tooltip="When on, a level is removed once price trades through it. If price gaps past a level without trading through it, the level stays.")
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 2 — CONSTANTS + HELPERS
// ══════════════════════════════════════════════════════════════════════════════
 
string TZ          = "America/New_York"
int    RTH_START   = 9 * 60 + 30     // 09:30, minutes after midnight
int    RTH_END     = 16 * 60         // 16:00
int    DAY_MS      = 86400000
 
f_lineStyle(s) =>
    switch s
        "Dashed" => line.style_dashed
        "Dotted" => line.style_dotted
        =>          line.style_solid
 
f_isRTH(int t) =>
    int m = hour(t, TZ) * 60 + minute(t, TZ)
    m >= RTH_START and m < RTH_END
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 3 — DATA UNDER THE CHART
// ══════════════════════════════════════════════════════════════════════════════
// Charts whose bars split cleanly at 09:30 and 16:00 (30m and below, dividing 30m)
// read their own bars. Anything else reads 30m bars (above 30m) or 1m bars (below),
// so every session boundary is exact.
 
int    tfSec   = timeframe.in_seconds()
bool   aligned = tfSec < 60 or 1800 % tfSec == 0
bool   active  = timeframe.isintraday and tfSec <= 4 * 3600   // 4 hour charts and below
string dataTF  = aligned or not active ? timeframe.period : tfSec > 1800 ? "30" : "1"
 
[subTime, subHigh, subLow] = request.security_lower_tf(syminfo.tickerid, dataTF, [time, high, low],
     ignore_invalid_timeframe=true)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 4 — STATE
// ══════════════════════════════════════════════════════════════════════════════
 
var float ethHigh     = na
var float ethLow      = na
var int   ethHighTime = na
var int   ethLowTime  = na
 
var float rthHigh     = na
var float rthLow      = na
var int   rthHighTime = na
var int   rthLowTime  = na
 
var int   prevTime    = na
var bool  prevRTH     = false
// A session is only drawn if its start was seen, so the first, partial session
// at the left edge of the data never produces a false level.
var bool  ethFull     = false
var bool  rthFull     = false
 
var line[]  ethH_lines  = array.new<line>(0)
var label[] ethH_labels = array.new<label>(0)
var float[] ethH_levels = array.new<float>(0)
 
var line[]  ethL_lines  = array.new<line>(0)
var label[] ethL_labels = array.new<label>(0)
var float[] ethL_levels = array.new<float>(0)
 
var line[]  rthH_lines  = array.new<line>(0)
var label[] rthH_labels = array.new<label>(0)
var float[] rthH_levels = array.new<float>(0)
 
var line[]  rthL_lines  = array.new<line>(0)
var label[] rthL_labels = array.new<label>(0)
var float[] rthL_levels = array.new<float>(0)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 5 — DRAWING HELPERS
// ══════════════════════════════════════════════════════════════════════════════
 
// Prune the oldest entry when over the limit, then add a new line and label.
f_spawn(array<line> lns, array<label> lbs, array<float> lvls,
     int x1, float lvl, string txt, color col, string sty, int wid) =>
    if array.size(lns) >= i_histDays
        line.delete(array.shift(lns))
        label.delete(array.shift(lbs))
        array.shift(lvls)
    ln = line.new(x1=x1, y1=lvl, x2=time, y2=lvl, xloc=xloc.bar_time,
         color=col, style=f_lineStyle(sty), width=wid)
    lb = label.new(x=time, y=lvl, text=txt, xloc=xloc.bar_time,
         style=label.style_none, textcolor=col, size=size.small)
    array.push(lns, ln)
    array.push(lbs, lb)
    array.push(lvls, lvl)
 
// Remove any level that the given price range traded through.
// Reverse iteration keeps the indexes valid while removing.
f_mitigate(array<line> lns, array<label> lbs, array<float> lvls, float h, float l) =>
    int i = array.size(lns) - 1
    while i >= 0
        float lvl = array.get(lvls, i)
        if h >= lvl and l <= lvl
            line.delete(array.get(lns, i))
            label.delete(array.get(lbs, i))
            array.remove(lns,  i)
            array.remove(lbs,  i)
            array.remove(lvls, i)
        i := i - 1
 
f_extend(array<line> lns, array<label> lbs) =>
    for [i, ln] in lns
        line.set_x2(ln, time)
        label.set_x(array.get(lbs, i), time)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 6 — WALK THE DATA, SESSION BY SESSION
// ══════════════════════════════════════════════════════════════════════════════
 
if active
    for [k, t] in subTime
        float h     = array.get(subHigh, k)
        float l     = array.get(subLow,  k)
        bool  isRTH = f_isRTH(t)
        bool  first = na(prevTime)
 
        // Weekend: more than a day since the last bar. Start a fresh ETH at the
        // Sunday open, dropping Friday's 16:00 to 17:00 hour.
        if not first and t - prevTime > DAY_MS and not isRTH
            ethHigh := na
            ethLow  := na
            ethFull := true
 
        // ETH just closed (first RTH bar of the day): draw the ETH levels.
        if not first and isRTH and not prevRTH and not na(ethHigh)
            if ethFull and i_showETH and i_showETHHigh
                f_spawn(ethH_lines, ethH_labels, ethH_levels,
                     ethHighTime, ethHigh, "ETH H", i_ethColor, i_ethStyle, i_ethWidth)
            if ethFull and i_showETH and i_showETHLow
                f_spawn(ethL_lines, ethL_labels, ethL_levels,
                     ethLowTime, ethLow, "ETH L", i_ethColor, i_ethStyle, i_ethWidth)
            ethHigh := na
            ethLow  := na
            rthHigh := na
            rthLow  := na
            rthFull := true
 
        // RTH just closed (first bar after 16:00), or a new day on a symbol that
        // only trades the regular session: draw the RTH levels.
        bool rthClosed = not first and prevRTH and
             (not isRTH or dayofmonth(t, TZ) != dayofmonth(prevTime, TZ))
        if rthClosed and not na(rthHigh)
            if rthFull and i_showRTH and i_showRTHHigh
                f_spawn(rthH_lines, rthH_labels, rthH_levels,
                     rthHighTime, rthHigh, "RTH H", i_rthColor, i_rthStyle, i_rthWidth)
            if rthFull and i_showRTH and i_showRTHLow
                f_spawn(rthL_lines, rthL_labels, rthL_levels,
                     rthLowTime, rthLow, "RTH L", i_rthColor, i_rthStyle, i_rthWidth)
            rthHigh := na
            rthLow  := na
        if rthClosed
            ethFull := true
            rthFull := true
 
        // Only price after a level formed can mitigate it.
        if i_mitigation
            f_mitigate(ethH_lines, ethH_labels, ethH_levels, h, l)
            f_mitigate(ethL_lines, ethL_labels, ethL_levels, h, l)
            f_mitigate(rthH_lines, rthH_labels, rthH_levels, h, l)
            f_mitigate(rthL_lines, rthL_labels, rthL_levels, h, l)
 
        // Build the running high and low of the session this bar belongs to.
        if isRTH
            if na(rthHigh) or h > rthHigh
                rthHigh     := h
                rthHighTime := t
            if na(rthLow) or l < rthLow
                rthLow      := l
                rthLowTime  := t
        else
            if na(ethHigh) or h > ethHigh
                ethHigh     := h
                ethHighTime := t
            if na(ethLow) or l < ethLow
                ethLow      := l
                ethLowTime  := t
 
        prevTime := t
        prevRTH  := isRTH
 
    f_extend(ethH_lines, ethH_labels)
    f_extend(ethL_lines, ethL_labels)
    f_extend(rthH_lines, rthH_labels)
    f_extend(rthL_lines, rthL_labels)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 7 — NOTICE ABOVE THE 4 HOUR CHART
// ══════════════════════════════════════════════════════════════════════════════
 
var table notice = table.new(position.bottom_right, 1, 1)
if barstate.islast and not active
    table.cell(notice, 0, 0, "MonoIntel Session Levels works on 4 hour charts and below.",
         text_color=chart.fg_color, text_size=size.small)

← 返回列表