FIG. 01: SINCE X-PERFORMANCE OVERNIGHT (TOP) AND DURING THE DAY (BOTTOM).
Figure 1 shows the capital curve of a trading system that takes a long position in the DAX every day at 5 PM and closes again the following day at 9:30 AM. The bottom shows the accumulated DAX gains between 9:30 AM and 5:00 PM.
OPEN RANGE BREAKOUT.
Let us now come to a trading strategy that should be known to many readers: the Open Range Breakout. The target is to trade the breakout from an individually specified period. The time function permits this classic approach to be tested for robustness quickly and precisely. The following code is only a simple framework, but shows how a small trick permits determination of the high and low of a specific time of the day and to use breakouts from this zone to develop positions. Additionally, the number of trades per day and direction is limited to avoid overtrading on sideways days.
Let us start with specifying all inputs. The opening automatically marks the start of the open range of the underlying market and does not require any input of its own. StartTime ends the opening phase and trading can commence. The input EndTime controls the exit at the end of the day to avoid overnight positions.
Price( Close ),
StartTime( 1600 ),
EndTime( 2130 );
The variables hh and ll are needed to specify the highs and lows of the opening range; the last two variables act as counters that permit limiting the number of trades.
Variables: hh( 0 ), ll( 0 ), longTrade( 0 ), shortTrade( 0 );
When a new trading day begins – this is what the query in the first line does – the high and low of the first candle are defined as the “high” and “low”.
If Date <> Date Then
hh = high;
ll = low;
longTrade = 0;
shortTrade = 0;
After completion of the first intraday bar, the following loop starts, which is performed until the opening phase ends – in our case, this is at 4 PM. The target of this nested loop is to review each bar for which rate has the highest high and the lowest low of the opening phase so far.
If Date = Date And Time<= StartTime Then
If High > hh Then hh = High;
If Low < ll Then ll = Low;
Now the trading phase starts. First, the code checks if a long trade has already been performed or not. If this is not the case (longTrade=0), a stop buy order is placed at the high of the opening range. The low of the opening range serves as loss stop; the corresponding sales order is placed. This process is performed accordingly for the short side. If there is an entry on the long or short side, the corresponding counter (longTrade or shortTrade) is set to 1. In practice, this means that if a long trade has already been performed on that trading day, no further long trades are permitted anymore. The same applies to short trades.
If Time >= StartTime And Time <= EndTime Then
If longTrade = 0 Then Buy Next Bar at hh Stop;
Sell Next Bar at ll Stop;
If shortTrade = 0 Then Short Next Bar at ll Stop;
Cover Next Bar at hh Stop;
If MarketPosition = MarketPositionLong Then longTrade = 1;
If MarketPosition = MarketPositionShort Then shortTrade = 1;
To tell the computer when the exit is to take place at the end of the day, the following condition must be defined at the end:
If Time >= EndTime Then
Sell( "End of Day Exit" );
Cover( "End of Day Exit" );
How Can Weekdays Be Back Tested?
Practical Example: TURNAROUND TUESDAY.
In addition to using times, Equilla can, of course, also use other data formats. For example, if you would like to use specific days within the trading system, use the function “dayofweek”. We will use a simple code as an example that performs long or short entries on Tuesdays, depending on whether the rate is below or above a simple moving average (SMA).
First, we define the required variables and inputs, as always:
Inputs: price( close ), period( 50,1 );
sma=average( close,period );
The next step defines the condition for the short entry. “dayof week(date)=2” means Tuesday. If the market opens above the previous day’s SMA, a short position is taken:
If dayofweek( date )=2 and open>sma
then short this bar at open;
The condition for the long case is specified accordingly:
If dayofweek(date)=2 and open<sma
then buy this bar at open;
For the results of the Tuesday strategy to be properly reflected in the back test later, the exit condition at trading close is entered at the end of the code:
ExitPosition all shares this bar at close;
The date function permits flexible programming and evaluation of time- and calendar-based queries. At the same time, they can be used as a filter within a more complex trading system and thus contribute to improving performance.
Flexible Data Handling.
HOW TO USE DATA INPUTS AND INLINE INSTRUMENTS.
Finally, we would like to demonstrate with a simple example which opportunities Equilla offers for data handling. Traders can access any present data in the code using data inputs or inline instruments and use them to generate trading signals. Find a few examples for different data accesses below:
Data Input: The data series opened in the chart is used as source. If, for example, you want to draw the difference of one security (data1) and another (data2), the indicator is as follows:
DrawLine( Close of Data1 - Close of Data2 );
Inline-Instrument: The specific data are directly requested from the data provider here without the corresponding security having to be opened in the chart. The following code calculates a simple SMA for the time level currently chosen in the chart (note: the abbreviation .GDAXI means the DAX at Thomson Reuters).
DaxMA = AverageFC( close, 38 ) of '.GDAXI';
The following example shows the code presented above with a small change. The addition “60m” and “d” uses the 60-minute and daily chart for calculation. This way, e.g. information from the daily or weekly chart can be used on an intraday basis and vice versa. No matter if Bollinger bands, channels or swing points – your imagination need have no limits here.
Meta: subchart( false );
DrawLine( AverageFC( close of '.GDAXI 60m', 200 ) );
DrawLine( AverageFC( close of '.GDAXI d', 50 ) );