Fibo On Open Price

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

// @DayTradingOil

//@version=4
study(title="User-Inputed Time Range & Fibs", shorttitle="TRF", overlay=true,
max_bars_back=1000, max_lines_count=500, max_labels_count=500)
// --> Inputs
chartColor = input(title="Chart Colors", defval="Dark Mode", options=["Dark Mode",
"Light Mode"])
sessionPeriod = input(title = "Time In Range", type = input.session, defval =
"0830-0930")
extend = input(title="Extend Targets?", defval=false)
endPlot = input(title="End Plots on New Day?", defval=false)
displayBG = input(true, title="Display Highlighted Background for Time In Range
Selection?")
showHigh = input(defval = true, title = 'Show High')
showHL2 = input(defval = true, title = 'Show HL2')

showLow = input(defval = true, title = 'Show Low')


showDaily = input(defval = true, title = "Show Daily Open")
showWeekly = input(defval = true, title = 'Show Weekly Open')
shoMonthly = input(defval = true, title = 'Show Monthly Open')
showYearly = input(defval = true, title = 'Show Yearly Open')
srcHi = input(high, "Source for Highs")
srcLo = input(low, "Source for Lows")

color color1 = chartColor == "Dark Mode" ? color.white : color.black


posX_Labels = 10

// --> Custom Function to Check if New Bar == True


// --> (https://www.tradingview.com/wiki/Sessions_and_Time_Functions)
is_newbar(res) =>
t = time(res)
change(t) != 0 ? 1 : 0

// --> Custom Function to Round Values to Tick from @PineCoders Backtesting/Trading


Engine
// --> (https://www.tradingview.com/script/dYqL95JB-Backtesting-Trading-Engine-
PineCoders/)
Round( _val, _decimals) =>
// Rounds _val to _decimals places.
_p = pow(10,_decimals)
round(abs(_val)*_p)/_p*sign(_val)

// --> Function Used to Round Prices to Tick Size Everywhere We Calculate Prices
RoundToTick( _price) => round(_price/syminfo.mintick)*syminfo.mintick

// --> Function to Check if Session is Active


inSession(sess) => na(time(timeframe.period, sess)) == false
sessionActive = inSession(sessionPeriod)
startSession() => sessionActive ? true : false

// --> Retrieve the Highest Highs/Lowest Lows from Session Range


// --> (https://www.pinecoders.com/faq_and_code/#how-can-i-track-highslows-for-a-
specific-period-of-time)
timeIsAllowed = time(timeframe.period, sessionPeriod)
var hi = 10e-10
var lo = 10e10
if timeIsAllowed
// We are entering allowed hours; reset hi/lo.
if not timeIsAllowed[1]
hi := srcHi
lo := srcLo
else
// We are in allowed hours; track hi/lo.
hi := max(srcHi, hi)
lo := min(srcLo, lo)

// --> Plot the Highest Highs/Lowest Lows from Session Range


