5-Min ORB - Asia, London, NY

krisskross18 · study · 242 行 · 点赞 4 · TradingView 原页

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

Pine Script

//@version=6
indicator("5-Min ORB - Asia, London, NY",
    overlay = true,
    max_boxes_count = 500,
    max_lines_count = 500,
    max_labels_count = 300)
 
groupGen = "General"
allowMulti = input.bool(false, "Allow multiple signals per session", group=groupGen)
showMid = input.bool(true, "Draw ORB midline", group=groupGen)
showArrows = input.bool(true, "Plot breakout arrows", group=groupGen)
historyCount = input.int(5, "Previous sessions to keep", minval=0, maxval=10, group=groupGen)
 
groupAsia = "Asia"
asiaOn = input.bool(true, "Enable Asia", group=groupAsia)
asiaSess = input.session("1800-1805", "Session Period (first 5 minutes)", group=groupAsia)
asiaTzStr = input.string("America/New_York", "Session Timezone", group=groupAsia)
asiaFill = input.color(color.new(color.teal, 70), "Box fill", group=groupAsia)
asiaLineClr = input.color(color.teal, "Line color", group=groupAsia)
 
groupLon = "London"
lonOn = input.bool(true, "Enable London", group=groupLon)
lonSess = input.session("0800-0805", "London first 5 minutes", group=groupLon)
lonTzStr = input.string("Europe/London", "Session Timezone", group=groupLon)
lonFill = input.color(color.new(color.orange, 70), "Box fill", group=groupLon)
lonLineClr = input.color(color.orange, "Line color", group=groupLon)
 
groupNY = "New York"
nyOn = input.bool(true, "Enable New York", group=groupNY)
nySess = input.session("0930-0935", "New York first 5 minutes", group=groupNY)
nyTzStr = input.string("America/New_York", "Session Timezone", group=groupNY)
nyFill = input.color(color.new(color.aqua, 70), "Box fill", group=groupNY)
nyLineClr = input.color(color.aqua, "Line color", group=groupNY)
 
orbLenMs = 5 * 60 * 1000
 
// The session candle is read ONLY after it has closed. Using [1] with
// lookahead_on is deliberate: it exposes the PREVIOUS confirmed 5m bar,
// never an unfinished current bar or a future bar.
confirmedOrb(string sess, string tz) =>
    bool match = not na(time("5", sess, tz))
    [match[1], time[1], high[1], low[1], open[1], close[1]]
 
// Stop a session’s three horizontal levels at the next session opening.
// x2 is an absolute timestamp, so the level ends exactly at the next
// opening, even though that opening candle is confirmed five minutes later.
stopLines(line topLine, line botLine, line midLine, int stopAt) =>
    if not na(topLine) and not na(stopAt)
        line.set_extend(topLine, extend.none)
        line.set_x2(topLine, stopAt)
    if not na(botLine) and not na(stopAt)
        line.set_extend(botLine, extend.none)
        line.set_x2(botLine, stopAt)
    if not na(midLine) and not na(stopAt)
        line.set_extend(midLine, extend.none)
        line.set_x2(midLine, stopAt)
 
pushHistory(array<box> hB, array<line> hT, array<line> hBot, array<line> hM, box cB, line cT, line cBot, line cM, color clr, int maxH) =>
    if not na(cB)
        if maxH > 0
            array.unshift(hB, cB)
            array.unshift(hT, cT)
            array.unshift(hBot, cBot)
            array.unshift(hM, cM)
            for i = 0 to array.size(hB) - 1
                int lineFade = math.min(50 + i * 10, 90)
                int boxFade = math.min(82 + i * 3, 95)
                line.set_color(array.get(hT, i), color.new(clr, lineFade))
                line.set_color(array.get(hBot, i), color.new(clr, lineFade))
                line m = array.get(hM, i)
                if not na(m)
                    line.set_color(m, color.new(clr, lineFade))
                box.set_bgcolor(array.get(hB, i), color.new(clr, boxFade))
            while array.size(hB) > maxH
                box.delete(array.pop(hB))
                line.delete(array.pop(hT))
                line.delete(array.pop(hBot))
                line m = array.pop(hM)
                if not na(m)
                    line.delete(m)
        else
            box.delete(cB)
            if not na(cT)
                line.delete(cT)
            if not na(cBot)
                line.delete(cBot)
            if not na(cM)
                line.delete(cM)
 
