Algorithmic and Mechanical Forex Strategies | OneStepRemoved

  • Articles
  • Sophisticated Web Sites
  • Automated Trading
  • Testimonials
  • Contact

Neural Network

September 7, 2012 by Shaun Overton Leave a Comment

Neural network is one of the more recent buzzwords in trading. It sounds cool and sophisticated. Not too many people seem to understand what neural networks are all about.

Neurons in the Real World

Our brains are phenomenally complicated. What surprises most people, however, is that the brain is more or less an enormous box of circuits. Neurons are cells that act like circuits with “electrical wires”, called axons, that run out and connect across the human body. Every motion, perception or action that you do is the sum of all the axons firing electrical impulses. Change occurs whenever the frequency of electrical impulses sent from the neuron varies. More impulses cause one reaction, a reduction causes another.

Neural networks attempt to emulate processes of the human brain by organizing information into neurons. Unlike actual neuron cells, a network neuron only exists in the machine. It’s a machine weight that contains information about whatever is under study.

A neural network for a trading system might decide to study common indicators like a moving average, the RSI and Stochastics oscillator. The moving average value for the current bar counts as its own neuron. The RSI is different, so it gets to be a separate neuron. If I have ten indicators in my toolbox, then I have 10 neurons in my network.

Computers traditionally solve linear, simple problems. If you want to know the result of mathematical operations like the cube root of 355, computers are perfect for the task. They rapidly calculate a precise answer.

As in human brains, neural networks form synapses with other neurons. When trained, groups of neurons can learn to recognize patterns. It is this property that makes neural networks so useful. This allows us to create programs that would be impossible with traditional computing. Creating a software program to recognize a face, for example, would be extremely difficult. It is much easier to train a network to recognize a face by repeatedly showing the network faces.

The brain is a fascinating topic in its own right. As an aside, my wife and I are taking a survey course in neuroscience through a video series of The Great Courses. If you have any interest at all in the subject matter, I highly recommend Understanding the Brain by Jeanette Norden. It covers in detail how neurons connect to anatomy throughout the brain and entire body.

Neural Networks and Forex Trading

Neural networks come into play when the answer is not so precise. Sticking with this blog’s theme of forex trading, there is no right answer to what makes the perfect trading system. A typical retail investor might say the best trading system is the one that makes the most money. Another might say the best trading system is the one with the highest Sharpe ratio. Many want something in the middle.

The “best trading system” problem is ambiguous, which makes it an ideal candidate for attacking with neural networks. The designer outlines sets of rules which, in the trader’s opinion, form a numerical way of measuring the best system.

Human brains host approximately 100 billion neurons. Despite the best efforts of many of our customers, I have yet to meet anyone with 100 billion market indicators at their disposal. One way to amplify the effect of neurons in our toolbox is to create hidden layers.

A network is made up of multiple layers, each made up with multiple neurons. Each neuron is connected to every neuron in the next layer. Every connection then carries its own unique weighted value. A neuron will pass on its value by multiplying the value of the neuron and by the weight of the outgoing connection. The neuron at the end of the outgoing connection will sum up all of its incoming connections and propagate that result onto the next layer through all of its outgoing connections.

Pictures make the idea far more intuitive. Figure 1 contains a small example. The 2 and 3 on the left are the inputs into the network. These inputs get multiplied by the weight of the connection to the next layer. The 2 is multiplied by 0.5 giving us 1, and 3 by 2 giving us 6. The second layer contains one node which sums up the results from the previous layer, giving us 7. The next step would be to multiply 7 by the weights on the outgoing connections and pass it onto the next layer.

Figure 1: An example of a neural network propagating results forward.

The short example above can be repeated and chained together to form a larger network. Below, in Figure 2, we have an example of a larger network. The example network has 3 inputs which are connected to a hidden layer. The hidden layer is then connected to a single output. The hidden layers are to facilitate training. The more complex the problem the more layers and nodes needed.

Figure 2: An example of a larger neural network.

The network learns by updating the weights of its many connections. There are many software algorithms that are used to accomplish learning in neural networks. They fall into two categories, supervised learning and unsupervised learning. Supervised learning is accomplished with the user telling the network if its predictions are correct or not. The network then calculates its error and uses one of the algorithms to correct the mistake. An example of this is reverse propagation, which calculates the error of a network’s prediction. The network then uses a quick algorithm to update each of the connection weights with that error. Reverse propagation is one of the more common training strategies.

Unsupervised learning uses some type of fitness or scoring algorithm in which the network will score itself with and try to improve on every subsequent attempt. An example of unsupervised training is the genetic algorithm. This algorithm creates a population of neural networks and uses a scoring algorithm designed by the user to rank the population. After that, it is survival of the fittest. The top ranked networks get to stay and “reproduce” and the bottom ranked get thrown out. The networks reproduce by mixing and matching connection weights.

Neural networks can substantially assist systems traders in their algorithm design by exploring billions of combinations among a relatively small toolbox of indicators. This differs from standard optimization, which involves plugging numbers into various indicators looking for whatever combination returns the most money.

The fact that networks can consider multiple measures (balance, Sharpe Ratio, etc) to determine the best trading system helps reduce the likelihood that it overemphasizes one particular measure. A good example of this is account balance. If a system weighs the give and take between the net return and the risk adjusted return, it starts to step away from number crunching to discover the best numbers to use and head towards actual learning and pattern recognition.

Neural networks are proving themselves to be very useful in a wide range of applications from facial recognition to currency market predictions. They excel where there are patterns that are difficult for us to recognize. That ability makes networks invaluable in solving difficult problems involving multiple variables.

Filed Under: How does the forex market work?, Test your concepts historically, Trading strategy ideas Tagged With: back propogation, forex, genetic algorithm, neural network

