Opening Range Breakout (ORB) with Fib Retracement

gggoaaat · study · 276 行 · 点赞 365 · 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/
// © gggoaaat
 
//@version=6
indicator("Opening Range Breakout (ORB) with Fib Retracement", "ORB", overlay = true)
 
Time_Group = "Orb Time Range"
sessionTime = input.session("0930-0945", title="Range", group = Time_Group, inline="1", tooltip = "Time Range Start to End")
sessionZone = input.string("GMT-4", "Zone", tooltip = "Timezone", options= ['GMT-12', 'GMT-11', 'GMT-10', 'GMT-9', 'GMT-8', 'GMT-7', 'GMT-6', 'GMT-5', 'GMT-4', 'GMT-3', 'GMT-2', 'GMT-1', 'GMT+0', 'GMT+1', 'GMT+2', 'GMT+3', 'GMT+4', 'GMT+5', 'GMT+6', 'GMT+7', 'GMT+8', 'GMT+9', 'GMT+10', 'GMT+11', 'GMT+12', 'GMT+13', 'GMT+14'], group = Time_Group, inline="1")
 
group_lines = "Line Management"
showAllLevels = input.bool(true, "Show All Fib levels", group = group_lines)
showExtendedLines = input.bool(true, "Show Extended Fib Retracement Lines", group = group_lines)
 
group_colors = "Orb Colors"
inputFibColor1 = input.color(color.green, "Buy", inline="1", group = group_colors)
inputFibColor2 = input.color(color.red, "Sell", inline="1", group = group_colors)
inputFibColor3 = input.color(color.rgb(120, 123, 134, 50), "Lines", inline="1", group = group_colors)
 
toInteger(_type)=>                                                           // casts float, integer, or string to integer
    forceString = str.format("{0}", _type)                                   // casts type to string
    stripCommas = str.replace_all(forceString, ",", "")                      // strip string commas
    int(str.tonumber(stripCommas)) 
 
InSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
    not na(time(timeframe.period, sessionTime, sessionTimeZone))
 
inSession    = InSession(sessionTime, sessionZone) and timeframe.isintraday
sessionStart = inSession and not inSession[1]
// === CALCULATE HIGH/LOW ===
 
var finalTop = high
var finalLow = low
 
var float highestPrice = na
var float lowestPrice  = na
if sessionStart
    highestPrice := high
    lowestPrice  := low
else if inSession
    highestPrice := math.max(highestPrice, high)
    lowestPrice  := math.min(lowestPrice, low)
else
    finalTop := highestPrice
    finalLow := lowestPrice
 
// Fibonacci levels
fibLevels = array.new_float()
array.push(fibLevels, 0.0)
array.push(fibLevels, 0.236)
array.push(fibLevels, 0.382)
array.push(fibLevels, 0.5 )
array.push(fibLevels, 0.618)
array.push(fibLevels, 0.786)
array.push(fibLevels, 1)
array.push(fibLevels, input.float(1.618,  "Fib Val", inline="1", group = "Buy Retracement Values"))
array.push(fibLevels, input.float(2.618,  "Fib Val", inline="1", group = "Buy Retracement Values"))
array.push(fibLevels, input.float(3.618,  "Fib Val", inline="2", group = "Buy Retracement Values"))
array.push(fibLevels, input.float(4.618,  "Fib Val", inline="2", group = "Buy Retracement Values"))
array.push(fibLevels, input.float(-1.618, "Fib Val", inline="3", group = "Sell Retracement Values"))
array.push(fibLevels, input.float(-2.618, "Fib Val", inline="3", group = "Sell Retracement Values"))
array.push(fibLevels, input.float(-3.618, "Fib Val", inline="4", group = "Sell Retracement Values"))
array.push(fibLevels, input.float(-4.618, "Fib Val", inline="4", group = "Sell Retracement Values"))
 
// Calculate retracement levels
 
GetRange(_high, _low) =>
    retracementLevels1 = array.new_float()
    for i = 0 to array.size(fibLevels) - 1
        fib = array.get(fibLevels, i)
        if fib >= 0
            level = _low + (_high - _low) * fib
            array.push(retracementLevels1, level)
        else
            level = _high + (_high - _low) * fib
            array.push(retracementLevels1, level)
    
    retracementLevels1
 
retracementLevels = GetRange(finalTop, finalLow)
 
 
valueU4326  = array.get(retracementLevels, 10)
valueU3618  = array.get(retracementLevels, 09)
valueU2618  = array.get(retracementLevels, 08)
valueU1618  = array.get(retracementLevels, 07)
 
value236    = array.get(retracementLevels, 01)
value382    = array.get(retracementLevels, 02)
value500    = array.get(retracementLevels, 03)
value618    = array.get(retracementLevels, 04)
value786    = array.get(retracementLevels, 05)
 
