Session Candles - Asia / London / New York - AudriusTech

AudriusTech · study · 135 行 · 点赞 28 · TradingView 原页

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

Pine Script

//@version=6
indicator("Session Candles - Asia / London / New York", overlay = true, max_boxes_count = 10, max_lines_count = 20, max_labels_count = 10)
 
// One OHLC candle per session, drawn to the right of the latest chart bar.
// Use a standard intraday chart of 1 minute or higher. Enable extended hours
// on the chart for updates during the overnight sessions, where available.
// Each candle stays visible until the next occurrence of that session starts.
// The date beneath each candle is its session END date in New York time.
// Choose any left-to-right arrangement in Inputs > Candle order.
 
const string TZ = "America/New_York"
 
asiaSession = input.session("2100-0200", "Asia", group = "Sessions - New York time")
londonSession = input.session("0300-0600", "London", group = "Sessions - New York time")
nySession = input.session("0930-1630", "New York", group = "Sessions - New York time")
 
sessionOrder = input.string("Asia / London / New York", "Session order (left to right)", options = ["Asia / London / New York", "Asia / New York / London", "London / Asia / New York", "London / New York / Asia", "New York / Asia / London", "New York / London / Asia"], group = "Candle order", tooltip = "The first session is closest to the chart. Each session appears exactly once.")
 
// Slots 0, 1 and 2 run from left to right, starting nearest the chart.
[asiaSlot, londonSlot, nySlot] = switch sessionOrder
    "Asia / New York / London" => [0, 2, 1]
    "London / Asia / New York" => [1, 0, 2]
    "London / New York / Asia" => [2, 0, 1]
    "New York / Asia / London" => [1, 2, 0]
    "New York / London / Asia" => [2, 1, 0]
    => [0, 1, 2]
 
rightOffset = input.int(12, "Distance from chart (bars)", minval = 2, maxval = 100, group = "Appearance")
bodyWidth = input.int(8, "Candle width (bars)", minval = 2, maxval = 20, step = 2, group = "Appearance")
candleGap = input.int(10, "Gap between candles (bars)", minval = 2, maxval = 30, group = "Appearance")
bullColor = input.color(color.rgb(38, 166, 154), "Bullish candle", group = "Appearance")
bearColor = input.color(color.rgb(239, 83, 80), "Bearish candle", group = "Appearance")
showPrices = input.bool(false, "Show OHLC beneath candles", group = "Appearance")
 
if not chart.is_standard or not timeframe.isintraday or timeframe.isseconds
    runtime.error("Use a standard time-based intraday chart of 1 minute or higher.")
 
// Runs INSIDE the requested 1-minute context, so larger chart bars cannot
// contribute prices from outside a session. Start is inclusive; end exclusive.
sessionOHLC(simple string sessionHours) =>
    var float sessionOpen = na
    var float sessionHigh = na
    var float sessionLow = na
    var float sessionClose = na
    var int sessionStart = na
    var int sessionEnd = na
 
    int startHour = int(str.tonumber(str.substring(sessionHours, 0, 2)))
    int startMinute = int(str.tonumber(str.substring(sessionHours, 2, 4)))
    int endHour = int(str.tonumber(str.substring(sessionHours, 5, 7)))
    int endMinute = int(str.tonumber(str.substring(sessionHours, 7, 9)))
    int startMinutes = startHour * 60 + startMinute
    int endMinutes = endHour * 60 + endMinute
    int localMinutes = hour(time, TZ) * 60 + minute(time, TZ)
    bool inside = not na(time("1", sessionHours, TZ))
 
    if inside
        // After midnight, an overnight session still belongs to its start date.
        // Calendar arithmetic also handles month/year changes and NY DST.
        int previousDay = localMinutes < startMinutes ? 1 : 0
        int startStamp = timestamp(TZ, year(time, TZ), month(time, TZ), dayofmonth(time, TZ) - previousDay, startHour, startMinute)
        bool newSession = na(sessionStart) or startStamp != sessionStart
 
        if newSession
            sessionOpen := open
            sessionHigh := high
            sessionLow := low
            sessionStart := startStamp
            int overnight = endMinutes <= startMinutes ? 1 : 0
            sessionEnd := timestamp(TZ, year(startStamp, TZ), month(startStamp, TZ), dayofmonth(startStamp, TZ) + overnight, endHour, endMinute)
        else
            sessionHigh := math.max(sessionHigh, high)
            sessionLow := math.min(sessionLow, low)
 
        sessionClose := close
 
    [sessionOpen, sessionHigh, sessionLow, sessionClose, sessionStart, sessionEnd]
 
