In this blog article we look we look at how easy it is to set-up the conditions in CandleScanner in order to perform a backtest.
First we need to customize the settings for the backtest, defining the entry and exit conditions. For the sake of illustration we assume the default entry and exit settings (found in Backtester Settings -> New), which can, of course, be altered to suit individual requirements. We have a choice regarding the direction of the trade, i.e. whether to go long or short, and, in what follows we will use a long position in the example. Selecting a long trade entry condition, the default settings are shown in Figure 1.
Entry Condition (long position)
The position is set to be opened when the price increases by 20% from the last pattern’s candle high-low range (the percentage of the absolute value). Furthermore, we require that this condition has to be met within the next 3 candles since the pattern's occurrence (Expiration time of entry condition).
A cautionary note
It is always possible that the transaction may be executed at a worse price should the price gap on opening of the market. For example, if the long position condition was met and was expected to be opened at 102 then, should the market open at 103, the trade will executed at this price (even if the lower shadow was on a lower level).
Exit condition (trailing stop and stop loss)
During backtesting CandleScanner simulates the transactions which are opened based on the signals from the candlestick patterns. Once the position is opened it is always closed based on one of the following situations:
- Stop Loss (SL) – activated when the price reaches the maximum specified loss on the trade.
- Trailing Stop (TS) – activated when the price reverses, but the position is closed at a price better than determined by the SL. This method locks in pre-defined profits following an increase in price (if a long position is taken) or decrease in price (if a short position is taken) and closes the position once the market reverses against the position.
- Take Profit (TP) – activated when the price reaches a planned level of profit we planned.
The following examples show the formulation of the SL and TS conditions.
Stop Loss
Figure 2 shows the settings screen. In this case, we can see that during the backtest we use a Stop Loss set at levels of 2%, 3% and 4% from the open price. In this optimization, we simply we run tests for different Stop Loss values, ranging between 2% and 4%, in steps of 1%.
Example
On the daily chart, a Bullish Engulfing pattern occurred (consisting of 2 candles). The next day the long position was opened at 100. The SL was set to 2% which is 98 (100 – 100 * 0.02 = 98). On the same day the price fell to 96, but position was, however, closed out at 98 before the price had fallen to 96.
A cautionary note
Should the price gap on the opening, it is possible that the loss can be bigger than the one determined by the SL (unless one has a guaranteed stop in place).
Trailing Stop
The Trailing Stop (TS) is similar to Stop Loss (SL). However, the main difference is that the TS, as the name implies, tracks the price behaviour, locking in any profitable position (or loss smaller than the one generated by SL) if the price moves in the right direction. When the position is opened on the first candle only the SL is active – the TS can subsequently become active, at the earliest, on the second candle. If the price moves in the anticipated direction, the SL at some point is transformed into a TS, following the price move. In other words, by using SL and TS an investor specifies the maximum possible loss, without setting a limit on the maximum possible gain.
In our backtest example we use a TS linked to the ATR (Average True Range). This stop follows the price at a ‘distance’ correlated with price volatility. Price volatility is calculated as a multiple (which can be a fraction) of Average True Range (ATR). Here, the TS is adapting to the behaviour of the price, i.e. the higher the price volatility, the wider is the TS.
A range of values can be tested by specifying numeric values in the fields From, To and Step, see Figure 3. (To see those fields click on the arrow icon.)
The TS based on ATR for a long position is calculated as follows (the opposite is true for a short trade):
Trailing Stop (ATR) = Current Open Price – ATR(period) * Factor
Summarising the backtest conditions
Summarising the position, we optimize over the range of values which have been set, i.e. CandleScanner runs the optimization test over all the combinations of parameters shown in Figures 1 to 3. Hence, we have 3 parameters needing to be optimized, namely:
- Stop Loss (values: 2%, 3%, 4%)
- Trailing Stop ATR (values of periods for which ATR is calculated: 8, 10, 12, 14, 16)
- Trailing Stop Factor (values: 0.8, 1.0, 1.2, 1.4, 1.6, 1.8)
Initial equity position
We will assume that we start with an initial value of our portfolio equal to 100 (set in General) and shown in Figure 4.
We can save the settings, Figure 5, (here called 'test1'), ensuring that they can be recalled and used for running the backtest:
Finally, we need to specify the dates for which the backtest will be performed, the pattern we wish to examine (one pattern at a time) and, of course, the company (or companies) we want to backtest (we can backtest several companies all at once).
Part 3 will look at the results and their interpretation.