valueD1618  = array.get(retracementLevels, 11)
valueD2618  = array.get(retracementLevels, 12)
valueD3618  = array.get(retracementLevels, 13)
valueD4326  = array.get(retracementLevels, 14)
 
retracementLevelsBelow1 = GetRange(finalLow, valueD1618)
retracementLevelsBelow2 = GetRange(valueD1618, valueD2618)
retracementLevelsBelow3 = GetRange(valueD2618, valueD3618)
retracementLevelsBelow4 = GetRange(valueD3618, valueD4326)
 
retracementLevelsAbove1 = GetRange(finalTop, valueU1618)
retracementLevelsAbove2 = GetRange(valueU1618, valueU2618)
retracementLevelsAbove3 = GetRange(valueU2618, valueU3618)
retracementLevelsAbove4 = GetRange(valueU3618, valueU4326)
 
// isItAboveHigh = ta.highest(60) > finalTop
// isItBelowLow =  ta.lowest(60) < finalLow
// ifAboveLevel1 = isItAboveHigh //and high > array.get(retracementLevels, 07) 
// ifAboveLevel2 = ta.highest(60) > valueU1618
// ifAboveLevel3 = ta.highest(60) > valueU2618
// ifAboveLevel4 = ta.highest(60) > valueU3618
 
// ifBelowLevel1 = isItBelowLow //and low < valueD1618
// ifBelowLevel2 = ta.lowest(60) < valueD1618
// ifBelowLevel3 = ta.lowest(60) < valueD2618
// ifBelowLevel4 = ta.lowest(60) < valueD3618
 
 
// // === PLOT ===
plotHigh = plot(finalTop, "Top", color=color.green, linewidth=4, trackprice = true, style = plot.style_steplinebr)
plotLow = plot(finalLow,  "Bottom", color=color.red, linewidth=4, trackprice = true, style = plot.style_steplinebr)
 
plotFib01 = plot(showAllLevels ? value236 : na, color=inputFibColor3, title="Fib 23.6%",   linewidth=1, trackprice = false, style = plot.style_steplinebr)
plotFib02 = plot(showAllLevels ? value382 : na, color=inputFibColor3, title="Fib 38.2%",   linewidth=1, trackprice = false, style = plot.style_steplinebr)
plotFib03 = plot(showAllLevels ? value500 : na, color=inputFibColor3, title="Fib 50.00%",  linewidth=1, trackprice = false, style = plot.style_steplinebr)
plotFib04 = plot(showAllLevels ? value618 : na, color=inputFibColor3, title="Fib 61.8%",   linewidth=1, trackprice = false, style = plot.style_steplinebr)
plotFib05 = plot(showAllLevels ? value786 : na, color=inputFibColor3, title="Fib 78.6%",   linewidth=1, trackprice = false, style = plot.style_steplinebr)
 
