How to setup Algo-Trading for Simple Breakout Trading Strategy?

How to setup Algo-Trading for Simple Breakout Trading Strategy?

How to setup Algo-Trading for Simple Breakout Trading Strategy?

In this episode , we are going to learn about how to set-up a simple breakout trade.

First, let’s see the trade set-up first.

Condition: It is very simple. If price breaks out above the previous day’s high, go long. If price breaks out below the previous day’s low, short. There is one additional check — price should have opened between previous day’s low and high. As simple as that.

This script will serve as a template for all out future trade set-ups, so pay at most attention to this episode.

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 sripts. So please do refer the previous episodes in case you are not able to replicate this.

Script:

_SECTION_BEGIN(“Previous day 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);
PrevDayHigh=TimeFrameGetPrice(“H”,inDaily,-1);
PrevDayLow=TimeFrameGetPrice(“L”,inDaily,-1);

DayHigh = HighestSince(NewDay,H,1); 
DayLow = LowestSince(NewDay,L,1);

printf(“\nDayOpen : “ + DayOpen ); 
printf(“\nDayHigh : “ + DayHigh ); 
printf(“\nDayLow : “ + DayLow ); 
printf(“\nPrevDayHigh : “ + PrevDayHigh ); 
printf(“\nPrevDayLow : “ + PrevDayLow );

Buy = DayOpen<PrevDayHigh AND High>=PrevDayHigh AND (TimeNum() >= FirstTradeTime) AND TimeNum()<SquareOffTime; 
Short = DayOpen>PrevDayLow AND Low<=PrevDayLow AND (TimeNum() >= FirstTradeTime) AND TimeNum()<SquareOffTime;

Sell = TimeNum() >= SquareOffTime;
Cover = TimeNum() >= SquareOffTime;

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 in previous episodes. 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 previous day’s low and high.

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

DayOpen=TimeFrameGetPrice(“O”,inDaily);
PrevDayHigh=TimeFrameGetPrice(“H”,inDaily,-1);
PrevDayLow=TimeFrameGetPrice(“L”,inDaily,-1);

Next line is the buy condition.

DayHigh = HighestSince(NewDay,H,1); 
DayLow = LowestSince(NewDay,L,1);

I’ll go long if

· Today’s opening price is less than previous day’s high

· Today’s high is greater than previous day’s high (this is break out to upside)

· Current time is between 9.45 & 3.15

Buy = DayOpen<PrevDayHigh AND High>=PrevDayHigh AND (TimeNum() >= FirstTradeTime) AND TimeNum()<SquareOffTime; 
Short = DayOpen>PrevDayLow AND Low<=PrevDayLow AND (TimeNum() >= FirstTradeTime) AND TimeNum()<SquareOffTime;

I’ll short if

· Today’s opening price is higher than previous day’s low

· Today’s low is lower than previous day’s low (this is break out to downside)

· 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.

Sell = TimeNum() >= SquareOffTime;
Cover = TimeNum() >= SquareOffTime;

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 simple set-up can be used to create AFLs for any of the breakout patterns in including Flag, Triangles, etc

We will use this code as base template in future episodes to teach various breakout patterns. Hope this episodes was useful, let us know in case of any queries.

 

Leave a Reply

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

error: Content is protected !!