Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Simple Open Range Breakout Trading System with Exploration – Amibroker AFL Code

5 min read

Here is a full-fledged Open Range Breakout Trading System with Exploration Controls designed in Amibroker.

What is a Open Range Breakout Trading System?

Opening Range is very often a measure of security high and low generally monitored by breakout traders to gauge the sentiment of the market. The very often first 15-minute high – low range made by the stock/index is used as a reference by many traders to monitor for breakout trading.

A Simple Open Range Breakout Trading system is primarily useful for intraday traders who is looking for a price breakout from the opening range. Typically traders very often use a 15min / 30min opening range to monitor breakout with pre-defined stops/targets.

Features of Opening Range Breakout

1)Multiple ORB Breakout Selection of the Traders Choice
2)Limit the Number of Entry Signals per day
3)Intraday Trading System with signal starttime, endtime and squareofftime controls
4)Mode of Trading: i) No Stoploss/Target mode, ii)Fixed Point Stoploss/Target , iii)Fixed Percentage Stoploss/Target mode.
5)Backtesting Capabilities
6)Exploration/Scanner Capabilities
6)Trading Dashboard that tracks the Signals and Profit/Loss

Open Range Breakout Trading – Parameter Controls

Open Range Breakout – Exploration Results

Open Range Breakout Amibroker AFL Code

//Desinging a Simple ORB Based Trading system with Stoploss and Target controls
//Coded by Rajandran R - www.marketcalls.in / www.algomojo.com
//Date : 20th Sep 2022
//Version : 1.00

_SECTION_BEGIN("Simple ORB Based Trading System");

SetPositionSize(1*RoundLotSize,spsShares);

newday = Day() != Ref(Day(),-1);

mins = Param("Breakout(mins)",15,1,60,1);

starttime = ParamTime("Start Time","09:30");
endtime = ParamTime("End Time","14:30");
sqofftime  = ParamTime("Sqoff Time","15:15");

mode = ParamList("Risk Control(Stop/Target)","DISABLED|POINTS|PERCENTAGE");
buffer = Param("Buffer",0,0.05,1000,0.01);
stop = Param("Stoploss",20,0.05,1000,0.01);
target = Param("Target",50,0.05,1000,0.01);

TickSz = Param("Tick Size",0.05,0.0001,1,0.0001);

TradeLimit = Param("Trade Limit Per Day",2,1,10,1);


orbh = ValueWhen(newday,TimeFrameGetPrice("H",in1Minute*mins,0))+buffer;
orbl = ValueWhen(newday,TimeFrameGetPrice("L",in1Minute*mins,0))-buffer;

//removing the values of ORBH and ORBL until the breakoutmeasuretime is reached
orbh = IIf(TimeNum()<starttime,Null,orbh); //redefining the ORBH value
orbl = IIf(TimeNum()<starttime,Null,orbl); //redefining the ORBL value

Plot(orbh,"ORBH",colorYellow,styleThick);
Plot(orbl,"ORBL",colorYellow,styleThick);

//Initialization

Buy =0;
Sell = 0;
Short = 0;
Cover = 0;

longstoplevel = Null;
longtargetlevel = Null;

shortstoplevel = Null;
shorttargetlevel = Null;

//Trading Logic without Stop and Target

orbcondlong = IsNull(Ref(orbh,-1)) AND orbh>0 AND Open < orbh AND High > orbh;
orbcondshort = IsNull(Ref(orbl,-1)) AND orbl>0 AND Open > orbl AND low < orbl;


