FIG. 2: ENHANCED DAY RANGE INDICATOR WITH VOLATILITY BAND.
With the use of standard deviation as a measure of “normal” daily fluctuations, the enhanced version of the Day Range Indicator can be used in all markets.
Coding the strategy in Equilla.
Now we have got everything that is needed for the basic idea of the trading strategy. Go long when the indicator breaks above its upper band (or fixed level) and close the position at the end of the day. And it`s just the same on the short side; go short at the lower band, cash in at the end of the day.
As an experienced trader you will notice that this cannot be the whole truth. Some kind of profit protection and stop loss will be needed. Therefore I added a simple profit trailing stop. The default setting is two standard deviations. And I coded a condition that only allows one trade per day. This is meant as a simple protection against a market environment with high volatility but no direction. This might trigger several losing trades on one day.
The coding of the strategy is quite straight forward. First, all needed inputs and variables are defined:
Inputs: dev(2.0),period(500), traildev(2.0,1,10),trademode(breakout, reversalmode), fixedlevel(0.0);
Variables:oo( Invalid ),colour, std, endtime, starttime, tt;
In the next step all the things needed for the trading logic are calculated. When does the market open? When does it close? How many trades have occured? What is the standard deviation for the current bar? See the green comments in the code and you will quickly get how it is done. If you are not familiar with the used syntax, right click on any blue word to get help. If you are still stuck, don’t hesitate to contact email@example.com.
colour=black; // colour of indicator bars
If Date <> Date Then begin // first bar of day
oo = Open; //remember opening price
starttime=time; // timestamp of first bar of day
endtime=time; // timestamp of last bar of yesterdays session
tt=totaltrades; // number of trades in history
The following code calculates the standard deviation, the colouring of the bars and plots the enhanced Day Range Indicator.
std=stdev((high+low+close)/3 - oo,period); // standard deviation of day range
if fixedlevel>0 then std=fixedlevel; // if level is set overwrite standard deviation
if high-oo>dev*std then colour=darkgreen; // green above band
if low-oo<-1*dev*std then colour=red; // red below band
drawline(0+dev*std); // upper band
drawline(0-dev*std); // lower band
Drawline( 0, "Zero Line" ); // zero line
DrawBar( Open - oo, High - oo, Low - oo, Close - oo,colour,colour); // day range bars
Now the trading logic is programmed. The aim of this strategy is to enter long positions when the indicator breaks above the upper volatility band (The inverse rules are applied to short entries.). The following prerequisites are also required:
- the current time is at least one hour after market open
- the market close is at least one hour away
- we are not in a position and have not traded today
// if in core trading time (open+1h - 1 bar before end of session)
if time>starttime+100 and time<endtime and
marketposition=marketpositionflat and // and currently no position
totaltrades=tt then begin // and no trade today then ...
if trademode=breakout then begin // breakout mode
buy next bar at oo+dev*std stop; // buy at upper band,
short next bar at oo-dev*std stop; //short at lower band
if trademode=reversalmode then begin // (*) reversal mode
short next bar at oo+dev*std limit; // short at upper band
buy next bar at oo-dev*std limit; // buy at lower band
After the entry is done the exits automatically kick in. The first exit condition is simply the end of the day. The second exit is the trailing stop. It protects your profits and limits the possible losses.
FINDING ROBUST PARAMETERS BY USING THE OPTIMIZER.
All inputs of this strategy can be changed after optimisation:
- Deviation(2.0): Multiplier for the standard deviation.
TIP: Multiplier for the standard deviation.
- Period(500): Number of bars needed for the calculation of the standard deviation.
- TrailDeviation (2.0, 1,10): Number of standard deviations used for the trailing stop.
TIP: It can be optimized between one and ten standard deviations.
If necessary, a further parameter called „FixedLevel“ enables you to use absolute bands for the Day Range indicator instead of standard deviation.
BY THE WAY:
If you add the following commands to the strategy code, some additional points will show you the actual entry and exit levels. So you already know in advance the level at which you will be active.
if time=endtime then exitposition this bar on close; // exit trade at end of day
// set profit trailing stop