plotFib07 = plot(showAllLevels ? valueU1618 : na, color=inputFibColor1, title="1st Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
plotFib08 = plot(showAllLevels ? valueU2618 : na, color=inputFibColor1, title="2nd Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
plotFib09 = plot(showAllLevels ? valueU3618 : na, color=inputFibColor1, title="3rd Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
plotFib10 = plot(showAllLevels ? valueU4326 : na, color=inputFibColor1, title="4th Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
plotFib11 = plot(showAllLevels ? valueD1618 : na, color=inputFibColor2, title="1st Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
plotFib12 = plot(showAllLevels ? valueD2618 : na, color=inputFibColor2, title="2nd Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
plotFib13 = plot(showAllLevels ? valueD3618 : na, color=inputFibColor2, title="3rd Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
plotFib14 = plot(showAllLevels ? valueD4326 : na, color=inputFibColor2, title="4th Level",   linewidth=2, trackprice = true, style = plot.style_steplinebr)
 
plotLow1Fib03  = plot(showAllLevels ? array.get(retracementLevelsBelow1, 03) : na, style=plot.style_steplinebr,  color=inputFibColor3, title="LL1 50.00%", linewidth=1, trackprice = false)
plotHigh1Fib03 = plot(showAllLevels ? array.get(retracementLevelsAbove1, 03) : na, style=plot.style_steplinebr,  color=inputFibColor3, title="HL1 50.00%", linewidth=1, trackprice = false)
plotLow2Fib03  = plot(showAllLevels ? array.get(retracementLevelsBelow2, 03) : na, style=plot.style_steplinebr,  color=inputFibColor3, title="LL2 50.00%", linewidth=1, trackprice = false)
plotHigh2Fib03 = plot(showAllLevels ? array.get(retracementLevelsAbove2, 03) : na, style=plot.style_steplinebr,  color=inputFibColor3, title="HL2 50.00%", linewidth=1, trackprice = false)
plotLow3Fib03  = plot(showAllLevels ? array.get(retracementLevelsBelow3, 03) : na , style=plot.style_steplinebr, color=inputFibColor3, title="Fib LL3 50.00%", linewidth=1, trackprice = false)
plotHigh3Fib03 = plot(showAllLevels ? array.get(retracementLevelsAbove3, 03) : na , style=plot.style_steplinebr, color=inputFibColor3, title="Fib HL3 50.00%", linewidth=1, trackprice = false)
plotLow4Fib03  = plot(showAllLevels ? array.get(retracementLevelsBelow4, 03) : na , style=plot.style_steplinebr, color=inputFibColor3, title="Fib LL4 50.00%", linewidth=1, trackprice = false)
plotHigh4Fib03 = plot(showAllLevels ? array.get(retracementLevelsAbove4, 03) : na , style=plot.style_steplinebr, color=inputFibColor3, title="Fib HL4 50.00%", linewidth=1, trackprice = false)
 
 
drawLineToNextDay(level, title, _color) =>
    var line lineID = na
    // Calculate the time of the next day
    nextDay = timestamp(year, month, dayofmonth + 14, 0, 0)
    lineID := line.new(x1=bar_index + 1, y1=level, x2=bar_index + 2, y2=level, width=1, color=_color, extend = extend.right, style = line.style_solid)
    lineID
 
 
if showExtendedLines
    extLineFibU1236 = drawLineToNextDay(array.get(retracementLevelsAbove1, 01), " ", inputFibColor3)
    extLineFibU2236 = drawLineToNextDay(array.get(retracementLevelsAbove2, 01), " ", inputFibColor3)
    extLineFibU3236 = drawLineToNextDay(array.get(retracementLevelsAbove3, 01), " ", inputFibColor3)
    extLineFibU4236 = drawLineToNextDay(array.get(retracementLevelsAbove4, 01), " ", inputFibColor3)
    
    extLineFibD1236 = drawLineToNextDay(array.get(retracementLevelsBelow1, 01), " ", inputFibColor3)
    extLineFibD2236 = drawLineToNextDay(array.get(retracementLevelsBelow2, 01), " ", inputFibColor3)
    extLineFibD3236 = drawLineToNextDay(array.get(retracementLevelsBelow3, 01), " ", inputFibColor3)
    extLineFibD4236 = drawLineToNextDay(array.get(retracementLevelsBelow4, 01), " ", inputFibColor3)
    
    extLineFibU1382 = drawLineToNextDay(array.get(retracementLevelsAbove1, 02), " ", inputFibColor3)
    extLineFibU2382 = drawLineToNextDay(array.get(retracementLevelsAbove2, 02), " ", inputFibColor3)
    extLineFibU3382 = drawLineToNextDay(array.get(retracementLevelsAbove3, 02), " ", inputFibColor3)
    extLineFibU4382 = drawLineToNextDay(array.get(retracementLevelsAbove4, 02), " ", inputFibColor3)
    
    extLineFibD1382 = drawLineToNextDay(array.get(retracementLevelsBelow1, 02), " ", inputFibColor3)
    extLineFibD2382 = drawLineToNextDay(array.get(retracementLevelsBelow2, 02), " ", inputFibColor3)
    extLineFibD3382 = drawLineToNextDay(array.get(retracementLevelsBelow3, 02), " ", inputFibColor3)
    extLineFibD4382 = drawLineToNextDay(array.get(retracementLevelsBelow4, 02), " ", inputFibColor3)
    
    extLineFibU1500 = drawLineToNextDay(array.get(retracementLevelsAbove1, 03), " ", inputFibColor3)
    extLineFibU2500 = drawLineToNextDay(array.get(retracementLevelsAbove2, 03), " ", inputFibColor3)
    extLineFibU3500 = drawLineToNextDay(array.get(retracementLevelsAbove3, 03), " ", inputFibColor3)
    extLineFibU4500 = drawLineToNextDay(array.get(retracementLevelsAbove4, 03), " ", inputFibColor3)
    
    extLineFibD1500 = drawLineToNextDay(array.get(retracementLevelsBelow1, 03), " ", inputFibColor3)
    extLineFibD2500 = drawLineToNextDay(array.get(retracementLevelsBelow2, 03), " ", inputFibColor3)
    extLineFibD3500 = drawLineToNextDay(array.get(retracementLevelsBelow3, 03), " ", inputFibColor3)
    extLineFibD4500 = drawLineToNextDay(array.get(retracementLevelsBelow4, 03), " ", inputFibColor3)
    
    extLineFibU1618 = drawLineToNextDay(array.get(retracementLevelsAbove1, 04), " ", inputFibColor3)
    extLineFibU2618 = drawLineToNextDay(array.get(retracementLevelsAbove2, 04), " ", inputFibColor3)
    extLineFibU3618 = drawLineToNextDay(array.get(retracementLevelsAbove3, 04), " ", inputFibColor3)
    extLineFibU4618 = drawLineToNextDay(array.get(retracementLevelsAbove4, 04), " ", inputFibColor3)
    
    extLineFibD1618 = drawLineToNextDay(array.get(retracementLevelsBelow1, 04), " ", inputFibColor3)
    extLineFibD2618 = drawLineToNextDay(array.get(retracementLevelsBelow2, 04), " ", inputFibColor3)
    extLineFibD3618 = drawLineToNextDay(array.get(retracementLevelsBelow3, 04), " ", inputFibColor3)
    extLineFibD4618 = drawLineToNextDay(array.get(retracementLevelsBelow4, 04), " ", inputFibColor3)
    
    extLineFibU1786 = drawLineToNextDay(array.get(retracementLevelsAbove1, 05), " ", inputFibColor3)
    extLineFibU2786 = drawLineToNextDay(array.get(retracementLevelsAbove2, 05), " ", inputFibColor3)
    extLineFibU3786 = drawLineToNextDay(array.get(retracementLevelsAbove3, 05), " ", inputFibColor3)
    extLineFibU4786 = drawLineToNextDay(array.get(retracementLevelsAbove4, 05), " ", inputFibColor3)
    
    extLineFibD1786 = drawLineToNextDay(array.get(retracementLevelsBelow1, 05), " ", inputFibColor3)
    extLineFibD2786 = drawLineToNextDay(array.get(retracementLevelsBelow2, 05), " ", inputFibColor3)
    extLineFibD3786 = drawLineToNextDay(array.get(retracementLevelsBelow3, 05), " ", inputFibColor3)
    extLineFibD4786 = drawLineToNextDay(array.get(retracementLevelsBelow4, 05), " ", inputFibColor3)
 
    
    extLineFib00 = drawLineToNextDay(finalTop, "",   inputFibColor3)
    extLineFib01 = drawLineToNextDay(value236 , " ", inputFibColor3)
    extLineFib02 = drawLineToNextDay(value382 , " ", inputFibColor3)
    extLineFib03 = drawLineToNextDay(value500, " ",  inputFibColor3)
    extLineFib04 = drawLineToNextDay(value618, " ",  inputFibColor3)
    extLineFib05 = drawLineToNextDay(value786, " ",  inputFibColor3)
    extLineFib06 = drawLineToNextDay(finalLow, " ",  inputFibColor3)
    
    line.delete(extLineFibU1236[1])
    line.delete(extLineFibU2236[1])
    line.delete(extLineFibU3236[1])
    line.delete(extLineFibU4236[1])
    
    line.delete(extLineFibD1236[1])
    line.delete(extLineFibD2236[1])
    line.delete(extLineFibD3236[1])
    line.delete(extLineFibD4236[1])
    
    line.delete(extLineFibU1382[1])
    line.delete(extLineFibU2382[1])
    line.delete(extLineFibU3382[1])
    line.delete(extLineFibU4382[1])
    
    line.delete(extLineFibD1382[1])
    line.delete(extLineFibD2382[1])
    line.delete(extLineFibD3382[1])
    line.delete(extLineFibD4382[1])
    
    line.delete(extLineFibU1500[1])
    line.delete(extLineFibU2500[1])
    line.delete(extLineFibU3500[1])
    line.delete(extLineFibU4500[1])
    
    line.delete(extLineFibD1500[1])
    line.delete(extLineFibD2500[1])
    line.delete(extLineFibD3500[1])
    line.delete(extLineFibD4500[1])
    
    line.delete(extLineFibU1618[1])
    line.delete(extLineFibU2618[1])
    line.delete(extLineFibU3618[1])
    line.delete(extLineFibU4618[1])
    
    line.delete(extLineFibD1618[1])
    line.delete(extLineFibD2618[1])
    line.delete(extLineFibD3618[1])
    line.delete(extLineFibD4618[1])
    
    
    line.delete(extLineFibU1786[1])
    line.delete(extLineFibU2786[1])
    line.delete(extLineFibU3786[1])
    line.delete(extLineFibU4786[1])
    
    line.delete(extLineFibD1786[1])
    line.delete(extLineFibD2786[1])
    line.delete(extLineFibD3786[1])
    line.delete(extLineFibD4786[1])
    
    line.delete(extLineFib00[1])
    line.delete(extLineFib01[1])
    line.delete(extLineFib02[1])
    line.delete(extLineFib03[1])
    line.delete(extLineFib04[1])
    line.delete(extLineFib05[1])
    line.delete(extLineFib06[1])

← 返回列表