# Open Range BreakOut with Trade Management

How difficult is it to program a day trading system using an open range break out, protective stop and a trailing stop?  Let’s see.  I started working on this and it got a little more detailed than I wanted so I have now split it up into two posts.  Part 2 will follow very soon.

What inputs do we need?  How about the number of days used in the volatility calculation?  What percentage of the volatility from the open do you want to buy or sell?  Should we have a different value for buys and sells?  Do we want to use a volatility based protective stop or a fixed dollar?  How about a trailing stop?  Should we wait to get to a certain profit level before engaging the trailing stop?  Should it also be based on volatility or fixed dollar amt?  How much should we trail – again an amount of volatility or fixed dollar?

## Proposed inputs:

inputs: volCalcLen(10), orboBuyPer(.2), orboSellPer(.2), volStopPer(.7), \$Stop(500), volProfThreshPer(.5), \$ProfThresh(250),volTrailPer(.2),\$Trail(200);

That should do it for the inputs – we can change later if necessary.

## Calculations:

We need to determine the volatility and a good way do this is calculating the average range over the past N days.   There are two ways to do this: 1) incorporate a daily chart as data2 and use a built-in function for the calculation or 2) use a for-loop and use the built-in functions HighD and LowD and just use one data feed.   Both have their drawbacks.  The first is you need to have a multi-data chart and the second you get a warning that you shouldn’t put a series function call inside the body of a for-loop.   I have done it both ways and I prefer to deal with the warning – so far it has worked out nicely – so let’s go with a single data chart.

## Building the code:

### Inputs:

Since we are combining a volatility and fixed \$ amount in our trade management, you will need to set either the vol or dollar amounts to a high value to disable them.  You can use both but I am taking the smaller of the respective values.

``inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2); inputs: volStopPer(.7),\$Stop(500/bigPointValue); inputs: volThreshPer(.5),\$ProfThresh(250/bigPointValue);inputs: volTrailPer(.2),\$Trail(200/bigPointValue);``
Inputs We Will Need - Can Changer Later

### Variables:

``vars:vol(0),buyPoint(0),sellPoint(0),	longStopAmt(0),shortStopAmt(0),longExitPoint(0),shortExitPoint(0),	longThreshAmt(0),shortThreshAmt(0),	longTrailAmt(0),shortTrailAmt(0),	longTrailLevel(0),shortTrailLevel(0),	hiSinceLong(0),loSinceShort(0),mp(0),	rangeSum(0),iCnt(0),	buysToday(0),shortsToday(0)``
Variables That We Might Need

### Once A Day Calculations:

Since we will be working with five-minute bars we don’t want to do daily calculations on each bar.  If we do it will slow down the process.  So let’s do these calculation on the first bar of the day only.

``If d <> d[1] thenBegin	rangeSum = 0.0;  // range calculation for entry	For iCnt = 1 to volCalcLen	Begin		rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));	end;	vol = rangeSum/volCalcLen;	buyPoint =  openD(0) + vol*orboBuyPer; 	sellPoint = openD(0) - vol*orboSellPer; 		longStopAmt = vol * volStopPer; 	longStopAmt = minList(longStopAmt,Stop\$);		shortStopAmt = vol *volStopPer; 	shortStopAmt = minList(shortStopAmt,Stop\$);	longThreshAmt = vol  * volThreshPer;	longThreshAmt = minList(longThreshAmt,ProfThresh\$);		shortThreshAmt = vol * volThreshPer;	shortThreshAmt = minList(shortThreshAmt,ProfThresh\$);		longTrailAmt = vol * volTrailPer;	longTrailAmt = minList(longTrailAmt,Trail\$);		shortTrailAmt = vol * volTrailPer;	shortTrailAmt = minList(shortTrailAmt,Trail\$);		longTrailLevel = 0;	shortTrailLevel = 999999;	buysToday = 0;	shortsToday = 0;end;``
Do These Just Once A Day

#### For All of You Who Don’t Want To Wait – Beta Version Is Available Below:

In my next post, I will dissect the following code for a better understanding.  Sorry I just ran out of time.

``inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2); inputs: volStopPer(.7),Stop\$(500/bigPointValue); inputs: volThreshPer(.3),ProfThresh\$(250/bigPointValue);inputs: volTrailPer(.2),Trail\$(200/bigPointValue);vars:vol(0),buyPoint(0),sellPoint(0),	longStopAmt(0),shortStopAmt(0),longExitPoint(0),shortExitPoint(0),	longThreshAmt(0),shortThreshAmt(0),	longTrailAmt(0),shortTrailAmt(0),	longTrailLevel(0),shortTrailLevel(0),	hiSinceLong(0),loSinceShort(0),mp(0),	rangeSum(0),iCnt(0),	buysToday(0),shortsToday(0);If d <> d[1] thenBegin	rangeSum = 0.0;  // range calculation for entry	For iCnt = 1 to volCalcLen	Begin		rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));	end;	vol = rangeSum/volCalcLen;	buyPoint =  openD(0) + vol*orboBuyPer; 	sellPoint = openD(0) - vol*orboSellPer; 		longStopAmt = vol * volStopPer; 	longStopAmt = minList(longStopAmt,Stop\$);		shortStopAmt = vol *volStopPer; 	shortStopAmt = minList(shortStopAmt,Stop\$);	longThreshAmt = vol  * volThreshPer;	longThreshAmt = minList(longThreshAmt,ProfThresh\$);		shortThreshAmt = vol * volThreshPer;	shortThreshAmt = minList(shortThreshAmt,ProfThresh\$);		longTrailAmt = vol * volTrailPer;	longTrailAmt = minList(longTrailAmt,Trail\$);		shortTrailAmt = vol * volTrailPer;	shortTrailAmt = minList(shortTrailAmt,Trail\$);		longTrailLevel = 0;	shortTrailLevel = 999999;	buysToday = 0;	shortsToday = 0;end;mp = marketPosition;If mp = 1 and mp[1] <> 1 then buysToday = buysToday + 1;If mp = -1 and mp[1] <> -1 then shortsToday = shortsToday + 1;if time < sessionendTime(0,1) and buysToday < 1 then Buy("ORBo-B") next bar at buyPoint stop;if time < sessionendTime(0,1) and shortsToday < 1 then Sellshort("ORBo-S") next bar at sellPoint stop;	  	If marketposition = 1 thenBegin 	longExitPoint = entryPrice - longStopAmt;	sell("L-Exit") next bar at longExitPoint stop;end;If marketposition = -1 thenBegin	shortExitPoint = entryPrice + shortStopAmt;	buyToCover("S-Exit") next bar at shortExitPoint stop;end;If marketPosition = 1 and maxContractProfit/bigPointValue >= longThreshAmt thenBegin	longTrailLevel = maxList(highest(h,barsSinceEntry) - longTrailAmt,longTrailLevel);	sell("TrailSell") next bar at longTrailLevel stop;end;If marketPosition = -1 and maxContractProfit/bigPointValue >= shortThreshAmt thenBegin	shortTrailLevel = minList(lowest(l,barsSinceEntry) + shortTrailAmt,ShortTraillevel);	buyToCover("TrailCover") next bar at shortTrailLevel stop;end;SetExitOnClose;``
Beta Version - I will clean up later and post it