ORB opening range breakout

nkwdesmond · study · 186 行 · 点赞 207 · 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/
// © nkwdesmond
 
//@version=6
indicator('ORB opening range breakout', 'ORB', max_lines_count = 500, max_labels_count = 500, overlay = true)
 
timeRange = input.int(5, title = 'ORB time (minutes)', minval = 1, group = 'ORB', inline='ORB')
ORBBodyBool = input.bool(true, title = 'ORB region', group = 'ORB', inline='ORB')
ORBColour = input(title = '', defval = color.rgb(227, 166, 83, transp = 75), group = 'ORB', inline='ORB')
var int timeRangeMilli = timeRange * 60 * 1000
 
levelSettingOption = input.string('Individual level for each day', title = 'Display', options = ['Individual level for each day', 'Most recent level on most recent day', 'Most recent level across the chart'], group = 'Price level', display = display.none)
levelSetting = levelSettingOption == 'Most recent level on most recent day' ? 'b' : levelSettingOption == 'Most recent level across the chart' ? 'c' : 'a'
 
ORBHighLevel = input.bool(true, title = 'ORB high', group = 'Price level', inline = 'ORBHigh')
ORBHighPrice = input.bool(true, title = 'ORB high', group = 'Price axis')
ORBHighColour = input(title = '', defval = color.rgb(227, 166, 83, transp = 100), group = 'Price level', inline = 'ORBHigh')
ORBHighLineWidth = input(1, 'Width', group = 'Price level', inline = 'ORBHigh', display = display.none)
ORBHighStyleOption = input.string('solid (─)', title = '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group = 'Price level', inline = 'ORBHigh', display = display.none)
ORBHighLineStyle = ORBHighStyleOption == 'dotted (┈)' ? line.style_dotted : ORBHighStyleOption == 'dashed (╌)' ? line.style_dashed : ORBHighStyleOption == 'arrow left (←)' ? line.style_arrow_left : ORBHighStyleOption == 'arrow right (→)' ? line.style_arrow_right : ORBHighStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
 
ORBLowLevel = input.bool(true, title = 'ORB low', group = 'Price level', inline = 'ORBLow')
ORBLowPrice = input.bool(true, title = 'ORB low', group = 'Price axis')
ORBLowColour = input(title = '', defval = color.rgb(227, 166, 83, transp = 100), group = 'Price level', inline = 'ORBLow')
ORBLowLineWidth = input(1, 'Width', group = 'Price level', inline = 'ORBLow', display = display.none)
ORBLowStyleOption = input.string('solid (─)', title = '', options = ['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group = 'Price level', inline = 'ORBLow', display = display.none)
ORBLowLineStyle = ORBLowStyleOption == 'dotted (┈)' ? line.style_dotted : ORBLowStyleOption == 'dashed (╌)' ? line.style_dashed : ORBLowStyleOption == 'arrow left (←)' ? line.style_arrow_left : ORBLowStyleOption == 'arrow right (→)' ? line.style_arrow_right : ORBLowStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
 
LabelBool = input.bool(false, title = 'Enable labels (Alternatively go to Chart settings > Scales and lines > Indicators and financials > check Name to display label on price axis)', group = 'Label')
 
GetORB() => // going into a 1min chart and running this entire function
    var float ORBHigh = na
    var float ORBLow = na
    var int StartTime = na // get the UNIX time for the first bar
    var int EndTime = na
    if session.isfirstbar_regular
        StartTime := time
        EndTime := time + timeRangeMilli
        ORBHigh := high
        ORBLow := low
    else if StartTime <= time and time < EndTime // check time again in function in the event main chart timeframe is higher than ORB time range
        ORBHigh := na(ORBHigh) ? high : math.max(ORBHigh, high)
        ORBLow := na(ORBLow) ? low : math.min(ORBLow, low)
    [ORBHigh, ORBLow]
[ORBHigh, ORBLow] = request.security(syminfo.tickerid, "1", GetORB(), lookahead=barmerge.lookahead_on)
// plot(ORBLow , style=plot.style_line, color=color.green, title="ORB High", linewidth=2)
 
// var float ORBHigh = na
// var float ORBLow = na
 
if timeframe.isintraday
    var line ORBHighLine = na
    var line ORBLowLine = na
 
    var box ORBBody = na
    
    var int StartTime = na
    var int EndTime = na
 
    if bar_index == 0
        if ORBHighLevel // bar_index +1 because there is a glitch where when both x-values are the same, a vertical line ends up forming
            ORBHighLine := line.new(bar_index, ORBHigh, bar_index + 1, ORBHigh, color = ORBHighColour, style = ORBHighLineStyle, width = ORBHighLineWidth, extend = levelSetting == 'c' ? extend.both : extend.right)
        if ORBLowLevel
            ORBLowLine := line.new(bar_index, ORBLow, bar_index + 1, ORBLow, color = ORBLowColour, style = ORBLowLineStyle, width = ORBLowLineWidth, extend = levelSetting == 'c' ? extend.both : extend.right)
        if ORBBodyBool
            ORBBody := box.new(bar_index, ORBHigh, bar_index + 1, ORBLow, border_color = na, bgcolor = ORBColour)
 
    if session.isfirstbar_regular // ORBHighLine line should only be drawn starting from first regular bar
        // StartTime := time // issue in hourly chart as first half hour is not treated as market hour
        // EndTime := time + timeRangeMilli
        // [oh, ol] = request.security(syminfo.tickerid, "1", GetORB())
        // ORBHigh := oh
        // ORBLow := ol
        if levelSetting != 'a' // adjust the old lines if option 'a' is not selected
            line.set_x1(ORBHighLine, bar_index)
            line.set_x2(ORBHighLine, bar_index + 1)
            line.set_y1(ORBHighLine, ORBHigh)
            line.set_y2(ORBHighLine, ORBHigh)
 
            line.set_x1(ORBLowLine, bar_index)
            line.set_x2(ORBLowLine, bar_index + 1)
            line.set_y1(ORBLowLine, ORBLow)
            line.set_y2(ORBLowLine, ORBLow)
 
            box.set_left(ORBBody, bar_index)
            box.set_right(ORBBody, bar_index + 1)
            box.set_top(ORBBody, ORBHigh)
            box.set_bottom(ORBBody, ORBLow)
        else // option 'a' is selected
            line.set_extend(ORBHighLine, extend.none)
            line.set_extend(ORBLowLine, extend.none)
            if ORBHighLevel // bar_index +1 because there is a glitch where when both x-values are the same, a vertical line ends up forming
                ORBHighLine := line.new(bar_index, ORBHigh, bar_index + 1, ORBHigh, color = ORBHighColour, style = ORBHighLineStyle, width = ORBHighLineWidth, extend = levelSetting == 'c' ? extend.both : extend.right)
            if ORBLowLevel
                ORBLowLine := line.new(bar_index, ORBLow, bar_index + 1, ORBLow, color = ORBLowColour, style = ORBLowLineStyle, width = ORBLowLineWidth, extend = levelSetting == 'c' ? extend.both : extend.right)
            if ORBBodyBool
                ORBBody := box.new(bar_index, ORBHigh, bar_index + 1, ORBLow, border_color = na, bgcolor = ORBColour)
    if not session.isfirstbar_regular and session.ismarket
        // if StartTime <= time and time < EndTime // issue in hourly chart as first half hour is not treated as market hour
        //     [oh, ol] = request.security(syminfo.tickerid, "1", GetORB())
        //     ORBHigh := oh
        //     ORBLow := ol
        if ORBHigh != ORBHigh[1]
            line.set_y1(ORBHighLine, ORBHigh) // shift ORBHigh in real-time
            line.set_y2(ORBHighLine, ORBHigh)
            box.set_top(ORBBody, ORBHigh)
        if ORBLow != ORBLow[1]
            line.set_y1(ORBLowLine, ORBLow) // shift ORBLow in real-time
            line.set_y2(ORBLowLine, ORBLow)
            box.set_bottom(ORBBody, ORBLow)
        line.set_x2(ORBHighLine, bar_index)
        line.set_x2(ORBLowLine, bar_index)
        box.set_right(ORBBody, bar_index)
 
    if LabelBool
        ORBHighLabelBool = input.bool(true, title = 'ORB high', group = 'Label', inline = 'ORBHigh')
        ORBHighLabelPriceBool = input.bool(true, title = 'Display price in label', group = 'Label', inline = 'ORBHigh')
        ORBHighLabelAlignOption = input.string('align left', title = '', options = ['align left', 'align center', 'align right'], group = 'Label', inline = 'ORBHigh', display = display.none)
        ORBHighLabelAlign = ORBHighLabelAlignOption == 'align left' ? text.align_left : ORBHighLabelAlignOption == 'align center' ? text.align_center : text.align_right
        ORBHighLabelText = input.string('ORB high ', title = 'Text', group = 'Label', inline = 'ORBHigh', display = display.none)
        ORBHighLabelTextColour = input(title = '', defval = #000000, group = 'Label', inline = 'ORBHigh')
        ORBHighLabelTextSize = input.int(12, title = '', minval = 1, group = 'Label', display = display.none, inline = 'ORBHigh')
        ORBHighOffset = input.int(0, title = 'Bar offset', group = 'Label', display = display.none, inline = 'ORBHigh')
 
        ORBLowLabelBool = input.bool(true, title = 'ORB low', group = 'Label', inline = 'ORBLow')
        ORBLowLabelPriceBool = input.bool(true, title = 'Display price in label', group = 'Label', inline = 'ORBLow')
        ORBLowLabelAlignOption = input.string('align left', title = '', options = ['align left', 'align center', 'align right'], group = 'Label', inline = 'ORBLow', display = display.none)
        ORBLowLabelAlign = ORBLowLabelAlignOption == 'align left' ? text.align_left : ORBLowLabelAlignOption == 'align center' ? text.align_center : text.align_right
        ORBLowLabelText = input.string('ORB low ', title = 'Text', group = 'Label', inline = 'ORBLow', display = display.none)
        ORBLowLabelTextColour = input(title = '', defval = #000000, group = 'Label', inline = 'ORBLow')
        ORBLowLabelTextSize = input.int(12, title = '', minval = 1, group = 'Label', display = display.none, inline = 'ORBLow')
        ORBLowOffset = input.int(0, title = 'Bar offset', group = 'Label', display = display.none, inline = 'ORBLow')
 
        var label ORBHighLabel = na
        var label ORBLowLabel = na
 
        if bar_index == 0
            if ORBHighLabelBool
                ORBHighLabel := label.new(bar_index + ORBHighOffset, ORBHigh, text = ORBHighLabelPriceBool ? ORBHighLabelText + str.tostring(ORBHigh, format.mintick) : ORBHighLabelText, color = ORBHighColour, style = label.style_label_left, textcolor = ORBHighLabelTextColour, size = ORBHighLabelTextSize, textalign = ORBHighLabelAlign)
            if ORBLowLabelBool
                ORBLowLabel := label.new(bar_index + ORBLowOffset, ORBLow, text = ORBLowLabelPriceBool ? ORBLowLabelText + str.tostring(ORBLow, format.mintick) : ORBLowLabelText, color = ORBLowColour, style = label.style_label_left, textcolor = ORBLowLabelTextColour, size = ORBLowLabelTextSize, textalign = ORBLowLabelAlign)
 
        if session.ismarket
            if ORBHigh != ORBHigh[1]
                label.set_y(ORBHighLabel, ORBHigh)
                if ORBHighLabelPriceBool
                    label.set_text(ORBHighLabel, ORBHighLabelText + str.tostring(ORBHigh, format.mintick))
            if ORBLow != ORBLow[1]
                label.set_y(ORBLowLabel, ORBLow)
                if ORBLowLabelPriceBool
                    label.set_text(ORBLowLabel, ORBLowLabelText + str.tostring(ORBLow, format.mintick))
        
        label.set_x(ORBHighLabel, bar_index + ORBHighOffset)
        label.set_x(ORBLowLabel, bar_index + ORBLowOffset)
 
plot(timeframe.isintraday and ORBHighPrice ? ORBHigh : na, title = 'High', color = ORBHighColour, editable = false, display = display.price_scale)
plot(timeframe.isintraday and ORBLowPrice ? ORBLow : na, title = 'Low', color = ORBLowColour, editable = false, display = display.price_scale)
 
BBBool = input.bool(false, title = 'Bollinger Band', group = 'BB')
length = input.int(20, minval=1, group = 'BB')
maType = input.string("SMA", "Basis MA Type", options = ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group = 'BB')
src = input(close, title="Source", group = 'BB')
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev", group = 'BB')
offset = input.int(0, "Offset", minval = -500, maxval = 500, display = display.data_window, group = 'BB')
 
ma(source, length, _type) =>
    switch _type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
 
var series float basis = na
var series float upper = na
var series float lower = na
 
if BBBool
    basis := ma(src, length, maType)
    dev = mult * ta.stdev(src, length)
    upper := basis + dev
    lower := basis - dev
plot(BBBool ? basis : na, "Basis", color=#2962FF, offset = offset)
p1 = plot(BBBool ? upper : na, "Upper", color=#F23645, offset = offset)
p2 = plot(BBBool ? lower : na, "Lower", color=#089981, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

← 返回列表