ńňđ. 6 |

Intermarket Divergence Neural Network for the S&P500

An when gold is positively correlated to T-Bonds. This is because when in-

terest rates fall quickly, inflation fears are fueled and can cause a major

To develop a neural network model based on our intermarket divergence

rally in gold. This type of information is very valuable when trading using

example for the S&P500, we will use a forward-feed neural network. This

intermarket analysis.

type of network includes all of the variations on backpropagation. On the

The second important tool that can improve the performance of inter-

basis of my research, I have found that the following neural network prod-

market analysis-based systems is the concept of predictive correlation.

ucts, all based on variations of backpropagation, work best: Braincel from

Briefly, the concept works as follows. Letâ€™s say we are trading the S&P500

Promised Land Technologies, Inc.; N-TrainTM, from Scientific Consul-

using T-Bonds. We could correlate a five-day change in the T-Bonds five

tant Services; and Predict, from NeuralWare.

days ago to the current five-day change in the S&PSOO. Effectively, we

Before we select our target and design our preprocessing, we need to

would learn how predictive the past changes in T-Bonds are with regard

understand how a human expert trader would use our simple system and

to future price changes in the S&P500 over the period used in the corre-

intermarket analysis as a tool for his or her discretionary trading. If we

lation. If the predictive correlations remained constant, which they do not,

can understand how a great trader processes this information, we can de-

we would effectively be able to predict future price changes and direction

velop preprocessing that will allow a neural network to extract from these

using intermarket oscillators, price changes, and the predictive correla-

data the same information that a human expert would look for.

tions. But they do not remain constant, so we need some way for the model

The first priority of a human expert would be patterns between the os-

to estimate these correlations in the future. We can attempt this estimate

cillators for the intermarket and the market we are trading. For example,

by using rates of change of the predictive correlation in our model.

if the intermarket oscillator for T-Bonds makes higher highs and the os-

cillator for the S&P500 makes lower highs, it might mean that the mar-

ket will move with a choppy upward bias. On the other hand, if the Developing Preprocessing for Intermarket Neural Networks

S&P500 oscillator is making lower lows while T-Bonds are making higher

Now that we have a general understanding of how we can use intermar-

highs, then we could be on the verge of a major rally in the S&PSOO. This

ket divergences to predict future market price action, letâ€™s discuss our

is only one example of the kinds of patterns an expert trader can learn to

preprocessor based on intermarket divergence,

identify. A human expert learns how to identify these patterns by seeing

The first step is to decide what target to use for the neural network. In

how each pattern affects future price changes in the market he or she is

our example, we will use a five-period percentage change with a smooth

trading. The trader may not even be aware of exactly what rules he or she

three-bar moving average. The smoothing makes this an easier target for

is using.

Usinr! Advanced Technoloplies to Develop Trading Strategies

272

Developing a Neural Network 273

the network to predict, but the prediction is still made up of all future TABLE 18.7 INPUTS TO AN INTERMARKET

data. This target follows future trends but is resistant to daily noise. We DIVERGENCE NEURAL NETWORK.

can predict a price-based target because intermarket analysis can be used

Standard Technical Indicators

1.

to infer future price changes based on the equilibrium between strongly ADX with a period of 10

linked markets. This relationship is strong enough that we can consider ADX Difference 2 day

intermarket analysis a form of real-time fundamental analysis. ADX Difference 5 day

After we have selected our target, we need to design a preprocessor

SlowK 14 day

based on a combination of technical and intermarket analysis. We se- SlowD 14 day

lected the periods used in our indicators on the basis of the profitability

SlowK - SlowD

