Machine learning functions

Zorro can use internal or external machine learning algorithms, f.i. from R packages, for finding price or indicator patterns that precede profitable trades. Three internal prediction methods are available: a decision tree, a simple neural network, and a candle pattern detector. All methods automatically generate trading rules separately for any WFO cycle, asset, algorithm identifier, and long or short trade. The advise functions allow to implement machine learning in a strategy just like a simple indicator:

adviseLong (int Method, var Objective, var Signal0, var Signal1, ..., var Signal19): var

adviseShort (int Method, var Objective, var Signal0, var Signal1, ... var Signal19): var

adviseLong (int Method, var Objective, var* Signals, int NumSignals): var

adviseShort (int Method, var Objective, var* Signals, int NumSignals): var

Machine learning functions for training or predicting trade returns or price curves. The function takes the signals as parameters, and returns the win or loss prediction. In training mode, Zorro learns rules that predict the success of a trade from the signals, and generates a C function or model containing those rules. In test or trade mode, the trained model or C function is executed on every advise call, applies the rules to the current signals, and returns the trade success prediction.

Parameters:

Method

SIGNALS - don't predict; just export Signals and Objective in [Train] mode to a Data\signals.csv file for testing external machine learning functions.
NEURAL - train and predict with an external machine learning algorithm.
DTREE
- train and predict with a decision tree.
PERCEPTRON - train and predict with a simple neural net (20 signals max).
PATTERN - train and predict with a signal pattern analyzer (20 signals max).
+FAST - fast and large pattern finding (for PATTERN).
+FUZZY - fuzzy pattern finding (for PATTERN).
+2 .. +6 - number of pattern groups (for PATTERN).
+BALANCED - enforce the same number of positive and negative target values by replication (for SIGNALS, NEURAL, DTREE, PERCEPTRON).
0 - use the method and signals of the last advise call. Only when trade returns are used for the training target, not when Objective is used.
If a 0 or omitted, use the method and signals of the last advise call.

Objective The training target value. If at 0 or omitted, use the next trade return for the target. In that case the target is the profit or loss including trading costs of the immediately following trade with the same asset, algo, and matching trade direction. A positive value advises to enter a trade when this signal combination occurs, a negative value advises against a trade. The Objective parameter is only used in the training run and has no meaning in test or trade mode. Use the PEEK flag for accessing future prices in training. When using a direct training target, make sure that it is not 0 at the first advise() calls, or else the next trade return will be used.
Signal0,
... Signal19
Up to 20 parameters that are used as features to the machine learning algorithm for training or prediction. Use signals that carry information about the current market situation, for instance candle patterns, price differences, indicators, filters, or statistics functions. All signal values should be in the same range, for instance 0..1, -1..+1, or -100..+100 (see remarks). Signals largely outside that range will generate a warning message. If the signals are omitted, the signals from the last advise call are used.
Signals Alternative input method, a var array of arbitrary length containing the features to the machine learning algorithm.
NumSignals Length of the Signals array.

Returns:

In [Train] mode: 0 when Objective != 0, otherwise 100 (so that dependent trades are always executed for training).
In Test], [Trade] mode: Prediction value returned from the trained algorithm, for triggering trades when exceeding a threshold. The internal methods normally return a value in the -100 .. +100 range.
 

Decision Tree

A decision tree is a tree-like graph of decisions by comparing signals with fixed values (a short algorithm description can be found on the machine learning overview). The values and the tree structure are generated in the training run. For this the training process iterates through the sets of signals and finds the signal values with the lowest information entropy. These values are then used to split the data space in a profitable and a non profitable part, then the process continues with iterating through the parts. Details about the decision tree algorithm can be found in books about machine learning.

The signals should be normalized roughly to the -100..100 range for best precision. They should be carefully selected so that the displayed prediction accuracy is well above 60% in all WFO cycles. Decision trees work best with signals that are independent of each other. They do not work very well when the prediction depends on a linear combination of the signals. In order to reduce overfitting, the resulting trees are pruned by removing non-predictive signals. The output of the tree is a number between -100 .. +100 dependent on the predictive quality of the current signal combination.

