Previous Time-Based Ranges

Manage2Damage · study · 547 行 · 点赞 57 · TradingView 原页

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

Pine Script

//@version=6
indicator("Previous Time-Based Ranges", overlay = true, max_lines_count = 50, max_labels_count = 50, max_boxes_count = 10)
 
// ================================================================================ //
//                              STYLE HELPER                                        //
// ================================================================================ //
f_get_style(s) =>
    s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
 
f_get_tbl_pos(pos) =>
    pos == "Top Left"      ? position.top_left      :
     pos == "Top Center"    ? position.top_center    :
     pos == "Top Right"     ? position.top_right     :
     pos == "Middle Left"   ? position.middle_left   :
     pos == "Middle Center" ? position.middle_center :
     pos == "Middle Right"  ? position.middle_right  :
     pos == "Bottom Left"   ? position.bottom_left   :
     pos == "Bottom Center" ? position.bottom_center : position.bottom_right
 
// "720" → "12H", "240" → "4H", "D"/"1D" → "D", "W" → "W", "M" → "M".
// Chart labels prefix this with P: P12H High, P4H High, PD High.
f_tf_tag(string tf) =>
    string unit = str.endswith(tf, "S") ? "S" :
         str.endswith(tf, "D") ? "D" :
         str.endswith(tf, "W") ? "W" :
         str.endswith(tf, "M") ? "M" :
         str.endswith(tf, "T") ? "T" :
         str.endswith(tf, "R") ? "R" :
         str.endswith(tf, "H") ? "H" : ""
    string tag = tf
    if unit == "D" or unit == "W" or unit == "M"
        string n = str.replace(tf, unit, "")
        tag := (n == "" or n == "1") ? unit : n + unit
    else if unit != ""
        string n = str.replace(tf, unit, "")
        tag := (n == "" ? "1" : n) + unit
    else
        float mins = str.tonumber(tf)
        tag := not na(mins) and mins >= 60 and mins % 60 == 0 ? str.tostring(mins / 60, "#") + "H" : tf
    tag
 
f_get_tbl_size(sz) =>
    sz == "Tiny"   ? size.tiny   :
     sz == "Small"  ? size.small  :
     sz == "Normal" ? size.normal :
     sz == "Large"  ? size.large  : size.auto
 
