MonoIntel Session Framework

monobosh · study · 331 行 · 点赞 3 · TradingView 原页

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

Pine Script

//@version=6
indicator(
    title            = "MonoIntel Session Framework: Asia Range, Key Opens and Day Dividers",
    shorttitle       = "MI Framework",
    overlay          = true,
    max_lines_count  = 500,
    max_labels_count = 500,
    max_boxes_count  = 500
)
 
// Built for CME futures on charts from 1 minute to 1 hour (seconds charts work too). All times are New York.
// Asia range = 20:00 to midnight, drawn as a shaded box once midnight prints.
// SD steps: multiples of the Asia range, 1 to 4 above the high and below the low, as wide as the session.
// Open lines: midnight (00:00), regular session (09:30), week and month. Each shows the current period only.
// Day dividers: a vertical line at every midnight, with the weekday name at noon.
// Charts whose bars split cleanly at 00:00 and 09:30 read their own bars. Other timeframes read finer
// bars underneath the chart so the opens and the Asia range stay exact.
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 1 — INPUTS
// Each line type has its own style and width on a single row (via inline=).
// ══════════════════════════════════════════════════════════════════════════════
 
string OPT_SOLID  = "Solid"
string OPT_DASHED = "Dashed"
string OPT_DOTTED = "Dotted"
 
// ── Asia Session ─────────────────────────────────────────────────────────────
string G_ASIA = "Asia Session (8pm – 12am)"
i_showAsia     = input.bool(true,                    "Show Asia Range + SDs", group=G_ASIA,
     tooltip="Draws the 20:00 to midnight range and its SD steps.")