// ================= ASIA =================
[a_match, a_time, a_h, a_l, a_o, a_c] = request.security(syminfo.tickerid, "5", confirmedOrb(asiaSess, asiaTzStr), lookahead=barmerge.lookahead_on)
var float a_hi = na
var float a_lo = na
var float a_open = na
var float a_close = na
var int a_lastTime = na
var bool a_ready = false
var bool a_locked = false
var box a_box = na
var line a_top = na
var line a_bot = na
var line a_mid = na
var array<box> a_hB = array.new<box>()
var array<line> a_hT = array.new<line>()
var array<line> a_hBot = array.new<line>()
var array<line> a_hM = array.new<line>()
a_new = asiaOn and a_match and not na(a_time) and (na(a_lastTime) or a_time != a_lastTime)
if a_new
    pushHistory(a_hB, a_hT, a_hBot, a_hM, a_box, a_top, a_bot, a_mid, asiaLineClr, historyCount)
    a_lastTime := a_time
    a_hi := a_h
    a_lo := a_l
    a_open := a_o
    a_close := a_c
    a_ready := true
    a_locked := false
    a_mid := na
    a_box := box.new(a_time, a_hi, a_time + orbLenMs, a_lo, xloc=xloc.bar_time, border_color=color.new(color.gray, 100), bgcolor=asiaFill)
    a_top := line.new(a_time, a_hi, a_time + orbLenMs, a_hi, xloc=xloc.bar_time, extend=extend.right, color=asiaLineClr)
    a_bot := line.new(a_time, a_lo, a_time + orbLenMs, a_lo, xloc=xloc.bar_time, extend=extend.right, color=asiaLineClr)
    if showMid
        float mid = (a_hi + a_lo) / 2
        a_mid := line.new(a_time, mid, a_time + orbLenMs, mid, xloc=xloc.bar_time, extend=extend.right, color=asiaLineClr, style=line.style_dotted)
a_crossUp = ta.crossover(close, a_hi)
a_crossDown = ta.crossunder(close, a_lo)
a_longSig = asiaOn and a_ready and not a_new and a_close > a_open and a_crossUp and (allowMulti or not a_locked)
a_shortSig = asiaOn and a_ready and not a_new and a_close <= a_open and a_crossDown and (allowMulti or not a_locked)
if a_longSig or a_shortSig
    a_locked := true
if showArrows and a_longSig
    label.new(bar_index, a_lo, "", style=label.style_triangleup, size=size.tiny, color=color.green)
if showArrows and a_shortSig
    label.new(bar_index, a_hi, "", style=label.style_triangledown, size=size.tiny, color=color.red)
 
// ================= LONDON =================
[l_match, l_time, l_h, l_l, l_o, l_c] = request.security(syminfo.tickerid, "5", confirmedOrb(lonSess, lonTzStr), lookahead=barmerge.lookahead_on)
var float l_hi = na
var float l_lo = na
var float l_open = na
var float l_close = na
var int l_lastTime = na
var bool l_ready = false
var bool l_locked = false
var box l_box = na
var line l_top = na
var line l_bot = na
var line l_mid = na
var array<box> l_hB = array.new<box>()
var array<line> l_hT = array.new<line>()
var array<line> l_hBot = array.new<line>()
var array<line> l_hM = array.new<line>()
l_new = lonOn and l_match and not na(l_time) and (na(l_lastTime) or l_time != l_lastTime)
if l_new
    pushHistory(l_hB, l_hT, l_hBot, l_hM, l_box, l_top, l_bot, l_mid, lonLineClr, historyCount)
    l_lastTime := l_time
    l_hi := l_h
    l_lo := l_l
    l_open := l_o
    l_close := l_c
    l_ready := true
    l_locked := false
    l_mid := na
    l_box := box.new(l_time, l_hi, l_time + orbLenMs, l_lo, xloc=xloc.bar_time, border_color=color.new(color.gray, 100), bgcolor=lonFill)
    l_top := line.new(l_time, l_hi, l_time + orbLenMs, l_hi, xloc=xloc.bar_time, extend=extend.right, color=lonLineClr)
    l_bot := line.new(l_time, l_lo, l_time + orbLenMs, l_lo, xloc=xloc.bar_time, extend=extend.right, color=lonLineClr)
    if showMid
        float mid = (l_hi + l_lo) / 2
        l_mid := line.new(l_time, mid, l_time + orbLenMs, mid, xloc=xloc.bar_time, extend=extend.right, color=lonLineClr, style=line.style_dotted)
