7H Sessions+ (M1D)

mayb1dayy · study · 354 行 · 点赞 232 · 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/
// © mayb1dayy
 
//@version=6
indicator("7H Sessions+ (M1D)", shorttitle = "7H Po3+", overlay = true,
     max_lines_count = 200, max_labels_count = 200, max_boxes_count = 50)
 
// 7H candles anchored 18:00 New York: Asia 18-01, London 01-08, New York 08-15.
// The 15:00-17:00 stub is not a session here.
 
const string NY   = "America/New_York"
const int    H_MS = 3600000
 
// == INPUTS ==
string G_LV  = "Levels"
bool   lvOn  = input.bool(true, "Previous 7H 25% and 50%", group = G_LV, tooltip = "The last closed 7H candle's 25% and 50%, drawn from the next session's open to the right of price and named at the end: London 25%, London 50%. The 25% is a quarter of the range down from the high on a up candle, up from the low on a down candle.")
bool   lvOld = input.bool(true, "The one before it", group = G_LV, tooltip = "Also the candle before, across its own next session, dotted.")
 
string G_DV  = "Dividers"
bool   dvOn  = input.bool(true, "Session dividers", group = G_DV, tooltip = "A vertical line at each 7H open, named at the top: Asia 18:00, London 01:00, New York 08:00, and Stub at the 15:00 close, New York time.")
int    dvDay = input.int(1, "Days back", minval = 1, maxval = 5, group = G_DV)
 
string G_BK  = "7H candles"
bool   bkOn  = input.bool(true, "7H candles right of price", group = G_BK, tooltip = "The last 7H candles, named Asia, London, New York under each one. The forming one is outlined dotted.")
int    bkN   = input.int(3, "Candles", minval = 2, maxval = 9, group = G_BK)
int    bkOff = input.int(30, "Bars right of price", minval = 2, maxval = 200, group = G_BK)
int    bkW   = input.int(5, "Candle width (bars)", minval = 1, maxval = 15, step = 2, group = G_BK)
bool   bkLv  = input.bool(true, "25% and 50% beside the candles", group = G_BK)
 
