Free email newsletter with articles on systematic trading

 

 

 

 

 

 

Adaptrade Software Newsletter Article
 

 

Hybrid Neural Network Stop-and-Reverse Strategies for Forex

by Michael R. Bryant

 

 

Neural networks have been used in trading systems for many years with varying degrees of success. Their primary attraction is that their nonlinear structure is better able to capture the complexities of price movement than standard, indicator-based trading rules. One of the criticisms has been that neural network-based trading strategies tend to be over-fit and therefore don't perform well on new data. A possible solution to this problem is to combine neural networks with rule-based strategy logic to create a hybrid type of strategy. This article will show how this can be done using Adaptrade Builder.

 

In particular, this article will illustrate the following:

 

Combining neural network and rule-based logic for trade entries

Targeting multiple platforms simultaneously (MetaTrader 4 and TradeStation)

Developing a strategy with asymmetrical stop-and-reverse logic

Using intraday forex data

  

A three-segment data approach will be used, with the third segment used to validate the final strategies. The resulting strategy code for both MetaTrader 4 and TradeStation will be shown, and it will be demonstrated that the validation results are positive for each platform.

 

Neural Networks as Trade Entry Filters

Mathematically, a neural network is a nonlinear combination of one or more weighted inputs that generates one or more output values. For trading, a neural network is generally used in one of two ways: (1) as a prediction of future price movement, or (2) as an indicator or filter for trading. Here, its use as an indicator or trade filter will be considered.

 

As an indicator, a neural network acts as an additional condition or filter that must be satisfied before a trade can be entered. The inputs to the network are typically other technical indicators, such as momentum, stochastics, ADX, moving averages, and so on, as well as prices and combinations of the preceding. The inputs are scaled and the neural network is designed so that the output is a value between -1 and +1. One approach is to allow a long entry if the output is greater than or equal to a threshold value, such as 0.5, and a short entry if the output is less than or equal to the negative of the threshold; e.g., -0.5. This condition would be in addition to any existing entry conditions. For example, if there were a long entry condition, it would have to be true and the neural network output would have to be at least equal to the threshold value for a long entry.

 

When setting up a neural network, a trader would typically be responsible for choosing the inputs and the network topology and for "training" the network, which determines the optimal weights values. As will be shown below, Adaptrade Builder performs these steps automatically as part of the evolutionary build process that the software is based on. Using the neural network as a trade filter allows it to be easily combined with other rules to create a hybrid trading strategy, one that combines the best features of traditional, rule-based approaches with the advantages of neural networks. As a simple example, Builder might combine a moving average crossover rule with a neural network so that a long position is taken when the fast moving average crosses above the slow moving average and the neural network output is at or above its threshold.

 

Stop-and-Reverse Trading Strategies

A stop-and-reverse trading strategy is one that is always in the market, either long or short. Strictly speaking, "stop-and-reverse" means that you reverse the trade when your stop order is hit. However, I use it as a short-hand for any trading strategy that reverses from long to short to long and so on, so that you're always in the market. By this definition, it's not necessary for the orders to be stop orders. You could enter and reverse using market or limit orders as well. It's also not necessary that each side use the same logic or even the same order type. For example, you could enter long (and exit short) on a stop order and enter short (and exit long) on a market order, using different rules and conditions for each entry/exit. This would be an example of an asymmetrical stop-and-reverse strategy.

 

The primary advantage of a stop-and-reverse strategy is that by always being in the market, you never miss any big moves. Another advantage is simplicity. When there are separate rules and conditions for entering and exiting trades, there is more complexity and more that can go wrong. Combining entries and exits means fewer timing decisions have to be made, which can mean fewer mistakes.

 

On the other hand, it can be argued that the best conditions for exiting a trade are rarely the same as those for entering in the opposite direction; that entering and exiting trades are inherently separate decisions that should therefore employ separate rules and logic. Another potential drawback of always being in the market is that the strategy will trade through every opening gap. A large opening gap against the position can mean a large loss before the strategy is able to reverse. Strategies that enter and exit more selectively or that exit by the end of the day can minimize the impact of opening gaps.

 

Build Settings

Since the goal is to build a forex strategy, MetaTrader 4 (MT4) is an obvious choice for the trading platform given that MetaTrader 4 is designed primarily for forex and is widely used for trading those markets (see, for example, MetaTrader vs. TradeStation: A Language Comparison). However, in recent years, TradeStation has targeted the forex markets much more aggressively. Depending on your trading volume and/or account level, it's possible to trade the forex markets through TradeStation without incurring any platform fees or paying any commissions. Spreads are reportedly tight with good liquidity on the major forex pairs. For these reasons, both platforms were targeted for this project.

 