if(mode=="DISABLED")
{


//Entry Logic
Buy = (Cross(High,orbh) OR orbcondlong)  AND TimeNum()>=starttime AND TimeNum()<=endtime;
Sell = Cross(orbl,Low) OR TimeNum()>=sqofftime;
Short = (Cross(orbl,Low) OR orbcondshort) AND TimeNum()>=starttime AND TimeNum()<=endtime;
Cover = Cross(High,orbh) OR TimeNum()>=sqofftime;

//Removing Excessive Signals only for Entry
Buy = ExRem(Buy,Sell);
Short = ExRem(Short,Cover);

//removing excessive signals for the day
Buy = Buy AND Sum(Buy OR Short,BarsSince(newday)+1)<=TradeLimit;
short = short AND Sum(Buy OR Short,BarsSince(newday)+1)<=TradeLimit;

//Remove Excessive Entry and Exit Signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);

BuyPrice = ValueWhen(Buy,orbh);
SellPrice = ValueWhen(Sell,IIf(Cross(orbl,Low),orbl,Close));

ShortPrice = ValueWhen(Short,orbl);
CoverPrice = ValueWhen(Cover,IIf(Cross(High,orbh),orbh,Close));

buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);

}

if(mode=="POINTS")
{
//Trading Logic with Stop and Target (points)

//Entry Logic
Buy = (Cross(High,orbh) OR orbcondlong)  AND TimeNum()>=starttime AND TimeNum()<=endtime;
iSell = Cross(orbl,Low) OR TimeNum()>=sqofftime;
Short = (Cross(orbl,Low) OR orbcondshort) AND TimeNum()>=starttime AND TimeNum()<=endtime;
iCover = Cross(High,orbh) OR TimeNum()>=sqofftime;

//Removing Excessive Signals only for Entry
Buy = ExRem(Buy,iSell);
Short = ExRem(Short,iCover);

//removing excessive signals for the day
Buy = Buy AND Sum(Buy OR Short,BarsSince(newday)+1)<=TradeLimit;
short = short AND Sum(Buy OR Short,BarsSince(newday)+1)<=TradeLimit;

//Calculate the Entry Price
BuyPrice = ValueWhen(Buy,orbh);
ShortPrice = ValueWhen(Short,orbl);

longstoplevel = BuyPrice - stop;
longtargetlevel = BuyPrice + target;

shortstoplevel = shortPrice + stop;
shorttargetlevel = shortPrice - target;

//Exit signal can happen due to 1)Negative Crossover 2)Stophit 3)Targethit
Sell = isell OR Cross(longstoplevel,Low) OR Cross(High,longtargetlevel);
Cover = icover OR Cross(High,shortstoplevel) OR Cross(shorttargetlevel,Low);

//Remove Excessive Entry and Exit Signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);

//Long Exit Price
SellPrice = IIf(isell,ValueWhen(isell,orbl),
				IIf(Cross(longstoplevel,Low),longstoplevel,
					IIf(Cross(High,longtargetlevel),longtargetlevel,null)));
					
//Short Exit Price
CoverPrice = IIf(icover,ValueWhen(icover,orbh),
				IIf(Cross(High,shortstoplevel),shortstoplevel,
					IIf(Cross(shorttargetlevel,Low),shorttargetlevel,null)));

buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);

Plot(IIf(buycontinue,longstoplevel,Null),"BuyStop Level",colorRed,styleDashed);
Plot(IIf(buycontinue,longtargetlevel,Null),"BuyTarget Level",colorgreen,styleDashed);

Plot(IIf(shortcontinue,shortstoplevel,Null),"ShortStop Level",colorRed,styleDashed);
Plot(IIf(shortcontinue,shorttargetlevel,Null),"ShortTarget Level",colorgreen,styleDashed);


}