The decision tree functions are stored in C source code in the \Data\*.c file. The functions are automatically included in the strategy script and used by the advise function in test and trade mode. They can also be exported for using them in strategy scripts or expert advisors of other platforms.The example below is a typical Zorro-generated decision tree:

int EURUSD_S(var* sig)
{
if(sig[1] <= 12.938) {
if(sig[3] <= 0.953) return -70;
else {
if(sig[2] <= 43) return 25;
else {
if(sig[3] <= 0.962) return -67;
else return 15;
}
}
}
else {
if(sig[3] <= 0.732) return -71;
else {
if(sig[1] > 30.61) return 27;
else {
if(sig[2] > 46) return 80;
else return -62;
}
}
}
}

The advise() call used 5 signals, of which the first and the last one - sig[0] and sig[4] - had no predictive power, and thus were pruned and do not appear in the tree. Unpredictive signals are displayed in the message window.

Example of a script for generating a decision tree:

void run()
{
  BarPeriod = 60;
  LookBack = 150;
  TradesPerBar = 2;
  if(Train) Hedge = 2;
  set(RULES|TESTNOW);
// generate price series
  vars H = series(priceHigh()), 
    L = series(priceLow()),
    C = series(priceClose());

// generate some signals from H,L,C in the -100..100 range 
  var Signal1 = (LowPass(H,1000)-LowPass(L,1000))/PIP;
  var Signal2 = 100*FisherN(C,100);

// train and trade the signals 
  Stop = 4*ATR(100); 
  TakeProfit = 4*ATR(100);
  if(adviseLong(DTREE,0,Signal1,Signal2) > 0)
    enterLong();
  if(adviseShort(DTREE,0,Signal1,Signal2) > 0)
    enterShort();
}

Perceptron

A perceptron is a simple neural net, consisting of one neuron with up to 20 signal inputs and one binary output. It calculates its predictions from a linear combination of weighted signals. A short preceptron algorithm description can be found on the machine learning overview. The signal weights are generated in the training run for producing the best possible prediction.

The perceptron algorithm works best when a weighted sum of the signals has predictive power. It does not work well when the prediction requires a nonlinear signal combination, i.e. when trade successes and failures are not separated by a straight plane in the signal space. A classical example of a function that a perceptron can not emulate is a logical XOR. Often a perceptron can be used where a decision tree fails, and vice versa.

The perceptron learning algorithm generates prediction functions in C source code in the \Data\*.c file. The functions are automatically included in the strategy script and used by the advise function in test and trade mode. They can also be exported for using them in strategy scripts or expert advisors of other platforms. The output is binary: either >0 for a positive or <0 for a negative prediction. A generated perceptron function with 3 signals can look like this:

int EURUSD_S(var* sig)
{
if(-27.99*sig[0] + 1.24*sig[1] - 3.54*sig[2] > -21.50)
return 100;
else
return -100;
}
Signals that do not contain useful market information get a weight of 0.
 

Pattern Analyzer

The pattern analyzer is an intelligent version of classic candle pattern indicators. It does not use some old predefined patterns, but learns them from historic price data. It's normally fed with up to 20 open, close, high or low prices of a number of candles. It compares every signal with every other signal, and uses the comparison results - greater, smaller, or equal - for classifying the pattern. Equality is detected in a 'fuzzy' way: Signals that differ less than the FuzzyRange are considered equal.

The signals can be divided into groups with the PATTERN+2 .. PATTERN+6 methods. They divide the signals into two to six pattern groups and only compare signals within the same group. This is useful when, for instance, only the first two candles and the last two candles of a 3-candle pattern should be compared with each other, but not the first candle with the third candle. PATTERN+2 requires an even number of signals, of which the first half belongs to the first and and the second half to the second group. PATTERN+3 likewise requires a number of signals that is divisible by 3, and so on. Pattern groups can share signals - for instance, the open, high, low, and close of the middle candle can appear in the first as well as in the second group - as long as the total number of signals does not exceed 20.