// Extended-hours data is requested where the instrument/provider supports it.
string sourceTicker = ticker.modify(syminfo.tickerid, session = session.extended)
[aO, aH, aL, aC, aStart, aEnd] = request.security(sourceTicker, "1", sessionOHLC(asiaSession), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, calc_bars_count = 20000)
[lO, lH, lL, lC, lStart, lEnd] = request.security(sourceTicker, "1", sessionOHLC(londonSession), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, calc_bars_count = 20000)
[nO, nH, nL, nC, nStart, nEnd] = request.security(sourceTicker, "1", sessionOHLC(nySession), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, calc_bars_count = 20000)
 
priceText(float value) =>
    str.tostring(value, format.mintick)
 
// Each written call has its own persistent drawing objects.
drawCandle(int slot, string sessionName, color nameColor, float o, float h, float l, float c, int starts, int ends, int referenceTime, float labelY) =>
    var line wick = line.new(x1 = na, y1 = na, x2 = na, y2 = na, xloc = xloc.bar_index, width = 2)
    var box body = box.new(left = na, top = na, right = na, bottom = na, xloc = xloc.bar_index, border_width = 2)
    var line doji = line.new(x1 = na, y1 = na, x2 = na, y2 = na, xloc = xloc.bar_index, width = 2)
    var label caption = label.new(x = na, y = na, text = "", xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_up, color = color.new(color.black, 100), size = size.small)
 
    int leftX = bar_index + rightOffset + slot * (bodyWidth + candleGap)
    int rightX = leftX + bodyWidth
    int centerX = leftX + int(bodyWidth / 2)
    label.set_xy(caption, centerX, labelY)
    label.set_textcolor(caption, nameColor)
 
    if not na(o)
        color candleColor = c >= o ? bullColor : bearColor
        line.set_xy1(wick, centerX, h)
        line.set_xy2(wick, centerX, l)
        line.set_color(wick, candleColor)
 
        box.set_lefttop(body, leftX, math.max(o, c))
        box.set_rightbottom(body, rightX, math.min(o, c))
        box.set_border_color(body, candleColor)
        box.set_bgcolor(body, candleColor)
 
        // Explicit horizontal body for a true doji; no artificial price height.
        line.set_xy1(doji, leftX, o)
        line.set_xy2(doji, rightX, o)
        line.set_color(doji, o == c ? candleColor : color.new(candleColor, 100))
 
        bool active = referenceTime >= starts and referenceTime < ends
        string stateText = active ? "LIVE" : "CLOSED"
        string values = "O: " + priceText(o) + "\nH: " + priceText(h) + "\nL: " + priceText(l) + "\nC: " + priceText(c)
        string captionText = sessionName + "\n" + str.format_time(ends, "dd MMM", TZ) + "\n" + stateText
        label.set_text(caption, captionText + (showPrices ? "\n" + values : ""))
        label.set_tooltip(caption, sessionName + " | New York time\n" + str.format_time(starts, "dd MMM yyyy HH:mm", TZ) + " to\n" + str.format_time(ends, "dd MMM yyyy HH:mm", TZ) + "\n\n" + values)
    else
        label.set_text(caption, sessionName + "\nNO SESSION DATA")
        label.set_tooltip(caption, "No 1-minute bars found for this session. Check the instrument's trading hours and data availability.")
 
if barstate.islast
    float lowest = math.min(nz(aL, low), math.min(nz(lL, low), nz(nL, low)))
    float highest = math.max(nz(aH, high), math.max(nz(lH, high), nz(nH, high)))
    float padding = math.max((highest - lowest) * 0.08, syminfo.mintick * 10)
    int referenceTime = barstate.isrealtime ? timenow : time_close
 
    drawCandle(asiaSlot, "ASIA", color.orange, aO, aH, aL, aC, aStart, aEnd, referenceTime, lowest - padding)
    drawCandle(londonSlot, "LONDON", color.rgb(66, 165, 245), lO, lH, lL, lC, lStart, lEnd, referenceTime, lowest - padding)
    drawCandle(nySlot, "NEW YORK", color.rgb(171, 71, 188), nO, nH, nL, nC, nStart, nEnd, referenceTime, lowest - padding)

← 返回列表