RTH Opening Range with Extensions

LeoTheTiger · study · 95 行 · 点赞 625 · TradingView 原页

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

Pine Script

//@version=6
indicator('RTH Opening Range with Extensions', overlay = true, max_boxes_count = 500, max_lines_count = 500)
 
// ==================== INPUTS ====================
 
// Opening Range Settings
orDuration = input.int(5, 'Opening Range Duration (minutes)', options = [1, 5, 15, 30])
rangeColor = input.color(color.gray, 'Range Line Color', group = 'Opening Range')
rangeFillColor = input.color(color.new(color.gray, 85), 'Range Fill Color', group = 'Opening Range')
rangeStyle = input.string('Solid', 'Range Line Style', options = ['Solid', 'Dashed', 'Dotted'], group = 'Opening Range')
rangeWidth = input.int(2, 'Range Line Width', minval = 1, maxval = 4, group = 'Opening Range')
 
// Midline Settings
showMidline = input.bool(true, 'Show Midline', group = 'Midline')
midColor = input.color(color.green, 'Midline Color', group = 'Midline')
midStyle = input.string('Solid', 'Midline Style', options = ['Solid', 'Dashed', 'Dotted'], group = 'Midline')
midWidth = input.int(2, 'Midline Width', minval = 1, maxval = 4, group = 'Midline')
 
// Extension 1 Settings
showExt1 = input.bool(true, 'Show Extension 1', group = 'Extension 1')
ext1Mult = input.float(1.0, 'Extension 1 Multiplier', minval = 0.1, step = 0.1, group = 'Extension 1')
ext1Color = input.color(color.green, 'Extension 1 Color', group = 'Extension 1')
ext1Style = input.string('Solid', 'Extension 1 Style', options = ['Solid', 'Dashed', 'Dotted'], group = 'Extension 1')
ext1Width = input.int(1, 'Extension 1 Width', minval = 1, maxval = 4, group = 'Extension 1')
 
// Extension 2 Settings
showExt2 = input.bool(false, 'Show Extension 2', group = 'Extension 2')
ext2Mult = input.float(2.0, 'Extension 2 Multiplier', minval = 0.1, step = 0.1, group = 'Extension 2')
ext2Color = input.color(color.blue, 'Extension 2 Color', group = 'Extension 2')
ext2Style = input.string('Dotted', 'Extension 2 Style', options = ['Solid', 'Dashed', 'Dotted'], group = 'Extension 2')
ext2Width = input.int(1, 'Extension 2 Width', minval = 1, maxval = 4, group = 'Extension 2')
 
// ==================== FUNCTIONS ====================
 
getLineStyle(style) =>
    switch style
        'Solid' => line.style_solid
        'Dashed' => line.style_dashed
        'Dotted' => line.style_dotted
 
// ==================== TIME CALCULATIONS ====================
 
rth_start = timestamp('America/New_York', year, month, dayofmonth, 9, 30)
rth_end = timestamp('America/New_York', year, month, dayofmonth, 9, 30 + orDuration)
rth_close = timestamp('America/New_York', year, month, dayofmonth, 16, 0)
 
// ==================== VARIABLES ====================
 
var float orHigh = na
var float orLow = na
var bool rangeDrawn = false
 
// ==================== LOGIC ====================
 
newDay = ta.change(time('D'))
if bool(newDay)
    orHigh := na
    orLow := na
    rangeDrawn := false
 
inOpeningRange = time >= rth_start and time < rth_end
if inOpeningRange
    orHigh := na(orHigh) ? high : math.max(orHigh, high)
    orLow := na(orLow) ? low : math.min(orLow, low)
 
if not inOpeningRange and not rangeDrawn and not na(orHigh) and not na(orLow)
    orRange = orHigh - orLow
    mid = (orHigh + orLow) / 2
    ext1High = orHigh + ext1Mult * orRange
    ext1Low = orLow - ext1Mult * orRange
    ext2High = orHigh + ext2Mult * orRange
    ext2Low = orLow - ext2Mult * orRange
 
    // Draw shaded range box (backpainted to 9:30)
    box.new(left = rth_start, top = orHigh, right = rth_close, bottom = orLow, xloc = xloc.bar_time, bgcolor = rangeFillColor, border_color = color.new(color.white, 100))
 
    // Draw range lines (backpainted to 9:30)
    line.new(x1 = rth_start, y1 = orHigh, x2 = rth_close, y2 = orHigh, xloc = xloc.bar_time, color = rangeColor, style = getLineStyle(rangeStyle), width = rangeWidth)
    line.new(x1 = rth_start, y1 = orLow, x2 = rth_close, y2 = orLow, xloc = xloc.bar_time, color = rangeColor, style = getLineStyle(rangeStyle), width = rangeWidth)
 
    // Draw midline (backpainted to 9:30)
    if showMidline
        line.new(x1 = rth_start, y1 = mid, x2 = rth_close, y2 = mid, xloc = xloc.bar_time, color = midColor, style = getLineStyle(midStyle), width = midWidth)
 
    // Draw Extension 1 (backpainted to 9:30)
    if showExt1
        line.new(x1 = rth_start, y1 = ext1High, x2 = rth_close, y2 = ext1High, xloc = xloc.bar_time, color = ext1Color, style = getLineStyle(ext1Style), width = ext1Width)
        line.new(x1 = rth_start, y1 = ext1Low, x2 = rth_close, y2 = ext1Low, xloc = xloc.bar_time, color = ext1Color, style = getLineStyle(ext1Style), width = ext1Width)
 
    // Draw Extension 2 (backpainted to 9:30)
    if showExt2
        line.new(x1 = rth_start, y1 = ext2High, x2 = rth_close, y2 = ext2High, xloc = xloc.bar_time, color = ext2Color, style = getLineStyle(ext2Style), width = ext2Width)
        line.new(x1 = rth_start, y1 = ext2Low, x2 = rth_close, y2 = ext2Low, xloc = xloc.bar_time, color = ext2Color, style = getLineStyle(ext2Style), width = ext2Width)
 
    rangeDrawn := true

← 返回列表