and robustness of simple rule-based intermarket divergence systems that Slowk - SlowK[51

used these same indicators. Sample five-day difference both two and five days back

Our preprocessor used nine different classes of indicators that were

2. Intermarket Inputs

sampled using the method discussed in Chapter 17. This preprocessing is Let Inter equal the close of the intermarket.

shown in Table 18.7. Let TrMarket equal the close of the market we are trading.

The inputs and target shown in Table 18.7 are a good starting point for N = N bars ago.

developing even a generic intermarket and technical analysis neural net- +N= N bars into the future.

work preprocessor. This preprocessing model works best on the S&P500

3. Intermarket Oscillators

but also produces acceptable results on T-Bonds using the CRB index as

well as on T-Bonds using the UTY index.

Letâ€™s IKJW discuss how we selected the inputs used in our preprocessor.

The first inputs we used were technically based indicators. In this case, Inter

lnterosc = Log

we used both ADX and stochastic. We used a IO-bar period for ADX, Average(fnter, NI

and both a two-day and a five-day difference. The ADX value tells us

Sample these indicators using bars that are 0,2,5,10,15,20,25 bars ago.

whether the market is trending, and the difference tells us whether the

4. Intermarket Percent Change

trend is getting stronger or weaker.

Next, we used a 14-day SlowK and SlowD (SlowK - SlowD) and a Inter - InterN

InterPerChg = *100

five-day rate of change of SlowK. Stochastics can tell us whether the Inter

market is overbought or oversold. Another little kwwn use of stochas-

v Sampled using 2,5,7,10,15,25 bars ago.

tics is in predicting a price target for a given move. George Lane, in a

5. Traded Market OsciNators

seminar at Futures West in September 1996, showed that when a cycle-

Let N = 5

tuned FastK crosses above 75, we have reached the midpoint of a given

rally, and when it crosses at 25, we have reached a midpoint of a given de- TrMarket o,

TrMarketOsc = Log

cline. The SlowK and SlowD crossings are useful in developing filters TrMarket,

for detecting a possible market turning point. Finally, the most important TrMarket

TrMarketOsc = Log

stochastic signal is divergence between the price and the oscillator. Be- AverageiTrMarket, N)

cause we are using a five-period difference in SlowK and the same period

i, Sample these indicators Using bars that are 0,2,5,10,15,20,25 bars ago,

log ratio for the price series, we˜have given the neural network what it

needs to detect divergence between stochastics Bnd price. Divergences, (continued)

as we have shown earlier, occur at tops and bottoms.

275

274 Developing: a Neural Network

Usine Advanced Technoloeies to Develop Trading. Strategies

(Continued) intermarket. The long-term correlation is used as a switch so that we can

TABLE 18.7

tell whether the market should continue to trend based on correlation.

6. lnternwket Percent Change

The short-term correlation is the same type of filter we discussed in

Let N = 5

Chapter 8. The predictive correlation between our intermarket oscillator

TrMarker - TrMarket, five days ago and our target is actually one of the most important inputs

â€˜100

, â€™ TrPerChg =

TrMarket in our model. This correlation gives us a measure of how currently pre-

dictive an intermarket oscillator is of our target, which, in turn, helps the

, Sampled using 0,2,5,7,10.15,25 bars ago

neural network develop price-based forecasts. Predictive correlations are

7. Intermarket Correlation

not constant, so we need a way of judging future values of these predic-

IntCor = Correlation(Inter,TrMarket,N)

tive correlations. We have attempted to supply this information to the

Sampled using 0,2,5,10 bars ago.

network by using simple rates of change of the predictive correlation

IntCorLong = Correlation(lnter,TrMarket,YJ

values.

â€śse a long-term correlation (for example, 40 or 50 bars) and only use

We used these inputs to develop a preprocessor coded in TradeStationâ€™s

todayâ€™s value.

EasyLanguage. We then applied this preprocessor to a multidata chart

8. Predictive Correlation

in TradeStation, using continuous backadjusted contracts for both the

PredCorrel = Correlation(lnterOscl51,Average˜TrPerChg,3):

S&P500 and T-Bonds for the period from 4/21/82 to 8/30/96. We will

Sampled using 0,2,5,10 bars ago.

use data for the period from 4/21/82 to 12/31/94 for training our neural

network. Our testing set will be from 111195 to 3/l/96, and our out-of-

9. Predictive Correlation Differences

sample set will be from 3/2/96 to g/30/96.

PredCorrel2 = Predcorrel - PredCorrel,

We used the results of our original intermarket-divergence S&PSOO/T-

PredCorrel5 = PredCorrel - Predcorrel,

Bond system, as well as several systems that filtered our original model

PredCorrel5 = PredCorrel - Predcorrel,,

using correlation and predictive correlation for discovering the best

These inputs were used to predict the following output:

periods to use for our preprocessed inputs. Table 18.8 shows the periods

JrMarket - JrMarket, *, o.

Target = Average selected on the basis of our research.

TrMarket

We generated a comma separated values (CSV) file, which we imported

into Excel. We split our data into the training (development), testing, and

out-of-sample sets. and trained our neural network using Braince13.0. Our

After our technical indicators, we have the components of our inter-

market divergence system. Because we want to normalize our inputs, we

TABLE 18.8 PARAMETERS USED TO GENERATE

will use logs of ratios to express simple differences. Our preprocessor A NEURAL NETWORK FOR THE S&P500.

will use rhe log ratio of either the current price to past prices, or the cur-

We used prices relative to a moving average for both the intermarket and trading

rent price to a moving average of price. This is done both for the market

market oscillators, and the following periods for our indicators:

we are trading and for the intermarket. In our S&P500 example, we will

S&P500 moving average length 12

use prices relative to a moving average for both the S&P500 and T-Bonds.

T-Bond moving average length 26

Next, we calculate a five-period percentage change for both the inter-

Short-term correlation length 15

market and the market we are trading. Long-term correlation length 40

After we have added our correlation inputs, we use both a long-term Len,qth used in predictive correlation 15

and a short-term correlation between the market we are trading and the

Developing a Neural Network

Using Advanced Technologies to Develop Trading Strategies

276 277

preprocessor created 49 inputs and 1 output. We tested various configu- TABLE 18.9 RAW RESULTS OF S&P500 NEURAL NETWORK

ON TRAINING, TESTING, AND OUT-OF-SAMPLE SETS.

rations using between 6 and 12 hidden nodes. We used an initial weight

range of? .lO and an initial learning rate of .lO. Braincel has an auto- Network 1 Network 2

learning rate adjuster that we had to disable.

Training:

We trained each network for between 150 and 350 cycles. Each con-

63.7%

% Correct 64.2%

figuration was trained at least five times. We had to train each configu-

,570

Correlation ,574

ration multiple times because training a neural network is an optimization

Testing/Out-of-sample combined set:

problem. If we start with different sets of initial weights, we will get dif-

63.5% 67.6%

% correct

ferent solutions to our problems. In general, a neural network that pro- .373 ,353

Correlation

duces similar results when trained with different sets of initial weights is

Out-of-sample set:

more robust and has a better chance of holding up in the future.

66.4%

% correct 68.8%

When developing a neural network, it is best to use the lowest number

,400 ,369

Correlation

of hidden nodes that will still produce good results. The reason is: When

evaluating the robustness of a neural network, you want to develop a net-

work with as high a ratio of training cases to weights as possible. The

higher this ratio is, the better chance that the network will continue to Neural networks are not magic; they must be used as part of a larger

work in the future. We first trained our neural network for the S&PSOO, system. If we use the raw output of a neural network and primitive rules,

using all of our inputs. We found that using 10 hidden nodes worked best. we can create a very profitable system, but it will often have properties

We then tested it after removing various inputs and found that, even that will make it untradable, such as large losing trades or high drawdown.

though we could remove some of these inputs without degrading perfor- Letâ€™s now see how Network 1 in Table 18.9 would have worked in a very

simple trading system. Our simple rule is: We will go long when this net-

mance based on the correlation to our target output, we did degrade its

trading performance. Removing those inputs changed the distribution of work crosses above zero, and short when it crosses below zero.

Letâ€™s see how this simple system performed during our training and

error between the target and our output. In this case, the network had the

combined testing/out-of-sample sets. This network was trained on Au-

same correlation and percentage of accuracy on the direction of our tar-

gust 30, 1996. We will include results up to October 4, 1996. These extra

get, but the distribution of errors changed. We now had more larger er-

data are truly out-of-sample.

rors that led to larger losing trades. Based on these results, we decided to

The results to October 4, 1996, for Network 1 (with $50.00 allowed

use the original set of inputs.

for slippage and commissions) are shown in Table 18.10.

Table 18.9 shows how two of the neural networks that were trained to

use 10 hidden nodes performed. Note the percent correct on direction Our total profit over the complete data set was $423,625.00 with a

drawdown of -$43,225.00. This is about the same ratio of drawdown to

and the correlation to the target for the training and the combined test-

profit as our original model, but we had almost 800 trades during this pe-

ing/out-of-sample sets, as well as the out-of-sample set by itself.

The network was constant over all three sets in predicting direction riod, or about six times more trades than our original model. More im-

portant, this model has done much better than the original model since

(measured by the percentage of the time the sign of the target was pre-

January 1995: it has made over seven times more money! The network has

dicted correctly). All of the networks that we trained using 10 hidden

held up well, from its training, on August 30, 1996, until the testing was

nodes and our original inputs trained well and produced consistent re-

completed on October 4, 1996. Over a two-month period, this network

sults.˜Our networks all had a lower correlation during the testing period

made $15,450.00 per contract, including open equity. The problem is that

than during the training set. This is normal and was within an acceptable

this system is untradable because of the high drawdown and large losing

range.

278 Using Advanced Technoloeies to Develoo Tradine Strategies

Developing a Neural Network 279

TABLE 18.10 TRADING RESULTS OF OUR until October 4, 1996, for both the training period and the combined test-

SELECTED NEURAL NETWORK. ing/out-of-sample period, are shown in Table 18.11,

Combined Testing and These results show how much better this system performed. Our new

Training Set Out-of-Sample Set neural-network-based system made $513,325.00 during the period from

4/21/82 to 1014196, while the buy and hold profit was only $217,000.00.

Net profit $329,575.00 $94,050.00 + open $8.600.00

Our profits on the short side were $114,025.00 during the training set

Trades 591 101 + open

Win% 50 54 and $30,&X00.00 during the combined testing and out-of-sample set. This

Profit factor 1.68 1 .a0 shows that the model outperforms the S&P500 during both bear and bull

Drawdown -$43,225.00 -$33,825.00 markets.

This was not the outcome for our rule-based simple divergence model.

During the period from E/30/96 to 10/4/96, we took the following trades:

trades. In general, most neural networks are not tradable when using such

Entry Date Exit Date Profir

primitive rules.

1. 8/16/96 (buy)

Letâ€™s now try to develop a more tradable system using this neural net- 916196 $8.200.00

2. 9/10/96 (buy)

work. Our first change is to use a two-day average of our neural network 9116196 $7,650.00

output to generate our signals. Next, we correlate a five-day percentage

A third and final trade was a buy on 9/23/96 at 697.05. We exited this

change smoothed using a three-day average with our neural network out-

trade with a $4,150.00 profit on 1 O/10/96. Our results show that this tech-

put five days ago. In effect, we are doing a predictive correlation between

â€śology can be used to develop a profitable and tradable S&P500 neural

our neural network output and our target. We also found that when the

network.

simple five-day difference of this correlation drops below -.20 we should

Our early analysis also shows that this intermarket divergence prepro-

not take any entry signals. Next. we found that the neural network thresh-

cessing model can be used to develop neural networks in other markets

old should be above .10 for our buy signals and below -. 10 for our sell sig-

where intermarket divergence can produce profitable trading systems.

nals. We exit long positions when the neural two-day average output

Neural networks have fallen out of favor on Wall Street, but the reports

crosses below zero, and short positions when it crosses above zero.

of their death have been grossly exaggerated.

This system significantly outperformed our original simple network

system; in fact, it produced a good S&P500 trading model. Our results,

TABLE 18.11 NEURAL NETWORK TRADING RESULTS

USING THRESHOLDS AND CORRELATION FILTER.

Combined Testing and

Training Set Out-of-Sample Set

$383,175.00 $122.050.00 + $&lOO.OO

Net profit

Trades 62 + open

374

Win% 55 66

Profit factor 2.27 2.74

Drawdown -$22,850 -$22,950.00

Machine Learning Methods for Develo˜ine Tradine Strateeies 281

variables are needed to train a neural network with maximum perfor-

mance and a minimum of architecture.

19 USING MACHINE INDUCTION FOR DEVELOPING

TRADING RULES

Machine learning The classic use of machine induction is to develop sets of rules that

Met hods for Developing classify a target output based on input variables. We can use machine

induction methods to develop trading strategies by developing rules that

Trading Strategies predict the output class of a target. For example, we can predict whether

the market will be higher or lower five days from now. When developing

rules using a machine induction method such as C4.5 or rough sets, which

are based on supervised learning, the process is very similar to the one

used for a neural network. Our first step is to select the target we want

to predict. Next, we need to develop preprocessing that is predictive of

that target. The main difference between developing a machine induc-

tion application and one using neural networks is that both the inputs and

the outputs must be made into discrete variables.

When developing our output classes, we use human expertise to either

Machine induction methods extract rules from data. We discussed the the-

select a discrete set of outputs or convert a continuous output into a se-

ory behind some of these methods in Chapter 11. This chapter gives an

ries of discrete values. One very simple but useful method is to use the

overview of how to use machine induction in various trading applications.

sign of a standard continuous output class like a five-day percentage

Table 19.1 lists some of the ways in which machine induction can be

change-for example, negative output = -1 and positive output = +l.

used to develop trading strategies.

When using more complex output classes, we should limit the number of

The examples given in Table 19.1 use machine induction either to de-

classes to less than 1 class per 500 training cases.

velop or to improve a trading strategy. In the first four examples in the

Letâ€™s now discuss how we can use a human expert to develop discrete

table, the rules developed by the machine induction method are used di-

values for our input variables. A human expert might set the number of

rectly as part of a trading strategy. In the fifth example in the table, the

classes and their levels, or just the number of classes. We can then use

information generated by machine induction helps in selecting which

statistical analysis or machine learning methods to find the correct lev-

els. As an example, if we were using SlowK as an input, we could break

it into three classes: (1) overbought, (2) neutral, and (3) oversold. We

19.1

TABLE USES OF MACHINE INDUCTION.

could set the level based on domain expertise. Normally, in this case,

we would use 30 and 70. We could also use various statistical and ma-

1. Developing trading rules.

chine learning methods to find these levels. For example, when analyz-

2. Extracting rules from a neural network.

ing the T-Bond market, I found that the critical levels for Slow K are 30

3. Combining trading strategies.

and 62. These are close to our standard values, and if we were to collect

4. Developing postprocessing for a neural network.

these statistics across multiple markets, they would probably be even

5. Eliminating inputs for use in other modeling method5 such as neural networks.

282 Using Advanced Technologies to Develop Trading Strategies Machine learning Methods for Developing Trading Strategies 283

closer to the standard levels, but we can fine-tune performance in a given than the number of supporting cases for the rules, because a rule will

market using these analytical methods. often be true for several days in a row.

One problem with using automatic methods for generating classes is

that sometimes the classes will cover a very small range of values and be

EXTRACTING RULES FROM A NEURAL NETWORK

based on a statistical artifact without any cause-and-effect relationship.

For example, if we have 10 cases when stochastics were between 51 and

One of the biggest problems with neural networks is that a trader cannot

53, and all 10 times the market rose, our statistical methods might de-

see how they arrived at their answers. This is a problem because it is hard

velop an input class with a range between 51 and 53. A human expert

to place a trade based on a black box when the premise that produces the

would know that this is just curve fitting and would not use such a class.

signal is unknown. Given this problem, why donâ€™t we just directly gener-

Machine generated classes are usually good, but they should be filtered

ate the rules from the data instead of using the neural network? The an-

with the expertise of a human trader.

swer is: Training the neural network gives us an input-output mapping

After developing our input and output classes, we need to divide our

without any conflicts. This mapping can be converted into rules very eas-

data into the development, testing, and out-of-sample sets. We then apply

ily because it does not contain any noise or conflicting cases. For the same

the machine learning method to the data. If we are using C4.5 or another

reason, it will often generate rules that are different from those directly

decision-tree-type method, we can develop these trees and then prune

generated from the original data.

their leaves in order to develop the best rules with the fewest terms. These

The concept of inducting rules from a neural network has been used by

rules then need to be tested further. If we are using rough sets, which are

many successful high-tech money management firms. LBS Capital Man-

currently available only as DataLogic/R from Reduct Systems, we need

agement used sensitivity analysis to develop rules from a neural network.

to assign a roughness level and a precision to our model. The roughness

LBS adjusted the level of the inputs, recorded the changes in output, and

level controls the complexity of the rules, and the precision determines

then used this information to develop rules. However, this method is very

the accuracy of the rules in identifying the output classes. If we use a

time-intensive, and the number of combinations that can be used for the

high level of roughness, such as .90, we will develop very simple rules

inputs, even in a small neural network with 20 inputs using real numbers.

with few terms, and these should generalize well. This removes the step

is so large that they could not be all tested in our lifetime.

of pruning that is normal when developing rules based on a commercial

We could use machine induction and a library of generated cases to

decision-tree-type product.

develop an approximate set of rules for our neural network. We could

After we have developed our rules, we will select the best candidates

then use sensitivity analysis to test only cases at the edge of the rules.

for trading. This process is discussed later in this chapter.

For example, if one condition of a given rule is Slow K > 60, we could

When we have finished this selection process, we need to develop a

test 59.5,59.0,58.5, and so on, until the output value changes class. This

trading strategy. For example, if we are predicting whether a market will

will make the problem solvable and will allow us to generate an approx-

be higher or lower five days from now, we must decide how we are going

imate rule set for the neural network.

to exit the trade. We can use any number of classic exit methods, or we

The rules we generate from our neural network will give us some idea

can just hold our position for the lookahead period of the target. We must

of the reliability of our neural network for a given set of input values.

be careful not to write our exits so that we exit and reenter in the same

For example, when we analyze the rules from the network, we might

direction and at the same price on the same day. This would happen, for

find that an area of the solution space is based on relationships that do

example, if we exited on the next open after being in a trade for five days,

not have any theoretical basis. This is very valuable information and

and our rule for entering the market also generated our exit. We also must

could allow us to use a neural network with more confidence. We can

realize that when we predict a classic target like percentage change and

also learn whether there are any areas of the solution space where the

use it as part of a trading strategy, the˜number of trades will be much less

284 285

Machine Learning Methods for Developing Trading Strategies

Usine Advanced Technologies to Develop Trading Strategies

TABLE 19.2 INPUTS FOR AN IMPROVED CHANNEL BREAKOUT

networkâ€™s performance is substandard, For example, our network might

NEURAL NETWORK BASED SYSTEM.

be 69 percent accurate overall, but when ADX is below 20, we might find

that it is only 55 percent accurate based on the rules for this condition. Current position for both trading methods.

Also, when the neural network is not performing well, the rules will help Trade performance information for both systems.

us judge whether the neural network is beginning to fail or whether the ADX and simple differences of ADX.

market is currently in a mode where it is not performing well even dur- Stochastic and difference of stochastic.

ing the training set. This information can be very valuable when either Current dominant cycle and rates of change oi the dominant cycle.

developing trading strategy or evaluating its real-time performance. Current phase and rates of change of phase.

Letâ€™s now itemize the steps involved in arriving at rules for a neural Normalized price mmnentum data: either simple differences or price relative to

network using machine induction: a moving average.

1. Generate new cases using valid real-world input combinations. At

least 50 cases per weight should be generated, and their distribu-

trading either system, and one for when we should be trading the sto-

tion should be based on the density of the occurrence of a given

chastic system. In this application, we should use the types of inputs

value in real-world data.

shown in Table 19.2.

2. Have the network output a value for each case.

We should sample all of these types of inputs when developing a

3. Induce rules using either rough sets or a decision-tree algorithm like model. Our goal is to develop a model that is better than either existing

C4.5. The resulting rules will not be exactly the rules used by the model in trading the market these systems were designed for.

neural network, but will be a good first approximation. This type of application works well and has been discussed by Paul

4. Use sensitivity analysis to fine-tune the rules by testing values at Refene in several different articles. The inputs listed in Table 19.2 are

the boundaries of the rules. predictive of which model to use for our example. In my research, as well

as research published by Refene, the trading results over the last three to

5. Repeat the sensitivity analysis until you have generated a satisfac-

five trades for each method we are switching between are actually among

tory rule set.

the most important inputs to our model. Both the cycle-based inputs and

ADX are used to tell whether the market is in a trending, cycle, or con-

COMBINING TRADING STRATEGIES solidation mode. Stochastics and price data interact to give us informa-

tion that can be used to help predict which method is currently the best

to trade. The concept of developing a switching model has been little dis-

Another valuable use for machine-induced rules is combining of multiple

cussed outside of academia, but should become a hot area of research

trading methods or switching between trading methods. Letâ€™s discuss an

over the next few years.

example. Suppose we have two different methods; one is a channel break-

out and the other is a trading system based on stochastics. Each of these

systems is profitable on its own. We then notice that these systemsâ€™ eq-

POSTPROCESSING A NEURAL NETWORK

uity curves are negatively correlated. This is a good example of why you

should use machine induction to combine trading systems.

One of the most powerful applications of machine induction is for

Our first step in developing such an application is to select our output.

postprocessing the results based on another modeling method, such as

In this case, we will have three discrete output classes: one for when we

neural networks. For example, we could use the past outputs from a

should be trading the channel breakout, one for when we should not be

Machine Learning Methods for Developing Trading Strategies

286 Using Advanced Technologies to Develop Trading Strategies 287

process is similar to developing standard rules using machine induction,

neural network and some other preprocessed inputs to try to predict

except that we allow more curve fitting. For example, we might use more

whether the neural networkâ€™s next forecast will be right or wrong. 1 have

than one output class per 500 cases. We also might divide our input classes

used rough sets in several different systems to try to filter out periods

further, using as many as 10 classes for an indicator like stochastics. In

when a given neural network will perform at a substandard level. Often,

this application, we actually want to curve-fit because if the variables are

simple patterns exist that can tell the reliability of a given forecast. For

not used in overfitted rules, they most likely are not necessary to the so-

example, the higher the absolute value of the networkâ€™s output, the more

lution. If we are using C4.5, we would use all of the variables in the rules

likely that it will be correct. When developing this type of application, it

except the ones at the leaves of the decision tree. If we are using rough

is often a good idea to develop a different set of rules for different out-

sets, we would use a roughness of .60 and a rules precision of 55 percent

put classes from the neural network. For example, you might want one

to select our variables using DataLogic/R. These levels are near the mid-

set of rules for when the current forecast is positive and another for when

dle of the range. Roughness varies from 0 to 1, and rule precision from 0

it is negative. In these types of models, you would use either raw or pre-

percent to 100 percent. This method can often produce networks with re-

processed past values produced by the neural network, as well as other in-

duced inputs but the same accuracy of direction, as well as the same cor-

puts that may not have been included in the original model-for example,

relation between the neural network output and the target as in the original

you could add month of year. This is often a good input to add when gen-

configuration. One problem is that sometimes the distribution of errors

erating rules because it is hard to preprocess for a neural network and

will change and the network will not be as profitable. In these cases, we

would require some type of thermometer encoding. In my research, 1 have

start with the inputs used by this method and then, using either brute force

found that the accuracy of some neural networks differs greatly, based on

or a genetic algorithm, we try readding the variable that has been removed

month of year, for example this is very important in the T-Bond market.

and continuing until we develop a satisfactory model.

You can also include data that the original network used, because rough

sets or C4.5 will generate a model different from the original model,

which was generated using the neural network. The process of filtering

EVALUATING THE RELIABILITY OF

a neural network in this way is very powerful. Filtering out only a few

MACHINE-GENERATED RULES

large losing trades can make a huge difference in the overall performance

of the network.

How can you judge the probability that a given rule(s) will continue to

work into the future?

VARIABLE ELIMINATION USING MACHINE INDUCTION The first step in estimating the future reliability of any set of rules is

to rank the rules based on the number of conditions each rule contains.

The fewer conditions a rule contains, the higher its ranking. The next step

In the previous chapter, we learned that, when developing a neural net-

is to divide the ratio of supporting cases by the number of conditions.

work, it is important to eliminate variables that do not contribute to the

The higher this ratio, the higher the probability that the rule will con-

performance of the model, because the reliability of a neural network is

tinue to work in the future.

proportional to the ratio of the training cases to the number of connec-

In general, when selecting rules generated using machine induction

tions. In Chapter 18, we discussed using brute force to try to eliminate in-

methods in trading, we will select only rules that are at least 60 percent

puts that do cot improve the performance of the neural network. Using a

accurate when discriminating output, and are supported by a minimum

brute force search can be impossible for even a small neural network (30

of 5 percent of the cases in that output class in the database. Next, we ex-

inputs) and could require days of using a genetic algorithm to speed up the

amine where each case occurred in the data set and score rules higher

search. We can use machine induction to select inputs for our model, in-

based on how the uniform distribution of cases occurred.

duce rules, and then use only the inputs that are used in these rules. This

288 Using Advanced Technologies to Develop Trading Strategies Machine Learning Methods for Developing Trading Strategies

289

After rule selection based on these criteria, we need to have them an- one of our sample sets, and then measure the performance using a mov-

alyzed by a human expert. This expert will select rules that he or she feels ing window for our analysis. In this case, we would want an upward slop-

are based on a sound premise and should continue to work well in the fu- ing equity curve at the end of our combined data period.

ture, and will eliminate rules that are based on statistical artifacts (e.g.,

it rains every time I wash my car, so I can make it rain by washing my This chapter has presented several ways to use machine induction to

car). This is a necessary step because each rule we trade should be based develop trading strategies. This technology is not the egghead approach

on a valid premise and should have a cause-and-effect relationship that once reserved for the university laboratory. Some of the methods dis-

makes sense. cussed here can greatly improve your trading performance and help you

Next we need to have these rules coded for use in a trading simulating research how markets actually work. Machine induction will grow in

and real-time trading tool like Omega Researchâ€™s TradeStation with Ea- use over the next few years, and will give traders who use this technol-

sylanguage. We then test these rules on the development, testing, and out- ogy an important edge to increase their performance in rapidly chang-

of-sample sets. We evaluate these rules on the development set, to judge ing markets.

the tradability of a given rule. We need this analysis because, using most

machine learning methods with a standard target like percent change, we

can judge only the percent correct for a given output or class, normally

above or below zero, and no other criteria like drawdown or average trade.

It is possible to have rules that are 70 percent or more accurate and yet are

not tradable because of a small average trade or a high drawdown. We will

also analyze the uniformity of the performance of a given rule over the

development set. The more uniform the performance, the more likely a

given rule will continue to work in the future.

After selecting rules that we feel are robust and tradable, we test them

in the testing set.

Once we have collected the statistical information about our rulesâ€™

performance via both the development and the testing sets, we use this in-

formation to select rules that have performed similarly during both sets

of data. If the rules do not show similar performance but the performance

is good across both sets, we try to find other variables that can explain

these differences. One example of this would be a higher average trade in

the testing set because of an increase in volatility over the past three

years. If this is the case, we then standardize our trading results, for both

the development and the testing periods, based on the average N-day

range for the market we are trading. After this normalization, the results

for good rules are often within the standard error.

When our selection is finished based on the development and testing

sets, we make our final selection, using a blind data set if enough cases

are available. If we do not have enough data to make a testing and blind

set statistically valid, we can use a set that combines the testing set and

Using Genetic Algorithms for Trading Applications 291

TABLE 20.1 APPLICATIONS OF A

GENETIC ALGORITHM.

Evolving a neural network.

1.

20 2. Evolving trading rules.

3. Combining 01 selecting multiple trading strategies,

4. Money management applications.

Using Genetic Algorithms

for Trading Applications near optimal solutions for what are called NP Complete-type problems.

NP Complete means that the problem is so complex that the time required

to solve it cannot be determined using a polynomial. These are problems

in which the number of combinations makes it impossible to try all of

them in our lifetime. Genetic algorithms can intelligently search subsets

of these solutions to find a near optimal solution for these problems in

hours or days, even via a desktop computer. Using genetic algorithms

makes it practical to search for the best possible inputs and configurations

for our neural network architecture.

The second type of application actually evolves the connection weights

In Chapter 11, we discussed the basics of genetic algorithms. In this chap-

ter, we show how they can be used in a broad range of trading applica- for a neural network. Why would we use a genetic algorithm to evolve

weights for a neural network? The reason is simple. Most neural network

tions. We will first discuss several different applications for genetic

algorithms use error functions that are not optimal for developing trad-

algorithms in trading. Next, we will overview the steps involved in de-

ing applications. For example, in many backpropagation-like algorithms,

veloping a solution using a genetic algorithm. Finally, we will show how

the error function is usually a root mean squared error. This error func-

to develop a real trading system by evolving rules based on both techni-

tion will produce a model with a distribution of errors that produces small

cal and intermarket analysis.

errors on the more common small moves but allows larger errors on the

rarer large moves. This will result in large losing trades that will often

make a neural network untradable. If we use a genetic algorithm to evolve

USES OF GENETIC ALGORITHMS IN TRADING

the weights, we can then use an error function more appropriate for mar-

ket timing applications. This is impossible using standard neural networks

Genetic algorithms have many uses in developing trading applications.

because the mathematics involved in the trading algorithm often restrict

Some of these are listed in Table 20.1.

the error functions. One example occurs in the standard backpropagation

Letâ€™s now overview how genetic algorithms are used in each of these

type of network: the error function must have a derivative that is a con-

applications.

tinuous function. Using a genetic algorithm, we have no such restriction.

Genetic Algorithms and Neural Networks

Evolving Trading Rules Using Genetic Algorithms

Genetic algorithms are useful in evolving neural networks. This applica-

Another classic application of genetic algorithms is in evolving trading

tion is actually two different types˜of applications. The first is to evolve

rules. Just as when using genetic algorithms to develop a neural network,

inputs and/or the configuration of a network. Genetic algorithms can find

290

292 Usine Advanced Technoloeies to Develor, Tradine Strateaies Using Genetic Algorithms for Trading Applications 293

we can develop a fitness function that is optimal for our application. I DEVELOPING TRADING RULES USING A GENETIC

have used genetic algorithms to develop trading rules in two different ALGORITHM-AN EXAMPLE

ways. The first is to design a genetic algorithm to find both the structure

and parameters for a given rule. The second is to use the genetic algo- Now that we have overviewed some of the uses of genetic algorithms in

rithm to combine and optimize predefined rule templates. We will discuss trading applications, letâ€™s develop an actual application. We will use a

this application later in this chapter. genetic algorithm to combine and optimize template rules that will gen-

erate entry signals for trading the T-Bond market. In this example, we

will have a genetic algorithm combine and optimize parameters for three

Combining Trading Methods Using Genetic Algorithms

rule templates that will be banded together. We will only develop rules to

Genetic algorithms can also be used to either combine forecasts from trade the long side of the market. Our rule template will allow us to use

multiple models or select the best model to trade at any given time. These intermarket analysis combined with stochastics. The basic form of our

functions allow us to discover hidden patterns between models that can rule templates is shown in Table 20.2.

be used to maximize trading performance. We will use the momentum and moving-average templates not only for

Letâ€™s discuss these applications in a little more detail. To combine the the market we are trading, but also for different intermarkets used in our

output of several different neural networks, we use genetic algorithms to analysis.

develop weighting factors that can be applied to the output of the neural Letâ€™s now apply this basic concept to the T-Bond market. We will trade

networks. This weighting can be different. based on technical factors T-Bond futures using two different intermarkets in our analysis: (1) Eu-

such as where we are in the business cycle. We can combine different rodollars and (2) the XAU index. Our development period will be from

models by developing a new forecast based on a combination of models, l/1/86 to 12/31/94. Our combined testing and out-of-sample period will

including neural networks, rules-based models, or statistics-based mod- be from l/1/95 to 10/4/96. We will use a product called TSEvolve, devel-

els. For example, we could use one set of rules that trades T-Bonds using oped by Ruggiero Associates and Scientific Consultant Services, Inc., to

the CRB, and another set that uses UTY. We then could add month of evolve these rules in TradeStation.

year, CPI data, and other fundamental data to the model and have the ge- Our first step is to define our problem. We need to design both our

netic algorithm develop a new model that will give us a combined fore- basic rule templates and how they interface to our chromosome so that,

cast. We can also use a genetic algorithm to tell us which of two or more by changing the values on the chromosome, we can develop any valid so-

models we should be using to trade tomorrow. lution to our problem. Often, to solve a given problem, we will divide it

into subproblems. These subproblems can be thought of as multiple genes

on a chromosome. We will then allow mating only between genes of the

Using Genetic Algorithms for Money Management Applications

same type. In solving our problem, we will use three independent genes,

Genetic algorithms can be used as part of a money management strategy. one for each of the rules we are â€śandingâ€ť together. We will allow each

They can search millions of combinations and find near optimal solu- rule to use up to four parameters. This gives us a chromosome with twelve

tions. If we apply a money management method like optimalfto a large elements.

portfolio of commodities, the number of combinations that we need to

try to calculate our optimalfvalue for the portfolio can make this analy-

sis an NP Complete problem. In this case, we can use genetic algorithms TABLE 20.2 TYPES OF RULE TEMPLATE.

to intelligently search these combinations and make it possible to quickly

Momentum above or below a given threshold.

solve optimalfover a basket of commodities. In this case, we would set

Comparing two exponential moving averages to determine which one is higher.

up our optimalfequations and simply use the genetic algorithm to plug

FastD above or below a given trigger.

in numbers and then evaluate the fitness of each solution.

294 Using Advanced Technologies to Develop Trading Strategies Using Genetic Algorithms for Trading Applications 295

TABLE 20.3 CODE FOR CALCRULES FUNCTION.

Letâ€™s now discuss how these four elements on each gene are used. The

first position on each gene is the rule number for which we are currently User Function CalcRules

optimizing the parameters and combining them with the rules from the I

other two genes. We define 4 inputs. These completely specify a rule and its parameters.

Normally, these inputs are the elements of a rule-specifying gene.

Positions 2 through 4 are different parameters that can be used by the

I

rules we are combining. In our genetic algorithm code, we will give these

Inputs: vl(NumericSimple), v2(NumericSimple);

numbers a range from 1 to 1,000, and in the interface to our rules, we Inputs: v3(NumericSimple), v4(NumericSimple);

will map them back into valid numbers for our domain. Our chromosome I

is a set of three of these genes. Declare some local variables.

Letâ€™s now look at the code from TSEvolve, in TradeStation, to evolve I

Vars: Norm(O), Norm2(0), Norm3(0), Ka(O), Kb(O). Thr(0);

our rules. We will start with the code for our rule templates, shown in

I

Table 20.3. Initialize some variables. We want CalcRule to have a value of 0 unless some

The rules templates stored in CalcRules are combined and optimized rule fires. We want all thresholds expressed in a market-scale-independent way.

by the genetic algorithm to develop combinations of three trading rules Hence, the use of a normalization factor, Norm.

to buy T-Bonds at the next dayâ€™s open. We used the following rule to exit 1

CalcRule=O;

our long position:

Norm=Average(TrueRange, 100);

Norm2=Average(TrueRange Of Data2, 100);

If BarsSinceEntry>4 then exitlong at low-S*Average(TrueRange,3) Norm3=Average(TrueRange Of Data3. 100);

stop; I

Finally, we implement our rules!

This exit rule will exit our long position on a stop after the fourth day The first rule is a simple momentum threshold rule with a maximum lookback of

100 bars and a normalized threshold that takes into consideration both market

of the trade if the market breaks yesterdayâ€™s low by more than 50 percent

volatility and momentum period.

of the three-day average TrueRange.

I

Letâ€™s lxlw see how the CalcRules function is used in evolving our rules. If Floor(vl)=l Then Begin ( Simple momentum threshold rule I

The code, written in TradeStationâ€™s EasyLanguage and using TSEvolve, Kadloor ((v2*v2)/10000); ( Integer, O..lOO I

is shown in Table 20.4. Kb=Floor ((v3*v3)/10000); ( Integer, O..lOO I

Thr=Norm*SquareRoot (AbsValue (Ka-Kb))*(v4-500)/200;

Letâ€™s now discuss this code. Our main module is a system we optimize

If CIKaI-C[Kbl>Thr Then CalcRule=l;

(using the TradeStation optimizer) to evolve actual rules. We initialize

End;

twelve elements that form three genes of four elements each. This makes

f

up our chromosomes. We next initialize our genes. The first element of Rule #2 compares two exponential moving averages.

any gene stores the rule number. We initialize these in a range from 1 to It is real simple!

14.99. When using these values, we take the floor of these numbers in I

If Floor(vl)=2 Then Begin ( Moving average comparison 1

order to produce an integer number that is used to assign a rule number

Ka=Floor (l+(v2*v2)/lOOOO); ( Integer, 1 ..lOO 1

to a given gene. The floor will return the integer portion of a number. We Kb=Floor (l+(v3*v3)/1OOOO); I Integer, 1 ..lOO 1

initialize the next three elements on a gene between 1 and 1,000. The If ExpAvg(C, Ka) > ExpAvgfC, Kb) Then CalcRule=l;

CalcRules function will map them back into a parameter range that can End:

be used by a given rule. (continued)

Using Genetic Algorithms for Trading Applications 297

296 Using Advanced Technologies to Develop Trading Strategies

TABLE 20.3 (Continued)

TABLE 20.3 Gmtinued)

Rule #8 is same as #2 but for Data3

Rule #3 compares the Stochastic to a threshold for countertrend signals.

I

I

If Floor(vlk8 Then Begin ( Moving average comparison ]

If Floor(vl)=3 Then Begin I Stochastic comparison I

Ka=Floor (1 +(v2*v2)/10000): I Integer, 1 ..lOO ]

If FastD(14)<(v2/10) Then CalcRule=l;

Kb=Floor (1+k3*v3)/100001; 1 Integer, l..lOO 1

End;

If ExpAvg(C of D&al, Ka)>ExpAvg(C oi Data3, Kb) Then CalcRule=l;

I

End;

Rule #4 compares the Stochastic to a threshold for with-trend signals.

I I

Rule #9 is same as #2 but moving average Ka is less than Kb

If Floor(vl)=4 Then Begin 1 Stochastic comparison I

I

If FastD(14i>W!/lO˜ Then CalcRule=l;

If Floor(vl)=9 Then Begin [ Moving average comparison )

End;

Ka=Floor (1 +(v2=v2)/10000); I Integer, 1 ..lOO )

(

Kb=Floor (l+(v3*v3)/lOOOO); I Integer, 1 ..lOO I

Rule #5 is same as #l, momentum threshold, but for Data2

If ExpAvg(C of Data, Ka) < ExpAvg(C of Data, Kb) Then CalcRule=l;

I

End;

Ii Floor(vlk5 Then Begin ( Simple momentum threshold rule t

Ka=Floor ((v2*v2)/10000); I Integer, O..lOO t I

Rule #lo is same as #9 but ior Data2

Kb=Floor ((v3*v3) / 10000); ( Integer, O..lOO )

t

Thr=NormL*SquareRoot (AbsValue (Ka-Kb)i*W500M200;

If Floor(vl)=lO Then Begin [ Moving average comparison I

If C[Ka] Of DataL-C[Kbl Of Data2>Thr Then CalcRule=l;

Ka=Floor (1+(v2*â€ś2)110000); I Integer, 1 ..lOO t

End;

Kb=Floor U+(v3*v3)/10000); ( Integer, l..lOO 1

I

If ExpAvgiC of D&2, Ka) < ExpAvg(C of Data2, Kb) Then CalcRule=l;

Rule #6 is same as #l, but for Data3

End;

1

I

If Floor(vl)& Then Begin { Simple momentum threshold rule I

Rule #l 1 is same as #9 but for Data3

Ka=Floor ((v2*v2)/10000); 1 Integer. O..lOO ]

t

Kb=Floor ((v3*v3)/10000); I Integer, O..lOO t

If Floor(vl)=l 1 Then Begin I Moving average comparison 1

Thr=Norm3 * SquareRoot (AbsValue (Ka-KbW%4-500)/200;

Ka=Floor (l+(v2*v2)/lOOOO); I Integer, 1 ..lOO I

If C[Kal Of Data3 C[Kbl Of Data3 > Thr Then CalcRule=l;

Kb=Floor (l+(v3*v3i/lOOOO˜; 1 Integer, 1 ..lOO 1

End;

If ExpAvgiC, Ka) < ExpAvg(C, Kb) Then CalcRukl;

(

End;

Rule #7 is same as #2 but for Data2

I

I

Rule #12 is inverse rule of rule 1

If Floor(vl)=7 Then Begin { Moving average comparison I

t

Ka=Floor (1 +(v2*v2)/10000); ( integer, 1 ..lOO 1

If Floor(v1)=12 Then Begin [ Simple momentum threshold rule 1

Kb=Floor U+(v3*v3)/1OOOO); I Integer, 1 ..lOO ]

Ka=Floor ((v2*v2)/10000); i Integer, O..lOO 1

If ExpAvg(C of Data2, Ka)>ExpAvg(C of Data2, Kb) Then CalcRule=l;

Kb=Floor ((v3*v3)/10000); I Integer, O..lOO ]

End;

Thr=Norm*SquareRoot (AbsValue (Ka-Kb))*(v4-500)/200;

If C[Ka]-C[Kbl<-Thr Then CalcRule=l;

End;

Using Genetic Algorithms for Trading Applications 299

298 Usinp. Advanced Technoloeies to Develoo Tradine Strateeies

TABLE 20.4 Gmtinued)

TABLE 20.3 (Continued)

Input: C&(l); I Generation Number = 1 . ..N 1

I

I

Rule #13 is inverse rule of rule 1 but for Data2

We define sane local variables that we need in the code that follows.

)

I

If Hoor( 3 Then Begin { Simple momentum threshold rule

Vars: RecCnt(O), EFlag(O), Fitness(O);

Ka=Floor ((y2*v2)/10000); I Integer, O..lOO )

â€˜Jars: vi(O), v2(0), v3(0). v4(01, K(O);

Kb=Floor ((v3*v3MlOOOO); I Integer, O..lOO 1

Vars: Resl(O), ResZ(O), Res3@;

Thr=Norm2*SquareRoot (AbsValue (Ka-Kb))*(v4-500)/200:

Vars: FilePtdO);

If C[Kal Of D&2-C[Kbl Of Data2<-Thr Then CalcRule=l;

End; I

We need to initialize TSEvolve once before using it. This code accomplishes the ini-

I

tialization on the first bar of the first generation. Note that TEST.SOL is our current

Rule #14 is inverse rule of rule 1 but for Data3

solutions file that will contain all parameter sets (rule sets) generated, and that 12

I

is the number of parameters which is also equal to the record size of TESTSOL

If Hoor( Then Begin I Simple mwnentum threshold rule

Each rule consists of 4 numbers. and we are allowing 3 rules in each complete

Ka=Floor ((v2*v2)/10000); I Integer, 0.. 100 I

gene string.

Kb=Floor ((v3*v3)/10000); { Integer, O..lOO I

I

Thr=Norm3*SquareRoot (AbsValue (Ka-Kb))*iv4-500)/200;

If CurrentBar = 1 Then Begin

If C[Kal Of Dat&C[KbI Of Data3<-Thr Then CalcRule=l;

RecCnt = ChromSi&â€śC:\TESTSOL,â€ś.l2); I Record count 1

End;

If Gen > RecCnt Then EFlag = 1; I Set evolve mode I

If Cen < = RecCnt Then EFlag = 0; { Set retrieve mode 1

Ii Cen = 1 And EFlag = 1 Then Begin { if evolve mode.. 1

CA_lnit (12, 500); I 12 = no. parameters 50 = population size I

For K = 0 To 2 Begin ( 3 genes, each with 4 numbers J

CA-Scale (0 + 4 * K, 1, 14.99); ( Range 1 ..14.99 1

CA˜Scale (1 + 4 * K, 1, 1000); I Range 1 ..l 000 I

TABLE 20.4 SIMPLE GENETIC TEMPLATE FOR

CA˜Scale (2 + 4 * K, 1, 1000); I Range 1 ..l 000 I

EVOLVING COMBINATION OF THREE RULES.

GA_Scale (3 + 4 * K. 1, 1000); I Range 1 ..lOOO 1

End;

I

CA-Chunk (4); ( Set gene or block size to 4 I

TSEvolveTM for TradeStationTM.

CA-Random (93); I Randomize population with seed=93 J

Copyright 0 1995, 1996. All Rights Reserved.

CA-Mu (0.30); I Set mutation rate to 0.30 t

Ruggiero Associates and Scientific Consultant Services, Inc.

CA-Cross (0.30); ( Set crossover rate to 0.30 I

Tel: (US) 203-469-0880

End;

This is the code for a trading system which uses TSEvolve to discover good trading End;

rules. It illustrates just how this may be done. The â€śinputsâ€ť allow us to use the

I

TradeStation optimizer to loop through generations, with TSEvolve providing the ac- Now we either retrieve a guess as to the optimal parameters (rules) from

tual parameter-vector (rule set) guesses. If the generation number is less than the TSEvolve, or we retrieve a previously evolved solution from our solutions file. We

number of records saved to the solutions file, then we read the solutions file for our place the newly-generated guess or the retrieved solution into memory.

parameter vector. If the generation number is greater than what is in the solutions I

file, then we use TSEvolve to provide us with a parameter vector guess.

This way, we can select the desired solution in the standard TradeStation manner

without needing to play with our code!

300 301

Using Advanced Technologies to Develop Trading Strategies Using Genetic Algorithms for Trading Applications

TABLE 20.4 (Continued) TABLE 20.4 Gmtinued)

If CurrentBar = 1 Then Begin 1On the first bar.. 1 Finally, we need to tell TSEvolve how â€śgoodâ€ť its guess was. That is, we need to

If EFlag = 1 Then Begin I If we are evolving.. ) report the â€śfitnessâ€ť of the guess it provided. Using the information, TSEvolve will

CA-Guess (1); I We get a guess and place it in VR #1 ) be able to provide better and better guesses as the population maintained internal

CA-Save (1˜. 12, Cen, â€śC:\TEST.SOLâ€ť); I And we save it! ) to TSEvolve evolves. Of course, we must do this only on the last bar of each

End; generation, when all backtestisimulation data have been processed for that

generationâ€™s guess as to the best chromosome (i.e., set oi rules). The Date =

If EFlag = 0 Then Begin { If we are running a saved solution.. I

CA-Load (1, 12, Gen. â€śC:\TEST.SOLâ€ť); ( Load it into VR #ll LastCalcDate clause in the if.. statement below provides us with a crude way to

End; detect the last bar of a run. Of course, we only need to do this if we are evolving,

End; so we also have the EFlag = 1 clause in the if.. statement that follows.

I 1

Run the actual trading rules. We retrieve gene data. CalcRule then returns a 1 if EFlag = 1 And Date = LastCalcDate Then Begin

(True) or a 0 (False) based on the application of the rule defined by its gene-data Fitness = NetProfit-2â€™MaxlDDrawDown; [ We will maximize NetProfit I

inputs. The inputs, of course, are taken from a gene in the gene string. We are GA-Fit (Fitness); I Tell TSEvolve goodness of current guess 1

allowing 3 genes, hence rules, and requiring all rules be True in order to take a { Write out some useful info to a text file I

trade. SC-Fopen (&FilePtr, â€śC:\TEST.TXTâ€ť, â€śATâ€ť);

SC-Fprintf (&FilePtr, â€ś%&lfâ€ť, Gen); I Generation I

I

For K = 0 To 11 Begin

GetGeneElem (1, 0, &vl); ( Get first element of first gene 1

CetChromElem(1, K. &Valuel); 1 Gene data )

GetGeneElem (1, 1, &v2); I Get second element of first gene 1

GetGeneElem (1, 2, &v3); I Get third element of first gene 1 IfK=4OrK=8Then

SC-Fprintf i&FilePtr, I,â€ś, 1);

GetGeneElem (1, 3, 8˜˜4); I Get fourth element of first gene 1

SC-Fprintf (&FilePtr, â€ś%7,1fâ€ť, Valuel); ( Genes, 1 per line 1

Resl = CalcRule (˜1, ˜2, ˜3, ˜4); [ run the rule 1

If K = 3 Or K = 7 Or K = 11 Then

GetGeneElem (1, 4, &vl); { Get first element of second gene 1

SC-Fprintf (&FilePtr, â€śNâ€ť, I; I New line I

GetGeneElem (1, 5, &v2); [ Get second element of second gene I

GetGeneElem (1, 6. &v3); I Get third element of second gene 1 End;

GetGeneElem (1, 7, &v4); { Get fourth element of second gene I SC-Fprintf (&FilePtr, â€ś%10.2fâ€ť, NetProfit);

Res2 = CalcRule (˜1, ˜2, ˜3, ˜4); ( run the rule t SC_Fprintf (&FilePtr, â€ś%lO.Zfâ€ť, GrossProfit);

GetGeneElem (1, 8, &vl); [ Get first element of third gene 1 SC-Fprintf (&FilePtr, â€ś%10.2fâ€ť, PercentProfit);

SC-Fprintf (&FilePtr, â€ś%10.21â€ť, TotalTrades);

GetGeneElem (1, 9, &v2); ( Get second element of third gene 1

SC-Fprintf (&FilePtr, â€ś%10.2f Nâ€ť, MaxlDDrawdown);

GetGeneElem (1, 10, &v3); { Get third element of third gene 1

SC_Fclose (&FilePtr);

GetGeneElem (1, 11, &v4); { Get fourth element of third gene I

End;

Res3 = C.&Rule (˜1, ˜2, ˜3, ˜4); ( run the rule J

i

If Resl = 1 And Res2 = 1 And Res3 = 1 Then Begin

We are done. Verify this code, set up TradeStationâ€™s built-in optimizer to step the

If MarketPosition < = 0 Then Buy At Open;

single input Gen from 1 to whatever number of generations you want to run. Use

End;

an increment of one. To run the system, set Cen to whatever generation you liked

If MarketPosition > 0 Then Begin

best as determined from studying the data. To do another evolution run, change

If BarsSinceEntry(0) > 4 Then ExitLong At Low-.5*Average(TrueRange,3) stop;

the solutions file name or delete the existing solutions file from your disk

End:

(otherwise you will be retrieving old solutions rather than evolving new ones).

I I

302 Using. Advanced Technolo&to Develop Trading Strategies Using Genetic Algorithms for Trading Applications 303

On the first bar of every run, we get a chromosome that was the result moving average is less than the second and is applied to Datal. Rules 10

of the last mating operation. We then use the values on each gene to test and 11 are the same as Rule 9 but are applied to Data2 and Data3. Rule

our defined set of three rules which will be anded together. We will buy 12 is just like Rule 1, but the value must be below a given -1 x threshold

when all three of these rules are true. We hold our position for at least rule. Rules 13 and 14 are the same as Rule 12, but are used for Data2 and

four bars and exit it after that period on a stop at 50 percent of a three- Data3.

day average true range below yesterdayâ€™s low. We evolved these rules with a population size of 500, a crossover rate

When we are in an evolution mode, we evaluate the fitness of a given of .30, and a mutation rate of .30. They were evolved for 3,000 genera-

rule set based on the trading performance as reported in TradeStation. We tions. A generation is a single mating of the population. We found that,

use a simple fitness function in this evaluation: NetProfit - 2 x MaxID- in general, we had a number of good solutions. We then tested them, from

DrawDown. This function will produce profitable combinations with a l/1/95 to 10/4/96. Four of them performed very well in both the devel-

good drawdown-to-profit ratio. If instead of this measure we use a mea- opment set and the testing set. One of the nice features about TSEvolve

sure such as profit factor, the genetic algorithm might have evolved a so- is that we are able to rerun any generation without coding the rules. The

lution with five or fewer trades, all of which are winners and have a profit chromosomes for each generation are stored in a solution file and can be

factor of 100. These systems have too few trades and most likely will not used by TradeStation to reload a given set of rules without any coding.

be profitable in the future because they are curve-fitted systems. Letâ€™s now see how these four generations/solutions performed during

We then write to both a solution file â€śtest.sol,â€ť used by TSEvolve to both the development and combined testing/out-of-sample sets (allowing

reload solutions into TradeStation once we are done evolving, and an in- $50.00 for slippage and commissions). Our development set period was

formation file â€śtest&t,â€ť which we canuse to analyze our results and se- from l/1/86 to 12/31/94, and our combined set period was from l/1/95 to

lect the generation we would want to trade. 10/4/96. Note that we used 210 for maxbars back, so that the first 10

The way this code is written, it will evolve a generation if the value months of data did not generate any trades on the development set. The

passed to the system is greater than the maximum value stored in the so- results are shown in Table 20.5.

lution file. If the value passed is less than the maximum generation in We then translated our trading rules into both EasyLanguage and En-

this file, it will run that generation to produce its trading signal. glish so that we could see whether they would make sense to a domain ex-

Letâ€™s now discuss the code for CalcRules. This code first initializes pert. These restated rules are shown in Table 20.6.

the return value of this function to false and then calculates normaliza- Note that we can use genetic algorithms to induce rules that we can

tion factors based on loo-day average true ranges for Datal, Data2, and translate into both EasyLanguage and English, and then have them stud-

Data3. Next, we find the rule that is being requested. Letâ€™s look at each ied by a domain expert. We analyzed these rules and found several ex-

of these rule templates. amples of some of the theories discussed in earlier chapters. For example,

Rule 1 wants to see whether a momentum is above a given threshold. the rule from generation 1909 has, as one of its conditions, that the XAU

Both the lookbacks and the threshold are found using the genetic algo- has not gone up too much. This rule confirms the existence of the con-

rithm. Rule 2 compares two exponential moving averages and is true when cept of intermarket inversion, which we discussed earlier. Briefly, the

the first one is greater than the second. The genetic algorithm also sets concept is that intermarket relationships will sometimes invert. Positive

the periods used in these moving averages. ones become negative and negative ones become positive for some mar-

Rule 3 is true when a FastD is below a threshold, and Rule 4 is true kets, when these markets move too quickly. The classic example of this

when a FastD is above a threshold. Rule 5 is the same as Rule 1 but uses effect is that, when interest rates drop too quickly or gold rises too fast,

the inverse relationship between gold and T-Bonds will invert and they

Data2; so is Rule 6, but now for Data3. Rules 7 and 8 are the same as

will become positively correlated. Another example of this occurs with

Rule 2 but use Data2 and Data3, respectively. Rule 9 is when the first

Using Advanced Technologies to Develop Trading Strategies Using Genetic Algorithms for Trading Applications

304 305

TABLE 20.5 RESULTS OF SELECTED GENERATION OF RULES TABLE 20.6 TRANSLATION OF SELECTED RULES INTO

FOR T-BONDS USING OUR TEMPLATE. BOTH ENGLISH AND EASYLANGUAGE.

Development Set Combined TestinRlOut-of-sample Set Let Datal=T-Bonds

Let Data2=Eurodollars

Generation 1197

Let Data3=XAU index

Net profit $34,300.00 $10,681.25

Generation 7 197

Trades 74 17

EasyLanguage:

Average trade $463.51 $628.31

Win% 65 If FastD(14k61.9 and CL401 of Data2 -C[191 of Da&L>-

Drawdown -;! 668.75 -$5,175.00 6.69*Average(TrueRange of Data2,lOO) and XAverage(C,8)>XAverage(C,l4) then

Profit factor 1.46 2.48 buy at open;

If BarsSinceEntry>4 then exitlong at Low-.S*Average(TrueRange,3) stop;

Generation 1723

English:

Net profit $32,175.00 $11,900.00

If FastD is not too high and Eurodollars did not go up too much between 19

Trades

days ago and 40 days ago and the T-Bond eight-day average is above the 14.day

Average trade iZ34.80 ;:oo.oo

average, then buy.

Win% 66

Drawdown -$6,400.00 -:: 175.00 Generation 1723

Profit factor 1.90 2.$5 EasyLanguage:

If FastD(14k61.9 and FastD(14)>31.9 and XAverage(C,7)>XAverage(C,lS) then

Generation 1909

buy at open;

Net profit $38,350.00 $6,906.25 If BarsSinceEntry>4 then exitlong at Low-.5*Average(TrueRange,3) stop;

Trades 68

English:

Average trade $563.97 ii60.42

If Fast D is in the middle of the range and the trend is up, then buy at open;

Win% 66

Drawdown -$5,068.00 -::,537.50 Generation 1909

Profit factor 2.47 2.06

EasyLanguage:

Generation 2329 If C[81 of Data3- Cl1 1 of Data3>-1,53â€™Average(TrueRange of D&3,100) and

CL841 of D&3-Cl91 of Data3>2.16*Average(TrueRange of D&,3,1001 and

Net profit $34,156.00 $5.718.75

CL611 of D&2-C[321 of Data2>-5.23*Average(TrueRange of Data2,lOO) then

Trades 10

buy at open;

Average trade :ki3.13 $571.88

If BarsSinceEntry>4 then exitlong at Low-.5*Average(TrueRange,3) stop;

68

Win% 80

English:

Drawdown -$5,618.75 -$4,331.25

Profit factor 2.51 1.91 If the recent XAU is not going up too much and the past XAU is going down

and Eurodollars are either up or only slightly lower, then buy.

Generation 2329

EasyLanguage:

If FastD(14)>46.8 and CL481 of D&2-CL31 of DataZ>-13.52*Average

Eurodollars in generation 1197. Eurodollars often rally very quickly

(TrueRange of Data2,lOO) and XAverage(C,7) > XAverage(C,15) then buy.

during times of crisis, and this rally does not always follow through in

English:

the T-Bond market.

if FastD is not too low and Eurodollars are not down too much, then buy if

We found that the rules we selected make sense and use parameters T-Bonds are in a uptrend.

similar to those of many of the rule templates that we are combining. For

Using Advanced Technologies to Develop Trading Strategies

306

example, in three of the four generations, we have one condition that is

true when a short-term EMA is above a long-term EMA. The genetic al-

gorithm used either 7 or 8 for the short-term EMA and 14 or 15 for the

References and Readings

long-term EMA-an indication of the stability of these pairs of

parameters.

This chapter has shown how genetic algorithms can be used for a broad

range of trading applications and can also incorporate human trader ex-

pertise into the solutions. Given these capacities, I feel that genetic al-

gorithms will be one of the hottest areas of research in advanced

technologies for trading well into the next century.

Articles by Murray A. Ruggiero, jr.

In Futures Magazine (Chronological Order)

â€śGetting the lag out,â€ť April 1994, pages 46-48.

â€śInterpreting feedback to build a better system,â€ť July 1994, pages 46-48.

â€śTraining neural nets for intermarket analysis,â€ť August 1994, pages 42-44

â€śHow to build an artificial trader,â€ť September 1994, pages 56-58.

â€śPutting the components before the system,â€ť October 1994, pages 42-44.

â€śHow to build a system framework,â€ť November 1994, pages 50-56.

â€śTurning the key,â€ť December 1994, pages 38-40.

â€śArtificial trader jumps candlesticks.â€ť February 1995, pages 42-44.

â€śTesting the black box system that feeds you,â€ť March 1995, pages 44-46.

â€śTesting a real system,â€ť April 1995, pages 46-48.

â€śNothing like net for intermarket analysis,â€ť May 1995, pages 46-47.

â€śBuild a real neural net,â€ť June 1995, pages 44-46.

â€śEvolution of a trader,â€ť July 1995, pages 44-46.

â€śTaking evolution into warp speed,â€ť August 1995, pages 42-44.

â€śFine-tuning money management withxâ€™;â€ť September 1995, pages 48-50.

â€śTips to develop smarter exits,â€ť October 1995, pages 54-58.

â€śBuilding a system one day at a time,â€ť November 1995, pages 52-54.

â€śBuilding a real day-trading system,â€ť December 1995, pages 50-52.

References and Readings 309

308 References and Readines

MESA â€˜96for TradeStationâ€™s Userâ€™s Manual. Copyright 1996, MESA Software,

â€śHow to adapt systems to survive changing markets,â€ť January 1996, pages

P.O. Box 1801, Goleta, CA 93116.

48-50.

Murphy, John J. Intermarket Technical Analysis. New York: John Wiley & Sons,

â€śUsing correlation analysis to predict trends,â€ť February 1996, pages 46-49.

Inc., 1991.

â€śBuilding the wave,â€ť April 1996, pages 46-48.

Murphy, John J. Technical Analysis of the Futures Market: A Comprehensive

â€śHow to predict tomorrowâ€™s indicators today,â€ť May 1996, pages 44-48. Guide to Trading Methods and Applications. En&wood Cliffs, NJ: Prentice-

â€śFundamentals pave way to predicting interest rates,â€ť September 1996, Hall (NY Institute of Finance), 1986.

pages 46-48. Nison, Steve. Japanese Candlestick Charting Techniques. New York: John Wiley

& Sons, Inc., 1990.

In A/ in Finance

Refenes, Paul. Newal Networks in the Capital Markets, (pp. 213-219). New

â€śBuilding a great artificial trader,â€ť Premier Issue 1994, pages 39-44. York: John Wiley & Sons, Inc., 1995.

of Successjd Trading. New York: Simon &

â€śRules are made to be traded,â€ť Fall 1994, pages 35-40. Rotella, Robert P. The Elements

Schuster, 1992.

â€śNeural networks: Tahiti or bust.â€ť Spring 1995, pages 15-20.

Schwager, Jack D. The New Market Wizards: Conversarions with Americaâ€™s Top

Traders. New York: HarperCollins (HarperBusiness), 1992.

Articles by Other Authors

Zweig, Martin. Winning on Wall Street. New York: Warner Communications

co., 1990.

Jurik, Mark. â€śThe care and feeding of a neural network,â€ť Futures Magazine,

October 1992, pages 40-44.

Meyers, Dennis. â€śThe electric utility bond market indicator,â€ť Technical Analy-

sis ojSrocks & Commodities, January 1996, pages 18-31,

Pawlak, Zdzislaw, Grzymala-Busse, Slowinski, R., and Ziarko, W., â€śRough

sets,â€ť unpublished manuscript, 1995, pages l-17.

Books

of

Azoff, E. Michael. Neural Network Time Series Forecasting Financial Mar-

tits. New York: John Wiley & Sons, Inc., 1994.

Deboeck, Ciuido. Trading on the Edge. New York: John Wiley & Sons, Inc.,

1994.

Ehlers, John. MESA and Trading Market Cycles. New York: John Wiley&Sons,

Inc., 1992.

Joseph, Tom. Mechanical Elliott Wave Seminar. Cleveland, OH: Trading Tech-

niques, Inc., 1993.

Kaufman, Perry J. The New Commodity Trading Systems and Methods. New

York: John Wiley & Sons, Inc., 1987.

Lipschutz, Seymour. Theory and Problems ofFinite Mathematics. New York:

McGraw-Hill Book Co., 1966.

Index 311

Index

312 Index Index 313

314 Index Index 315

ńňđ. 6 |