if(mode=="PERCENTAGE")
{
//Trading Logic with Stop and Target (Percentage)

//Trading Logic with Stop and Target (points)

//Entry Logic
Buy = (Cross(High,orbh) OR orbcondlong)  AND TimeNum()>=starttime AND TimeNum()<=endtime;
iSell = Cross(orbl,Low) OR TimeNum()>=sqofftime;
Short = (Cross(orbl,Low) OR orbcondshort) AND TimeNum()>=starttime AND TimeNum()<=endtime;
iCover = Cross(High,orbh) OR TimeNum()>=sqofftime;

//Removing Excessive Signals only for Entry
Buy = ExRem(Buy,iSell);
Short = ExRem(Short,iCover);

//removing excessive signals for the day
Buy = Buy AND Sum(Buy OR Short,BarsSince(newday)+1)<=TradeLimit;
short = short AND Sum(Buy OR Short,BarsSince(newday)+1)<=TradeLimit;

//Calculate the Entry Price
BuyPrice = ValueWhen(Buy,orbh);
ShortPrice = ValueWhen(Short,orbl);

longstoplevel = BuyPrice*(1 - stop/100);
longtargetlevel = BuyPrice * (1+target/100);

//Round it of to nearest Tick Size
longstoplevel = TickSz * round(longstoplevel/TickSz);
longtargetlevel = TickSz * round(longtargetlevel/TickSz);

shortstoplevel = shortPrice * (1 + stop/100);
shorttargetlevel = shortPrice * (1-target/100);

//Round it of to nearest Tick Size
shortstoplevel = TickSz * round(shortstoplevel/TickSz);  
shorttargetlevel = TickSz * round(shorttargetlevel/TickSz);

//Exit signal can happen due to 1)Negative Crossover 2)Stophit 3)Targethit
Sell = isell OR Cross(longstoplevel,Low) OR Cross(High,longtargetlevel);
Cover = icover OR Cross(High,shortstoplevel) OR Cross(shorttargetlevel,Low);

//Remove Excessive Entry and Exit Signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);

//Long Exit Price
SellPrice = IIf(isell,ValueWhen(isell,orbl),
				IIf(Cross(longstoplevel,Low),longstoplevel,
					IIf(Cross(High,longtargetlevel),longtargetlevel,null)));
					
//Short Exit Price
CoverPrice = IIf(icover,ValueWhen(icover,orbh),
				IIf(Cross(High,shortstoplevel),shortstoplevel,
					IIf(Cross(shorttargetlevel,Low),shorttargetlevel,null)));

buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);

Plot(IIf(buycontinue,longstoplevel,Null),"BuyStop Level",colorRed,styleDashed);
Plot(IIf(buycontinue,longtargetlevel,Null),"BuyTarget Level",colorgreen,styleDashed);

Plot(IIf(shortcontinue,shortstoplevel,Null),"ShortStop Level",colorRed,styleDashed);
Plot(IIf(shortcontinue,shorttargetlevel,Null),"ShortTarget Level",colorgreen,styleDashed);


}

_SECTION_END();

_SECTION_BEGIN("Trading Signals");

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);

_SECTION_END();

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();



//Dashboard Controls.


_SECTION_BEGIN("Trading Dashboard");



strg_name = ParamStr("Strategy Name","ORB Trading System");
fontsize = Param("Font Size",14,12,36,1);

GfxSelectFont("BOOK ANTIQUA", fontsize, 400);
GfxSetBkMode(1);  //Transparent Mode
GfxSetTextColor(colorWhite);

//build dyanmic dashboard colors based on the ongoing trades
color = IIf(buycontinue,colorGreen,IIf(shortcontinue,colorRed,colorGrey40));

GfxSelectPen(colorWhite);
GfxSelectSolidBrush(SelectedValue(color));

width = Status("pxchartwidth"); //output will be in terms of number of pixels
height = Status("pxchartheight");

//GfxRoundRect(20,height-150,320,height-30,15,15);

GfxGradientRect(20,height-150,320,height-30,SelectedValue(color),colorBlack);

sigstatus = WriteIf(buycontinue,"Buy Signal",WriteIf(shortcontinue,"Short Signal","No Trade - Relax"));
PNL =  IIf(buycontinue,Close-buyprice,IIf(shortcontinue,ShortPrice-close,Null));
GfxTextOut(strg_name,30,height-130);
GfxTextOut(sigstatus+" : "+IIf(buycontinue,BuyPrice,IIf(shortcontinue,ShortPrice,Null)),30,height-110);
GfxTextOut("Profit/Loss : "+SelectedValue(Prec(PNL,2)),30,height-90);


