Worldwide Sessions and Opening Range Breakout

tyler-johnson · study · 126 行 · 点赞 206 · 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/
// © tyler-johnson
 
//@version=6
indicator("Worldwide Sessions and Open Range Breakout", shorttitle = "ORB Sess", overlay = true)
 
string orb_tf = input("15", title = "ORB Timeframe", tooltip = "The timeframe to calculate the opening range.")
 
int orb_width_sec = timeframe.in_seconds(orb_tf)
int tf_sec = timeframe.in_seconds()
 
f_gcd(int a, int b) =>
    // Ensure numbers are positive, as GCD is defined for positive integers.
    int ra = math.abs(a)
    int rb = math.abs(b)
    
    // The Euclidean algorithm loop
    while rb != 0
        temp = rb
        rb := ra % rb
        ra := temp
 
    ra // The result is the final value of 'a'
 
f_ranges_overlap(float start1, float end1, float start2, float end2) =>
    // Normalize ranges to ensure the start is always less than or equal to the end.
    r1_start = math.min(start1, end1)
    r1_end   = math.max(start1, end1)
    r2_start = math.min(start2, end2)
    r2_end   = math.max(start2, end2)
    
    // The ranges overlap if the first range's start is before the second's end
    // AND the second range's start is before the first's end.
    r1_start < r2_end and r2_start < r1_end
 
f_slice_array_by_time(array<float> input_array, int array_start_time_ms, int sub_timeframe_duration_sec, int slice_start_time_ms, int slice_duration_sec) =>
    // --- CONVERSION STEP ---
    // Convert durations from seconds to milliseconds to match the start time units.
    sub_timeframe_duration_ms = sub_timeframe_duration_sec * 1000
    slice_duration_ms = slice_duration_sec * 1000
 
    // Get the size of the input array
    arr_size = array.size(input_array)
    
    // If the input array is empty or sub_timeframe is zero, we can't proceed.
    if arr_size == 0 or sub_timeframe_duration_ms <= 0
        array.new_float(0)
    else
        // 1. Calculate the end times for the source array and the desired slice (all in milliseconds)
        array_end_time_ms = array_start_time_ms + (arr_size * sub_timeframe_duration_ms)
        slice_end_time_ms = slice_start_time_ms + slice_duration_ms
 
        // 2. Determine the actual start and end times of the overlapping period
        intersection_start = math.max(array_start_time_ms, slice_start_time_ms)
        intersection_end = math.min(array_end_time_ms, slice_end_time_ms)
 
        // 3. If there is no overlap, return an empty array
        if intersection_start >= intersection_end
            array.new_float(0)
        else
            // 4. Calculate the starting and ending indices for the slice
            start_offset = intersection_start - array_start_time_ms
            start_index = int(math.floor(start_offset / sub_timeframe_duration_ms))
 
            end_offset = intersection_end - array_start_time_ms
            // Use ceil to include the partial interval at the end, as slicing is exclusive of the end index.
            end_index = int(math.ceil(end_offset / sub_timeframe_duration_ms))
 
            // 5. Ensure indices are within the bounds of the array
            start_index := math.max(0, start_index)
            end_index := math.min(arr_size, end_index)
 
            // 6. Slice the array and return the result
            array.slice(input_array, start_index, end_index)
 
f_get_range_in_window(int start_time, int end_time) =>
    float hi = na
    float lo = na
    int orb_end = start_time + orb_width_sec * 1000
 
    if f_ranges_overlap(time, time_close, start_time, orb_end)
        dtf = f_gcd(tf_sec, orb_width_sec)
        count = orb_width_sec / dtf
        [h, l] = request.security_lower_tf(syminfo.tickerid, timeframe.from_seconds(dtf), [high, low])
        r_hi = array.max(f_slice_array_by_time(h, time, dtf, start_time, orb_width_sec))
        r_lo = array.min(f_slice_array_by_time(l, time, dtf, start_time, orb_width_sec))
        hi := na(hi[1]) ? r_hi : math.max(hi[1], r_hi)
        lo := na(lo[1]) ? r_lo : math.min(lo[1], r_lo)
    else if f_ranges_overlap(time, time_close, start_time, end_time)
        hi := hi[1]
        lo := lo[1]
 
    if time_close < orb_end or time >= end_time
        [na, na]
    else 
        [hi, lo]
 
string london_timezone = "Europe/London"
int london_start = timestamp(london_timezone, year(time, london_timezone), month(time, london_timezone), dayofmonth(time, london_timezone), 8, 0)
int london_end = timestamp(london_timezone, year(time, london_timezone), month(time, london_timezone), dayofmonth(time, london_timezone), 16, 30)
bool is_london = time >= london_start and time < london_end
[london_high, london_low] = f_get_range_in_window(london_start, london_end)
 
bgcolor(is_london ? color.new(color.yellow, 90) : na)
plot(london_high, "London Opening Range High", color = color.yellow, style = plot.style_linebr)
plot(london_low, "London Opening Range Low", color = color.yellow, style = plot.style_linebr)
 
string asia_timezone = "Asia/Hong_Kong"
int asia_start = timestamp(asia_timezone, year(time, asia_timezone), month(time, asia_timezone), dayofmonth(time, asia_timezone), 8, 0)
int asia_end = timestamp(asia_timezone, year(time, asia_timezone), month(time, asia_timezone), dayofmonth(time, asia_timezone), 16, 0)
bool is_asia = time >= asia_start and time < asia_end
[asia_high, asia_low] = f_get_range_in_window(asia_start, asia_end)
 
bgcolor(is_asia ? color.new(color.blue, 90) : na)
plot(asia_high, "Asia Opening Range High", color = color.blue, style = plot.style_linebr)
plot(asia_low, "Asia Opening Range Low", color = color.blue, style = plot.style_linebr)
 
string newyork_timezone = "America/New_York"
int newyork_start = timestamp(newyork_timezone, year(time, newyork_timezone), month(time, newyork_timezone), dayofmonth(time, newyork_timezone), 9, 30)
int newyork_end = timestamp(newyork_timezone, year(time, newyork_timezone), month(time, newyork_timezone), dayofmonth(time, newyork_timezone), 16, 0)
bool is_newyork = time >= newyork_start and time < newyork_end
[newyork_high, newyork_low] = f_get_range_in_window(newyork_start, newyork_end)
 
bgcolor(is_newyork ? color.new(color.purple, 90) : na)
plot(newyork_high, "New York Opening Range High", color = color.purple, style = plot.style_linebr)
plot(newyork_low, "New York Opening Range Low", color = color.purple, style = plot.style_linebr)

← 返回列表