Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

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 Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

Stoploss, Multi Target, and Trailing Stoploss Module – Amibroker…

After Intraday V2.0 Amibroker Module - a readymade drag and drop module to convert any trading strategy into the intraday trading system with alerts,...
Rajandran R
6 min read

Line Trading – Algomojo Automation Module for Amibroker Users

The line Trading Automation tool is designed for Manual traders who want to perform level-based trade execution faster and also bring some advanced trade...
Rajandran R
58 sec read

Plotting Day Wise Equity Curve using Amibroker

We know that Amibroker provides an equity curve for the whole strategy. Is there a way where we can build a simple Day Wise...
Rajandran R
1 min read

4 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

Leave a Reply

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