Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Code Snippet: Tradingview Pinescript Code for Open Range Breakout

2 min read

Here is the Tradingview Pinescript code for open range breakout. It a prototype code to build your own indicator/trading system on top of open range breakout levels. Traders can now build backtestable trading systems on top this pinescript code snippet.

Open Range Breakout is a simple strategy that monitors the first 5min / 15min / 15min / 30min / 60min range from the start of the market. From the opening high range and low range is calculated for the specified timeframe. Most of the Pinescript code found on the internet is either buggy or not suitable for building back-testable trading systems.

To solve this issue we are releasing a prototype for ORB – Open Range Breakout Levels.

By default, the code uses a 60min range breakout from the market open. However, Parameters can be changed from the Parameter window. Currently Open Range breakout options are available for various timeframes.

Note : If your charting timeframe is 5min then higher resolution can be accessed from the Resolution Input section. And Ensure while setting the Breakout Timings are set from the start of the exchange timings and ends at the resolution timings. For example if in case you want to monitor first 15min breakout levels then Resolution should be set to 15min and Breakout Timings to 0915 – 0930 range

However, if someone needs a customized ORB range then code customization needs to be done accordingly on top of the code.

Code will start plotting the Open Range values only if the Open Range timeframe is completed else nothing will be plotted. For example for an Open Range value of 60min. The plot will start only from 10:15:00 onwards for NSE Futures instruments and will start from 11:00:00 onwards for MCX futures instruments automatically.

Alerts are also added to set automated alerts and can be later triggered into orders. If in case you are new to Automated Orders in Tradingview check out this tutorial

Tradingview Pinescript Code – Open Range Breakout with Alerts

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © marketcalls_in
// Founder - Marketcalls (www.marketcalls.in)
// Co-Founder - Algomojo (www.algomojo.com)

//@version=4
study("Open Range Breakout",overlay=true)

tf = input(title="Resolution",type = input.resolution,defval = "60")  //which timeframe you want to move
session = input(title="Breakout Timings",defval="0915-1015", type=input.session)
linew = input(title="Line Width",type = input.integer, defval=2,minval=1,maxval=5)


//is the market trading in between the session timings? If yes then it returns true
Barsinsession(session) => time(tf,session) != 0  //single line function

Insession = Barsinsession(session) ? 1 : 0
endofsession = Insession == 0 and Insession[1] == 1  //end of the session if it is happening -> returns ture

//high value and low value from 60min timeframe charts
hi = security(syminfo.tickerid,tf,high)  
lo = security(syminfo.tickerid,tf,low)


orbh = valuewhen(endofsession,hi,0)  //first time computation
orbl = valuewhen(endofsession,lo,0)

orbh := Barsinsession(session) ? na : orbh   //rewriting the value storing a different value
orbl := Barsinsession(session) ? na : orbl   //rewriting the value storing a different value


Buy = crossover(high,orbh)
Short = crossunder(low,orbl)

//flip
Buycontinue = barssince(Buy) < barssince(Short)
Shortcontinue = barssince(Short) < barssince(Buy)

newday = dayofmonth != dayofmonth[1]


var isLong = false
var isShort = false

//exrem
Buy := not isLong and Buy
Short := not isShort and Short

if Buy
    isLong := true
    isShort := false

if Short
    isLong := false
    isShort := true
    
if newday[1]
    isLong := false
    isShort := false
    
//indicates the buy or sell signal
plotshape(Buy,style=shape.labelup,location = location.belowbar,color=color.green,title = "Buy",text="Buy",textcolor=color.white)
plotshape(Short,style=shape.labeldown,location = location.abovebar,color=color.red,title = "Sell",text="Sell",textcolor=color.white)


alertcondition(Buy, title='Buy Alert', message='Buy Signal!')
alertcondition(Short, title='Short Alert', message='Short Signal!')

plot(orbh,color=color.red,style=plot.style_circles,linewidth=linew)
plot(orbl,color=color.lime,style=plot.style_circles,linewidth=linew)

In the next pine script tutorial will catch you with more interesting coding features.

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Responsive Coppock: Tradingview Pinescript Code

Coppock Indicator is one of investing style indicator to check whether to stick to the trends or not in the short term. Hull smoothing...
Rajandran R
32 sec read

Code Snippet – Simple Intraday Trading System with Target…

Thought of implementing a simple intraday trading system with stoploss and target controls with time-based entry and exit controls. Traders can use this code...
Rajandran R
2 min read

Algomojo Now Supports Multi-Client Trading with Amibroker, Metatrader, Python,…

Yes, from now onwards Multi-Account Execution from trading tools like Amibroker, Metatrader 4 and Metatrader 5, Ninjatrader 7, Ninjatrader 8, Excel is now possible...
Rajandran R
1 min read

10 Replies to “Code Snippet: Tradingview Pinescript Code for Open Range Breakout”

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More