_SECTION_END();

_SECTION_BEGIN("Exploration Module");

EntryPrice = IIf(Buy,BuyPrice, IIf(Short,ShortPrice,Null));
ExitPrice = IIf(Sell,SellPrice, IIf(Cover,CoverPrice,Null));
StopPrice = IIf(Buy,longstoplevel, IIf(Short,shortstoplevel,Null));
TargetPrice = IIf(Buy,longtargetlevel, IIf(Short,shorttargetlevel,Null));
isignal = IIf(Buy,'B',IIf(Sell,'X', IIf(Short,'S',IIf(Cover,'C',Null))));

Filter = Buy OR Sell OR Short OR Cover;

AddColumn(EntryPrice,"Entry Level",1.2);
AddColumn(ExitPrice,"Exit Level",1.2);
AddColumn(StopPrice,"Stop Level",1.2);
AddColumn(TargetPrice,"Target Level",1.2);
AddColumn(isignal,"Signal Status",formatChar);

SetSortColumns(-2); //sort with the time (recent signals at the top)



_SECTION_END();
Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Interactive Brokers – Smart Order Chart Trading Module using…

The IB Controller is an interface designed to facilitate automatic trading with AmiBroker and Interactive Brokers (IB) TWS (Trader Workstation). It serves as a...
Rajandran R
8 min read

Introducing OpenAlgo V1.0: The Ultimate Open-Source Algorithmic Trading Framework…

OpenAlgo V1.0 is an open-source algorithmic trading platform to automate your trading strategies using Amibroker, Tradingview, Metatrader, Python etc. Designed with the modern trader...
Rajandran R
2 min read

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

10 Replies to “Simple Open Range Breakout Trading System with Exploration –…”

  1. Thank You Rajandran, In Parameter session, we can able to see the BNF value details – SL 150 and TG-300. Would like to know the recommended value of SL & TG for Stocks Future and NF Future

    1. replace this line “SetPositionSize(1*RoundLotSize,spsShares);” with this “SetPositionSize(1,spsShares);”

  2. //Better Version of Open Range Breakout
    //Coded By Rajandran R – http://www.marketcalls.in
    //Created Date – 04 Dec 2018

    //Recommended Timeframe to Run this strategy : 1min or 5min charts
    //Higher timeframe may or may not yield desired results

    _SECTION_BEGIN(“Open Range Breakout”);

    SetBarsRequired(-2,-2); //Turn Quick AFL Off

    _N(Title = StrFormat(“{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g “, Ref(O,-1), Ref(H,-1), Ref(L,-1), Ref(C,-1)));

    SetChartOptions(0 , chartShowArrows | chartShowDates);
    Plot(Close,”Candle”, colorDefault, styleCandle);

    newday = Day() != Ref(Day(),-1); //check if new day or not

    starttime = ValueWhen(newday,TimeNum());
    cn=Param(“candle no.”,0,0,75,1);
    IBendtime = starttime+500;
    minh = ValueWhen(newday,TimeFrameGetPrice(“H”,in5Minute,cn));
    minl = ValueWhen(newday,TimeFrameGetPrice(“L”,in5Minute,cn));

    printf(“%g”,IBendtime);

    DayOpen = TimeFrameGetPrice(“O”,inDaily);
    DayHigh = TimeFrameGetPrice(“H”,inDaily);
    DayLow = TimeFrameGetPrice(“L”,inDaily);

    ORBH = IIf(TimeNum() < IBendtime, Null ,minh);
    ORBL = IIf(TimeNum() < IBendtime, Null, minl);

    Plot(ORBH,"ORBH",colorBlue);
    Plot(ORBL,"ORBL",colorred);

    sir this code plot right levels on previous day chart but on current day chart levels are not perfect . i just add candle no to plot desirous candle high and low

  3. Please modify this orb to “any candle we select” . I mean high low of any candle we choose instead of first candle. Thanks

Leave a Reply

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