rangeHigh = hi
rangeLow = lo
p1 = plot(displayBG and timeIsAllowed ? rangeHigh : na, title="Building Range
High", transp=100, style=plot.style_linebr)
p2 = plot(displayBG and timeIsAllowed ? rangeLow : na, title="Building Range Low",
transp=100, style=plot.style_linebr)
fill(p1, p2, color=color.red, transp=80)

// === Store the Highest Highs/Lowest Lows from Session Range ===
dayHigh = 0.0, dayHigh := startSession() ? rangeHigh : nz(dayHigh[1])
// if new day then new high else previous high
dayLow = 0.0, dayLow := startSession() ? rangeLow : nz(dayLow[1])
// if new day then new low else previous low
dayHL2 = 0.0, dayHL2 := startSession() ? (dayHigh + dayLow)/2 :
nz(dayHL2[1]) // if new day then new avg else previous avg

// === Extend Levels Above/Below ===


xl = (dayHigh - dayHL2) * 1 + dayHigh
xhl2 = (dayHigh - dayHL2) * 2 + dayHigh
xh = (dayHigh - dayHL2) * 3 + dayHigh
xhl21 = (dayHigh - dayHL2) * 4 + dayHigh
xh2 = (dayHigh - dayHL2) * 5 + dayHigh

yl = dayLow - (dayHigh - dayHL2) * 1


yhl2 = dayLow - (dayHigh - dayHL2) * 2
yh = dayLow - (dayHigh - dayHL2) * 3
yhl21 = dayLow - (dayHigh - dayHL2) * 4
yh1 = dayLow - (dayHigh - dayHL2) * 5

var endP = false


if endPlot == true
if is_newbar("D")
endP := true
if startSession()
endP := false

// === Plots ===


plotDH = plot(showHigh and not startSession() and not endP ? dayHigh : na,
title = 'Range High', color = color1, linewidth = 1, style = plot.style_linebr,
transp = 0)
plotDM = plot(showHL2 and not startSession() and not endP ? dayHL2 : na,
title = 'Range HL2', color = color1, linewidth = 1, style = plot.style_linebr,
transp = 0)
plotDL = plot(showLow and not startSession() and not endP ? dayLow : na,
title = 'Range Low', color = color1, linewidth = 1, style = plot.style_linebr,
transp = 0)
xplotDH = plot(extend and not startSession() and not endP ? xh : na, title = '+
Target 1', color = color1, linewidth = 1, style = plot.style_linebr, transp = 0)
xplotDM = plot(extend and not startSession() and not endP ? xhl2 : na, title
= '+ Target 2', color = color1, linewidth = 1, style = plot.style_linebr, transp =
0)
xplotDL = plot(extend and not startSession() and not endP ? xl : na, title =
'+ Target 3', color = color1, linewidth = 1, style = plot.style_linebr, transp =
0)
yplotDH = plot(extend and not startSession() and not endP ? yh : na, title = '+
Target 4', color = color1, linewidth = 1, style = plot.style_linebr, transp = 0)
yplotDM = plot(extend and not startSession() and not endP ? yhl2 : na, title
= '+ Target 5', color = color1, linewidth = 1, style = plot.style_linebr, transp =
0)
yplotDL = plot(extend and not startSession() and not endP ? yl : na, title =
'- Target 1', color = color1, linewidth = 1, style = plot.style_linebr, transp =
0)
xplotDH1 = plot(extend and not startSession() and not endP ? xh2 : na, title =
'- Target 2', color = color1, linewidth = 1, style = plot.style_linebr, transp =
0)
xplotDM1 = plot(extend and not startSession() and not endP ? xhl21 : na,
title = '- Target 3', color = color1, linewidth = 1, style = plot.style_linebr,
transp = 0)
yplotDH1 = plot(extend and not startSession() and not endP ? yh1 : na, title =
'- Target 4', color = color1, linewidth = 1, style = plot.style_linebr, transp =
0)
yplotDM1 = plot(extend and not startSession() and not endP ? yhl21 : na,
title = '- Target 5', color = color1, linewidth = 1, style = plot.style_linebr,
transp = 0)

fill(plotDH, plotDL, color=color1, transp=90)


dailyOpen = security(syminfo.tickerid, "D", open, barmerge.gaps_off,
barmerge.lookahead_on)
weeklyOpen = security(syminfo.tickerid, "W", open, barmerge.gaps_off,
barmerge.lookahead_on)
monthlyOpen = security(syminfo.tickerid, "M", open, barmerge.gaps_off,
barmerge.lookahead_on)
yearlyOpen = security(syminfo.tickerid, "12M", open, barmerge.gaps_off,
barmerge.lookahead_on)
plotDO1 = plot(showDaily and not is_newbar("D") ? dailyOpen : na, title = 'Daily
Open', color = color1, linewidth = 2, style = plot.style_linebr, transp = 20)
plotDO = plot(showWeekly and not is_newbar("W") ? weeklyOpen : na, title = 'Weekly
Open', color = color1, linewidth = 2, style = plot.style_linebr, transp = 20)
plotWO = plot(shoMonthly and not is_newbar("M") ? monthlyOpen : na, title = 'Montly
Open', color = color1, linewidth = 2, style = plot.style_linebr, transp = 20)
plotYO = plot(showYearly and not is_newbar("12M") ? yearlyOpen : na, title =
'Yearly Open', color = color1, linewidth = 2, style = plot.style_linebr, transp =
20)

// --> Labels
label1 = dayHigh// + buffer
label2 = dayHL2// + buffer
label3 = dayLow// + buffer
label4 = weeklyOpen
label5 = monthlyOpen
label6 = dailyOpen
label7 = yearlyOpen
label highLabel = na
label eqLabel = na
label lowLabel = na

l = "\n"
var float m = 0.0
length = input(title="Range Average Length Period", defval=5)
var a = array.new_float(length)
if startSession()[1] and not startSession()
m := dayHigh - dayLow
array.push(a, dayHigh-dayLow)
if array.size(a) > length
array.reverse(a)
array.pop(a)//emove(a, length + 1)
if array.size(a) == length
array.reverse(a)
b = array.avg(a)
c = label.new(time + time - time[1], dayHL2, xloc = xloc.bar_time, text="Average IB
Range: " + tostring(RoundToTick(b)) + l + "Current IB Range: " + tostring(m),
style=label.style_none, textcolor=color.white)
label.delete(c[1])
if not startSession()
if showHigh
highLabel := label.new(time+time-time[1], label1, text='RH',
style=label.style_none, xloc = xloc.bar_time, size=size.small, textcolor=color1)
label.delete(highLabel[1])
if showLow
lowLabel := label.new(time+time-time[1], label3, text='RL',
style=label.style_none, xloc = xloc.bar_time, size=size.small, textcolor=color1)
label.delete(lowLabel[1])
label dailyLabel = na
label dailyLabel1 = na
label weeklyLabel = na
label monthlyLabel = na
label yearlyLabel = na
if showDaily and dailyOpen == dailyOpen[1]
dailyLabel1 := label.new(time, label6, text='Daily Open Price',
style=label.style_none, xloc = xloc.bar_time, size=size.small, textcolor=color1)
label.delete(dailyLabel1[1])
if showWeekly and weeklyOpen == weeklyOpen[1]
dailyLabel := label.new(time-(time-time[3*posX_Labels]), label4, text='Weekly
Open Price', style=label.style_none, xloc = xloc.bar_time, size=size.small,
textcolor=color1)
label.delete(dailyLabel[1])
if shoMonthly and monthlyOpen == monthlyOpen[1]
monthlyLabel := label.new(time-(time-time[7*posX_Labels]), label5,
text='Monthly Open Price', style=label.style_none, xloc = xloc.bar_time,
size=size.small, textcolor=color1)
label.delete(monthlyLabel[1])
if showYearly and yearlyOpen == yearlyOpen[1]
yearlyLabel := label.new(time, label7, text='Yearly Open Price',
style=label.style_none, xloc = xloc.bar_time, size=size.small, textcolor=color1)
label.delete(yearlyLabel[1])

alertcondition(showHigh and not startSession() and not endP and crossover(close,


dayHigh), title='Close Crossed Above IB Range!', message='Close Crossed Above IB
Range!')
alertcondition(showLow and not startSession() and not endP and crossunder(close,
dayLow), title='Close Crossed Below IB Range!', message='Close Crossed Below IB
Range!')

You might also like