How to setup Algo-Trading for Open Range Breakout (ORB) Trading Strategy?

How to setup Algo-Trading for Open Range Breakout (ORB) Trading Strategy?

In the previous episodes, we have discussed on how to create a simple AFL for breakout trading. In this episode , we are going to enhance the code to create script for Open range breakout system.

Let’s get into the set-up.

Condition: Open Range breakout. This is exactly opposite to OHOL strategy. If open price of the stock is equal to today’s low and in case if price breaks and goes below today’s low, we can short the stock. Likewise If open price of the stock is equal to today’s high and in case if price breaks out and goes above today’s high, we can go long.

So before we get into the session, let me mention this. In the previous episodes, we have discussed on how to install amibroker, where to get 1 minute data, how to import the data into amibroker and how to set-up the database and charts. We also discussed on some of the commonly used scripts. So please do refer the previous episodes in case you are not able to understand anything discussed in this session.

 

Let’s begin with the script set-up now.

Script:

_SECTION_BEGIN(“Intraday Open Range BreakOut System”);

 

SetTradeDelays( 1, 1, 1, 1 );

SetOption( “InitialEquity”, 200000);

SetOption(“FuturesMode” ,True);

SetOption(“MinShares”,1);

SetOption(“CommissionMode”,2);

SetOption(“CommissionAmount”,100);

SetOption(“AccountMargin”,10);

SetOption(“RefreshWhenCompleted”,True);

SetPositionSize(50,spsPercentOfEquity);

SetOption( “AllowPositionShrinking”, True );

SetOption(“MaxOpenPositions”,10);

BuyPrice=Open;

SellPrice=Open;

ShortPrice=Open;

CoverPrice=Open;

 

 

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( Close, “Price”, colorWhite, styleCandle );

 

NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0; 

 

Plot(NewDay,””,colorlightGrey,styleHistogram|styleDots|styleNoLabel|styleOwnScale);

 

FirstTradeTime=094500;

SquareOffTime = 151500;

 

DayOpen=TimeFrameGetPrice(“O”,inDaily);

FirstCandleHigh=ValueWhen(Day()!=Ref(Day(),-1),H);

FirstCandleLow=ValueWhen(Day()!=Ref(Day(),-1),L);

DayHigh = HighestSince(NewDay,H,1); 

DayLow = LowestSince(NewDay,L,1); 

 

printf(“\nDayOpen : ” + DayOpen ); 

printf(“\nDayHigh : ” + DayHigh ); 

printf(“\nDayLow : ” + DayLow ); 

printf(“\nFirstCandleLow : ” + FirstCandleLow ); 

printf(“\nFirstCandleHigh : ” + FirstCandleHigh ); 

 

Buy = (round(DayOpen)==round(FirstCandleHigh)) AND High>=(sqrt(DayOpen)+0.000833)^2 AND (TimeNum()  >= FirstTradeTime) AND TimeNum()<SquareOffTime; 

Short = (round(DayOpen)==round(FirstCandleLow)) AND Low<=(sqrt(DayOpen)-0.000833)^2 AND (TimeNum()  >= FirstTradeTime) AND TimeNum()<SquareOffTime;

 

Sell = TimeNum() >= SquareOffTime OR High>BuyPrice+25;

Cover = TimeNum() >= SquareOffTime OR Low<ShortPrice-25;

 

Buy=ExRem(Buy,Sell);

Sell=ExRem(Sell,Buy);

 

Short=ExRem(Short,Cover);

Cover=ExRem(Cover,Short);

 

printf(“\nBuy : ” + Buy ); 

printf(“\nSell : ” + Sell ); 

printf(“\nShort : ” + Short ); 

printf(“\nCover : ” + Cover );

 

StopLoss=0.5;

ApplyStop(Type=0,Mode=1,Amount=StopLoss);

 

Target=0.5;

ApplyStop(Type=1,Mode=1,Amount=Target);

 

/* Plot Buy and Sell Signal Arrows */

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(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);

PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);

PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);

PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);

PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, 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);

 

_SECTION_END();

 

First part of the script is self explanatory – there are parameters used for backtesting, which we have already explored in detail. So I’m skipping this part.

Likewise, next few lines is for plotting the charts and it has been discussed in detail as well.

 

Now comes the actual part of the script.

First, I’m going to define the trade timing. I’ll trade only after 9.45 and I will close all trades by 3.15. I have defined 2 parameters for the same.

FirstTradeTime=094500;

SquareOffTime = 151500;

 

Next, I get today’s open price and then low and high of the first candle to determine whether stock falls under OH or OL category.

DayOpen=TimeFrameGetPrice(“O”,inDaily);

FirstCandleHigh=ValueWhen(Day()!=Ref(Day(),-1),H);

FirstCandleLow=ValueWhen(Day()!=Ref(Day(),-1),L);

DayHigh = HighestSince(NewDay,H,1); 

DayLow = LowestSince(NewDay,L,1);

Next I’ll get the today’s low and high at this point in time. Next, I’m printing the same.

 

Next line is the buy condition.

I’ll go long if

  • Stock is an OH candidate and if
  • price breaks out to upside from the OH level and
  • Current time is between 9.45 & 3.15

Buy = (round(DayOpen)==round(FirstCandleHigh)) AND High>=(sqrt(DayOpen)+0.000833)^2 AND (TimeNum()  >= FirstTradeTime) AND TimeNum()<SquareOffTime; 

Short = (round(DayOpen)==round(FirstCandleLow)) AND Low<=(sqrt(DayOpen)-0.000833)^2 AND (TimeNum()  >= FirstTradeTime) AND TimeNum()<SquareOffTime;

I’ll short if

  • Stock is an OL candidate and if
  • price breaks out to downside from the OL level and
  • Current time is between 9.45 & 3.15

 

I’ll square off when market hits 3.15. We can also exit at fixed targets and SL.

Rest of the lines are already discussed in previous episodes. So if you look at the charts, you can spot the signals, you can also run a backtest on this code.

This was one of the strategies commonly used for the reversal trades, especially if you are actively trading on OHOL strategy.

Do give this a try and let us know in case of any issues.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!