Several issues arise when targeting multiple platforms simultaneously. First, the data may be different on different platforms, with differences in time zones, price quotes for some bars, volume, and available date ranges. To smooth over these differences, data were obtained from both platforms, and the strategies were built over both data series simultaneously. The best strategies were therefore the ones that worked well on both data series despite any differences in the data.

 

The data settings used in Builder are shown below in Fig. 1. As can be inferred from the Market Data table in the figure, the Euro/dollar forex market was targeted (EURUSD) with a bar size of 4 hours (240 minutes). Other bar sizes or markets would have served just as well. I was only able to obtain as much data through my MT4 platform as indicated by the date range shown in Fig. 1 (data series #2), so the same date range was used in obtaining the equivalent data series from TradeStation (data series #1). 80% of the data was used for Building (combined in-sample and "out-of-sample"), with 20% (6/20/14 to 2/10/15) set aside for validation. 80% of the original 80% was then set to "in-sample" with 20% set to "out-of-sample," as shown in Fig. 1. The bid/ask spread was set to 5 pips, and trading costs of 6 pips or $60 per full-size lot (100,000 shares) were assumed per round-turn. Both data series were included in the build, as indicated by the checkmarks in the left-hand column of the Market Data table.

 

Markets tab in Adaptrade Builder, showing data settings.

Figure 1. Market data settings for building a forex strategy for MetaTrader 4 and TradeStation.

 

Another potential problem when targeting multiple platforms is that Builder is designed to duplicate the way each supported platform calculates its indicators, which can mean that the indicator values will be different depending on which platform is selected. To avoid this possible source of discrepancy, any indicators that evaluate differently in MetaTrader 4 than in TradeStation should be eliminated from the build, which means the following indicators should be avoided:

 

Slow D stochastic

Fast D stochastic

ADX

DI-, DI+

Momentum

 

All other indicators that are available for both platforms are calculated the same way in both platforms. TradeStation includes all of the indicators that are available in Builder, whereas MetaTrader 4 does not. Therefore, to include only indicators that are available in both platforms, the MetaTrader 4 platform should be selected as the code type in Builder. That will automatically remove any indicators from the build set that are not available for MT4, which will leave the indicators that are available in both platforms. Additionally, since I noticed differences in the volume data obtained from each platform, I removed all volume-dependent indicators from the build set. Lastly, the time-of-day indicator was removed because of differences in the time zones between data files.

 

In Fig. 2, below, the list of indicators used in the build set is shown sorted by whether or not the indicator was considered by the build process ("Consider" column). The indicators removed from consideration for the reasons discussed above are shown at the top of the list. The remaining indicators, starting with "Simple Mov Ave", were all part of the build set.

 

Indicators used in the build process.

Figure 2. Indicator selections in Builder, showing the indicators removed from the build set.

 

The evaluation options used in the build process are shown in Fig. 3. As discussed, MetaTrader 4 was selected as the code output choice. After strategies are built in Builder, any of the options on the Evaluation Options tab, including the code type, can be changed and the strategies re-evaluated, which will also rewrite the code in whichever language is selected. This feature was used to obtain the TradeStation code for the final strategy after the strategies were built for MetaTrader 4.

 

Evaluation options in Builder for a forex strategy.

Figure 3. Evaluation options in Builder for the EURUSD forex strategy.

 

To create stop-and-reverse strategies, all exit types were removed from the build set, as shown below in Fig. 4. All three types of entry orders -- market, stop, and limit -- were left as "consider", which means the build process could consider any of them during the build process.

 

Order types for a stop-and-reverse strategy.

Figure 4. Order types selected in Builder to create a stop-and-reverse strategy.

 

The Builder software automatically generates rule-based logical conditions for entry and/or exit. To add a neural network to the strategy, it's only necessary to select the option "Include a neural network in entry conditions" on the Strategy Options tab, as shown below in Fig. 5. The neural network settings were left at their defaults. As part of the stop-and-reverse logic, the Market Sides option was set to Long/Short, and the option to "Wait for exit before entering new trade" was unchecked. The latter is necessary to enable the entry order to exit the current position on a reversal. All other settings were left at the defaults.

 

Strategy options in Builder for a hybrid rule-based/neural network strategy.

Figure 5. Strategy options selected in Builder to create a hybrid strategy using both rule-based and neural network conditions.

 

The evolutionary nature of the build process in Builder is guided by the fitness, which is calculated from the objectives and conditions defined on the Metrics tab, as shown below in Fig. 6. The build objectives were kept simple: maximizing the net profit while minimizing the complexity, which was given a small weight relative to the net profit. More emphasis was placed on the build conditions, which included the correlation coefficient and significance for general strategy quality, as well as the average bars in trades and the number of trades.

 

Initially, only the average bars in trades was included as a build condition. However, in some of the early builds, the net profit was being favored over the trade length, so the number-of-trades metric was added. The specified range for the number of trades (between 209 and 418) is equivalent to average trade lengths between 15 and 30 bars based on the number of bars in the build period. As a result, adding this metric put more emphasis on the trade length goal, which resulted in more members of the population with the desired range of trade lengths.

 

Metrics tab in Builder for an intraday forex strategy.

Figure 6. Build objectives and conditions set on the Metrics tab determine how the fitness is calculated.

 

The "Conditions for Selecting Top Strategies" duplicate the build conditions except that the top strategies conditions are evaluated over the entire range of data (not including the validation segment, which is separate), rather than just over the build period, as is the case for the build conditions. The top strategies conditions are used by the program to set aside any strategies that meet all the conditions in a separate population.

 

The final settings are made on the Build Options tab, as shown below in Fig. 7. The most important options here are the population size, number of generations, and the option to reset based on the "out-of-sample" performance. The population size was chosen to be large enough to get good diversity in the population while still being small enough to build in a reasonable amount of time. The number of generations was based on how long it took during a few preliminary builds for the results to start to converge.

 

Build Options in Builder for an intraday forex strategy.

Figure 7. Build options include the population size, number of generations, and options for resetting the population based on "out-of-sample" performance.

 

The option to "Reset on Out-of-Sample (OOS) Performance" starts the build process over after the specified number of generations if the specified condition is met; in this case, the population will be reset if the "out-of-sample" net profit is less than $20,000. This value was chosen based on preliminary tests to be a high enough value that it probably would not be reached. As a result, the build process was repeated every 30 generations until manually stopped. This is a way to let the program identify strategies based on the Top Strategies conditions over an extended period of time. Periodically, the Top Strategies population can be checked and the build process cancelled when suitable strategies are found.

 

Notice that I put "out-of-sample" in quotes. When the "out-of-sample" period is used to reset the population in this manner, the "out-of-sample" period is no longer truly out-of-sample. Since that period is now being used to guide the build process, it's effectively part of the in-sample period. That's why it's advisable to set aside a third segment for validation, as was discussed above.

 

Build Results

After several hours of processing and a number of automatic rebuilds, a suitable strategy was found in the Top Strategies population. Its closed trade equity curve is shown below in Fig. 8. The equity curve demonstrates consistent performance across both data segments with an adequate number of trades and essentially the same results over both data series.

 

 

Closed trade equity for the EURUSD stop-and-reverse strategy.

Figure 8. Closed-trade equity curve for the EURUSD stop-and-reverse strategy.

 

To check the strategy over the validation period, the date controls on the Markets tab (see Fig. 1) were changed to the end date of the data (2/11/2015), and the strategy was re-evaluated by selecting the Evaluate command from the Strategy menu in Builder. The results are shown below in Fig. 9. The validation results in the red box demonstrate that the strategy held up on data not used during the build process.

 

Validation results for the EURUSD stop-and-reverse strategy.

Figure 9. Closed-trade equity curve for the EURUSD stop-and-reverse strategy, including the validation period.

 

The final check is to see how the strategy performed on each data series separately using the code output option for that platform. This is necessary because, as explained above, there may be differences in the results depending on (1) the code type, and (2) the data series. We need to verify that the chosen settings minimized these differences, as intended. To test the strategy for MetaTrader 4, the data series from TradeStation was deselected on the Markets tab, and the strategy was re-evaluated. The results are shown below in Fig. 10, which duplicates the bottom curve in Fig. 9.

 

Closed-trade equity curve for MetaTrader 4.

Figure 10. Closed-trade equity curve for the EURUSD stop-and-reverse strategy, including the validation period, for MetaTrader 4.

 

Finally, to test the strategy for TradeStation, the data series from TradeStation was selected and the series for MetaTrader 4 was deselected on the Markets tab, the code output was changed to "TradeStation," and the strategy was re-evaluated. The results are shown below in Fig. 11 and appear to be very similar to the middle curve in Fig. 9, as expected.

 

Closed-trade equity curve for TradeStation.

Figure 11. Closed-trade equity curve for the EURUSD stop-and-reverse strategy, including the validation period, for TradeStation.

 

The code for both platforms is provided below in Fig. 12. Click the image to open the code file for the corresponding platform. Examining the code reveals that the rule-based part of the strategy uses different volatility-related conditions for the long and short sides. The neural network inputs consist of a variety of indicators, including day-of-week, trend (ZLTrend), intraday high, oscillators (InvFisherCycle, InvFisherRSI), Bollinger bands, and standard deviation.

 

The hybrid nature of the strategy can be seen directly in the code statement (from the TradeStation code):

 

If EntCondL and NNOutput >= 0.5 then begin
     Buy("EnMark-L") NShares shares next bar at market;

 

The variable "EntCondL" represents the rule-based entry conditions, and "NNOuput" is the output of the neural network. Both conditions have to be true to place the long entry order. The short entry condition works the same way.

 

MT4 code for the EURUSD stop-and-reverse strategy; click to open. TradeStation code for EURUSD stop-and-reverse strategy; click to open.

Figure 12. Trading strategy code for the EURUSD stop-and-reverse strategy (left, MetaTrader 4; right, TradeStation). Click the figure to open the corresponding code file.

 

Download a Builder project (.gpstrat) file containing the settings described in this article.

 

Conclusions

This article looked at the process of building a hybrid rule-based/neural network strategy for the EURUSD using a stop-and-reverse (always in the market) approach with Adaptrade Builder. It was shown how the strategy code can be generated for multiple platforms by selecting a common subset of the indicators that work the same way in each platform. The settings necessary to generate strategies that reverse from long to short and back were described, and it was demonstrated that the resulting strategy performed positively on a separate, validation segment of data. It was also verified that the strategy generated similar results with the data and code option for each platform.

 

As discussed above, the stop-and-reverse approach has several drawbacks and may not appeal to everyone. However, an always-in-the-market approach may be more attractive with forex data because the forex markets trade around the clock. As a result, there are no session-opening gaps, and the trading orders are always active and available to reverse the trade when the market changes. The use of intraday data (4-hour bars) provided more bars of data for use in the build process but was otherwise fairly arbitrary in that the always-in-the-market nature of the strategy means that trades are carried overnight.

 

The build process was allowed to evolve different conditions for entering long and short, resulting in an asymmetric stop-and-reverse strategy. Despite the name, the resulting strategy enters both long and short trades on market orders, although market, stop, and limit orders were all considered by the build process independently for each side. In practice, reversing from long to short would mean selling short twice the number of shares at the market as the strategy was currently long; e.g., if the current long position was 100,000 shares, you would sell short 200,000 shares at market. Likewise, if the current short position was 100,000 shares, you would buy 200,000 shares at market to reverse from short to long.

 

A shorter price history was used than would be ideal. Nonetheless, the results were positive on the validation segment, suggesting the strategy was not over-fit. This supports the idea that a neural network can be used in a trading strategy without necessarily over-fitting the strategy to the market.

 

The strategy presented here is not intended for actual trading and was not tested in real-time tracking or trading. However, this article can be used as a template for developing similar strategies for the EURUSD or other markets. As always, any trading strategy you develop should be tested thoroughly in real-time tracking or on separate data to validate the results and to familiarize yourself with the trading characteristics of the strategy prior to live trading.

 

Mike Bryant

Adaptrade Software

 

____________

 

This article appeared in the February 2015 issue of the Adaptrade Software newsletter.

 

HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN INHERENT LIMITATIONS. UNLIKE AN ACTUAL PERFORMANCE RECORD, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES HAVE NOT ACTUALLY BEEN EXECUTED, THE RESULTS MAY HAVE UNDER- OR OVER-COMPENSATED FOR THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS, SUCH AS LACK OF LIQUIDITY. SIMULATED TRADING PROGRAMS IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN.

 

 

If you'd like to be informed of new developments, news, and special offers from Adaptrade Software, please join our email list. Thank you.

 

Join Our Email List
Email:  
For Email Marketing you can trust

 

 

 

Copyright (c) 2004-2019 Adaptrade Software. All rights reserved.