FX Market Sessions

boitoki · study · 87 行 · 点赞 16,995 · TradingView 原页

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

Pine Script

//@version=4
study("FX Market Sessions", overlay=true)
 
// Switches
bool show = timeframe.isintraday and timeframe.multiplier <= 60
bool show_realtime = input(true, "Show Market Sessions in Real time?")
bool show_label = input(true, "Show Label?")
bool show_oc = input(false, "Show Market's Open and Close?")
string col = input("Gray", "Color", options=["Black", "White", "Gray", "Blue", "Red"])
 
// Functions
get_posTopAndBottom (begin_index, to_index) => 
    float pT = 0
    float pB = high
 
    for i = begin_index to to_index
        pT := max(high[i], pT)
        pB := min(low[i], pB)
 
    [pT, pB]
 
create_marketRect (closedTime, sessionTime, sessionMins, t, col) =>
    bool run_realtime = not na(sessionTime)
    bool run_fixed = closedTime == time
    bool run = iff(show_realtime, run_realtime, run_fixed)
 
    if (run)
        int minsLen = round(sessionMins / timeframe.multiplier)
        int begin_index = 0
 
        for i = 1 to minsLen
            if (not na(sessionTime[i]))
                begin_index := begin_index + 1
 
        int pR = bar_index
        int pL = bar_index - begin_index
        [pT, pB] = get_posTopAndBottom(1, begin_index)
 
        line_r = line.new(pR, pT, pR, pB, style=line.style_dashed, color=col)
        line_l = line.new(pL, pT, pL, pB, style=line.style_dashed, color=col)
        line_t = line.new(pL, pT, pR, pT, style=line.style_dashed, color=col)
        line_b = line.new(pL, pB, pR, pB, style=line.style_dashed, color=col)
 
        if (run_realtime)
            line.delete(line_r[1])
            line.delete(line_l[1])
            line.delete(line_t[1])
            line.delete(line_b[1])
        
        if (show_oc)
            line_o = line.new(pL, open[begin_index], pR, open[begin_index], style=line.style_dashed, color=color.white)
            line_c = line.new(pL, close[1], pR, close[1], style=line.style_dashed, color=color.white)
 
            if (run_realtime)
                line.delete(line_o[1])
                line.delete(line_c[1])
 
 
        if (show_label)
            line_label = label.new(pL, pT, t, textcolor=col, style=label.style_none, size=size.small)
 
            if (run_realtime)
                label.delete(line_label[1])
 
// Calculating
bool is_summerTime = month >= 3 and month <= 10
int londonClose = timestamp(year, month, dayofmonth, iff(is_summerTime, 11, 12), 00)
int londonSession = iff(is_summerTime, time(timeframe.period, "0300-1100"), time(timeframe.period, "0400-1200"))
int londonMins = (8 * 60)
int newyorkClose = timestamp(year, month, dayofmonth, iff(is_summerTime, 16, 17), 00)
int newyorkSession = iff(is_summerTime, time(timeframe.period, "0930-1600"), time(timeframe.period, "1030-1700"))
int newyorkMins = (7 * 60)
int tokyoClose = timestamp("GMT+9", year, month, dayofmonth, 15, 00)
int tokyoSession = time(timeframe.period, "2000-0200")
int tokyoMins = (6 * 60)
color c_rect = col == "White" ? color.white
  : col == "Black" ? color.black
  : col == "Gray" ? color.gray
  : col == "Blue" ? color.blue
  : col == "Red" ? color.red
  : color.white
 
// Plotting
if (show)
    create_marketRect(londonClose, londonSession, londonMins, "London", c_rect)
    create_marketRect(newyorkClose, newyorkSession, newyorkMins, "NY", c_rect)
    create_marketRect(tokyoClose, tokyoSession, tokyoMins, "Tokyo", c_rect)

← 返回列表