TPP Global Market Sessions PRO v1.2

gerafitti · study · 337 行 · 点赞 19 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gerafitti
 
//@version=6
indicator("TPP Global Market Sessions PRO v1.2", shorttitle="TPP Sessions PRO v1.2", overlay=true, max_bars_back=5000, max_boxes_count=500, max_labels_count=500, max_lines_count=500)
 
// ============================================================================
// TPP Global Market Sessions PRO v1.2
// Visual context tool for New York, London, Tokyo and Sydney.
// ============================================================================
 
string G_GENERAL = "01 - General"
string G_NY      = "02 - New York"
string G_LONDON  = "03 - London"
string G_TOKYO   = "04 - Tokyo"
string G_SYDNEY  = "05 - Sydney"
string G_STYLE   = "06 - Style"
string G_DAYS    = "07 - Day Dividers"
string G_TRANS   = "08 - Session Transitions"
string G_PANEL   = "09 - Status Panel"
 
string TZ_BA      = "America/Argentina/Buenos_Aires"
string TZ_NY      = "America/New_York"
string TZ_LONDON  = "Europe/London"
string TZ_TOKYO   = "Asia/Tokyo"
string TZ_SYDNEY  = "Australia/Sydney"
 
color C_WHITE = #F5F7FA
color C_GRAY  = #8C949E
color C_DARK  = #11161D
 
string scheduleMode = input.string(
     "Automatic DST",
     "Session schedule mode",
     options=["Automatic DST", "NY Summer (BA UTC-3)", "NY Winter (BA UTC-3)", "Custom Buenos Aires"],
     group=G_GENERAL,
     tooltip="Automatic DST is recommended. It uses each market's native IANA timezone, so daylight-saving changes are handled automatically. Manual profiles are convenience presets in Buenos Aires time; transition weeks can differ because countries change clocks on different dates.")
 
int historyDays = input.int(45, "Draw recent history (days)", minval=5, maxval=180, group=G_GENERAL)
bool weekdaysOnly = input.bool(true, "Sessions Monday-Friday only", group=G_GENERAL)
bool showSessionBoxes = input.bool(true, "Show session range boxes", group=G_GENERAL)
bool showSessionLabels = input.bool(true, "Show session name + High/Low", group=G_GENERAL)
 
bool showNY = input.bool(true, "Enable New York", group=G_NY)
string nyNative = input.session("0930-1600", "Native hours (New York)", group=G_NY)
string nyCustomBA = input.session("1130-1800", "Custom Buenos Aires hours", group=G_NY)
 
bool showLondon = input.bool(true, "Enable London", group=G_LONDON)
string londonNative = input.session("0800-1630", "Native hours (London)", group=G_LONDON)
string londonCustomBA = input.session("0500-1330", "Custom Buenos Aires hours", group=G_LONDON)
 
bool showTokyo = input.bool(true, "Enable Tokyo", group=G_TOKYO)
string tokyoNative = input.session("0900-1530", "Native hours (Tokyo)", group=G_TOKYO,
     tooltip="Context envelope. TSE has a midday recess, but this indicator draws one continuous session box.")
string tokyoCustomBA = input.session("2100-0330", "Custom Buenos Aires hours", group=G_TOKYO)
 
bool showSydney = input.bool(true, "Enable Sydney", group=G_SYDNEY)
string sydneyNative = input.session("1000-1600", "Native hours (Sydney)", group=G_SYDNEY)
string sydneyCustomBA = input.session("2000-0200", "Custom Buenos Aires hours", group=G_SYDNEY)
 