// ================================================================================ //
//                                 SLOT INPUTS                                      //
// ================================================================================ //
// --- Slot 1 ---
g1              = "=== SLOT 1 CONFIGURATION ==="
slot_on1        = input.bool(true,                                   "Enable Slot 1",              group = g1)
tf1             = input.timeframe("D",                               "Select Timeframe",           group = g1)
bull_c1         = input.color(color.new(#204b57, 88),               "Box Colors: Bullish",        group = g1, inline = "box1")
bear_c1         = input.color(color.new(#bb6671, 88),               "Bearish",                    group = g1, inline = "box1")
vl_on1          = input.bool(true,                                   "Vertical Line",              group = g1, inline = "vl1")
vl_col1         = input.color(color.new(color.gray, 20),            "Color",                      group = g1, inline = "vl1")
vl_sty1         = input.string("Solid",  "Style", options=["Solid","Dashed","Dotted"],             group = g1, inline = "vl1")
vl_w1           = input.int(1,           "Width", minval=1,                                        group = g1, inline = "vl1")
ray_off1        = input.int(50,          "Ray Extension Offset (bars)", minval=0,                  group = g1, inline = "off1")
ray_now1        = input.bool(false, "Offset to current bar", group = g1, inline = "off1")
show_75_1       = input.bool(true, "75%", group = g1, inline = "lv1")
show_50_1       = input.bool(true, "50%", group = g1, inline = "lv1")
show_25_1       = input.bool(true, "25%", group = g1, inline = "lv1")
r_col1          = input.color(color.rgb(172,172,172), "Ray Color", group = g1, inline = "rs1")
r_sty1          = input.string("Solid", "Style", options=["Solid","Dashed","Dotted"],  group = g1, inline = "rs1")
r_w1            = input.int(1,           "Width", minval=1,                                    group = g1, inline = "rs1")
lbl_c1          = input.color(color.white,             "Label Color",                              group = g1)
 
// --- Slot 2 ---
g2              = "=== SLOT 2 CONFIGURATION ==="
slot_on2        = input.bool(true,                                   "Enable Slot 2",              group = g2)
tf2             = input.timeframe("W",                               "Select Timeframe",           group = g2)
bull_c2         = input.color(color.new(#204b57, 88),               "Box Colors: Bullish",        group = g2, inline = "box2")
bear_c2         = input.color(color.new(#bb6671, 88),               "Bearish",                    group = g2, inline = "box2")
vl_on2          = input.bool(true,                                   "Vertical Line",              group = g2, inline = "vl2")
vl_col2         = input.color(color.new(color.gray, 20),            "Color",                      group = g2, inline = "vl2")
vl_sty2         = input.string("Solid",  "Style", options=["Solid","Dashed","Dotted"],             group = g2, inline = "vl2")
vl_w2           = input.int(1,           "Width", minval=1,                                        group = g2, inline = "vl2")
ray_off2        = input.int(50,          "Ray Extension Offset (bars)", minval=0,                  group = g2, inline = "off2")
ray_now2        = input.bool(false, "Offset to current bar", group = g2, inline = "off2")
show_75_2       = input.bool(true, "75%", group = g2, inline = "lv2")
show_50_2       = input.bool(true, "50%", group = g2, inline = "lv2")
show_25_2       = input.bool(true, "25%", group = g2, inline = "lv2")
r_col2          = input.color(color.rgb(172,172,172), "Ray Color", group = g2, inline = "rs2")
r_sty2          = input.string("Solid", "Style", options=["Solid","Dashed","Dotted"],  group = g2, inline = "rs2")
r_w2            = input.int(1,           "Width", minval=1,                                    group = g2, inline = "rs2")
lbl_c2          = input.color(color.white,             "Label Color",                              group = g2)
 
// --- Slot 3 ---
g3              = "=== SLOT 3 CONFIGURATION ==="
slot_on3        = input.bool(true,                                   "Enable Slot 3",              group = g3)
tf3             = input.timeframe("M",                               "Select Timeframe",           group = g3)
bull_c3         = input.color(color.new(#204b57, 88),               "Box Colors: Bullish",        group = g3, inline = "box3")
bear_c3         = input.color(color.new(#bb6671, 88),               "Bearish",                    group = g3, inline = "box3")
vl_on3          = input.bool(true,                                   "Vertical Line",              group = g3, inline = "vl3")
vl_col3         = input.color(color.new(color.gray, 20),            "Color",                      group = g3, inline = "vl3")
vl_sty3         = input.string("Solid",  "Style", options=["Solid","Dashed","Dotted"],             group = g3, inline = "vl3")
vl_w3           = input.int(1,           "Width", minval=1,                                        group = g3, inline = "vl3")
ray_off3        = input.int(50,          "Ray Extension Offset (bars)", minval=0,                  group = g3, inline = "off3")
ray_now3        = input.bool(false, "Offset to current bar", group = g3, inline = "off3")
show_75_3       = input.bool(true, "75%", group = g3, inline = "lv3")
show_50_3       = input.bool(true, "50%", group = g3, inline = "lv3")
show_25_3       = input.bool(true, "25%", group = g3, inline = "lv3")
r_col3          = input.color(color.rgb(172,172,172), "Ray Color", group = g3, inline = "rs3")
r_sty3          = input.string("Solid", "Style", options=["Solid","Dashed","Dotted"],  group = g3, inline = "rs3")
r_w3            = input.int(1,           "Width", minval=1,                                    group = g3, inline = "rs3")
lbl_c3          = input.color(color.white,             "Label Color",                              group = g3)
 
// --- Dashboard ---
g_tbl           = "=== DASHBOARD TABLE ==="
tbl_show        = input.bool(true,           "Show Dashboard Table?",  group = g_tbl)
tbl_pos_str     = input.string("Bottom Right","Table Position", options=["Top Left","Top Center","Top Right","Middle Left","Middle Center","Middle Right","Bottom Left","Bottom Center","Bottom Right"], group = g_tbl)
tbl_size_str    = input.string("Small",       "Text Size",      options=["Tiny","Small","Normal","Large","Auto"], group = g_tbl)
tbl_bg_c        = input.color(color.new(color.black, 40), "Table Background", group = g_tbl)
tbl_txt_c       = input.color(color.white,   "Text Color",     group = g_tbl)
 
// ================================================================================ //
//                               DATA FETCHING                                      //
// ================================================================================ //
// Each slot: [prev_high, prev_low, prev_open, prev_close, prev_t_start, prev_t_end, curr_t_start]
// Previous candle uses lookahead_on + [1] so it does not repaint.
// `time` is the current HTF open, known from the first bar of that period; lookahead_on
// places it on every chart bar of the period so the session tracker resets at the open.
fetch_slot(tf) =>
    [h, l, o, c, ts, te] = request.security(syminfo.tickerid, tf,
         [high[1], low[1], open[1], close[1], time[1], time_close[1]],
         gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
    cs = request.security(syminfo.tickerid, tf, time,
         gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
    [h, l, o, c, ts, te, cs]
 
[raw_h1, raw_l1, raw_o1, raw_c1, raw_ts1, raw_te1, raw_cs1] = fetch_slot(tf1)
[raw_h2, raw_l2, raw_o2, raw_c2, raw_ts2, raw_te2, raw_cs2] = fetch_slot(tf2)
[raw_h3, raw_l3, raw_o3, raw_c3, raw_ts3, raw_te3, raw_cs3] = fetch_slot(tf3)
 
// ================================================================================ //
//                           FALLBACK / PERSISTENCE LOGIC                           //
// ================================================================================ //
// Commit a slot only when the whole previous candle arrived together, so a high
// can never pair with another candle's timestamp.
var float p_h1 = na
var float p_l1 = na
var float p_o1 = na
var float p_c1 = na
var int   p_ts1 = na
var int   p_te1 = na
var int   p_cs1 = na
if not na(raw_h1) and not na(raw_l1) and not na(raw_o1) and not na(raw_c1) and not na(raw_ts1) and not na(raw_te1)
    p_h1 := raw_h1
    p_l1 := raw_l1
    p_o1 := raw_o1
    p_c1 := raw_c1
    p_ts1 := raw_ts1
    p_te1 := raw_te1
if not na(raw_cs1)
    p_cs1 := raw_cs1
 
var float p_h2 = na
var float p_l2 = na
var float p_o2 = na
var float p_c2 = na
var int   p_ts2 = na
var int   p_te2 = na
var int   p_cs2 = na
if not na(raw_h2) and not na(raw_l2) and not na(raw_o2) and not na(raw_c2) and not na(raw_ts2) and not na(raw_te2)
    p_h2 := raw_h2
    p_l2 := raw_l2
    p_o2 := raw_o2
    p_c2 := raw_c2
    p_ts2 := raw_ts2
    p_te2 := raw_te2
if not na(raw_cs2)
    p_cs2 := raw_cs2
 
var float p_h3 = na
var float p_l3 = na
var float p_o3 = na
var float p_c3 = na
var int   p_ts3 = na
var int   p_te3 = na
var int   p_cs3 = na
if not na(raw_h3) and not na(raw_l3) and not na(raw_o3) and not na(raw_c3) and not na(raw_ts3) and not na(raw_te3)
    p_h3 := raw_h3
    p_l3 := raw_l3
    p_o3 := raw_o3
    p_c3 := raw_c3
    p_ts3 := raw_ts3
    p_te3 := raw_te3
if not na(raw_cs3)
    p_cs3 := raw_cs3
 
// ================================================================================ //
//                       LIVE SESSION EXTREMES (for table ✅)                       //
// ================================================================================ //
track_extremes(cs) =>
    var float mx = na
    var float mn = na
    if ta.change(cs) != 0
        mx := high
        mn := low
    else
        mx := math.max(nz(mx, high), high)
        mn := math.min(nz(mn, low),  low)
    [mx, mn]
 
[s1_max, s1_min] = track_extremes(p_cs1)
[s2_max, s2_min] = track_extremes(p_cs2)
[s3_max, s3_min] = track_extremes(p_cs3)
 
// ================================================================================ //
//                         PERSISTENT DRAWING OBJECTS                               //
// ================================================================================ //
// Each id must be its own `var`. A comma here starts a new statement, and `var`
// does not carry over — those objects would be recreated on every bar.
mk_ray()   => line.new(na, na, na, na,  xloc=xloc.bar_time, extend=extend.none)
mk_label() => label.new(na, na, "",     xloc=xloc.bar_time, style=label.style_label_left,
                 textcolor=color.white, color=na, size=size.small,
                 text_font_family=font.family_monospace)
 
// Slot 1
var box  box1     = box.new(na, na, na, na, xloc=xloc.bar_time)
var line vl1_line = line.new(na, na, na, na, xloc=xloc.bar_time, extend=extend.both)
var line rH1  = mk_ray()
var line r751 = mk_ray()
var line r501 = mk_ray()
var line r251 = mk_ray()
var line rL1  = mk_ray()
var label lH1  = mk_label()
var label l751 = mk_label()
var label l501 = mk_label()
var label l251 = mk_label()
var label lL1  = mk_label()
 
// Slot 2
var box  box2     = box.new(na, na, na, na, xloc=xloc.bar_time)
var line vl2_line = line.new(na, na, na, na, xloc=xloc.bar_time, extend=extend.both)
var line rH2  = mk_ray()
var line r752 = mk_ray()
var line r502 = mk_ray()
var line r252 = mk_ray()
var line rL2  = mk_ray()
var label lH2  = mk_label()
var label l752 = mk_label()
var label l502 = mk_label()
var label l252 = mk_label()
var label lL2  = mk_label()
 
// Slot 3
var box  box3     = box.new(na, na, na, na, xloc=xloc.bar_time)
var line vl3_line = line.new(na, na, na, na, xloc=xloc.bar_time, extend=extend.both)
var line rH3  = mk_ray()
var line r753 = mk_ray()
var line r503 = mk_ray()
var line r253 = mk_ray()
var line rL3  = mk_ray()
var label lH3  = mk_label()
var label l753 = mk_label()
var label l503 = mk_label()
var label l253 = mk_label()
var label lL3  = mk_label()
 
// Dashboard — created only when the table is enabled, so an empty frame is not left on the chart.
var table dash = na
if barstate.isfirst and tbl_show
    dash := table.new(f_get_tbl_pos(tbl_pos_str), 8, 4,
         bgcolor=tbl_bg_c, frame_color=color.gray, frame_width=1,
         border_color=color.gray, border_width=1)
 
// ================================================================================ //
//                          SHARED DRAWING + DASHBOARD LOGIC                        //
// ================================================================================ //
 
// Compute the 4 quadrant levels for a slot.
// 25/75 are retracements of the previous candle, not distances up from the low.
// Bullish: 75% retrace sits in the lower half, 25% retrace in the upper half.
calc_levels(ph, pl, is_bull) =>
    rng    = ph - pl
    l50    = pl + rng * 0.50
    l75    = pl + rng * (is_bull ? 0.25 : 0.75)
    l25    = pl + rng * (is_bull ? 0.75 : 0.25)
    [l25, l50, l75]
 
// Update a ray line.
update_ray(ln, x1, y, x2, col, sty, wid) =>
    line.set_xy1(ln, x1, y)
    line.set_xy2(ln, x2, y)
    line.set_color(ln, col)
    line.set_style(ln, sty)
    line.set_width(ln, wid)
 
// Update a label.
update_lbl(lb, x, y, txt) =>
    label.set_xy(lb, x, y)
    label.set_text(lb, txt)
 
hide_slot(bx, vln, rH, r75, r50, r25, rL, lH, lb75, lb50, lb25, lbL) =>
    box.set_bgcolor(bx, color.new(color.black, 100))
    box.set_border_color(bx, color.new(color.black, 100))
    line.set_color(vln, color.new(color.black, 100))
    line.set_color(rH,  color.new(color.black, 100))
    line.set_color(r75, color.new(color.black, 100))
    line.set_color(r50, color.new(color.black, 100))
    line.set_color(r25, color.new(color.black, 100))
    line.set_color(rL,  color.new(color.black, 100))
    label.set_text(lH, "")
    label.set_text(lb75, "")
    label.set_text(lb50, "")
    label.set_text(lb25, "")
    label.set_text(lbL, "")
 
hide_ray(ln, lb) =>
    line.set_color(ln, color.new(color.black, 100))
    label.set_text(lb, "")
 
// Draw everything for one slot.
draw_slot(bx, vln, rH, r75, r50, r25, rL, lH, lb75, lb50, lb25, lbL,
          ph, pl, po, pc, t_start, t_end, cs,
          slot_on, bull_col, bear_col, vl_on, vl_col, vl_sty, vl_wid,
          ray_off, ray_now, r_col, r_sty, r_wid,
          lbl_col, tf_str, bar_ms, on75, on50, on25) =>
 
    bool ready = not na(ph) and not na(pl) and not na(po) and not na(pc) and not na(t_start) and not na(t_end) and not na(cs)
    bool is_bull = ready and pc > po
    [l25, l50, l75] = calc_levels(ph, pl, is_bull)
 
    if not slot_on or not ready
        hide_slot(bx, vln, rH, r75, r50, r25, rL, lH, lb75, lb50, lb25, lbL)
    else
        box_col      = is_bull ? bull_col : bear_col
        int use_off  = ray_now ? 0 : ray_off
        ray_end      = time + use_off * bar_ms
        lbl_x        = ray_end + bar_ms
        ray_style    = f_get_style(r_sty)
 
        // Box
        box.set_lefttop(bx, t_start, ph)
        box.set_rightbottom(bx, t_end, pl)
        box.set_bgcolor(bx, box_col)
        box.set_border_width(bx, 0)
 
        // Vertical line at current period open. extend.both → full pane height.
        if vl_on
            line.set_xy1(vln, cs, pl)
            line.set_xy2(vln, cs, ph)
            line.set_color(vln, vl_col)
            line.set_style(vln, f_get_style(vl_sty))
            line.set_width(vln, vl_wid)
        else
            line.set_color(vln, color.new(color.black, 100))
 
        // High / low always. 25, 50, 75 follow the mid-level checkboxes.
        string tag = f_tf_tag(tf_str)
        update_ray(rH, t_start, ph, ray_end, r_col, ray_style, r_wid)
        update_ray(rL, t_start, pl, ray_end, r_col, ray_style, r_wid)
        label.set_textcolor(lH, lbl_col)
        label.set_textcolor(lbL, lbl_col)
        update_lbl(lH, lbl_x, ph, "P" + tag + " High")
        update_lbl(lbL, lbl_x, pl, "P" + tag + " Low")
 
        if on75
            update_ray(r75, t_start, l75, ray_end, r_col, ray_style, r_wid)
            label.set_textcolor(lb75, lbl_col)
            update_lbl(lb75, lbl_x, l75, "75%")
        else
            hide_ray(r75, lb75)
 
        if on50
            update_ray(r50, t_start, l50, ray_end, r_col, ray_style, r_wid)
            label.set_textcolor(lb50, lbl_col)
            update_lbl(lb50, lbl_x, l50, "50%")
        else
            hide_ray(r50, lb50)
 
        if on25
            update_ray(r25, t_start, l25, ray_end, r_col, ray_style, r_wid)
            label.set_textcolor(lb25, lbl_col)
            update_lbl(lb25, lbl_x, l25, "25%")
        else
            hide_ray(r25, lb25)
 
    [l25, l50, l75]
 
// Dashboard helpers
f_quadrant(c, ph, pl, l75, l50, l25) =>
    top_q = math.max(l75, l25)
    bot_q = math.min(l75, l25)
    c > ph      ? " Above High  " :
     c > top_q   ? " Deep Premium" :
     c > l50     ? " Premium     " :
     c > bot_q   ? " Discount    " :
     c >= pl     ? " D. Discount " : " Below Low   "
 
f_loc_pct(c, ph, pl) =>
    rng = ph - pl
    pct = rng == 0 ? 0.0 : (c - pl) / rng * 100
    str.tostring(pct, "#.00") + " % "
 
f_touched(lvl, mn, mx) =>
    not na(lvl) and not na(mn) and not na(mx) and lvl >= mn and lvl <= mx ? " ✅ " : "  "
 
f_cell(tb, col, row, txt) =>
    table.cell(tb, col, row, txt, text_color=tbl_txt_c,
         text_font_family=font.family_monospace,
         text_size=f_get_tbl_size(tbl_size_str))
 
fill_dash_row(tb, row, tf_str, c, ph, pl, l25, l50, l75, mn, mx, on75, on50, on25) =>
    if na(ph) or na(pl) or na(l50)
        f_cell(tb, 0, row, "  " + f_tf_tag(tf_str) + "  ")
        f_cell(tb, 1, row, " waiting     ")
        f_cell(tb, 2, row, "        ")
        f_cell(tb, 3, row, "  ")
        f_cell(tb, 4, row, "  ")
        f_cell(tb, 5, row, "  ")
        f_cell(tb, 6, row, "  ")
        f_cell(tb, 7, row, "  ")
    else
        f_cell(tb, 0, row, "  " + f_tf_tag(tf_str) + "  ")
        f_cell(tb, 1, row, f_quadrant(c, ph, pl, l75, l50, l25))
        f_cell(tb, 2, row, f_loc_pct(c, ph, pl))
        f_cell(tb, 3, row, f_touched(ph,  mn, mx))
        f_cell(tb, 4, row, on75 ? f_touched(l75, mn, mx) : " — ")
        f_cell(tb, 5, row, on50 ? f_touched(l50, mn, mx) : " — ")
        f_cell(tb, 6, row, on25 ? f_touched(l25, mn, mx) : " — ")
        f_cell(tb, 7, row, f_touched(pl,  mn, mx))
 
// ================================================================================ //
//                              FINAL EXECUTION (last bar)                          //
// ================================================================================ //
if barstate.islast
    // Chart timeframe in ms. `time - time[1]` is a session/weekend gap on the
    // first bar after a break, which stretches "50 bars" into days.
    int bar_ms = nz(timeframe.in_seconds() * 1000, nz(time - time[1], 60000))
 
    [d1_25, d1_50, d1_75] = draw_slot(
         box1, vl1_line, rH1, r751, r501, r251, rL1, lH1, l751, l501, l251, lL1,
         p_h1, p_l1, p_o1, p_c1, p_ts1, p_te1, p_cs1,
         slot_on1, bull_c1, bear_c1, vl_on1, vl_col1, vl_sty1, vl_w1,
         ray_off1, ray_now1, r_col1, r_sty1, r_w1,
         lbl_c1, tf1, bar_ms, show_75_1, show_50_1, show_25_1)
 
    [d2_25, d2_50, d2_75] = draw_slot(
         box2, vl2_line, rH2, r752, r502, r252, rL2, lH2, l752, l502, l252, lL2,
         p_h2, p_l2, p_o2, p_c2, p_ts2, p_te2, p_cs2,
         slot_on2, bull_c2, bear_c2, vl_on2, vl_col2, vl_sty2, vl_w2,
         ray_off2, ray_now2, r_col2, r_sty2, r_w2,
         lbl_c2, tf2, bar_ms, show_75_2, show_50_2, show_25_2)
 
    [d3_25, d3_50, d3_75] = draw_slot(
         box3, vl3_line, rH3, r753, r503, r253, rL3, lH3, l753, l503, l253, lL3,
         p_h3, p_l3, p_o3, p_c3, p_ts3, p_te3, p_cs3,
         slot_on3, bull_c3, bear_c3, vl_on3, vl_col3, vl_sty3, vl_w3,
         ray_off3, ray_now3, r_col3, r_sty3, r_w3,
         lbl_c3, tf3, bar_ms, show_75_3, show_50_3, show_25_3)
 
    if tbl_show and not na(dash)
        table.set_position(dash, f_get_tbl_pos(tbl_pos_str))
        f_cell(dash, 0, 0, " Slot ")
        f_cell(dash, 1, 0, " Range Status  ")
        f_cell(dash, 2, 0, " Loc %   ")
        f_cell(dash, 3, 0, " H ")
        f_cell(dash, 4, 0, " 75 ")
        f_cell(dash, 5, 0, " 50 ")
        f_cell(dash, 6, 0, " 25 ")
        f_cell(dash, 7, 0, " L ")
 
        fill_dash_row(dash, 1, tf1, close, p_h1, p_l1, d1_25, d1_50, d1_75, s1_min, s1_max, show_75_1, show_50_1, show_25_1)
        fill_dash_row(dash, 2, tf2, close, p_h2, p_l2, d2_25, d2_50, d2_75, s2_min, s2_max, show_75_2, show_50_2, show_25_2)
        fill_dash_row(dash, 3, tf3, close, p_h3, p_l3, d3_25, d3_50, d3_75, s3_min, s3_max, show_75_3, show_50_3, show_25_3)
 
// ================================================================================ //
//                                   ALERTS                                         //
// ================================================================================ //
// Fires once on the bar that first touches the level. High and low always.
// 25 / 50 / 75 only when that checkbox is on. A disabled slot does not alert.
// Create Alert → "Any alert() function call" for the detailed message, or pick
// one Slot condition below.
tf_label(string tf) =>
    f_tf_tag(tf)
 
f_fresh(float lvl) =>
    bool touched = not na(lvl) and high >= lvl and low <= lvl
    touched and not touched[1]
 
f_ping(bool cond, string msg) =>
    if cond
        alert(msg, alert.freq_once_per_bar)
 
[lv1_25, lv1_50, lv1_75] = calc_levels(p_h1, p_l1, p_c1 > p_o1)
[lv2_25, lv2_50, lv2_75] = calc_levels(p_h2, p_l2, p_c2 > p_o2)
[lv3_25, lv3_50, lv3_75] = calc_levels(p_h3, p_l3, p_c3 > p_o3)
 
h1   = slot_on1 and f_fresh(p_h1)
lo1  = slot_on1 and f_fresh(p_l1)
a75_1 = slot_on1 and show_75_1 and f_fresh(lv1_75)
a50_1 = slot_on1 and show_50_1 and f_fresh(lv1_50)
a25_1 = slot_on1 and show_25_1 and f_fresh(lv1_25)
 
h2   = slot_on2 and f_fresh(p_h2)
lo2  = slot_on2 and f_fresh(p_l2)
a75_2 = slot_on2 and show_75_2 and f_fresh(lv2_75)
a50_2 = slot_on2 and show_50_2 and f_fresh(lv2_50)
a25_2 = slot_on2 and show_25_2 and f_fresh(lv2_25)
 
h3   = slot_on3 and f_fresh(p_h3)
lo3  = slot_on3 and f_fresh(p_l3)
a75_3 = slot_on3 and show_75_3 and f_fresh(lv3_75)
a50_3 = slot_on3 and show_50_3 and f_fresh(lv3_50)
a25_3 = slot_on3 and show_25_3 and f_fresh(lv3_25)
 
f_ping(h1,    tf_label(tf1) + " previous high touched @ " + str.tostring(p_h1, format.mintick))
f_ping(lo1,   tf_label(tf1) + " previous low touched @ "  + str.tostring(p_l1, format.mintick))
f_ping(a75_1, tf_label(tf1) + " 75% touched @ "           + str.tostring(lv1_75, format.mintick))
f_ping(a50_1, tf_label(tf1) + " 50% touched @ "           + str.tostring(lv1_50, format.mintick))
f_ping(a25_1, tf_label(tf1) + " 25% touched @ "           + str.tostring(lv1_25, format.mintick))
 
f_ping(h2,    tf_label(tf2) + " previous high touched @ " + str.tostring(p_h2, format.mintick))
f_ping(lo2,   tf_label(tf2) + " previous low touched @ "  + str.tostring(p_l2, format.mintick))
f_ping(a75_2, tf_label(tf2) + " 75% touched @ "           + str.tostring(lv2_75, format.mintick))
f_ping(a50_2, tf_label(tf2) + " 50% touched @ "           + str.tostring(lv2_50, format.mintick))
f_ping(a25_2, tf_label(tf2) + " 25% touched @ "           + str.tostring(lv2_25, format.mintick))
 
f_ping(h3,    tf_label(tf3) + " previous high touched @ " + str.tostring(p_h3, format.mintick))
f_ping(lo3,   tf_label(tf3) + " previous low touched @ "  + str.tostring(p_l3, format.mintick))
f_ping(a75_3, tf_label(tf3) + " 75% touched @ "           + str.tostring(lv3_75, format.mintick))
f_ping(a50_3, tf_label(tf3) + " 50% touched @ "           + str.tostring(lv3_50, format.mintick))
f_ping(a25_3, tf_label(tf3) + " 25% touched @ "           + str.tostring(lv3_25, format.mintick))
 
any_lvl = h1 or lo1 or a75_1 or a50_1 or a25_1 or h2 or lo2 or a75_2 or a50_2 or a25_2 or h3 or lo3 or a75_3 or a50_3 or a25_3
 
alertcondition(h1,    "Slot 1 High", "Slot 1 previous high touched")
alertcondition(lo1,   "Slot 1 Low",  "Slot 1 previous low touched")
alertcondition(a75_1, "Slot 1 75%",  "Slot 1 75% level touched")
alertcondition(a50_1, "Slot 1 50%",  "Slot 1 50% level touched")
alertcondition(a25_1, "Slot 1 25%",  "Slot 1 25% level touched")
alertcondition(h2,    "Slot 2 High", "Slot 2 previous high touched")
alertcondition(lo2,   "Slot 2 Low",  "Slot 2 previous low touched")
alertcondition(a75_2, "Slot 2 75%",  "Slot 2 75% level touched")
alertcondition(a50_2, "Slot 2 50%",  "Slot 2 50% level touched")
alertcondition(a25_2, "Slot 2 25%",  "Slot 2 25% level touched")
alertcondition(h3,    "Slot 3 High", "Slot 3 previous high touched")
alertcondition(lo3,   "Slot 3 Low",  "Slot 3 previous low touched")
alertcondition(a75_3, "Slot 3 75%",  "Slot 3 75% level touched")
alertcondition(a50_3, "Slot 3 50%",  "Slot 3 50% level touched")
alertcondition(a25_3, "Slot 3 25%",  "Slot 3 25% level touched")
alertcondition(any_lvl, "Any level", "A previous range level was touched")

← 返回列表