string G_LK  = "Look"
color  BULL  = input.color(#A88BE6, "Bullish", group = G_LK, inline = "c")
color  BEAR  = input.color(#4A4A4A, "Bearish", group = G_LK, inline = "c")
color  C25   = input.color(#5101fd, "25%", group = G_LK, inline = "l")
color  C50   = input.color(color.rgb(230, 106, 23), "50%", group = G_LK, inline = "l")
int    TXT   = input.int(10, "Text size", minval = 6, maxval = 14, group = G_LK)
 
string G_TB  = "Table"
bool   tbOn  = input.bool(true, "Instrument and PDH / PDL", group = G_TB, tooltip = "The instrument, then whether today's range has traded through the previous day's high and low: a tick once taken, a cross until then. Hover a mark for the price and the time it was taken, New York time.")
string tbPos = input.string("Top Right", "Position", options = ["Top Right", "Middle Right", "Bottom Right", "Top Left", "Bottom Left"], group = G_TB)
 
bool  DARK   = color.r(chart.bg_color) + color.g(chart.bg_color) + color.b(chart.bg_color) < 384
color INK    = DARK ? #FFFFFF : #000000
color MUTE   = DARK ? #B2B5BE : #4A4A4A
color DIV    = DARK ? #5D606B : #B2B5BE
color NOFILL = color.new(#FFFFFF, 100)
 
// == 7H CANDLES ==
type Cd
    float o
    float h
    float l
    float c
    int   t0
    int   blk
 
//@function The 7H block a time falls in: 0 Asia, 1 London, 2 New York, 3 the stub.
//@param t (int) A bar time.
//@returns (int) Block 0 to 3.
f_blk(int t) =>
    math.floor(((hour(t, NY) + 6) % 24) / 7.0)
 
//@function The name of a session.
//@param b (int) Block 0 to 2.
//@returns (string) Its name.
f_name(int b) =>
    b == 0 ? "Asia" : b == 1 ? "London" : "New York"
 
//@function A price on the instrument's tick.
//@param p (float) The price.
//@returns (string) The price as text.
px(float p) =>
    str.tostring(p, format.mintick)
 
//@function A candle's 25%: a quarter of the range down from the high on a up candle, up from the low on a down candle.
//@param k (Cd) The candle.
//@returns (float) The level.
f_q25(Cd k) =>
    k.c >= k.o ? k.h - 0.25 * (k.h - k.l) : k.l + 0.25 * (k.h - k.l)
 
//@function The tooltip for a candle's 25%.
//@param k (Cd) The candle.
//@returns (string) The arithmetic.
f_qTip(Cd k) =>
    f_name(k.blk) + " 25% " + px(f_q25(k)) + " = " + (k.c >= k.o ? "high " + px(k.h) + " - " : "low " + px(k.l) + " + ") + "0.25 x range " + str.tostring(k.h - k.l, "#.##")
 
//@function The tooltip for a candle's 50%.
//@param k (Cd) The candle.
//@returns (string) The arithmetic.
f_mTip(Cd k) =>
    f_name(k.blk) + " 50% " + px((k.h + k.l) / 2) + " = (high " + px(k.h) + " + low " + px(k.l) + ") / 2"
 
var array<Cd> cs  = array.new<Cd>()
var Cd        cur = na
 
bool tfOk = timeframe.isintraday and timeframe.in_seconds() <= 3600
int  bNow = f_blk(time)
var array<int> stubT = array.new<int>()
if bNow == 3
    if not na(cur)
        cs.push(cur)
        cur := na
        stubT.push(time)
        if stubT.size() > 10
            stubT.shift()
else if na(cur) or bNow != cur.blk or time - cur.t0 >= 7 * H_MS
    if not na(cur)
        cs.push(cur)
    cur := Cd.new(o = open, h = high, l = low, c = close, t0 = time, blk = bNow)
else
    cur.h := math.max(cur.h, high)
    cur.l := math.min(cur.l, low)
    cur.c := close
if cs.size() > 40
    cs.shift()
 
// == PDH / PDL ==
[pdh, pdl] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead = barmerge.lookahead_on)
 
//@variable Today's high and low so far, from the daily session's first bar
var float dHi  = na
var float dLo  = na
//@variable The bar time today's range first traded through the previous day's high / low
var int   pdhT = na
var int   pdlT = na
if timeframe.change("D")
    dHi  := high
    dLo  := low
    pdhT := na
    pdlT := na
else
    dHi := na(dHi) ? high : math.max(dHi, high)
    dLo := na(dLo) ? low : math.min(dLo, low)
if na(pdhT) and not na(pdh) and dHi >= pdh
    pdhT := time
if na(pdlT) and not na(pdl) and dLo <= pdl
    pdlT := time
 
//@function A table position from its setting.
//@param p (string) The setting.
//@returns (string) The position.
f_pos(string p) =>
    switch p
        "Middle Right" => position.middle_right
        "Bottom Right" => position.bottom_right
        "Top Left"     => position.top_left
        "Bottom Left"  => position.bottom_left
        =>                position.top_right
 
var table tb = table.new(f_pos(tbPos), 1, 3, frame_color = INK, frame_width = 1, border_color = INK, border_width = 1)
 
//@function One PDH / PDL row: the name, then a tick once taken or a cross until then.
//@param r (int) The row.
//@param nm (string) PDH or PDL.
//@param lvl (float) The level.
//@param tk (int) The bar time it was taken, na while untouched.
f_pdRow(int r, string nm, float lvl, int tk) =>
    string tip = nm + " " + px(lvl) + (na(tk) ? " · not taken yet" : " · taken " + str.format_time(tk, "dd MMM HH:mm", NY) + " NY")
    table.cell(tb, 0, r, nm + " " + (na(tk) ? "×" : "✓"), text_color = INK, text_size = TXT, text_halign = text.align_left, text_font_family = font.family_monospace, tooltip = tip)
 
if barstate.islast
    table.clear(tb, 0, 0, 0, 2)
    if tbOn and tfOk
        table.cell(tb, 0, 0, syminfo.ticker, text_color = INK, text_size = TXT, text_halign = text.align_left, text_font_family = font.family_monospace, tooltip = syminfo.description)
        if not na(pdh)
            f_pdRow(1, "PDH", pdh, pdhT)
        if not na(pdl)
            f_pdRow(2, "PDL", pdl, pdlT)
 
// == DRAWING ==
//@variable How far back a name's placement reads candles, inside the history buffer below
const int NAME_REACH = 480
//@variable How many candles a name is taken to span
const int NAME_BARS  = 8
max_bars_back(time, 500)
max_bars_back(high, 500)
max_bars_back(low, 500)
//@variable The gap kept between a name and the candles, a quarter of the chart's ATR
float namePad = nz(ta.atr(14)) * 0.25
 
var array<line>  lns  = array.new<line>()
var array<label> lbs  = array.new<label>()
var array<box>   bxs  = array.new<box>()
 
//@function A transparent monospace label.
//@param x (int) Time or bar index, per xl.
//@param y (float) Price.
//@param txt (string) The text.
//@param sty (string) The label style.
//@param col (color) Text colour.
//@param xl (string) The x location.
//@param tip (string) The tooltip.
f_lbl(int x, float y, string txt, string sty, color col, string xl, string tip) =>
    lbs.push(label.new(x, y, txt, xloc = xl, style = sty, color = NOFILL, textcolor = col, size = TXT, text_font_family = font.family_monospace, tooltip = tip))
 
//@function A level line with its name at the right end.
//@param t0 (int) Start time.
//@param t1 (int) End time.
//@param y (float) Price.
//@param col (color) Line colour.
//@param sty (string) Line style.
//@param txt (string) The name.
//@param tip (string) The tooltip.
f_lvl(int t0, int t1, float y, color col, string sty, string txt, string tip) =>
    lns.push(line.new(t0, y, t1, y, xloc = xloc.bar_time, color = col, width = 1, style = sty))
    f_lbl(t1, y, txt, label.style_label_left, INK, xloc.bar_time, tip)
 
//@function The bar offset of the newest bar at or before a time, within the readable history.
//@param t (int) A time.
//@returns (int) The offset; na when the time is older than the readable history.
f_k(int t) =>
    int res = na
    for k = 0 to math.min(bar_index, NAME_REACH)
        if na(res) and time[k] <= t
            res := k
    res
 
//@function How many bars in a run of offsets trade through a price, padded by a quarter ATR.
//@param kNew (int) The newest offset of the run.
//@param kOld (int) The oldest offset of the run.
//@param y (float) The price.
//@returns (int) The count; bars in the future or past the readable history count as clear.
f_hits(int kNew, int kOld, float y) =>
    int n = 0
    if kOld >= kNew
        for k = kNew to kOld
            if k >= 0 and k <= math.min(bar_index, NAME_REACH)
                n += low[k] - namePad < y and high[k] + namePad > y ? 1 : 0
    n
 
//@function A level line whose name sits where no candle trades through it: past the right end, before the left
//          end, or on a clear stretch of the line, in that order; past the wicks at the right end when none is clear.
//@param t0 (int) Start time.
//@param t1 (int) End time.
//@param y (float) Price.
//@param col (color) Line colour.
//@param sty (string) Line style.
//@param txt (string) The name.
//@param tip (string) The tooltip.
f_lvlClear(int t0, int t1, float y, color col, string sty, string txt, string tip) =>
    lns.push(line.new(t0, y, t1, y, xloc = xloc.bar_time, color = col, width = 1, style = sty))
    int kR = f_k(t1)
    int kL = f_k(t0)
    int rHits = na(kR) ? NAME_BARS : f_hits(kR - NAME_BARS, kR - 1, y)
    int lHits = na(kL) ? NAME_BARS : f_hits(kL + 1, kL + NAME_BARS, y)
    if rHits == 0
        f_lbl(t1, y, txt, label.style_label_left, INK, xloc.bar_time, tip)
    else if lHits == 0
        f_lbl(t0, y, txt, label.style_label_right, INK, xloc.bar_time, tip)
    else
        // A clear stretch on the line itself, searched from its right end.
        int mid = na
        if not na(kR)
            int kEnd = math.min(na(kL) ? NAME_REACH : kL, math.min(bar_index, NAME_REACH)) - NAME_BARS
            int c = kR
            while na(mid) and c <= kEnd
                if f_hits(c, c + NAME_BARS - 1, y) == 0
                    mid := c + int(NAME_BARS / 2)
                c += 1
        if not na(mid)
            f_lbl(time[mid], y, txt, label.style_label_center, INK, xloc.bar_time, tip)
        else
            // No clear room on or beside the line: the name sits past the wicks it would cover, from the right end.
            float hiW = na
            float loW = na
            if not na(kR)
                for k = math.max(0, kR - NAME_BARS) to kR
                    hiW := na(hiW) ? high[k] : math.max(hiW, high[k])
                    loW := na(loW) ? low[k] : math.min(loW, low[k])
            if na(hiW)
                f_lbl(t1, y, txt, label.style_label_left, INK, xloc.bar_time, tip)
            else if hiW - y <= y - loW
                f_lbl(t1, hiW + namePad, txt, label.style_label_lower_left, INK, xloc.bar_time, tip)
            else
                f_lbl(t1, loW - namePad, txt, label.style_label_upper_left, INK, xloc.bar_time, tip)
 
if barstate.islast
    for l in lns
        line.delete(l)
    for lb in lbs
        label.delete(lb)
    for b in bxs
        box.delete(b)
    lns.clear()
    lbs.clear()
    bxs.clear()
    if tfOk
        // Every 7H candle, closed then forming, oldest first.
        array<Cd> all = array.new<Cd>()
        for k in cs
            all.push(k)
        bool forming = not na(cur)
        if forming
            all.push(cur)
        int n     = all.size()
        int nCl   = cs.size()
        int barMs = timeframe.in_seconds() * 1000
        int tEnd  = time_close + 2 * barMs
 
        if dvOn and n > 0
            int   from = math.max(0, n - 3 * dvDay)
            float top  = na
            for i = from to n - 1
                top := na(top) ? all.get(i).h : math.max(top, all.get(i).h)
            for i = from to n - 1
                Cd k = all.get(i)
                lns.push(line.new(k.t0, k.h, k.t0, k.l, xloc = xloc.bar_time, extend = extend.both, color = DIV, width = 1))
                f_lbl(k.t0, top, f_name(k.blk), label.style_label_down, MUTE, xloc.bar_time, f_name(k.blk) + " open " + str.format_time(k.t0, "dd MMM HH:mm", NY) + " NY")
            // The 15:00 New York close, where the stub starts.
            for t in stubT
                if t > all.get(from).t0
                    lns.push(line.new(t, top, t, top + syminfo.mintick, xloc = xloc.bar_time, extend = extend.both, color = DIV, width = 1))
                    f_lbl(t, top, "Stub", label.style_label_down, MUTE, xloc.bar_time, "New York close / stub " + str.format_time(t, "dd MMM HH:mm", NY) + " NY")
 
        if lvOn and nCl > 0
            // The newest closed candle, from the next session's open to the right of price.
            Cd lk = cs.get(nCl - 1)
            int s0 = forming ? cur.t0 : lk.t0 + 7 * H_MS
            f_lvl(s0, tEnd, (lk.h + lk.l) / 2, C50, line.style_dotted, f_name(lk.blk) + " 50%", f_mTip(lk))
            f_lvl(s0, tEnd, f_q25(lk), C25, line.style_dotted, f_name(lk.blk) + " 25%", f_qTip(lk))
            // The one before it, across its own next session.
            if lvOld and nCl > 1
                Cd pk = cs.get(nCl - 2)
                f_lvlClear(lk.t0, lk.t0 + 7 * H_MS, (pk.h + pk.l) / 2, C50, line.style_dotted, f_name(pk.blk) + " 50%", f_mTip(pk))
                f_lvlClear(lk.t0, lk.t0 + 7 * H_MS, f_q25(pk), C25, line.style_dotted, f_name(pk.blk) + " 25%", f_qTip(pk))
 
        if bkOn and n > 0
            int from = math.max(0, n - bkN)
            int half = int((bkW - 1) / 2)
            int gap  = 9
            int step = bkW + gap
            int x0   = bar_index + bkOff
            for i = from to n - 1
                Cd    k    = all.get(i)
                int   x    = x0 + (i - from) * step + half
                bool  live = forming and i == n - 1
                float bt   = math.max(k.o, k.c)
                float bb   = math.min(k.o, k.c)
                if bt == bb
                    bt += syminfo.mintick
                bxs.push(box.new(x - half, bt, x + half, bb, xloc = xloc.bar_index, bgcolor = k.c >= k.o ? BULL : BEAR, border_color = INK, border_width = 1, border_style = live ? line.style_dotted : line.style_solid))
                lns.push(line.new(x, k.h, x, bt, xloc = xloc.bar_index, color = INK, width = 1))
                lns.push(line.new(x, bb, x, k.l, xloc = xloc.bar_index, color = INK, width = 1))
                f_lbl(x, k.l, f_name(k.blk), label.style_label_up, INK, xloc.bar_index, f_name(k.blk) + " " + str.format_time(k.t0, "dd MMM HH:mm", NY) + " NY")
                // Its 25% and 50% run from the wick into the gap before the next candle: the upper name sits on its line, the lower one under it.
                if bkLv and not live
                    int   xs  = x + half + 1
                    int   xe  = x + half + gap - 1
                    float q   = f_q25(k)
                    float md  = (k.h + k.l) / 2
                    bool  qUp = q >= md
                    lns.push(line.new(x, q, xe, q, xloc = xloc.bar_index, color = C25, width = 1))
                    lns.push(line.new(x, md, xe, md, xloc = xloc.bar_index, color = C50, width = 1))
                    f_lbl(xs, q, "25%", qUp ? label.style_label_lower_left : label.style_label_upper_left, INK, xloc.bar_index, f_qTip(k))
                    f_lbl(xs, md, "50%", qUp ? label.style_label_upper_left : label.style_label_lower_left, INK, xloc.bar_index, f_mTip(k))

← 返回列表