Casting neural nets into modern markets

March 26, 2013 07:00 AM

The key to developing intelligent trading systems is to start with a reliable rule-based strategy. The neural network, genetic algorithm, kernel regression or other machine-learning method should be designed to enhance the system’s already positive performance.

However, this core system does not have to be the Holy Grail. While this building block may not be tradeable as a standalone system, it should demonstrate an ability to generate a positive return over time, even if that return is lacking in terms of its risk-reward balance. For example, in trend-following markets, moving-average-crossover systems perform well. Similarly, we could employ a classic n-day breakout of highs or lows. Intermarket divergence strategies also are viable, as are those that employ traditional technical indicators or fundamental analysis, such as breadth data for the S&P 500, the Volatility Index or price/earnings ratios, to name a few.

What’s important is the trader must have a strong understanding of how a given trading system works, knows its strengths and is aware of its weaknesses. This understanding can provide the foundation for enhancing the strategy using advance technologies.

Starting blocks

Let’s take a closer look at some of the better starting points for a technology-enhanced approach.

Consider the moving-average crossover strategy, composed of simple moving average difference oscillators. One of our goals using advanced analysis methods might be to predict these oscillators as a percentage of the shorter moving average some time in the future. 

Here’s how the process would work:

  1. Optimize a moving average crossover system, finding several robust sets of parameters.
  2. Test what would happen if you predicted the moving average crossovers a few bars into the future. Also, evaluate the result of being a few bars late. We want the results of slightly late predictions to be reasonable still, and we want early predictions to be far better. 
  3. When a viable system is identified, develop a model to predict the difference between the moving average or the crossovers a few bars ahead. This is what will generate the signals. 

With respect to the channel breakout, we might attempt to use neural networks to predict where prices n bars into the future will fall within the range over a set period. We might only take those breakouts if the market is still in that extreme price range so many bars into the future.

With respect to intermarket systems, which compare the price fluctuations of two related markets with respect to their historical norms, we can improve our performance by filtering out periods when these models are likely to break down. We also may be able to predict periods of stronger correlation.

Predictive indicators, such as the ADX, the relative strength index and stochastics, also can be employed. More complex models also might layer in advance/decline data, put/call ratios and sentiment. Neural networks are effective in combining such seemingly unrelated indicators into a single comprehensive signal.

Another good core approach is a relative strength model. These rank percentage returns over a given period, trading the top n-ranked issues in the basket. For example, in a basket of 100 stocks, we might trade the 10 with the highest returns. These strategies are popular among individual stock and exchange-traded fund traders. Here’s how it works:

  1. Calculate momentum for each stock in the basket. This momentum needs to be normalized. For example, this is a 50-bar momentum calculation: Rawreturns=(Close-Close[50])/Close[50]. We also could filter out all issues trading below their 200-day moving average. 
  2. Sort these raw returns and select some number of the highest returning stocks. For example, if we have 100 stocks, we might take the top 10. 

We then can use the neural network to predict future returns for each of these top performers. We also can re-balance the portofolio dynamically as the top stocks change. 

Adding the net

Regardless of which core strategy you employ, there is a general framework for adding the neural network. The basic approach goes like this:

  1. Decide on a classic rule-based trading system. 
  2. Identify the weaknesses of the system.
  3. Decide how to address that weakness, such as filtering, ranking or weighting the output. 
  4. Determine a target to predict that will accomplish what is identified in step three. This is a critical point and could make or break this effort. The algorithm affects exactly how the target should be created.
  5. Check system fault tolerance. For example, if the network fails, does it destroy system performance? Test the effect of late or early predictions. Ideally, late signals should cause minor degradations in performance while early signals should significantly increase performance; this indicates the core logic is sound and that results are not caused by chance. 
  6. Pre-process the data so that it’s comparable across time periods. 
  7. Design your training and testing scheme for your model, including your retraining period and walk-forward period.
  8. Test and analyze your model. Decide if the predictive component improves performance enough and is stable enough to test in live markets. 

Case study: Moving average crossover

The moving average crossover system is a classic trend-following method. Two popular variations of this strategy are the dual-moving-average system and the triple-moving-average crossover. 

Here’s the code for a simple dual-moving-average crossover system: 

Sub DualMACrossover(SLen As Integer,LLen As Integer)

 Dim ShortAve As BarArray

 Dim LongAve As BarArray

 ShortAve = Average(Close, SLen,0)

 LongAve = Average(Close, LLen,0)

 If ShortAve > LongAve Then

 Buy(“BuyEnt”,1,0,Market,Day)

 End If

 If ShortAve <LongAve Then

 Sell(“SellEnt”,1,0,Market,Day)

 End If 

End Sub

