Opening Range

 avatar
EBTURK
plain_text
a year ago
3.4 kB
19
Indexable
//@version=5
indicator(title='ORB', shorttitle='Opening Range Breakout', overlay=true)

// First session settings
use_trade_session1 = input(true, title='Use First Session?')
trade_session1 = input.session(title='First Session (xxxx-xxxx to define an interval):', defval='0845-1015', confirm=false)
isinsession1 = use_trade_session1 ? not na(time('1', trade_session1)) : true
percent_range_multiplier1 = input(0.15, title='Range Multiplier for First Session')

// Second session settings
use_trade_session2 = input(true, title='Use Second Session?')
trade_session2 = input.session(title='Second Session (xxxx-xxxx to define an interval):', defval='1100-1230', confirm=false)
isinsession2 = use_trade_session2 ? not na(time('1', trade_session2)) : true
percent_range_multiplier2 = input(0.10, title='Range Multiplier for Second Session')

r = request.security(syminfo.tickerid, 'D', ta.atr(100)[1])
open_price = open
open_price := ta.change(use_trade_session1 and isinsession1 ? 1 : 0) > 0 ? open : open_price[1]

// Calculate open price for the second session
open_price := ta.change(use_trade_session2 and isinsession2 ? 1 : 0) > 0 ? open : open_price

buy_line1 = open_price + r * percent_range_multiplier1
sel_line1 = open_price - r * percent_range_multiplier1

buy_line2 = open_price + r * percent_range_multiplier2
sel_line2 = open_price - r * percent_range_multiplier2

plot(series=not isinsession1 and not isinsession2 ? na : open_price, title='Session Open Price', color=color.new(color.black, 0), linewidth=4, style=plot.style_linebr)

plot(series=not isinsession1 ? na : buy_line1, title='Buy Line for First Session', color=color.new(color.green, 0), linewidth=4, style=plot.style_linebr)
plot(series=not isinsession1 ? na : sel_line1, title='Sell Line for First Session', color=color.new(color.maroon, 0), linewidth=4, style=plot.style_linebr)

plot(series=not isinsession2 ? na : buy_line2, title='Buy Line for Second Session', color=color.new(color.blue, 0), linewidth=4, style=plot.style_linebr)
plot(series=not isinsession2 ? na : sel_line2, title='Sell Line for Second Session', color=color.new(color.red, 0), linewidth=4, style=plot.style_linebr)

buy_on_1st_bar = ta.change(open_price) != 0 and high > (isinsession1 ? buy_line1 : buy_line2)
sel_on_1st_bar = ta.change(open_price) != 0 and low < (isinsession1 ? sel_line1 : sel_line2)

buy_trigger = ta.crossover(close, (isinsession1 ? buy_line1 : buy_line2)) and (isinsession1 or isinsession2)
sel_trigger = ta.crossunder(close, (isinsession1 ? sel_line1 : sel_line2)) and (isinsession1 or isinsession2)

plotshape(buy_trigger, style=shape.labelup, color=color.new(color.green, 0), location=location.belowbar, textcolor=color.new(color.white, 0), text='Buy', title='Buy')
plotshape(sel_trigger, style=shape.labeldown, color=color.new(color.red, 0), location=location.abovebar, textcolor=color.new(color.white, 0), text='Sell', title='Sell')

plotarrow(sel_trigger ? -1 : na, title='SELL Arrow', colordown=color.new(color.red, 20), maxheight=100, minheight=50)
plotarrow(buy_trigger ? 1 : na, title='BUY Arrow', colordown=color.new(color.green, 20), maxheight=100, minheight=50)

alertcondition(buy_trigger, title='Buy Alert', message='Breakout Lines Possible Buy')
alertcondition(sel_trigger, title='Sell Alert', message='Breakout Lines Possible Sell')