l_crossUp = ta.crossover(close, l_hi)
l_crossDown = ta.crossunder(close, l_lo)
l_longSig = lonOn and l_ready and not l_new and l_close > l_open and l_crossUp and (allowMulti or not l_locked)
l_shortSig = lonOn and l_ready and not l_new and l_close <= l_open and l_crossDown and (allowMulti or not l_locked)
if l_longSig or l_shortSig
    l_locked := true
if showArrows and l_longSig
    label.new(bar_index, l_lo, "", style=label.style_triangleup, size=size.tiny, color=color.green)
if showArrows and l_shortSig
    label.new(bar_index, l_hi, "", style=label.style_triangledown, size=size.tiny, color=color.red)
 
// ================= NEW YORK =================
[n_match, n_time, n_h, n_l, n_o, n_c] = request.security(syminfo.tickerid, "5", confirmedOrb(nySess, nyTzStr), lookahead=barmerge.lookahead_on)
var float n_hi = na
var float n_lo = na
var float n_open = na
var float n_close = na
var int n_lastTime = na
var bool n_ready = false
var bool n_locked = false
var box n_box = na
var line n_top = na
var line n_bot = na
var line n_mid = na
var array<box> n_hB = array.new<box>()
var array<line> n_hT = array.new<line>()
var array<line> n_hBot = array.new<line>()
var array<line> n_hM = array.new<line>()
n_new = nyOn and n_match and not na(n_time) and (na(n_lastTime) or n_time != n_lastTime)
if n_new
    pushHistory(n_hB, n_hT, n_hBot, n_hM, n_box, n_top, n_bot, n_mid, nyLineClr, historyCount)
    n_lastTime := n_time
    n_hi := n_h
    n_lo := n_l
    n_open := n_o
    n_close := n_c
    n_ready := true
    n_locked := false
    n_mid := na
    n_box := box.new(n_time, n_hi, n_time + orbLenMs, n_lo, xloc=xloc.bar_time, border_color=color.new(color.gray, 100), bgcolor=nyFill)
    n_top := line.new(n_time, n_hi, n_time + orbLenMs, n_hi, xloc=xloc.bar_time, extend=extend.right, color=nyLineClr)
    n_bot := line.new(n_time, n_lo, n_time + orbLenMs, n_lo, xloc=xloc.bar_time, extend=extend.right, color=nyLineClr)
    if showMid
        float mid = (n_hi + n_lo) / 2
        n_mid := line.new(n_time, mid, n_time + orbLenMs, mid, xloc=xloc.bar_time, extend=extend.right, color=nyLineClr, style=line.style_dotted)
n_crossUp = ta.crossover(close, n_hi)
n_crossDown = ta.crossunder(close, n_lo)
n_longSig = nyOn and n_ready and not n_new and n_close > n_open and n_crossUp and (allowMulti or not n_locked)
n_shortSig = nyOn and n_ready and not n_new and n_close <= n_open and n_crossDown and (allowMulti or not n_locked)
if n_longSig or n_shortSig
    n_locked := true
if showArrows and n_longSig
    label.new(bar_index, n_lo, "", style=label.style_triangleup, size=size.tiny, color=color.green)
if showArrows and n_shortSig
    label.new(bar_index, n_hi, "", style=label.style_triangledown, size=size.tiny, color=color.red)
 
// One session ends where the next starts. Historical drawings remain.
// Use the detected confirmed 5m opening-bar timestamp as the exact end.
if l_new
    stopLines(a_top, a_bot, a_mid, l_time)
    a_ready := false
if n_new
    stopLines(l_top, l_bot, l_mid, n_time)
    l_ready := false
if a_new
    stopLines(n_top, n_bot, n_mid, a_time)
    n_ready := false
 
alertcondition(a_longSig, "Asia ORB Long", "Asia ORB Long Breakout")
alertcondition(a_shortSig, "Asia ORB Short", "Asia ORB Short Breakout")
alertcondition(l_longSig, "London ORB Long", "London ORB Long Breakout")
alertcondition(l_shortSig, "London ORB Short", "London ORB Short Breakout")
alertcondition(n_longSig, "NY ORB Long", "New York ORB Long Breakout")
alertcondition(n_shortSig, "NY ORB Short", "New York ORB Short Breakout")

← 返回列表