This is a simple trend-following system that has some problems. First, if the short-term moving average is half of the dominant cycle, and the long-term moving average is a full dominant cycle, basic cycle theory says we are 180% out of phase, meaning regardless of how well the system does at other times, we will buy every top and will sell every bottom. This is why a dual-moving-average approach is flawed. It will catch big trends, but it will lose badly when trends are shorter term. 

Our tests will use the following basket of markets: Australian dollar, cotton, dollar index, euro, Japanese yen, Swiss franc, sugar and natural gas. Our analysis covers Jan. 4, 1989, to Jan. 29, 2013. The results of the optimization, with $75 per trade deducted for slippage and commissions, are shown in “First run” (below). There is a nice area of performance around the parameters 38 and 60, clearly the best set. That the parameters on each side of this set are also viable suggests that this is a robust combination. 

The next step is to see how we can improve this using a neural network. For example, we can predict the crossover as a percentage of the short-term average. Before we implement that approach, however, we need to do a quick test of its effectiveness.

“Back to the past” (below) shows this analysis on the 38, 60 parameter pair. It shows the performance of the moving average cross five bars into the future and five bars late. We only lose about 30% being five bars late but gain 250% being five bars early. When we break this down further and look at the results on a market-by-market basis, we see much of the same. A short delay doesn’t destroy the system, and a short lead improves it significantly. This is the type of delay/lead profile we are seeking. If we can use neural networks to improve our lead time, our results should follow. Because our short-term moving average is 38, predicting even five bars into the future is a reasonable goal. 

Case study: Keltner channel

Now let’s break down another example. We will turn our attention to a Keltner channel breakout trend-following system on the same sample basket, using the same dates and the same $75 for slippage and commissions.

Here’s the code:

Sub KeltnerBandBKSimple(SLen,BandMult,RFiltLB,RFiltMult)

Dim MAAve As BarArray

Dim StdVal As BarArray

Dim virprofit As BarArray

MAAve=Average(Close,SLen,0)

StdVal=Average(Range,SLen,0)

If Close>MAAve+BandMult*StdVal And Range<RFiltMult*Average(Range,RFiltLB,0) Then Buy(“LE”,1,0,Market,Day)

If Close<MAAve-BandMult*StdVal And Range<RFiltMult*Average(Range,RFiltLB,0) Then Sell(“SE”,1,0,Market,Day)

If MarketPosition=1 And Close<MAAve Then ExitLong(“LX”,”LE”,1,0,Market,Day)

If MarketPosition=-1 And Close>MAAve Then ExitShort(“SX”,”SE”,1,0,Market,Day)

End Sub

Our best performing parameters are the 80, 3, 20, 1 combination, which earns $538,656.50 on 518 trades with a 38.8% win percentage. The average trade makes $1,039.88. The worst drawdown over the period is $79,086.10. Our next step is to take this original system and add a predictive filter of some type. Toward that end, we develop the following forward-looking indicator:

Function ExcursionRatio(Offset,LB)

 Dim HighPrice As BarArray

 Dim LowPrice As BarArray

 Dim ClosePrice As BarArray

 Dim PostiveEx As BarArray

 Dim NegativeEx As BarArray

 HighPrice=tsgetdatastream(0,”High”,Offset)

 LowPrice= tsgetdatastream(0,”Low”, Offset)

 ClosePrice=tsgetdatastream(0,”Close”,Offset)

 PostiveEx=Highest(HighPrice,LB,0)-ClosePrice[LB]

 NegativeEx=ClosePrice[LB]-Lowest(LowPrice,LB,0)

 If NegativeEx<>0 Then

 ExcursionRatio=PostiveEx/ NegativeEx

 Else

 ExcursionRatio=999999

 End If

End Function

This indicator looks at the excursion from the first price in the window to the highest and lowest price in the future. It compares the ratio of price excursion. If positive excursion in the future is bigger than negative excursion, that’s bullish and the ratio will be greater than 1.00. If the ratio is less than 1.00, then that’s bearish. Using our excursion indicator and looking five bars into the future, we about double profits over our standard system (see “Time bandits,” below).

 

Unfortunately, making such predictions in real life is easier to synthesize than actually to do. In the case of the moving average crossover, predicting five bars into the future for a 38-60 crossover is effectively eliminating lag; although not easy by any means, it’s a viable target. The target we are using for this Keltner channel system, however, is far less stable and much harder to predict. Based on this initial research, the moving-average crossover is the better prospect for improvement. The five-day lag elimination increases profits by 2.5 times, while the excursion prediction for the Keltner channel is much harder to accomplish and only doubles profits.

The next step is to develop a hybrid system neural network model based on this analysis and examine some additional case studies that round out our understanding of this approach. 

Murray A. Ruggiero Jr. is the author of “Cybernetic Trading Strategies” (Wiley). E-mail him at ruggieroassoc@aol.com.

About the Author

Murray A. Ruggiero Jr. is the author of "Cybernetic Trading Strategies" (Wiley). E-mail him at ruggieroassoc@aol.com.