The [B]advise[/B] function is the machine learning algorithm. It looks like a strange entry condition for a long trade:
[B]if (adviseLong (PATTERN+2,0,
priceHigh(2),priceLow(2),priceClose(2),
priceHigh(1),priceLow(1),priceClose(1),
priceHigh(1),priceLow(1),priceClose(1),
priceHigh(0),priceLow(0),priceClose(0) ) > 30)
enterLong();[/B]
Alice calls [B]adviseLong [/B]with the [B]PATTERN[/B] method and the High, Low, and Close prices of the last 3 candles. If [B]adviseLong[/B] returns a value above 30, a long trade is entered. But when does this happen?
In training mode, the [B]adviseLong[/B] function always returns [B]100[/B]. So a trade is always entered. The function stores a âsnapshotâ of its signal parameters - in this case, 12 signals from the High, Low, and Close prices of the last 3 candles - in an internal list. It then waits for the result of the trade, and stores the profit or loss of the trade together with the signal snapshot. Thus, after the training run Zorro has a long internal list containing all signal snapshots and their corresponding trade profits or losses.
The signals are then classified into patterns. Alice uses the [B]PATTERN+2[/B] classification method. It splits the signals into two equal groups, each with 6 signals. The first group contains the prices of the first two candles of the 3-candle sequence:
[B]priceHigh(2),priceLow(2),priceClose(2),
priceHigh(1),priceLow(1),priceClose(1)[/B]
And the second group contains the prices of the last two candles:
[B]priceHigh(1),priceLow(1),priceClose(1),
priceHigh(0),priceLow(0),priceClose(0)[/B]
Note that the middle candle, with offset 1, appears in both groups. The Open price is not used in the signals because currencies are traded 24 hours a day, so the Close of a daily bar is normally identical to the Open of the next bar. Using the Open price would emphasize outliers and weekend patterns, which is not desired.
Within every signal group, Zorro now compares every signal with every other signal. This generates a huge set of greater, smaller, or equal results. This set of comparison results classifies a pattern. It does not matter if [B]priceHigh(2)[/B] is far smaller or only a little smaller than [B]priceHigh(1)[/B] - the resulting pattern is the same. The patterns of the two groups are now glued together to form a single pattern. It contains all information about all price comparisons within the first and the second and within the second and the third candle, but the pattern does not contain any information about how the first candle compares with the third. Bert had told Bob that itâs best for price action trading to compare only adjacent candles - therefore the two independent pattern groups. If Alice had looked for 4-candle-patterns, sheâd used three groups.
After the pattern was generated, Zorro checks how often it appears in the list, and sums up all its profits or losses. If a pattern appears often and with a profit, it is considered a profitable pattern. Zorro removes all unprofitable or insignificant patterns from the list - patterns that donât have a positive profit sum or appear less than 4 times. The remaining patterns are stored in the [B]workshop7_EURUSD.rul[/B] files in the [B]Data[/B] folder - one file per walk forward cycle. Such a file looks like this:
// Prediction rules generated by Zorro
/* Pattern list for EURUSD_L
FCDEABFACEBD 25 4.334 11.562
FADEBCFACEBD 8 2.992 9.572
FACEBDECDFAB 19 8.289 9.673
ECDFABFBCEAD 21 3.263 10.264
FADEBCFCDEAB 27 1.641 13.833
FCDEABDBCFAE 10 6.041 16.218
DBCFAEEADFBC 19 1.207 12.651
EACFBDFACEBD 11 8.549 13.986
DACFBEEACFBD 29 2.534 16.527
FBDEACEADFBC 5 3.901 11.461
EADFBCFBDEAC 5 3.697 7.563
ECDFABFCDEAB 35 0.894 15.564
EACFBDDACFBE 22 6.782 23.896
...
We can see that any line in the list begins with a strange letter combination, like [B]FCDEABFACEBD[/B]. This combination is the unique pattern name that represents the set of comparison results. The number next to the name is the pattern frequency - [B]FCDEABFACEBD[/B] appeared [B]25[/B] times in the training period. Its average profit per trade was [B]4.334 $[/B], and the standard deviation of the profits was [B]11.562 $[/B]. The frequency, average profit, and standard deviation are later used by Zorro for calculating the patternâs information ratio.
This happens when testing or trading the strategy. The [B]adviseLong[/B] function generates a pattern from the current signals, and compares it with the stored patterns in the [B].rul[/B] file. If no stored pattern matches the current one, the function returns [B]0[/B]. Otherwise it returns the patternâs information ratio multiplied with [B]100[/B]. The higher the information ratio, the more profitable is the pattern. Of course, patterns with high information ratio are less frequent. So the trade entry threshold should be a compromise between pattern profitability and frequency. Alice used a threshold of [B]30[/B] here, meaning that a trade is entered for any pattern with information ratio above [B]0.3[/B].
Short trading just works the same way:
[B]if(adviseShort(PATTERN+2,0) > 30)
enterShort(); [/B]
The [B]adviseShort[/B] call has no signal parameters. In that case the function uses the same signals as the last advise call, which was the preceding [B]adviseLong[/B]. This way long signal lists donât have to be written twice.
Tomorrow weâll go through the rest of the script. Pattern recognition is one of the few machine learning functions that work for trading with a relatively simple setup. Donât hesitate to ask if something is unclear with this method.