Aside from grouping, Zorro makes no assumptions of the signals and their relations. Therefore the pattern analyzer can be also used for other signals than candle prices. All signals within a pattern group should have the same unit for being comparable, but different groups can have different units. For candle patterns, usually the high, low, and close of the last 3 bars is used for the signals - the open is not needed as it's normally identical with the close of the previous candle. More signals, such as the moving average of the price, can be added for improving the prediction (but in most cases won't).

The pattern analyzer generates pattern finding functions in C source code in the \Data\*.c file. The functions are automatically included in the strategy script and used by the advise function in test and trade mode. They can also be exported for using them in strategy scripts or expert advisors of other platforms. They find all patterns that occurred 4 or more times in the training data set and had a positive profit expectancy. They return the pattern's information ratio - the ratio of profit mean to standard deviation - multiplied with 100. The better the information ratio, the more predictive is the pattern. A typical pattern finding function with 12 signals looks like this:

int EURUSD_S(float* sig)
{
  if(sig[1]<sig[2] && eqF(sig[2]-sig[4]) && sig[4]<sig[0] && sig[0]<sig[5] && sig[5]<sig[3]
    && sig[10]<sig[11] && sig[11]<sig[7] && sig[7]<sig[8] && sig[8]<sig[9] && sig[9]<sig[6])
      return 19;
  if(sig[4]<sig[1] && sig[1]<sig[2] && sig[2]<sig[5] && sig[5]<sig[3] && sig[3]<sig[0] && sig[7]<sig[8]
    && eqF(sig[8]-sig[10]) && sig[10]<sig[6] && sig[6]<sig[11] && sig[11]<sig[9])
      return 170;
  if(sig[1]<sig[4] && eqF(sig[4]-sig[5]) && sig[5]<sig[2] && sig[2]<sig[3] && sig[3]<sig[0]
    && sig[10]<sig[7] && eqF(sig[7]-sig[8]) && sig[8]<sig[6] && sig[6]<sig[11] && sig[11]<sig[9])
      return 74;
  if(sig[1]<sig[4] && sig[4]<sig[5] && sig[5]<sig[2] && sig[2]<sig[0] && sig[0]<sig[3] && sig[7]<sig[8]
    && eqF(sig[8]-sig[10]) && sig[10]<sig[11] && sig[11]<sig[9] && sig[9]<sig[6])
      return 143;
  if(sig[1]<sig[2] && eqF(sig[2]-sig[4]) && sig[4]<sig[5] && sig[5]<sig[3] && sig[3]<sig[0]
    && sig[10]<sig[7] && sig[7]<sig[8] && sig[8]<sig[6] && sig[6]<sig[11] && sig[11]<sig[9])
      return 168;
  ....
  return 0;
}

The eqF function in the code above checks if two signals are almost equal, i.e. differ less than FuzzyRange.

There are two additional special methods for the pattern analyzer. The FUZZY method generates a pattern finding function that also finds patterns that can slightly deviate from the profitable patterns in the training data set. It gives patterns a higher score when they 'match better'. The deviation can be set up with FuzzyRange. A typical fuzzy pattern finding function looks like this:

