MetaTrader 5 HelpStrategy TesterWorking with TesterGeneration of Ticks

# Generation of Ticks

Ticks are required for testing and optimizing Expert Advisors, because the Expert Advisor works on ticks. Tick data are generated by the tester based on cached minute records in the integer format. It means, the tester copies the required history data from the terminal and transfers them to the integer format to speed up calculations.

 There are several modes of tick generation implemented in the strategy tester. The most accurate "Every tick" mode is described below.

## Generation of Ticks Depending on the Tick Volume

The generation of tick differs for bars with different tick volumes:

### Tick Volume = 1

For bars with the tick volume equal to one, ticks are not generated, a tick with the value of its Close price is written for it:

### Tick Volume = 2

Also ticks are not generated for bars with two ticks. First a tick with the Open price is written, and then the tick with the Close price is written:

### Tick Volume >= 3

For bars with 3 or more ticks, there are different schemes of tick generation depending on their number.

## Scheme of Bar Progress

Four different schemes are possible for bars with three or more ticks:

Progress Variant

Image

The price moved in one direction and returned to the open price level. Thus a bar has only High (the highest price) and Low (the lowest price).

The price moved in one direction and returned, breaking through the open level. In this case the bar also has only High (the highest price) or only Low (the lowest price), but the Open and Close prices are not equal.

The price moved in one direction, but didn't reach the Open price while returning back.

The price moved several points in one direction only. In this case the bar does not have High or Low.

## Generation of Ticks for Bars with Three or More Ticks

Ticks are generated based on reference points. The number of such points cannot exceed the tick volume, and cannot be larger than 11 (the point of the Open price is not taken into account).

Reference points are divided into three parts: those for forming open shadow, the candlestick's range (between its High and Low) and the close shadow.

Depending on the number of ticks, the following variants of reference points distribution are possible:

 Number of Reference Points Open Shadow Candlestick Range Close Shadow 11 3 5 3 10 2 6 2 9 2 5 2 8 2 4 2 7 2 3 2 6 1 4 1 5 1 3 1 4 1 2 1 3 1 1 1
 •If a candlestick doesn't have some of the shadows, this shadow's points are given to the range of the candlestick.•The candlestick's range is generated by the uneven number of reference points. If the range has the even number of points, the excessive one is given to one of its shadows provided that the shadow already has 2 points. Otherwise the excessive point is omitted.

### The Ideal Distribution (3-5-3)

Reference points are calculated as the number of points from the open price. The ideal distribution of points (3-5-3) is calculated the following way:

Bull Candlestick

 Calculation of Points Image •Open•3/4*(O — L)•1/2*(O — L)•Low•(H — L)/3•(H — L)*3 — 1•2/3*(H — L)•2/3*(H — L) — 1•High•3/4*(H — C)•1/2*(H — C)•Close

Bear Candlestick

 Calculation of Points Image •Open•3/4*(O — H)•1/2*(O — H)•High•(L — H)/3•(L — H)/3 + 1•2/3*(L — H)•2/3(L — H) + 1•Low•3/4*(L — C)•1/2*(L — C)•Close

### Doji Candlesticks

If a candlestick is doji (Close = Open), previous candlesticks are analyzed. If the previous candlestick was rising, this one is considered falling, and vice versa.

### Plotting a Shadow on Three Points

If three points are used for generating a shadow, and integer values of 3/4 of the shadow size and 1/2 of the shadow size are equal (this happens when the difference between the Open and Low or Open and High is not more than 2 points, i.e. distances of price steps forward and backward are equal), then the shadow generation changes the following way:

Bull Candlestick

 Calculation of Points Image •Open•Open — 1•Open•Low

Bear Candlestick

 Calculation of Points Image •Open•Open + 1•Open•High
 A close shadow is generated the same way.

### Plotting a Shadow on Two Points

If a shadow is generated based on two points, the generation is performed the following way:

Bull Candlestick

 Calculation of Points Image •Open•Open + 1•Low

Bear Candlestick

 Calculation of Points Image •Open•Open — 1•High
 A close shadow is generated the same way.

### Plotting the Candlestick Range

The candlestick range is generated by waves in loops. If Prev = Low (previous price is the Low price), the following loop is used:

N1 = Prev + Step

N2 = Prev + Step — 1

Prev = N2

If Prev = High (previous price is the High price), the following loop is used:

N1 = Prev — Step

N2 = Prev — Step + 1

Prev = N2

Here:

N1, N2 — reference points in one loop;
Prev — previous price;
Step — step size. The step is calculated by the formula: (H — L — 1)/(Number of loops) + 1;
The number of loops is calculated by the following formula: (Number of reference points in the range + 1)/2.

### Intermediate Ticks

Intermediate ticks between reference points are generated according to the following rules:

If the number of ticks is larger than the number of pips between the reference points, "saw" is generated (initial value +/— 1).

If the number of pips between reference points is large enough, a linear sequence of ticks is generated.

## Tick Generation Modes

In the window of the strategy tester settings, you can select a mode of generation of ticks. The following variants are available:

### Every Tick

In this mode all ticks are generated — OHLC and intermediate ticks. The procedure of such generation of ticks is described above.

### 1 Minute OHLC

In this mode only OHLC ticks of 1 minute bars are generated. If the tick volume of a candlestick is greater than 4, then only four prices (Open, High, Low and Close) are generated. If the tick volume is less than 4, then the rules of generating, described above, are applied.

 Choosing this mode doesn't mean that the testing/optimization will be performed exactly on the M1 period. For example, if you select the H1 period in the "OHLC on M1" mode, the prices will be generated on each minute bar for the values Open, High, Low and Close. In this case the OnTick() event of the Expert Advisor will be run four times a minute — at open, close, minimum and maximum of each minute bar, although testing is performed on H1.

In fact, OHLC prices exist in history data. Thus, only the time when Open, High, Low and Close ticks were received, is generated during testing, the price values are taken from the history.

### Open Prices Only

In this mode, OHLC prices of bars of the timeframe selected for testing are generated. The Expert Advisor function OnTick() runs only at the beginning of the bar (at the Open price). Due to this feature, Stop Levels and pending orders may trigger at a price different from the specified one (especially when testing at higher timeframes). But this allows you to quickly run an evaluation test of an Expert Advisor.

 The exceptions are periods W1 and MN1, for which bars are generated once a day instead of once a week or a month respectively.

There are some limitations on the "Open Prices Only" mode:

You cannot use the Random Delay execution mode.

In the tested Expert Advisor, you cannot access data of the timeframe lower than that used for testing/optimization. For example, if your run testing/optimization on the H1 period, you can access data of H2, H3, H4 etc., but not M30, M20, M10 etc. In addition, the higher timeframes that are accessed must be multiple of the testing timeframe. For example, if you run testing in M20, you cannot access data of M30, but it is possible to access H1. These limitations are connected with the impossibility to obtain data of lower or non-multiple timeframes out of the bars generated during testing/optimization.

Limitations on accessing data of other timeframes also apply to other symbols whose data are used by the Expert Advisor. In this case the limitation for each symbol depends on the first timeframe accessed during testing/optimization. Suppose, during testing on EURUSD H1, an Expert Advisor accesses data of GBPUSD M20. In this case the Expert Advisor will be able to further use data of EURUSD H1, H2, etc., as well as GBPUSD M20, H1, H2 etc.

 The mode of all ticks generation is the most accurate, but the slowest one. For quick, but rough testing/optimization, use the "Open prices only" mode.

### Mathematical Calculations

This mode allows you using the Strategy Tester for mathematical computations. It does not require and therefore does not load historical data and information about symbols, and it does not generate ticks. In the tested Expert Advisors, only OnInit(), OnTester() and OnDeinit() are called sequentially.

In this mode, the Custom optimization criterion is automatically applied as the optimization criterion. All the fields in the tester settings, except for the optimization mode and Expert Advisor selection, become inactive.

Mathematical calculations are useful for search of an extremum of a mathematical function, whose value should be returned from OnTester(). Optimization is conducted for finding the maximum value of the function. With a large number of combinations of input parameters of a mathematical function, it is recommended to use the "Genetic algorithm". This allows us to significantly accelerate the search for an extremum.

MetaTrader 5 — a complex trading platform for providing brokerage services on the retail Forex market.