本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// Original Code: © asenski at gmail dot com
// Modded DeuceDavis (TheTradingHawk)
//@version=5
indicator("Opening Range with Fibs", "OR Fibs", overlay=true)
orSpec = input.session("0930-1000", "Opening Range with Fib Extensions")
refsym = input.symbol("SPX500USD", "Reference for time range (So that it is in EST)")
tradeEntrySession = input.session("1000-1130", "Trade Entry Times")
showPrior = input.bool(true, "Show Prior Boxes")
rgColor = input.color(color.rgb(255, 255, 0), "Range color")
L1Color = input.color(color.rgb(0, 0, 255), "L1 color")
L2Color = input.color(color.rgb(255, 115, 40), "L2 color")
L3Color = input.color(color.rgb(255, 0, 255), "L3 color")
L1 = input.float(1.272, "L1")
L2 = input.float(1.618, "L2")
L3 = input.float(2.618, "L3")
t = request.security(refsym, timeframe.period, time(timeframe.period, orSpec), gaps=barmerge.gaps_on)
z = request.security(refsym, timeframe.period, time(timeframe.period, tradeEntrySession), gaps=barmerge.gaps_on)
var float orh = na
var float orl = na
var int orbars = 0
var float extL1up = na
var float extL2up = na
var float extL3up = na
var float extL1dn = na
var float extL2dn = na
var float extL3dn = na
var float midPoint = na
var float tpValue = na
var float slValue = na
var bool longTrigger = false
var bool shortTrigger = false
var float entryValue = na
var float exitValue = na
var int longStops = 0
var int shortStops = 0
var int longWins = 0
var int shortWins = 0
var bool noTradeYet = true
var float maxProfit = 0.00
newSession = na(t[1]) and not na(t)
inSession = not na(t[1]) and not na(t)
endSession = not na(t[1]) and na(t)
inSessionToEnterTrade = not na(z[1]) and not na(z)
if newSession
orh := high
orl := low
orbars := 0
extL1up := na
extL2up := na
extL3up := na
extL1dn := na
extL2dn := na
extL3dn := na
midPoint := na
tpValue := na
slValue := na
entryValue := na
maxProfit := 0.0
longTrigger := false
shortTrigger := false
noTradeYet := true
if inSession
orh := math.max(high, orh)
orl := math.min(low, orl)
orbars += 1
var float vwapsum = na
var float volumesum = na
if endSession
extL1up := orl + (orh - orl) * L1
extL2up := orl + (orh - orl) * L2
extL3up := orl + (orh - orl) * L3
extL1dn := orh - (orh - orl) * L1
extL2dn := orh - (orh - orl) * L2
extL3dn := orh - (orh - orl) * L3
tpValue := (extL1up + extL1dn)/2
b1 = box.new(bar_index - orbars - 1, orh, bar_index - 1, orl, border_color = rgColor, bgcolor = color.new(color.gray, 50))
b2 = box.new(bar_index - orbars - 1, extL1up, bar_index - 1, extL2up, border_color = rgColor, bgcolor = color.new(color.red, 50))
b3 = box.new(bar_index - orbars - 1, extL1dn, bar_index - 1, extL2dn, border_color = rgColor, bgcolor = color.new(color.green, 50))
if barstate.islast
box.delete(b1[1])
box.delete(b2[1])
box.delete(b3[1])
if (not inSession) and (not longTrigger) and not (shortTrigger) and inSessionToEnterTrade and noTradeYet
longTrigger := low <= extL1dn
slValue := extL1dn - (extL1up - tpValue)
entryValue := extL1dn
if (not inSession) and (not shortTrigger) and (not longTrigger) and inSessionToEnterTrade and noTradeYet
shortTrigger := high >= extL1up
slValue := extL1up + (extL1up - tpValue)
entryValue := extL1up
if longTrigger
longTrigger := high >= tpValue ? false :
low <= slValue ? false : true
exitValue := high >= tpValue ? tpValue :
low <= slValue ? slValue : close
var stoppedOut = false
var profitOut = false
stoppedOut := low <= slValue
profitOut := high >= tpValue
longWins := profitOut ? longWins + 1 : longWins
longStops := stoppedOut ? longStops + 1 : longStops
noTradeYet := false
if shortTrigger
shortTrigger := low <= tpValue ? false :
high >= slValue ? false : true
exitValue := low <= tpValue ? tpValue :
high >= slValue ? slValue : close
var stoppedOut = false
var profitOut = false
stoppedOut := high >= slValue
profitOut := low <= tpValue
shortWins := profitOut ? shortWins + 1 : shortWins
shortStops := stoppedOut ? shortStops + 1 : shortStops
noTradeYet := false
profitTrigger = (shortWins + longWins) > (shortWins[1] + longWins[1])
stopTrigger = (shortStops + longStops) > (shortStops[1] + longStops[1])
colorForExit = profitTrigger ? color.new(color.green, 50) :
stopTrigger ? color.new(color.red, 50) : na
//bgcolor(colorForExit)
plot((not inSession) and longTrigger ? slValue : na, "Stop Loss", color=color.red, style=plot.style_linebr)
plot((not inSession) and longTrigger ? tpValue : na, "Take Profit", color=color.green, style=plot.style_linebr)
plot((not inSession) and shortTrigger ? slValue : na, "Stop Loss", color=color.red, style=plot.style_linebr)
plot((not inSession) and shortTrigger ? tpValue : na, "Take Profit", color=color.green, style=plot.style_linebr)
plot((not inSession) and (shortTrigger or longTrigger) ? exitValue : na, "Exit Value", color=color.yellow, style=plot.style_linebr)
plot((not inSession) and (shortTrigger or longTrigger) ? entryValue : na, "Entry Value", color=color.purple, style=plot.style_linebr)
inTrade = (not inSession) and (shortTrigger or longTrigger)
var float minLowSinceTrade = na
var float maxHiSinceTrade = na
var float closeSinceTrade = na
if not inTrade
minLowSinceTrade := na
maxHiSinceTrade := na
closeSinceTrade := na
if inTrade
minLowSinceTrade := na(minLowSinceTrade) ? low : math.min(minLowSinceTrade, low)
maxHiSinceTrade := na(maxHiSinceTrade) ? high : math.max(maxHiSinceTrade, high)
closeSinceTrade := close
plot(minLowSinceTrade, "Lowest Low Since Trade", color=color.new(color.blue, 100))
plot(maxHiSinceTrade, "Highest High Since Trade", color=color.new(color.blue, 100))
plot(closeSinceTrade, "Close Since Trade", color=color.new(color.blue, 100))
//plot(newSession ? na : orh, "OR high", color=rgColor,style=plot.style_linebr)
//plot(newSession ? na : orl, "OR low", color=rgColor,style=plot.style_linebr)
highExt3 = plot(newSession ? na : extL3up, "L3 above", color=L3Color, style=plot.style_linebr)
highExt2 = plot(newSession ? na : extL2up, "L2 above", color=L2Color, style=plot.style_linebr)
highExt1 = plot(newSession ? na : extL1up, "L1 above", color=L1Color, style=plot.style_linebr)
orhPlot = plot(newSession ? na : orh, "ORH", color=color.red, style=plot.style_linebr)
plot(tpValue, title="OR Midpoint (Target)", color=color.new(color.purple, 0), style=plot.style_cross, linewidth=1)
orlPlot = plot(newSession ? na : orl, "ORL", color=color.green, style=plot.style_linebr)
lowExt1 = plot(newSession ? na : extL1dn, "L1 below", color=L1Color, style=plot.style_linebr)
lowExt2 = plot(newSession ? na : extL2dn, "L2 below", color=L2Color, style=plot.style_linebr)
lowExt3 = plot(newSession ? na : extL3dn, "L3 below", color=L3Color, style=plot.style_linebr)
//plot(newSession ? na : extL3dn, "L3 below", color=L3Color, style=plot.style_linebr)
fill(highExt1, highExt2, color=inSessionToEnterTrade ? color.new(color.red, 50) : na, title="Put Zone")
fill(lowExt1, lowExt2, color=inSessionToEnterTrade ? color.new(color.green, 50) : na, title="Call Zone")
//alerts
extL1upTouched = ta.crossover(high, extL1up) and inSessionToEnterTrade
extL2upTouched = ta.crossover(high, extL2up) and inSessionToEnterTrade
extL3upTouched = ta.crossover(high, extL3up) and inSessionToEnterTrade
extL1dnTouched = ta.crossunder(low, extL1dn) and inSessionToEnterTrade
extL2dnTouched = ta.crossunder(low, extL2dn) and inSessionToEnterTrade
extL3dnTouched = ta.crossunder(low, extL3dn) and inSessionToEnterTrade
if extL1upTouched
alert("Opening Range Upper Fib 1 Touched - Short Opportunity 1 " + syminfo.ticker, alert.freq_once_per_bar)
if extL2upTouched
alert("Opening Range Upper Fib 2 Touched - Short Opportunity 2 " + syminfo.ticker, alert.freq_once_per_bar)
if extL3upTouched
alert("Opening Range Upper Fib 3 Touched - Short Opportunity 3 " + syminfo.ticker, alert.freq_once_per_bar)
if extL1dnTouched
alert("Opening Range Lower Fib 1 Touched - Long Opportunity 1 " + syminfo.ticker, alert.freq_once_per_bar)
if extL2dnTouched
alert("Opening Range Lower Fib 2 Touched - Long Opportunity 2 " + syminfo.ticker, alert.freq_once_per_bar)
if extL3dnTouched
alert("Opening Range Lower Fib 3 Touched - Long Opportunity 3 " + syminfo.ticker, alert.freq_once_per_bar)