i_asiaBoxColor = input.color(color.new(#888888, 65), "Range Fill",            group=G_ASIA,
     tooltip="Fill colour of the Asia range box.")
i_asiaSDColor  = input.color(#777777,                "SD Color",              inline="asiaSD", group=G_ASIA,
     tooltip="Colour of the SD step lines.")
i_asiaSDStyle  = input.string(OPT_SOLID,             "", options=[OPT_SOLID, OPT_DASHED, OPT_DOTTED], inline="asiaSD", group=G_ASIA,
     tooltip="Line style of the SD steps.")
i_asiaSDWidth  = input.int(1,                        "", minval=1, maxval=5,  inline="asiaSD", group=G_ASIA,
     tooltip="Line width of the SD steps.")
i_asiaHistory  = input.int(20,                       "History (sessions)",    minval=1, maxval=50, group=G_ASIA,
     tooltip="How many Asia sessions stay on the chart.")
 
// ── Open Lines ───────────────────────────────────────────────────────────────
string G_OPEN = "Open Lines"
i_openColor     = input.color(#777777, "Line Color", group=G_OPEN,
     tooltip="Colour of every open line.")
i_openTextColor = input.color(#555555, "Tag Color",  group=G_OPEN,
     tooltip="Colour of the open line tags.")
 
i_showMidnightOpen = input.bool(true,         "00:00  Midnight Open", inline="mid", group=G_OPEN,
     tooltip="Price at midnight New York time.")
i_midnightStyle    = input.string(OPT_DOTTED, "", options=[OPT_SOLID, OPT_DASHED, OPT_DOTTED], inline="mid", group=G_OPEN,
     tooltip="Line style of the midnight open.")
i_midnightWidth    = input.int(1,             "", minval=1, maxval=5, inline="mid", group=G_OPEN,
     tooltip="Line width of the midnight open.")
 
i_showRTHOpen = input.bool(true,         "9:30   RTH Open", inline="rth", group=G_OPEN,
     tooltip="Price at the 09:30 regular session open.")
i_rthStyle    = input.string(OPT_DOTTED, "", options=[OPT_SOLID, OPT_DASHED, OPT_DOTTED], inline="rth", group=G_OPEN,
     tooltip="Line style of the 09:30 open.")
i_rthWidth    = input.int(1,             "", minval=1, maxval=5, inline="rth", group=G_OPEN,
     tooltip="Line width of the 09:30 open.")
 
i_showWeeklyOpen = input.bool(true,         "W.O.   Weekly Open", inline="wk", group=G_OPEN,
     tooltip="Price at the first bar of the week.")
i_weeklyStyle    = input.string(OPT_DOTTED, "", options=[OPT_SOLID, OPT_DASHED, OPT_DOTTED], inline="wk", group=G_OPEN,
     tooltip="Line style of the weekly open.")
i_weeklyWidth    = input.int(1,             "", minval=1, maxval=5, inline="wk", group=G_OPEN,
     tooltip="Line width of the weekly open.")
 
i_showMonthlyOpen = input.bool(true,         "M.O.   Monthly Open", inline="mo", group=G_OPEN,
     tooltip="Price at the first bar of the month.")
i_monthlyStyle    = input.string(OPT_DOTTED, "", options=[OPT_SOLID, OPT_DASHED, OPT_DOTTED], inline="mo", group=G_OPEN,
     tooltip="Line style of the monthly open.")
i_monthlyWidth    = input.int(1,             "", minval=1, maxval=5, inline="mo", group=G_OPEN,
     tooltip="Line width of the monthly open.")
 
// ── Daily Separator + Day Name ───────────────────────────────────────────────
string G_DAY = "Daily Separator"
i_showDaySep   = input.bool(true,         "Show Midnight Separator", inline="sep", group=G_DAY,
     tooltip="Vertical line at every midnight New York time.")
i_daySepStyle  = input.string(OPT_DOTTED, "", options=[OPT_SOLID, OPT_DASHED, OPT_DOTTED], inline="sep", group=G_DAY,
     tooltip="Line style of the separator.")
i_daySepWidth  = input.int(1,             "", minval=1, maxval=5, inline="sep", group=G_DAY,
     tooltip="Line width of the separator.")
i_dayColor     = input.color(#888888,     "Separator Color", group=G_DAY,
     tooltip="Colour of the separator.")
i_showDayName  = input.bool(true,         "Show Day Name",   group=G_DAY,
     tooltip="Weekday name at the bottom of the chart, placed at noon.")
i_dayNameColor = input.color(#777777,     "Day Name Color",  group=G_DAY,
     tooltip="Colour of the weekday name.")
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 2 — CONSTANTS + HELPERS
// ══════════════════════════════════════════════════════════════════════════════
 
string TZ = "America/New_York"
 
f_lineStyle(string s) =>
    switch s
        OPT_DASHED => line.style_dashed
        OPT_DOTTED => line.style_dotted
        =>            line.style_solid
 
// Greatest common divisor, used to pick a data timeframe that nests inside both
// the chart bar and the 30-minute grid that 00:00, 09:30 and 20:00 sit on.
f_gcd(int a, int b) =>
    int x = a
    int y = b
    while y != 0
        int r = x % y
        x := y
        y := r
    x
 
// Open-price level: line plus a tag that sits past the line end.
// label.style_label_left with a transparent bubble puts the text to the right of the anchor.
f_drawOpen(int t, float price, string txt, string ttip, color lineCol, color textCol, string sty, int wid) =>
    ln = line.new(t, price, t, price, xloc=xloc.bar_time, color=lineCol, style=f_lineStyle(sty), width=wid)
    lb = label.new(t, price, txt, xloc=xloc.bar_time, style=label.style_label_left,
         color=color.new(color.black, 100), textcolor=textCol, size=size.normal, tooltip=ttip)
    [ln, lb]
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 3 — DATA UNDER THE CHART
// Seconds charts and minute charts that divide 30 minutes (1, 2, 3, 5, 6, 10, 15, 30)
// split cleanly at 00:00, 09:30 and 20:00, so they read their own bars and keep full
// history. Other timeframes (4, 20, 45 minutes, 1 hour...) read the largest finer
// timeframe that nests in both the chart bar and the 30-minute grid: 30 minutes on
// 1 hour, 15 on 45, 10 on 20, 2 on 4, 1 on 7. That data has a history cap, so beyond it
// those charts fall back to their own bars and some opens may not draw.
// ══════════════════════════════════════════════════════════════════════════════
 
int    tfSec   = timeframe.in_seconds()
bool   active  = timeframe.isintraday and tfSec <= 3600   // 1 hour charts and below
bool   aligned = tfSec < 60 or 1800 % tfSec == 0
string dataTF  = aligned or not active ? timeframe.period : str.tostring(f_gcd(tfSec / 60, 30))
 
[subTime, subOpen, subHigh, subLow] = request.security_lower_tf(syminfo.tickerid, dataTF,
     [time, open, high, low], ignore_invalid_timeframe=true)
 
// Use the finer data only when the chart doesn't line up and the request returned bars.
bool useSub = active and not aligned and not na(subTime) and array.size(subTime) > 0
 
array<int>   bTime = useSub ? subTime : array.from(time)
array<float> bOpen = useSub ? subOpen : array.from(open)
array<float> bHigh = useSub ? subHigh : array.from(high)
array<float> bLow  = useSub ? subLow  : array.from(low)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 4 — WALK THE DATA: ASIA RANGE + KEY MINUTES
// ══════════════════════════════════════════════════════════════════════════════
 
var float asiaHigh      = na
var float asiaLow       = na
var int   asiaStartTime = na
var bool  prevInAsia    = false
var bool  seenData      = false   // no Asia range starts on the first data, which may begin mid-session
 
// Set on the chart bar that holds the minute, na otherwise.
int   mid_t      = na
float mid_o      = na
int   rth_t      = na
float rth_o      = na
int   noon_t     = na
 
// Asia close: the box and SD steps get drawn on this bar.
bool  asiaClosed = false
int   asiaEndT   = na
float closedHigh = na
float closedLow  = na
int   closedStart = na
 
if active
    for [k, t] in bTime
        int  h      = hour(t, TZ)
        int  m      = minute(t, TZ)
        bool s0     = second(t, TZ) == 0   // one bar per minute on seconds charts
        bool inAsia = h >= 20              // 20:00 to 23:59 New York time
 
        if h == 0 and m == 0 and s0
            mid_t := t
            mid_o := array.get(bOpen, k)
        if h == 9 and m == 30 and s0
            rth_t := t
            rth_o := array.get(bOpen, k)
        if h == 12 and m == 0 and s0
            noon_t := t
 
        if inAsia and not prevInAsia and seenData
            asiaHigh      := array.get(bHigh, k)
            asiaLow       := array.get(bLow,  k)
            asiaStartTime := t
        else if inAsia
            if not na(asiaHigh)
                asiaHigh := math.max(asiaHigh, array.get(bHigh, k))
                asiaLow  := math.min(asiaLow,  array.get(bLow,  k))
        else if prevInAsia
            asiaClosed  := true
            asiaEndT    := t
            closedHigh  := asiaHigh
            closedLow   := asiaLow
            closedStart := asiaStartTime
 
        prevInAsia := inAsia
        seenData   := true
 
// Weekly and monthly bar starts follow the symbol's session (Sunday 18:00 New York
// time for CME futures), so the chart bar's own open is already the right price.
int  weekStart  = time("W")
int  monthStart = time("M")
bool newWeek    = active and not na(weekStart[1])  and weekStart  != weekStart[1]
bool newMonth   = active and not na(monthStart[1]) and monthStart != monthStart[1]
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 5 — ASIA BOX + SD STEPS
// At midnight: a shaded box for the 20:00 to midnight range, plus 8 short lines at
// 1 to 4 times the range above the high and below the low, ending at midnight.
// Oldest sessions are removed past the history limit.
// ══════════════════════════════════════════════════════════════════════════════
 
const int SD_LINES_PER_SESSION = 8
 
var box[]  asiaBoxes   = array.new<box>(0)
var line[] asiaSDLines = array.new<line>(0)
 
if asiaClosed and i_showAsia and not na(closedHigh) and not na(closedStart)
    while array.size(asiaBoxes) >= i_asiaHistory
        box.delete(array.shift(asiaBoxes))
    while array.size(asiaSDLines) > (i_asiaHistory - 1) * SD_LINES_PER_SESSION
        line.delete(array.shift(asiaSDLines))
 
    float rng = closedHigh - closedLow
 
    array.push(asiaBoxes, box.new(closedStart, closedHigh, asiaEndT, closedLow, xloc=xloc.bar_time,
         bgcolor=i_asiaBoxColor, border_color=na))
 
    for int n = 1 to 4
        array.push(asiaSDLines, line.new(closedStart, closedHigh + n * rng, asiaEndT, closedHigh + n * rng,
             xloc=xloc.bar_time, color=i_asiaSDColor, style=f_lineStyle(i_asiaSDStyle), width=i_asiaSDWidth))
        array.push(asiaSDLines, line.new(closedStart, closedLow  - n * rng, asiaEndT, closedLow  - n * rng,
             xloc=xloc.bar_time, color=i_asiaSDColor, style=f_lineStyle(i_asiaSDStyle), width=i_asiaSDWidth))
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 6 — OPEN PRICE LINES
// One live line per open type. When a new period starts, the previous line and tag
// are deleted and a fresh one is drawn: current period only.
// ══════════════════════════════════════════════════════════════════════════════
 
var line  midLine = na
var label midLbl  = na
var line  rthLine = na
var label rthLbl  = na
var line  wkLine  = na
var label wkLbl   = na
var line  moLine  = na
var label moLbl   = na
 
if not na(mid_t) and i_showMidnightOpen
    line.delete(midLine)
    label.delete(midLbl)
    [newLn, newLb] = f_drawOpen(mid_t, mid_o, "00:00", "Midnight Open", i_openColor, i_openTextColor, i_midnightStyle, i_midnightWidth)
    midLine := newLn
    midLbl  := newLb
 
if not na(rth_t) and i_showRTHOpen
    line.delete(rthLine)
    label.delete(rthLbl)
    [newLn, newLb] = f_drawOpen(rth_t, rth_o, "9:30", "RTH Open", i_openColor, i_openTextColor, i_rthStyle, i_rthWidth)
    rthLine := newLn
    rthLbl  := newLb
 
if newWeek and i_showWeeklyOpen
    line.delete(wkLine)
    label.delete(wkLbl)
    [newLn, newLb] = f_drawOpen(time, open, "W.O.", "Weekly Open", i_openColor, i_openTextColor, i_weeklyStyle, i_weeklyWidth)
    wkLine := newLn
    wkLbl  := newLb
 
if newMonth and i_showMonthlyOpen
    line.delete(moLine)
    label.delete(moLbl)
    [newLn, newLb] = f_drawOpen(time, open, "M.O.", "Monthly Open", i_openColor, i_openTextColor, i_monthlyStyle, i_monthlyWidth)
    moLine := newLn
    moLbl  := newLb
 
// Extend open lines to the current bar; the tags follow.
if active
    if not na(midLine)
        line.set_x2(midLine, time)
        label.set_x(midLbl,  time)
    if not na(rthLine)
        line.set_x2(rthLine, time)
        label.set_x(rthLbl,  time)
    if not na(wkLine)
        line.set_x2(wkLine, time)
        label.set_x(wkLbl,  time)
    if not na(moLine)
        line.set_x2(moLine, time)
        label.set_x(moLbl,  time)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 7 — DAILY SEPARATOR + DAY NAME
// Vertical line at every midnight New York time. The day name sits at the chart
// bottom via plotshape(location=location.bottom), one call per weekday because
// plotshape's text must be a constant. Futures have no Saturday or Sunday noon.
// ══════════════════════════════════════════════════════════════════════════════
 
if not na(mid_t) and i_showDaySep
    line.new(mid_t, low, mid_t, high, xloc=xloc.bar_time, extend=extend.both,
         color=i_dayColor, style=f_lineStyle(i_daySepStyle), width=i_daySepWidth)
 
// Fired at noon so the name lands mid-day, away from the midnight separator.
int   dow      = dayofweek(noon_t, TZ)
bool  showName = i_showDayName and not na(noon_t)
color transp   = color.new(color.gray, 100)
 
plotshape(showName and dow == dayofweek.monday,    "Monday",    text="MON", style=shape.labelup, location=location.bottom, color=transp, textcolor=i_dayNameColor, size=size.normal)
plotshape(showName and dow == dayofweek.tuesday,   "Tuesday",   text="TUE", style=shape.labelup, location=location.bottom, color=transp, textcolor=i_dayNameColor, size=size.normal)
plotshape(showName and dow == dayofweek.wednesday, "Wednesday", text="WED", style=shape.labelup, location=location.bottom, color=transp, textcolor=i_dayNameColor, size=size.normal)
plotshape(showName and dow == dayofweek.thursday,  "Thursday",  text="THU", style=shape.labelup, location=location.bottom, color=transp, textcolor=i_dayNameColor, size=size.normal)
plotshape(showName and dow == dayofweek.friday,    "Friday",    text="FRI", style=shape.labelup, location=location.bottom, color=transp, textcolor=i_dayNameColor, size=size.normal)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 8 — NOTICE ABOVE THE 1 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 Framework works on 1 hour charts and below.",
         text_color=chart.fg_color, text_size=size.small)

← 返回列表