int EURUSD_S(float* sig)
{
  double result = 0.;
  result += belowF(sig[1],sig[4]) * belowF(sig[4],sig[2]) * belowF(sig[2],sig[5]) * belowF(sig[5],sig[3]) * belowF(sig[3],sig[0])
    * belowF(sig[10],sig[11]) * belowF(sig[11],sig[7]) * belowF(sig[7],sig[8]) * belowF(sig[8],sig[9]) * belowF(sig[9],sig[6]) * 19;
  result += belowF(sig[4],sig[5]) * belowF(sig[5],sig[1]) * belowF(sig[1],sig[2]) * belowF(sig[2],sig[3]) * belowF(sig[3],sig[0])
    * belowF(sig[10],sig[7]) * belowF(sig[7],sig[11]) * belowF(sig[11],sig[8]) * belowF(sig[8],sig[9]) * belowF(sig[9],sig[6]) * 66;
  result += belowF(sig[4],sig[1]) * belowF(sig[1],sig[2]) * belowF(sig[2],sig[0]) * belowF(sig[0],sig[5]) * belowF(sig[5],sig[3])
    * belowF(sig[10],sig[11]) * belowF(sig[11],sig[7]) * belowF(sig[7],sig[8]) * belowF(sig[8],sig[6]) * belowF(sig[6],sig[9]) * 30;
  result += belowF(sig[1],sig[4]) * belowF(sig[4],sig[2]) * belowF(sig[2],sig[5]) * belowF(sig[5],sig[3]) * belowF(sig[3],sig[0])
    * belowF(sig[7],sig[10]) * belowF(sig[10],sig[11]) * belowF(sig[11],sig[8]) * belowF(sig[8],sig[6]) * belowF(sig[6],sig[9]) * 70;
  result += belowF(sig[4],sig[5]) * belowF(sig[5],sig[1]) * belowF(sig[1],sig[2]) * belowF(sig[2],sig[3]) * belowF(sig[3],sig[0])
    * belowF(sig[7],sig[10]) * belowF(sig[10],sig[8]) * belowF(sig[8],sig[11]) * belowF(sig[11],sig[9]) * belowF(sig[9],sig[6]) * 108;
  ...
  return result;
}

The belowF function is described on the Fuzzy Logic page.

The FAST method does not generate C code; instead it generates a list of patterns that are classified with alphanumeric names. For finding a pattern, it is classified and its name compared with the pattern list. This is about 4 times faster than the pattern finding function in C code, and can also handle bigger and more complex patterns. It can make a remarkable difference in backtest time or when additional parameters have to be trained. A pattern name list looks like this (the numbers behind the name are the information ratios):

/* Pattern list for EURUSD_S
HIECBFGAD 61
BEFHCAIGD 152
EHBCIFGAD 73
BEFHCIGDA 69
BHFECAIGD 95
BHIFECGAD 86
HBEIFCADG 67
HEICBFGDA 108 ...*/

The FAST method can not be used in combination with FUZZY or with FuzzyRange. But the FAST as well as the FUZZY method can be combined with pattern groups (f.i. PATTERN+FAST+2).

The find rate of the pattern analyzer can be adjusted with two variables:

PatternCount

The minimum number of occurrences of the found patterns in the analyzed price curve; default = 4.

PatternRate

The minimum win rate of the found patterns, in percent; default = 50.

An example of a pattern trading script can be found in Workshop 7.
 

General Machine Learning

The NEURAL method uses external machine learning libraries with neural networks, support vector machines, or deep learning algorithms for predicting trade results. Such algorithms are available in R packages; therefore the NEURAL method will often call R functions for training and prediction. Alternatively, any DLL-based machine learning library can be used (look here for accessing DLL classes and functions). The algorithm is implemented with a single user-provided function:

neural (int mode, ínt model, int numSignals, void* Data): var

This function is called several times during the training, test, or trade process. It has access to all global and predefined variables. Its behavior depends on mode:

mode Parameters Description
NEURAL_INIT --- Initialize the machine learning library (f.i. by calling Rstart) before running the simulation. Return 0 if the initialization failed, otherwise 1. The script is aborted if the system can not be initialized.
NEURAL_EXIT --- Close the machine learning library (not required for R).
NEURAL_LEARN model, numSignals, Data Use a single sample of signals contained in the Data double array for training the model identified by the model index. The last element, Data[numSignals], is the Objective parameter or the trade result. The function is triggered by any advise call in [Train] mode, either immediately (Objective != 0) or when the trade is closed (Objective == 0).
NEURAL_TRAIN model, numSignals, Data

Batch training. Alternative to NEURAL_LEARN: train a model (identified by the model index) with all collected data samples. The function is called at the end of every asset/algo loop cycle in the training run. The model number counts the asset, algo, and long/short combinations. The samples are provided in CSV format in the Data string. The columns of the CSV table are the signals, the last column is the Objective parameter or the trade result. The prediction accuracy in percent can be optionally returned by the neural function; otherwise return 1 if no accuracy is calculated, or 0 for aborting the script when the training failed.