Optimize an Expert Advisor

February 20, 2012 by Shaun Overton 1 Comment

One of the lesser known features of the MetaTrader backtester is the optimization feature. It’s so small that you could be forgiven for overlooking it.

Optimization is the process to maximize a certain outcome. In this case, it’s profit. Any EA developer wants to maximize the amount of profit made over a given period of time. The MetaTrader optimizer allows the trader to search for the combination of inputs that yielded the maximum profit over a given period of time.

The process is identical to running a backtest, except that MT4 runs multiple backtests at the same time. It then organizes the results and offers up the best combination.

Telling the backtester to run in optimization mode is easy. Simply put a check next to the word Optimization. MetaTrader will then sort through the combinations that you tell it to consider.

MetaTrader EA Optimization option

Place a check in the box next to Optimization in the MT4 backtester

The next step is to click on the Expert properties button to the right. A new window appears that contains three tabs: Testing, Inputs and Optimization. These screens allow the trader to inform MetaTrader which variables to consider for testing and how to weight the results.

Testing

The top of the testing section applies to every type of backtest. Here you can select the starting balance. MetaTrader defaults the option to $10,000, although you can make this any amount of your choosing.

The second default option allows the trader to restrict the direction of trades. It’s a frequent expert advisor programming request. It’s also one that is unnecessary. Both the backtester and expert advisor options screen allow the trader the option of restricting trades to long only or short only without additional programming. If the EA is not well programmed, this setting may cause errors 4110 or 4100 to appear all over the trading journal. It’s harmless. The only effect should be that the backtester slows down. It’s the result of writing to the journal hundreds of times or more.

The testing tab of the MetaTrader backtester

The testing tab of the MetaTrader backtester

A groupbox appears underneath these options that inexplicably relates to the optimization process. You’d think it would make more sense to place it in its namesake tab. That’s typical MetaQuotes logic at work.

The first line contains numerous parameters for choosing the best option. User overwhelmingly select for the largest account balance, but other options include the profit factor, expected payoff, maximum drawdown and drawdown percent.

The last line automatically uses a genetic algorithm. Optimization processes use either brute force methods or genetic algorithms. Brute force strikes most people as intuitive although obviously exhausting. The software tests every combination possible. Genetic algorithm’s attempt to make the process more intelligent. When the software sees that certain parameters almost inevitably lead to a losing performance, the algorithm skips similar tests where it expects to lose.

This is a great idea if you have a quality genetic algorithm. My opinion of the MetaTrader backtester is less than stellar. I don’t feel very confident about the algorithm at all. If you don’t mind spending extra time waiting for test results then I suggest unchecking this option. You don’t want to miss a potentially important combination.

Inputs

Most people find this screen confusing. The first column, called value, strictly controls inputs for simple backtests. The Value column is totally ignored during an optimization run.

The inputs tab of the MT4 backtester expert settings

The inputs tab of the MT4 backtester expert settings

The important columns for this task are Start, Step and Stop. Start is the lowest number that the MT4 backtester will consider. Step refers to the interval between the lowest value and the highest value. Tightly controlling this setting allows the user to gain quick insights into how changing the variable values affects performance without dragging the tests out for a full week. Stop is the highest number that the expert advisor will use.

The most obvious candidate for testing in this example is the Take Profit value. The default setting is listed at 50. If you trade the majors, you might want to consider settings ranging between 10 pips and 200 pips. That means that you set Take Profit row to 10 for the Start column and 200 for the Stop column. The real trick here is selecting the Step. If you choose Step = 1, then MetaTrader will run a separate test for every value between 10 and 200. That’s 190 tests, which is overkill. A step of 10 cuts the total number of tests down to 19.

Optimization

This section is the nit-picky part. If a trader feels it’s unacceptable to have 10 consecutive losses in a row, he can place a check next the the Consecutive wins box. MT4 automatically discards any tests which yield a result that contains anything checked off.

The optimization tab in the MT4 backtester expert properties

The optimization tab in the MT4 backtester allows users to discard tests with undesirable traits.

When you finish going through each of the tabs, push OK in the bottom right corner. It’s time to launch the tests.

Curve fitting in the MT4 Optimizer

A word of warning: my personal opinion is that optimizing an expert advisor is usually a very bad idea. The unique settings that yield the most profit in 2012 are unlikely to yield the most profit in 2013. If you don’t control for random chance, there’s a good probability that the 2012 best combination may result in catastrophic losses in 2013.

I recommend that traders pursue any strategy development work in NinjaTrader. I don’t like the idea of optimizing at all. Instead, I always focus on testing strategies for entry and exit efficiency. I know from years of experience that these values never fundamentally change on instruments of the charts traded. Entry and exit efficiencies make wonderful metrics for automated trading because they are so stable.

Filed Under: MetaTrader Tips, Test your concepts historically, Trading strategy ideas Tagged With: backtest, backtester, brute force, curve fitting, drawdown, EA, expert advisor, genetic algorithm, inputs, MetaQuotes, metatrader, mt4, optimization, optimizer, profit factor, Take Profit, testing

FREE trading strategies by email

Trending

Sorry. No data so far.

Archives

  • Dominari
  • How does the forex market work?
  • Indicators
  • MetaTrader Tips
  • MQL (for nerds)
  • NinjaTrader Tips
  • Pilum
  • QB Pro
  • Stop losing money
  • Test your concepts historically
  • Trading strategy ideas
  • Uncategorized
  • What's happening in the current markets?

Translation


Free Trading Strategies

Privacy PolicyRisk Disclosure

Copyright © 2023 OneStepRemoved.com, Inc. All Rights Reserved.