ICT Midnight Opening Range & Standard Deviation [TradeSymmetry]

TradeSymmetry · study · 649 行 · 点赞 372 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradeSymmetry
 
//@version=6
indicator("ICT Midnight Opening Range & Standard Deviation [TradeSymmetry]", shorttitle="MOR StdDev", overlay=true, max_bars_back=5000, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
 
// ==============================================================================
// === 1. INPUTS ===
// ==============================================================================
 
// --- Group: Calculation Period ---
group_calc               = "Calculation Period"
mor_session              = input.session("0000-0030", title="Midnight Range Period (HHMM-HHMM)", group=group_calc, tooltip="Duration of the Midnight Opening Range (MOR) starting at 00:00 New York Midnight.")
tz_input                 = input.string("America/New_York", "Timezone", options=["America/New_York", "UTC", "Exchange", "Europe/London", "Asia/Tokyo"], group=group_calc, tooltip="ICT methodology strictly anchors to New York local time (EST/EDT) for the 00:00 True Day Open.")
tf_limit                 = input.timeframe("60", "Max Timeframe Display Limit", group=group_calc, tooltip="Drawings will not appear on timeframes greater than this limit.")
 
// --- Group: Information & Visibility ---
group_info               = "Information & Visibility"
show_extra_levels        = input.bool(true,  "Show Extra SD Levels (2.0 to 4.5 SD)",                  group=group_info)
show_intermediate_levels = input.bool(true,  "Show Intermediate 50% Levels (0.5 EQ, 1.5, 2.25, 4.25)", group=group_info)
show_mor_calc_area       = input.bool(true,  "Midnight Range calculation period coloration (bgcolor)", group=group_info)
fill_mor_areas           = input.bool(true,  "Colour Midnight Range box",                             group=group_info)
show_tdo                 = input.bool(true,  "Show Midnight Open (00:00 TDO)",                        group=group_info)
show_zone_fills          = input.bool(true,  "Highlight 2.0-2.5 SD & 4.0-4.5 SD Target Zones",        group=group_info)
only_current_levels      = input.bool(false, "Only display the current Day Levels",                   group=group_info)
only_current_zone        = input.bool(false, "Only display the current Day Range Box",                group=group_info)
extend_until_next        = input.bool(true,  "Extend Lines Until Next Session Starts",                group=group_info, tooltip="When enabled, lines continue extending across the day right up until the next Midnight session begins, exactly like IB.")
extend_mode              = input.string("Until Next Session Starts", "Line Extension Mode", options=["Until Next Session Starts", "Until Next Session Ends", "End of Day (17:00 NY)"], group=group_info, tooltip="Choose when the horizontal lines stop extending: when the next session starts, when the next session finishes calculating, or at End of Day.")
max_periods              = input.int(10,     "Historical Days to Keep", minval=1, maxval=20,          group=group_info, tooltip="Number of historical days to display on chart (up to 20 days). When increased, historical sessions appear seamlessly across all timeframes.")
 
// --- Group: Standard Deviation Multipliers (0, -2, -2.25, 1, -1, -2.5, -4, -4.25, -4.5, -1.5) ---
group_sd                 = "Standard Deviation Multipliers"
show_upper_sd            = input.bool(true,  "Show Upper Projections (Bullish Targets)", group=group_sd)
show_lower_sd            = input.bool(true,  "Show Lower Projections (Bearish Targets)", group=group_sd)
 
show_sd_1_0              = input.bool(false,  "1.00 SD Target", inline="sd1",  group=group_sd)
val_sd_1_0               = input.float(1.0,  "", inline="sd1", group=group_sd)
show_sd_1_5              = input.bool(false,  "1.50 SD Target", inline="sd15", group=group_sd)
val_sd_1_5               = input.float(1.5,  "", inline="sd15", group=group_sd)
show_sd_2_0              = input.bool(true,  "2.00 SD Target (Primary TP)", inline="sd20", group=group_sd)
val_sd_2_0               = input.float(2.0,  "", inline="sd20", group=group_sd)
show_sd_2_25             = input.bool(true,  "2.25 SD Target", inline="sd225", group=group_sd)
val_sd_2_25              = input.float(2.25, "", inline="sd225", group=group_sd)
show_sd_2_5              = input.bool(true,  "2.50 SD Target (Exhaustion)", inline="sd25", group=group_sd)
val_sd_2_5               = input.float(2.5,  "", inline="sd25", group=group_sd)
show_sd_4_0              = input.bool(true,  "4.00 SD Target (Macro Run)", inline="sd40", group=group_sd)
val_sd_4_0               = input.float(4.0,  "", inline="sd40", group=group_sd)
show_sd_4_25             = input.bool(true,  "4.25 SD Target", inline="sd425", group=group_sd)
val_sd_4_25              = input.float(4.25, "", inline="sd425", group=group_sd)
show_sd_4_5              = input.bool(true,  "4.50 SD Target (Macro Extreme)", inline="sd45", group=group_sd)
val_sd_4_5               = input.float(4.5,  "", inline="sd45", group=group_sd)
 
// --- Group: Label Settings (Decluttered & Clean) ---
group_label              = "Label Settings"
show_labels              = input.bool(true,  "Show Labels", group=group_label)
label_filter             = input.string("Major Targets Only", "Levels to Label", options=["Major Targets Only", "Key Targets Only", "All Levels", "Range Bounds Only", "None (Lines Only)"], group=group_label, tooltip="Filters out intermediate labels on tight ranges so text does not collide vertically. Lines and zone fills still display.")
stagger_labels           = input.bool(true,  "Stagger Alternate Labels Horizontally", group=group_label, tooltip="Places intermediate labels further right (+6 bars) so adjacent price levels never overlap each other.")
stagger_distance         = input.int(6,      "Stagger Offset (Bars)", minval=2, maxval=20, group=group_label)
show_price_in_label      = input.bool(true,  "Show Price in Labels", group=group_label)
label_format             = input.string("+/- SD", "Label Format", options=["+/- SD", "Multiplier Only", "MOR +/- Δ", "Full Title", "Price Only"], group=group_label)
label_x_offset_bars      = input.int(2,      "Label Base X Offset (Bars)", minval=0, group=group_label)
label_y_offset           = input.float(0.0,  "Label Y Offset (Price)", step=0.1, group=group_label)
label_size_str           = input.string("Small", "Label Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=group_label)
hide_mitigated_labels    = input.bool(false, "Hide Labels When Level Crossed", group=group_label, tooltip="Automatically hides labels once price trades through the level, keeping only future unreached targets visible.")
 
// --- Group: Drawing Parameters & Colors ---
group_drawings           = "Drawings & Colors"
lvl_width                = input.int(1,           "Main price level width",                               group=group_drawings)
tdo_col                  = input.color(#ffd700,   "Midnight Open (00:00 TDO) color",                      group=group_drawings)
high_col                 = input.color(color.green, "Midnight range high (MORH) color",                   group=group_drawings)
low_col                  = input.color(color.red,   "Midnight range low (MORL) color",                    group=group_drawings)
middle_col               = input.color(#ffa726,   "50% Equilibrium (MORM) color",                         group=group_drawings)
upper_sd_col             = input.color(#26a69a,   "Upper SD Levels color",                                group=group_drawings)
lower_sd_col             = input.color(#ef5350,   "Lower SD Levels color",                                group=group_drawings)
zone_2_col               = input.color(color.new(#ff9800, 85), "2.0 - 2.5 SD Reaction Zone Fill",         group=group_drawings)
zone_4_col               = input.color(color.new(#ab47bc, 85), "4.0 - 4.5 SD Macro Zone Fill",            group=group_drawings)
 
main_levels_style        = input.string("Solid",  "Main levels line style",     options=["Solid","Dashed","Dotted"], group=group_drawings)
tdo_levels_style         = input.string("Dashed", "Midnight Open line style",   options=["Solid","Dashed","Dotted"], group=group_drawings)
ext_levels_style         = input.string("Solid",  "Extended SD line style",     options=["Solid","Dashed","Dotted"], group=group_drawings)
int_levels_style         = input.string("Dotted", "Intermediate 50% line style", options=["Solid","Dashed","Dotted"], group=group_drawings)
 
fill_mor_color           = input.color(color.new(#2962ff, 85), "Midnight range area background color",     group=group_drawings)
mor_calc_color           = input.color(#673ab730, "Calculation zone highlight (bgcolor)",                 group=group_drawings)
 
// --- Group: Dashboard Table ---
group_table              = "Dashboard Table"
show_table               = input.bool(false, "Show Dashboard Table", group=group_table)
table_pos_str            = input.string("Top Right", "Table Position", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group=group_table)
table_size_str           = input.string("Small", "Table Size", options=["Tiny", "Small", "Normal"], group=group_table)
 
// ==============================================================================
// === 2. HELPER FUNCTIONS & TYPES ===
// ==============================================================================
 
type MorLabelItem
    label lbl
    int   offset_x
    float price
    bool  is_upper
 
get_line_style(string styleStr) =>
    styleStr == "Solid" ? line.style_solid : styleStr == "Dashed" ? line.style_dashed : line.style_dotted
 
get_label_size(string sizeStr) =>
    switch sizeStr
        "Tiny"   => size.tiny
        "Small"  => size.small
        "Normal" => size.normal
        "Large"  => size.large
        "Huge"   => size.huge
        => size.small
 
get_table_pos(string s) =>
    s == "Top Right" ? position.top_right : s == "Bottom Right" ? position.bottom_right : s == "Top Left" ? position.top_left : position.bottom_left
 
get_table_size(string s) =>
    s == "Tiny" ? size.tiny : s == "Small" ? size.small : size.normal
 
// Check if current bar falls within the midnight opening range session
inSession(string sess, string tz) =>
    not na(time(timeframe.period, sess + ":1234567", tz))
 
// Calculate range levels from offset bars
get_levels(int n) =>
    float h = high[1]
    float l = low[1]
    float o = open[math.max(1, n)]
    for i = 1 to n
        if low[i] < l
            l := low[i]
        if high[i] > h
            h := high[i]
    [h, l, (h + l) / 2, o]
 
// Functions to manage history arrays
manage_line_history(array<line> line_array, int max_len) =>
    while array.size(line_array) > max_len
        oldest_line = array.pop(line_array)
        if not na(oldest_line)
            line.delete(oldest_line)
 
manage_box_history(array<box> box_array, int max_len) =>
    while array.size(box_array) > max_len
        oldest_box = array.pop(box_array)
        if not na(oldest_box)
            box.delete(oldest_box)
 
manage_fill_history(array<linefill> fill_array, int max_len) =>
    while array.size(fill_array) > max_len
        oldest_fill = array.pop(fill_array)
        if not na(oldest_fill)
            linefill.delete(oldest_fill)
 
// Delete all active Mor labels
delete_all_mor_labels(array<MorLabelItem> arr) =>
    if array.size(arr) > 0
        for i = 0 to array.size(arr) - 1
            item = array.get(arr, i)
            if not na(item.lbl)
                label.delete(item.lbl)
        array.clear(arr)
 
// Check if a specific level should display a text label
f_should_label(string level_type, float mult) =>
    bool allow = false
    if label_filter == "None (Lines Only)"
        allow := false
    else if label_filter == "All Levels"
        allow := true
    else if label_filter == "Range Bounds Only"
        allow := (level_type == "TDO" or level_type == "MORH" or level_type == "MORL")
    else if label_filter == "Key Targets Only"
        allow := (level_type == "TDO" or level_type == "MORH" or level_type == "MORL" or mult == val_sd_2_0 or mult == val_sd_2_5 or mult == val_sd_4_0)
    else if label_filter == "Major Targets Only"
        allow := (level_type == "TDO" or level_type == "MORH" or level_type == "MORL" or mult == val_sd_1_0 or mult == val_sd_2_0 or mult == val_sd_2_5 or mult == val_sd_4_0 or mult == val_sd_4_5)
    allow
 
// Format the label text
f_label_txt(string base_title, float mult, float price, bool is_upper) =>
    string prc_str  = show_price_in_label ? str.format(" ({0})", str.tostring(price, format.mintick)) : ""
    string sign     = is_upper ? "+" : "-"
    string mult_str = str.tostring(mult, "#.##")
    string name     = ""
    if mult == 0.0
        name := base_title + prc_str
    else if mult == 0.5
        name := (label_format == "Multiplier Only" or label_format == "+/- SD") ? "50% EQ" : "MORM (50%)"
        name := name + prc_str
    else
        if label_format == "Multiplier Only"
            name := sign + mult_str + prc_str
        else if label_format == "+/- SD"
            name := sign + mult_str + " SD" + prc_str
        else if label_format == "MOR +/- Δ"
            name := (is_upper ? "MORH+" : "MORL-") + mult_str + "Δ" + prc_str
        else if label_format == "Full Title"
            name := (is_upper ? "MORH +" : "MORL -") + mult_str + " SD" + prc_str
        else if label_format == "Price Only"
            name := str.tostring(price, format.mintick)
        else
            name := sign + mult_str + " SD" + prc_str
    name
 
// ==============================================================================
// === 3. STATE & DATA STRUCTURES ===
// ==============================================================================
 
tf_valid = timeframe.isintraday and (timeframe.in_seconds() <= timeframe.in_seconds(tf_limit))
 
var line_style_value = get_line_style(main_levels_style)
var tdo_style_value  = get_line_style(tdo_levels_style)
var ext_style_value  = get_line_style(ext_levels_style)
var int_style_value  = get_line_style(int_levels_style)
var label_size_value = get_label_size(label_size_str)
 
var float mor_delta       = na
var float current_tdo     = na
var float current_h       = na
var float current_l       = na
var float current_m       = na
var int   offset          = 0
var int   mor_start_time  = na
var float mor_open_price  = na
 
ins = tf_valid and inSession(mor_session, tz_input)
 
// Global bar step time calculation (calculated on each bar, zero ta.* inside helper functions)
var int bar_step_ms = 60000
if not na(time[1]) and time > time[1]
    bar_step_ms := time - time[1]
 
// Background coloration during the Midnight Opening Range calculation zone
bgcolor(show_mor_calc_area and ins ? mor_calc_color : na, title="Midnight OR Calculation Zone")
 
// Arrays for historical lines (supporting multi-day retention up to max_periods)
var array<line> morh_lines   = array.new<line>()
var array<line> morl_lines   = array.new<line>()
var array<line> morm_lines   = array.new<line>()
var array<line> tdo_lines    = array.new<line>()
 
var array<line> u_1_lines    = array.new<line>()
var array<line> u_15_lines   = array.new<line>()
var array<line> u_2_lines    = array.new<line>()
var array<line> u_225_lines  = array.new<line>()
var array<line> u_25_lines   = array.new<line>()
var array<line> u_4_lines    = array.new<line>()
var array<line> u_425_lines  = array.new<line>()
var array<line> u_45_lines   = array.new<line>()
 
var array<line> l_1_lines    = array.new<line>()
var array<line> l_15_lines   = array.new<line>()
var array<line> l_2_lines    = array.new<line>()
var array<line> l_225_lines  = array.new<line>()
var array<line> l_25_lines   = array.new<line>()
var array<line> l_4_lines    = array.new<line>()
var array<line> l_425_lines  = array.new<line>()
var array<line> l_45_lines   = array.new<line>()
 
var array<box>      mor_boxes  = array.new<box>()
var array<linefill> zone_fills = array.new<linefill>()
 
// Array for active labels (UDT items)
var array<MorLabelItem> active_mor_labels = array.new<MorLabelItem>()
 
// Helper to create and track a target label with staggering (uses global bar_step_ms, NO ta.* inside)
f_create_target_label(float price_level, string level_name, float mult, color col, bool is_upper, bool is_staggered) =>
    label res_lbl = na
    if show_labels and f_should_label(level_name, mult)
        int x_off_bars = label_x_offset_bars + (stagger_labels and is_staggered ? stagger_distance : 0)
        int x_time = time + x_off_bars * bar_step_ms
        string txt = f_label_txt(level_name, mult, price_level, is_upper)
        res_lbl := label.new(x_time, price_level + label_y_offset, txt, xloc=xloc.bar_time, style=label.style_label_left, size=label_size_value, textcolor=col, color=color.new(color.black, 100))
        item = MorLabelItem.new(res_lbl, x_off_bars, price_level, is_upper)
        array.push(active_mor_labels, item)
    res_lbl
 
// Current Day Drawing Variables (all use xloc.bar_time)
var line  morh_line  = na
var line  morl_line  = na
var line  morm_line  = na
var line  tdo_line   = na
var box   mor_box    = na
var box   live_box   = na
 
var line  u_1_line   = na
var line  u_15_line  = na
var line  u_2_line   = na
var line  u_225_line = na
var line  u_25_line  = na
var line  u_4_line   = na
var line  u_425_line = na
var line  u_45_line  = na
 
var line  l_1_line   = na
var line  l_15_line  = na
var line  l_2_line   = na
var line  l_225_line = na
var line  l_25_line  = na
var line  l_4_line   = na
var line  l_425_line = na
var line  l_45_line  = na
 
var label morh_label = na
var label morl_label = na
var label morm_label = na
var label tdo_label  = na
 
// ==============================================================================
// === 4. BUILD SESSION & DRAWINGS ===
// ==============================================================================
 
// Lock in session start time and true day open
if ins and not ins[1]
    mor_start_time  := time
    mor_open_price  := open
    offset          := 0
 
// During calculation period: count bars and update live visual box
if ins
    offset += 1
    if fill_mor_areas
        if na(live_box)
            live_box := box.new(left = mor_start_time, top = high, right = time, bottom = low, bgcolor = fill_mor_color, border_color = color.new(high_col, 50), border_width = 1, xloc = xloc.bar_time)
        else
            box.set_top(live_box, math.max(box.get_top(live_box), high))
            box.set_bottom(live_box, math.min(box.get_bottom(live_box), low))
            box.set_right(live_box, time)
 
// When Midnight session completes: calculate levels and establish standard deviations
if ins[1] and not ins
    [h, l, m, o] = get_levels(offset)
    mor_delta   := h - l
    current_tdo := o
    current_h   := h
    current_l   := l
    current_m   := m
 
    // Remove live forming box
    if not na(live_box)
        box.delete(live_box)
        live_box := na
 
    // Unconditionally delete previous active labels (matching Initial Balance reference)
    if show_labels
        delete_all_mor_labels(active_mor_labels)
 
    // Purge previous day drawings if 'only_current_levels' is active
    if only_current_levels
        manage_line_history(morh_lines, 0)
        manage_line_history(morl_lines, 0)
        manage_line_history(morm_lines, 0)
        manage_line_history(tdo_lines,  0)
        manage_line_history(u_1_lines,  0)
        manage_line_history(u_15_lines, 0)
        manage_line_history(u_2_lines,  0)
        manage_line_history(u_225_lines,0)
        manage_line_history(u_25_lines, 0)
        manage_line_history(u_4_lines,  0)
        manage_line_history(u_425_lines,0)
        manage_line_history(u_45_lines, 0)
        manage_line_history(l_1_lines,  0)
        manage_line_history(l_15_lines, 0)
        manage_line_history(l_2_lines,  0)
        manage_line_history(l_225_lines,0)
        manage_line_history(l_25_lines, 0)
        manage_line_history(l_4_lines,  0)
        manage_line_history(l_425_lines,0)
        manage_line_history(l_45_lines, 0)
        manage_fill_history(zone_fills, 0)
 
    // 1. Draw Range Box
    if fill_mor_areas
        if only_current_zone
            manage_box_history(mor_boxes, 0)
        mor_box := box.new(left = mor_start_time, top = h, right = time, bottom = l, bgcolor = fill_mor_color, border_color = color.new(color.black, 100), xloc = xloc.bar_time)
        array.unshift(mor_boxes, mor_box)
        manage_box_history(mor_boxes, max_periods)
 
    // 2. Draw Midnight Open (TDO)
    if show_tdo
        tdo_line := line.new(mor_start_time, o, time, o, color=tdo_col, width=lvl_width + 1, style=tdo_style_value, xloc=xloc.bar_time)
        array.unshift(tdo_lines, tdo_line)
        manage_line_history(tdo_lines, max_periods)
        tdo_label := f_create_target_label(o, "TDO", 0.0, tdo_col, true, false)
 
    // 3. Draw MOR High & Low Lines
    morh_line := line.new(mor_start_time, h, time, h, color=high_col, width=lvl_width, style=line_style_value, xloc=xloc.bar_time)
    array.unshift(morh_lines, morh_line)
    manage_line_history(morh_lines, max_periods)
    morh_label := f_create_target_label(h, "MORH", 0.0, high_col, true, false)
 
    morl_line := line.new(mor_start_time, l, time, l, color=low_col, width=lvl_width, style=line_style_value, xloc=xloc.bar_time)
    array.unshift(morl_lines, morl_line)
    manage_line_history(morl_lines, max_periods)
    morl_label := f_create_target_label(l, "MORL", 0.0, low_col, false, false)
 
    // 4. Draw Intermediate 50% Equilibrium Line
    if show_intermediate_levels
        morm_line := line.new(mor_start_time, m, time, m, color=middle_col, width=lvl_width, style=int_style_value, xloc=xloc.bar_time)
        array.unshift(morm_lines, morm_line)
        manage_line_history(morm_lines, max_periods)
        morm_label := f_create_target_label(m, "MORM", 0.5, middle_col, true, true)
 
    // 5. Draw Standard Deviation Projection Lines
    // Upper Levels
    if show_upper_sd
        if show_sd_1_0
            u_1_line := line.new(mor_start_time, h + mor_delta * val_sd_1_0, time, h + mor_delta * val_sd_1_0, color=upper_sd_col, width=lvl_width, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(u_1_lines, u_1_line)
            manage_line_history(u_1_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_1_0, "1.0", val_sd_1_0, upper_sd_col, true, false)
 
        if show_intermediate_levels and show_sd_1_5
            u_15_line := line.new(mor_start_time, h + mor_delta * val_sd_1_5, time, h + mor_delta * val_sd_1_5, color=upper_sd_col, width=lvl_width, style=int_style_value, xloc=xloc.bar_time)
            array.unshift(u_15_lines, u_15_line)
            manage_line_history(u_15_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_1_5, "1.5", val_sd_1_5, upper_sd_col, true, true)
 
        if show_extra_levels and show_sd_2_0
            u_2_line := line.new(mor_start_time, h + mor_delta * val_sd_2_0, time, h + mor_delta * val_sd_2_0, color=upper_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(u_2_lines, u_2_line)
            manage_line_history(u_2_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_2_0, "2.0", val_sd_2_0, upper_sd_col, true, false)
 
        if show_extra_levels and show_intermediate_levels and show_sd_2_25
            u_225_line := line.new(mor_start_time, h + mor_delta * val_sd_2_25, time, h + mor_delta * val_sd_2_25, color=upper_sd_col, width=lvl_width, style=int_style_value, xloc=xloc.bar_time)
            array.unshift(u_225_lines, u_225_line)
            manage_line_history(u_225_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_2_25, "2.25", val_sd_2_25, upper_sd_col, true, true)
 
        if show_extra_levels and show_sd_2_5
            u_25_line := line.new(mor_start_time, h + mor_delta * val_sd_2_5, time, h + mor_delta * val_sd_2_5, color=upper_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(u_25_lines, u_25_line)
            manage_line_history(u_25_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_2_5, "2.5", val_sd_2_5, upper_sd_col, true, false)
 
        if show_extra_levels and show_sd_4_0
            u_4_line := line.new(mor_start_time, h + mor_delta * val_sd_4_0, time, h + mor_delta * val_sd_4_0, color=upper_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(u_4_lines, u_4_line)
            manage_line_history(u_4_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_4_0, "4.0", val_sd_4_0, upper_sd_col, true, false)
 
        if show_extra_levels and show_intermediate_levels and show_sd_4_25
            u_425_line := line.new(mor_start_time, h + mor_delta * val_sd_4_25, time, h + mor_delta * val_sd_4_25, color=upper_sd_col, width=lvl_width, style=int_style_value, xloc=xloc.bar_time)
            array.unshift(u_425_lines, u_425_line)
            manage_line_history(u_425_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_4_25, "4.25", val_sd_4_25, upper_sd_col, true, true)
 
        if show_extra_levels and show_sd_4_5
            u_45_line := line.new(mor_start_time, h + mor_delta * val_sd_4_5, time, h + mor_delta * val_sd_4_5, color=upper_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(u_45_lines, u_45_line)
            manage_line_history(u_45_lines, max_periods)
            lbl = f_create_target_label(h + mor_delta * val_sd_4_5, "4.5", val_sd_4_5, upper_sd_col, true, false)
 
    // Lower Levels
    if show_lower_sd
        if show_sd_1_0
            l_1_line := line.new(mor_start_time, l - mor_delta * val_sd_1_0, time, l - mor_delta * val_sd_1_0, color=lower_sd_col, width=lvl_width, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(l_1_lines, l_1_line)
            manage_line_history(l_1_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_1_0, "1.0", val_sd_1_0, lower_sd_col, false, false)
 
        if show_intermediate_levels and show_sd_1_5
            l_15_line := line.new(mor_start_time, l - mor_delta * val_sd_1_5, time, l - mor_delta * val_sd_1_5, color=lower_sd_col, width=lvl_width, style=int_style_value, xloc=xloc.bar_time)
            array.unshift(l_15_lines, l_15_line)
            manage_line_history(l_15_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_1_5, "1.5", val_sd_1_5, lower_sd_col, false, true)
 
        if show_extra_levels and show_sd_2_0
            l_2_line := line.new(mor_start_time, l - mor_delta * val_sd_2_0, time, l - mor_delta * val_sd_2_0, color=lower_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(l_2_lines, l_2_line)
            manage_line_history(l_2_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_2_0, "2.0", val_sd_2_0, lower_sd_col, false, false)
 
        if show_extra_levels and show_intermediate_levels and show_sd_2_25
            l_225_line := line.new(mor_start_time, l - mor_delta * val_sd_2_25, time, l - mor_delta * val_sd_2_25, color=lower_sd_col, width=lvl_width, style=int_style_value, xloc=xloc.bar_time)
            array.unshift(l_225_lines, l_225_line)
            manage_line_history(l_225_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_2_25, "2.25", val_sd_2_25, lower_sd_col, false, true)
 
        if show_extra_levels and show_sd_2_5
            l_25_line := line.new(mor_start_time, l - mor_delta * val_sd_2_5, time, l - mor_delta * val_sd_2_5, color=lower_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(l_25_lines, l_25_line)
            manage_line_history(l_25_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_2_5, "2.5", val_sd_2_5, lower_sd_col, false, false)
 
        if show_extra_levels and show_sd_4_0
            l_4_line := line.new(mor_start_time, l - mor_delta * val_sd_4_0, time, l - mor_delta * val_sd_4_0, color=lower_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(l_4_lines, l_4_line)
            manage_line_history(l_4_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_4_0, "4.0", val_sd_4_0, lower_sd_col, false, false)
 
        if show_extra_levels and show_intermediate_levels and show_sd_4_25
            l_425_line := line.new(mor_start_time, l - mor_delta * val_sd_4_25, time, l - mor_delta * val_sd_4_25, color=lower_sd_col, width=lvl_width, style=int_style_value, xloc=xloc.bar_time)
            array.unshift(l_425_lines, l_425_line)
            manage_line_history(l_425_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_4_25, "4.25", val_sd_4_25, lower_sd_col, false, true)
 
        if show_extra_levels and show_sd_4_5
            l_45_line := line.new(mor_start_time, l - mor_delta * val_sd_4_5, time, l - mor_delta * val_sd_4_5, color=lower_sd_col, width=lvl_width + 1, style=ext_style_value, xloc=xloc.bar_time)
            array.unshift(l_45_lines, l_45_line)
            manage_line_history(l_45_lines, max_periods)
            lbl = f_create_target_label(l - mor_delta * val_sd_4_5, "4.5", val_sd_4_5, lower_sd_col, false, false)
 
    // Shaded Reaction Zones (2.0-2.5 SD and 4.0-4.5 SD)
    if show_zone_fills
        if not na(u_2_line) and not na(u_25_line)
            array.push(zone_fills, linefill.new(u_2_line, u_25_line, zone_2_col))
        if not na(l_2_line) and not na(l_25_line)
            array.push(zone_fills, linefill.new(l_2_line, l_25_line, zone_2_col))
        if not na(u_4_line) and not na(u_45_line)
            array.push(zone_fills, linefill.new(u_4_line, u_45_line, zone_4_col))
        if not na(l_4_line) and not na(l_45_line)
            array.push(zone_fills, linefill.new(l_4_line, l_45_line, zone_4_col))
        manage_fill_history(zone_fills, max_periods * 4)
 
    offset := 0
 
// ==============================================================================
// === 5. UPDATE LINES DURING THE TRADING DAY (Matching Reference Style) ===
// ==============================================================================
 
// Determine if lines should extend on the current bar
bool should_extend = false
 
if extend_until_next
    if extend_mode == "Until Next Session Starts"
        should_extend := not ins
    else if extend_mode == "Until Next Session Ends"
        should_extend := not (ins[1] and not ins)
    else if extend_mode == "End of Day (17:00 NY)"
        ny_hour = hour(time, tz_input)
        should_extend := not ins and (ny_hour < 17)
else
    ny_hour = hour(time, tz_input)
    should_extend := not ins and (ny_hour < 17)
 
if should_extend and not na(morh_line)
    // Update main level lines
    line.set_x2(morh_line, time)
    line.set_x2(morl_line, time)
 
    if show_tdo and not na(tdo_line)
        line.set_x2(tdo_line, time)
 
    if show_intermediate_levels and not na(morm_line)
        line.set_x2(morm_line, time)
 
    // Update Upper Projections
    if show_upper_sd
        if not na(u_1_line)
            line.set_x2(u_1_line, time)
        if not na(u_15_line)
            line.set_x2(u_15_line, time)
        if not na(u_2_line)
            line.set_x2(u_2_line, time)
        if not na(u_225_line)
            line.set_x2(u_225_line, time)
        if not na(u_25_line)
            line.set_x2(u_25_line, time)
        if not na(u_4_line)
            line.set_x2(u_4_line, time)
        if not na(u_425_line)
            line.set_x2(u_425_line, time)
        if not na(u_45_line)
            line.set_x2(u_45_line, time)
 
    // Update Lower Projections
    if show_lower_sd
        if not na(l_1_line)
            line.set_x2(l_1_line, time)
        if not na(l_15_line)
            line.set_x2(l_15_line, time)
        if not na(l_2_line)
            line.set_x2(l_2_line, time)
        if not na(l_225_line)
            line.set_x2(l_225_line, time)
        if not na(l_25_line)
            line.set_x2(l_25_line, time)
        if not na(l_4_line)
            line.set_x2(l_4_line, time)
        if not na(l_425_line)
            line.set_x2(l_425_line, time)
        if not na(l_45_line)
            line.set_x2(l_45_line, time)
 
    // Update label positions using global bar_step_ms
    if show_labels and array.size(active_mor_labels) > 0
        for i = 0 to array.size(active_mor_labels) - 1
            item = array.get(active_mor_labels, i)
            if not na(item.lbl)
                label.set_x(item.lbl, time + item.offset_x * bar_step_ms)
 
// Mitigated Labels Logic
if hide_mitigated_labels and array.size(active_mor_labels) > 0
    for i = 0 to array.size(active_mor_labels) - 1
        item = array.get(active_mor_labels, i)
        if not na(item.lbl)
            if item.is_upper and high >= item.price
                label.delete(item.lbl)
                item.lbl := na
            else if not item.is_upper and low <= item.price
                label.delete(item.lbl)
                item.lbl := na
 
// ==============================================================================
// === 6. DASHBOARD TABLE ===
// ==============================================================================
 
var table info_tbl = table.new(get_table_pos(table_pos_str), 2, 6, bgcolor = #131722, frame_color = #363c4e, frame_width = 1, border_color = #2a2e39, border_width = 1)
 
if show_table and barstate.islast
    string status_str = ins ? "FORMING (00:00-00:30)" : "ESTABLISHED"
    color  status_col = ins ? #ffa726 : #26a69a
    string bias_str   = na(current_m) ? "WAITING" : close > current_h ? "PREMIUM (BULL)" : close < current_l ? "DISCOUNT (BEAR)" : "EQUILIBRIUM"
    color  bias_col   = na(current_m) ? color.gray : close > current_h ? high_col : close < current_l ? low_col : middle_col
 
    table.cell(info_tbl, 0, 0, "Midnight Range [MOR]", bgcolor = #1e222d, text_color = color.white, text_size = get_table_size(table_size_str))
    table.cell(info_tbl, 1, 0, status_str,               bgcolor = #1e222d, text_color = status_col,  text_size = get_table_size(table_size_str))
 
    table.cell(info_tbl, 0, 1, "00:00 Open (TDO)",       bgcolor = #131722, text_color = tdo_col,      text_size = get_table_size(table_size_str))
    table.cell(info_tbl, 1, 1, na(current_tdo) ? "-" : str.tostring(current_tdo, format.mintick), bgcolor = #131722, text_color = color.white, text_size = get_table_size(table_size_str))
 
    table.cell(info_tbl, 0, 2, "MOR High (MORH)",        bgcolor = #131722, text_color = high_col,     text_size = get_table_size(table_size_str))
    table.cell(info_tbl, 1, 2, na(current_h) ? "-" : str.tostring(current_h, format.mintick),     bgcolor = #131722, text_color = color.white, text_size = get_table_size(table_size_str))
 
    table.cell(info_tbl, 0, 3, "MOR Low (MORL)",         bgcolor = #131722, text_color = low_col,      text_size = get_table_size(table_size_str))
    table.cell(info_tbl, 1, 3, na(current_l) ? "-" : str.tostring(current_l, format.mintick),     bgcolor = #131722, text_color = color.white, text_size = get_table_size(table_size_str))
 
    table.cell(info_tbl, 0, 4, "Range Size",             bgcolor = #131722, text_color = middle_col,   text_size = get_table_size(table_size_str))
    table.cell(info_tbl, 1, 4, na(mor_delta) ? "-" : str.tostring(mor_delta, format.mintick),     bgcolor = #131722, text_color = color.white, text_size = get_table_size(table_size_str))
 
    table.cell(info_tbl, 0, 5, "Current Bias",           bgcolor = #131722, text_color = color.white,  text_size = get_table_size(table_size_str))
    table.cell(info_tbl, 1, 5, bias_str,                 bgcolor = #131722, text_color = bias_col,     text_size = get_table_size(table_size_str))
 
// ==============================================================================
// === 7. ALERTS ===
// ==============================================================================
 
bool alert_cond_formed     = not ins and ins[1]
bool alert_cond_bull_break = not na(current_h) and ta.crossover(close, current_h)
bool alert_cond_bear_break = not na(current_l) and ta.crossunder(close, current_l)
bool alert_cond_tp_hit     = not na(current_h) and not na(mor_delta) and (ta.crossover(high, current_h + mor_delta * val_sd_2_0) or ta.crossunder(low, current_l - mor_delta * val_sd_2_0))
 
alertcondition(alert_cond_formed,     title="MOR Established",          message="ICT Midnight Opening Range has completed calculation.")
alertcondition(alert_cond_bull_break, title="MOR High Breakout (Bull)", message="Price has crossed above the Midnight Opening Range High (MORH).")
alertcondition(alert_cond_bear_break, title="MOR Low Breakdown (Bear)", message="Price has crossed below the Midnight Opening Range Low (MORL).")
alertcondition(alert_cond_tp_hit,     title="2.0 SD Target Hit",        message="Price reached the 2.0 Standard Deviation target level.")

← 返回列表