NEURAL_PREDICT model, numSignals, Data Return the value predicted by the model with the given model index, using the signals contained in the Data double array. The function is called by an advise call in [Test] and [Trade] mode. The model parameter is the number of the model.
NEURAL_SAVE Data Save all trained models in the file with the name given by the string Data. The function is called at the end of every WFO cycle in [Train] mode.
NEURAL_LOAD Data Load all trained models from the file with the name given by the string Data. The function is called at the begin of every WFO cycle in [Test] mode, and at the begin of a [Trade] session. It is also called every time when the model file is updated by re-training.

The model index is the number of the trained model - for instance a set of decision rules, or a set of weights of a neural network - starting with 0. When several models are trained for long and short predictions and for different assets or algos, the index selects one of the models. In R, models can be stored in a list of lists and accessed through their index (f.i. Models[[model+1]]). Any aditional parameters generated in the training process - for instance, a set of normalization factors or selection masks for the signals - can be saved together with the models.

The numSignals parameter is the number of signals passed to the advise function. It is normally identical to the number of trained features.

The Data parameter provides data to the function. The data can be of different type. For NEURAL_LEARN/NEURAL_PREDICT it's a pointer to a double array of length numSignals+1, containing the signal values plus the prediction objective or trade result at the end. Note that a plain data array has no "dim names" or other R gimmicks - if they are needed in the R training or predicting function, add them there. For NEURAL_TRAIN the Data parameter is a text string containing all samples in CSV format. The string can be stored in a temporary CSV file and then read by the machine learning algorithm for training the model. For NEURAL_SAVE/NEURAL_LOAD the Data parameter is the suggested file name for saving or loading the trained models separately for any WFO cycle in the Data folder. Use the slash(string) function for converting backslashes to slashes when required for R file paths.

This is the default neural function in the r.h file for using a R machine learning algorithm. It expects 4 R functions named neural.train, neural.predict, neural.save, and neural.init in a R script in the Strategy folder. The R script has the same name as the strategy script, but extension .r instead of .c. If required for special purposes, the default neural function can be replaced by a user-supplied function.

var neural(int mode, int model, int numSignals, void* Data)
{
if(!wait(0)) return 0;
// open an R script with the same name as the stratefy script
if(mode == NEURAL_INIT) {
if(!Rstart(strf("%s.r",Script),2)) return 0;
Rx("neural.init()");
return 1;
}
// export batch training samples and call the R training function
if(mode == NEURAL_TRAIN) {
string name = strf("Data\\signals%i.csv",Core);
file_write(name,Data,0);
Rx(strf("XY <- read.csv('%s%s',header = F)",slash(ZorroFolder),slash(name)));
Rset("AlgoVar",AlgoVar,8);
if(!Rx(strf("neural.train(%i,XY)",model+1),2))
return 0;
return 1;
}
// predict the target with the R predict function
if(mode == NEURAL_PREDICT) {
Rset("AlgoVar",AlgoVar,8);
Rset("X",(double*)Data,numSignals);
Rx(strf("Y <- neural.predict(%i,X)",model+1));
return Rd("Y[1]");
}
// save all trained models
if(mode == NEURAL_SAVE) {
print(TO_ANY,"\nStore %s",strrchr(Data,'\\')+1);
return Rx(strf("neural.save('%s')",slash(Data)),2);
}
// load all trained models
if(mode == NEURAL_LOAD) {
printf("\nLoad %s",strrchr(Data,'\\')+1);
return Rx(strf("load('%s')",slash(Data)),2);
}
return 1;
}

The neural function is compatible with all Zorro trading, test, and training methods, including walk forward analysis, multi-core parallel training, and multiple assets and algorithms. An example of using advise(NEURAL,...) for a short-term deep learning system can be found on Financial Hacker.

Remarks:

See also:

optimize, frechet, polyfit, predict, Workshop 7, R Bridge, user supplied functions

► latest version online