color nyColor = input.color(#00BCD4, "New York", group=G_STYLE, inline="c1")
color londonColor = input.color(#3F6DF6, "London", group=G_STYLE, inline="c1")
color tokyoColor = input.color(#E040FB, "Tokyo", group=G_STYLE, inline="c2")
color sydneyColor = input.color(#FFB300, "Sydney", group=G_STYLE, inline="c2")
int boxTransparency = input.int(88, "Box fill transparency", minval=60, maxval=100, group=G_STYLE)
int borderTransparency = input.int(15, "Border transparency", minval=0, maxval=100, group=G_STYLE)
string borderStyleInput = input.string("Dotted", "Box border style", options=["Solid", "Dotted", "Dashed"], group=G_STYLE)
string labelSizeInput = input.string("Tiny", "Session label size", options=["Tiny", "Small", "Normal"], group=G_STYLE)
 
bool showDayDividers = input.bool(true, "Show Buenos Aires day dividers", group=G_DAYS)
color dayDividerColor = input.color(#FFFFFF, "Divider / weekday text", group=G_DAYS)
int dayDividerTransparency = input.int(65, "Divider transparency", minval=0, maxval=95, group=G_DAYS)
bool showWeekdayText = input.bool(true, "Show weekday names", group=G_DAYS)
 
bool showAsiaEuropeTransition = input.bool(true, "Show Asia → Europe transition", group=G_TRANS,
     tooltip="Highlights the natural gap between the end of Tokyo and the opening of London. Automatic DST lets London determine the real end of the band.")
bool showAmericasAsiaTransition = input.bool(true, "Show Americas → Asia transition", group=G_TRANS,
     tooltip="Highlights the natural gap after New York closes and before Sydney opens. Automatic DST lets both markets adjust independently.")
color asiaEuropeTransitionColor = input.color(#A7AFBA, "Asia → Europe band", group=G_TRANS, inline="tr1")
color americasAsiaTransitionColor = input.color(#8F9AA8, "Americas → Asia band", group=G_TRANS, inline="tr1")
int transitionTransparency = input.int(93, "Transition transparency", minval=80, maxval=100, group=G_TRANS)
bool showTransitionLabel = input.bool(true, "Show transition labels", group=G_TRANS)
 
bool showPanel = input.bool(true, "Show status panel", group=G_PANEL)
string panelPositionInput = input.string("Top Right", "Position", options=["Top Right", "Bottom Right", "Bottom Left"], group=G_PANEL)
 
f_border_style(string value) =>
    value == "Solid" ? line.style_solid : value == "Dashed" ? line.style_dashed : line.style_dotted
 
f_label_size(string value) =>
    value == "Normal" ? size.normal : value == "Small" ? size.small : size.tiny
 
f_panel_position(string value) =>
    value == "Bottom Right" ? position.bottom_right : value == "Bottom Left" ? position.bottom_left : position.top_right
 
f_days_suffix() =>
    weekdaysOnly ? ":23456" : ":1234567"
 
f_session_string(string market, string nativeSession, string customBA) =>
    string result = nativeSession
    if scheduleMode == "NY Summer (BA UTC-3)"
        result := market == "New York" ? "1030-1700" :
                  market == "London"   ? "0400-1230" :
                  market == "Tokyo"    ? "2100-0330" :
                                         "2100-0300"
    else if scheduleMode == "NY Winter (BA UTC-3)"
        result := market == "New York" ? "1130-1800" :
                  market == "London"   ? "0500-1330" :
                  market == "Tokyo"    ? "2100-0330" :
                                         "2000-0200"
    else if scheduleMode == "Custom Buenos Aires"
        result := customBA
    result + f_days_suffix()
 
f_session_timezone(string nativeTimezone) =>
    scheduleMode == "Automatic DST" ? nativeTimezone : TZ_BA
 
f_in_session(string sessionString, string timezoneString) =>
    not na(time(timeframe.period, sessionString, timezoneString))
 
f_recent_enough() =>
    time >= timenow - historyDays * 86400000
 
f_render_session(bool enabled, bool active, string sessionName, color sessionColor) =>
    var box rangeBox = na
    var label rangeLabel = na
    var float sessionHigh = na
    var float sessionLow = na
    var int startBar = na
 
    bool started = enabled and active and not active[1] and f_recent_enough()
    bool running = enabled and active and f_recent_enough()
 
    if started
        sessionHigh := high
        sessionLow := low
        startBar := bar_index
 
        if showSessionBoxes
            rangeBox := box.new(left=bar_index, top=sessionHigh, right=bar_index, bottom=sessionLow,
                 xloc=xloc.bar_index,
                 bgcolor=color.new(sessionColor, boxTransparency),
                 border_color=color.new(sessionColor, borderTransparency),
                 border_style=f_border_style(borderStyleInput),
                 border_width=1)
 
        if showSessionLabels
            rangeLabel := label.new(x=bar_index, y=sessionHigh, text="",
                 xloc=xloc.bar_index, yloc=yloc.price,
                 style=label.style_none,
                 color=color.new(color.black, 100),
                 textcolor=C_WHITE,
                 size=f_label_size(labelSizeInput))
 
    else if running
        sessionHigh := na(sessionHigh) ? high : math.max(sessionHigh, high)
        sessionLow := na(sessionLow) ? low : math.min(sessionLow, low)
 
    if running
        if showSessionBoxes and not na(rangeBox)
            box.set_top(rangeBox, sessionHigh)
            box.set_bottom(rangeBox, sessionLow)
            box.set_right(rangeBox, bar_index)
 
        if showSessionLabels and not na(rangeLabel)
            int midBar = int(math.round((startBar + bar_index) / 2.0))
            label.set_x(rangeLabel, midBar)
            label.set_y(rangeLabel, sessionHigh)
            label.set_text(rangeLabel, sessionName + "\nH " + str.tostring(sessionHigh, format.mintick) + "   L " + str.tostring(sessionLow, format.mintick))
 
    [sessionHigh, sessionLow]
 
string nySession = f_session_string("New York", nyNative, nyCustomBA)
string londonSession = f_session_string("London", londonNative, londonCustomBA)
string tokyoSession = f_session_string("Tokyo", tokyoNative, tokyoCustomBA)
string sydneySession = f_session_string("Sydney", sydneyNative, sydneyCustomBA)
 
string nyTZ = f_session_timezone(TZ_NY)
string londonTZ = f_session_timezone(TZ_LONDON)
string tokyoTZ = f_session_timezone(TZ_TOKYO)
string sydneyTZ = f_session_timezone(TZ_SYDNEY)
 
bool nyActive = timeframe.isintraday and showNY and f_in_session(nySession, nyTZ)
bool londonActive = timeframe.isintraday and showLondon and f_in_session(londonSession, londonTZ)
bool tokyoActive = timeframe.isintraday and showTokyo and f_in_session(tokyoSession, tokyoTZ)
bool sydneyActive = timeframe.isintraday and showSydney and f_in_session(sydneySession, sydneyTZ)
 
[nyHigh, nyLow] = f_render_session(showNY, nyActive, "NEW YORK", nyColor)
[londonHigh, londonLow] = f_render_session(showLondon, londonActive, "LONDON", londonColor)
[tokyoHigh, tokyoLow] = f_render_session(showTokyo, tokyoActive, "TOKYO", tokyoColor)
[sydneyHigh, sydneyLow] = f_render_session(showSydney, sydneyActive, "SYDNEY", sydneyColor)
 
// ============================================================================
// 14. SESSION TRANSITIONS
// ============================================================================
// These are context bands, NOT market sessions. They do not calculate their own
// High/Low and do not create a trade signal.
//
// ASIA → EUROPE:
// Maximum BA window 03:30-05:00. In Automatic DST, London can open at 04:00 BA
// during UK summer time, so the band ends automatically as soon as London is active.
//
// AMERICAS → ASIA:
// Maximum BA window 17:00-21:00. The actual band begins only after New York is
// inactive and ends as soon as Sydney becomes active. This accommodates the
// independent DST calendars of the US and Australia.
int baHourNow = hour(time, TZ_BA)
int baMinuteNow = minute(time, TZ_BA)
int baMinutesNow = baHourNow * 60 + baMinuteNow
 
bool asiaEuropeClockWindow = baMinutesNow >= 210 and baMinutesNow < 300
bool americasAsiaClockWindow = baMinutesNow >= 1020 and baMinutesNow < 1260
 
bool asiaEuropeTransition =
     timeframe.isintraday and
     showAsiaEuropeTransition and
     asiaEuropeClockWindow and
     not tokyoActive and
     not londonActive
 
bool americasAsiaTransition =
     timeframe.isintraday and
     showAmericasAsiaTransition and
     americasAsiaClockWindow and
     not nyActive and
     not sydneyActive
 
bool asiaEuropeTransitionStarted = asiaEuropeTransition and not asiaEuropeTransition[1]
bool americasAsiaTransitionStarted = americasAsiaTransition and not americasAsiaTransition[1]
 
bgcolor(
     asiaEuropeTransition ? color.new(asiaEuropeTransitionColor, transitionTransparency) :
     americasAsiaTransition ? color.new(americasAsiaTransitionColor, transitionTransparency) :
     na,
     title="Session Transition Bands")
 
plotshape(showTransitionLabel and asiaEuropeTransitionStarted,
     title="Asia to Europe Transition",
     style=shape.labeldown,
     location=location.top,
     color=color.new(C_DARK, 35),
     text="ASIA → EUROPE\nTRANSITION",
     textcolor=C_WHITE,
     size=size.tiny,
     display=display.all - display.status_line)
 
plotshape(showTransitionLabel and americasAsiaTransitionStarted,
     title="Americas to Asia Transition",
     style=shape.labeldown,
     location=location.top,
     color=color.new(C_DARK, 35),
     text="AMERICAS → ASIA\nTRANSITION",
     textcolor=C_WHITE,
     size=size.tiny,
     display=display.all - display.status_line)
 
// ============================================================================
// 15. BUENOS AIRES DAY DIVIDERS
// ============================================================================
int baDay = dayofmonth(time, TZ_BA)
int baDow = dayofweek(time, TZ_BA)
bool newBADay = not na(baDay[1]) and baDay != baDay[1]
 
if showDayDividers and newBADay
    line.new(x1=bar_index, y1=low, x2=bar_index, y2=high,
         xloc=xloc.bar_index, extend=extend.both,
         color=color.new(dayDividerColor, dayDividerTransparency),
         style=line.style_dotted, width=1)
 
plotshape(showDayDividers and showWeekdayText and newBADay and baDow == dayofweek.monday,
     title="Monday", style=shape.labeldown, location=location.top, color=na, text="MONDAY", textcolor=dayDividerColor, size=size.tiny, display=display.all - display.status_line)
plotshape(showDayDividers and showWeekdayText and newBADay and baDow == dayofweek.tuesday,
     title="Tuesday", style=shape.labeldown, location=location.top, color=na, text="TUESDAY", textcolor=dayDividerColor, size=size.tiny, display=display.all - display.status_line)
plotshape(showDayDividers and showWeekdayText and newBADay and baDow == dayofweek.wednesday,
     title="Wednesday", style=shape.labeldown, location=location.top, color=na, text="WEDNESDAY", textcolor=dayDividerColor, size=size.tiny, display=display.all - display.status_line)
plotshape(showDayDividers and showWeekdayText and newBADay and baDow == dayofweek.thursday,
     title="Thursday", style=shape.labeldown, location=location.top, color=na, text="THURSDAY", textcolor=dayDividerColor, size=size.tiny, display=display.all - display.status_line)
plotshape(showDayDividers and showWeekdayText and newBADay and baDow == dayofweek.friday,
     title="Friday", style=shape.labeldown, location=location.top, color=na, text="FRIDAY", textcolor=dayDividerColor, size=size.tiny, display=display.all - display.status_line)
plotshape(showDayDividers and showWeekdayText and newBADay and baDow == dayofweek.saturday,
     title="Saturday", style=shape.labeldown, location=location.top, color=na, text="SATURDAY", textcolor=dayDividerColor, size=size.tiny, display=display.all - display.status_line)
plotshape(showDayDividers and showWeekdayText and newBADay and baDow == dayofweek.sunday,
     title="Sunday", style=shape.labeldown, location=location.top, color=na, text="SUNDAY", textcolor=dayDividerColor, size=size.tiny, display=display.all - display.status_line)
 
var table panel = table.new(f_panel_position(panelPositionInput), 3, 8,
     bgcolor=color.new(C_DARK, 8), frame_color=color.new(C_WHITE, 80),
     frame_width=1, border_width=0, force_overlay=true)
 
f_status_text(bool enabled, bool active) =>
    not enabled ? "OFF" : active ? "ACTIVE" : "CLOSED"
 
f_status_color(bool enabled, bool active, color marketColor) =>
    not enabled ? C_GRAY : active ? marketColor : C_GRAY
 
if barstate.islast and showPanel
    table.cell(panel, 0, 0, "TPP SESSIONS", text_color=C_WHITE, bgcolor=C_DARK, text_halign=text.align_left)
    table.cell(panel, 1, 0, "BUENOS AIRES", text_color=C_WHITE, bgcolor=C_DARK)
    table.cell(panel, 2, 0, scheduleMode == "Automatic DST" ? "AUTO DST" : scheduleMode, text_color=scheduleMode == "Automatic DST" ? #00E5FF : #FFB300, bgcolor=C_DARK)
 
    table.cell(panel, 0, 1, "NEW YORK", text_color=nyColor, text_halign=text.align_left)
    table.cell(panel, 1, 1, f_status_text(showNY, nyActive), text_color=f_status_color(showNY, nyActive, nyColor))
    table.cell(panel, 2, 1, scheduleMode == "Automatic DST" ? "09:30-16:00 NY" : str.substring(nySession, 0, 9), text_color=C_GRAY)
 
    table.cell(panel, 0, 2, "LONDON", text_color=londonColor, text_halign=text.align_left)
    table.cell(panel, 1, 2, f_status_text(showLondon, londonActive), text_color=f_status_color(showLondon, londonActive, londonColor))
    table.cell(panel, 2, 2, scheduleMode == "Automatic DST" ? "08:00-16:30 UK" : str.substring(londonSession, 0, 9), text_color=C_GRAY)
 
    table.cell(panel, 0, 3, "TOKYO", text_color=tokyoColor, text_halign=text.align_left)
    table.cell(panel, 1, 3, f_status_text(showTokyo, tokyoActive), text_color=f_status_color(showTokyo, tokyoActive, tokyoColor))
    table.cell(panel, 2, 3, scheduleMode == "Automatic DST" ? "09:00-15:30 JP" : str.substring(tokyoSession, 0, 9), text_color=C_GRAY)
 
    table.cell(panel, 0, 4, "SYDNEY", text_color=sydneyColor, text_halign=text.align_left)
    table.cell(panel, 1, 4, f_status_text(showSydney, sydneyActive), text_color=f_status_color(showSydney, sydneyActive, sydneyColor))
    table.cell(panel, 2, 4, scheduleMode == "Automatic DST" ? "10:00-16:00 AU" : str.substring(sydneySession, 0, 9), text_color=C_GRAY)
 
    table.cell(panel, 0, 5, "TRANSITION", text_color=asiaEuropeTransitionColor, text_halign=text.align_left)
    table.cell(panel, 1, 5, showAsiaEuropeTransition ? (asiaEuropeTransition ? "ACTIVE" : "CLOSED") : "OFF",
         text_color=showAsiaEuropeTransition ? (asiaEuropeTransition ? C_WHITE : C_GRAY) : C_GRAY)
    table.cell(panel, 2, 5, "ASIA → EUROPE", text_color=C_GRAY)
 
    table.cell(panel, 0, 6, "TRANSITION", text_color=americasAsiaTransitionColor, text_halign=text.align_left)
    table.cell(panel, 1, 6, showAmericasAsiaTransition ? (americasAsiaTransition ? "ACTIVE" : "CLOSED") : "OFF",
         text_color=showAmericasAsiaTransition ? (americasAsiaTransition ? C_WHITE : C_GRAY) : C_GRAY)
    table.cell(panel, 2, 6, "AMERICAS → ASIA", text_color=C_GRAY)
 
    table.cell(panel, 0, 7, "TIMEFRAME", text_color=C_GRAY, text_halign=text.align_left)
    table.cell(panel, 1, 7, timeframe.period, text_color=C_WHITE)
    table.cell(panel, 2, 7, timeframe.isintraday ? "READY" : "USE INTRADAY", text_color=timeframe.isintraday ? #00BFA5 : #E53935)
 
plot(nyActive ? nyHigh : na, "NY Session High", display=display.data_window)
plot(nyActive ? nyLow : na, "NY Session Low", display=display.data_window)
plot(londonActive ? londonHigh : na, "London Session High", display=display.data_window)
plot(londonActive ? londonLow : na, "London Session Low", display=display.data_window)
plot(tokyoActive ? tokyoHigh : na, "Tokyo Session High", display=display.data_window)
plot(tokyoActive ? tokyoLow : na, "Tokyo Session Low", display=display.data_window)
plot(sydneyActive ? sydneyHigh : na, "Sydney Session High", display=display.data_window)
plot(sydneyActive ? sydneyLow : na, "Sydney Session Low", display=display.data_window)

← 返回列表