Algorithmic and Mechanical Forex Strategies | OneStepRemoved

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

Walk Forward Optimization

January 13, 2014 by Shaun Overton 6 Comments

If you were walking and randomly it started to rain, would you consider carrying an umbrella tomorrow? Of course you would.

The reason I ask a rhetorical question like that is when people observe a behavior, they respond accordingly. If they expect that something might happen again, they change their behavior to accommodate the change in outcomes.

When you think about forex robots, everybody has the dream of developing a strategy that works forever. It requires no changes. The initial settings always work. Turn it on and move to the beach.

Reality, of course, is more complicated than that.

walk forward optimization

Walk forward optimization continually optimizes throughout time instead of looking for one set of static settings

That leads to expectations of what you need to do when your strategy inevitably goes awry. It’s very possible that you come up with a strategy that works and does amazingly well on the current market. However, a past genius doesn’t mean future genius. There’s always the chance that your strategy will no longer work in the future.

Why is that? It’s the same reason that you might carry an umbrella tomorrow if it rains today. People observe the market performing in a consistent manner. As more and more people make the observation, people start trading on it.  The market responds to those changes, and eventually the opportunity completely washes out as too many people have eared about it.

Walk forward testing is the process of determining whether or not your strategy has washed out. By testing on one set of data, and then testing it on a blind set, you can give yourself an indication of whether your strategy is bad or not. The goal of walk forward isn’t to prove that your strategy is good. It’s to prove that your strategy is not known to be bad.

The process of walk forward testing is very simple. You identify a set of information that you want to use for your testing and optimization. Using a real example, right now it’s the beginning of 2014. So maybe you want to look and test data from 2011 through 2012. That would be your in sample data, and then your out of sample data might be all of 2013.

In order to conduct a walk forward test, you would test and analyze your strategy 2011-2012. Then, to determine if it’s “not known to be bad”, you then walk forward to 2103 to see review the performance.

What you’ve done is a blind test. You didn’t know what how the strategy would perform in 2013 when you tested it in 2011-2012. By putting it on a blind sample, you give it the opportunity to fail.

The reason so many traders put their faith in walk forward testing is because it’s the absolute best tool to identify weaknesses in your optimization. When you’re testing a strategy, it is very likely that you’ve overfit to past opportunities.

Self feedback loops in the current market

Let me give you an example. In the current markets, a lot of traders have been banging gold on the market open where every day at market open., they sell as much gold as they possibly can. Sometimes it’s several multiples of the annual production in a span of a few minutes. What you see is an absolute freefall for five or ten minutes. That state persists for days at a time. But that doesn’t last forever. When enough traders start seeing that people bang gold on the open, they start doing the same thing.

Effectively, whoever wants gold to falloff on the market open has taught other traders to do that trade for them. As people expect gold to fall in the first five minutes of the open, they then change their behavior. Some try to jump on banging the open and go short.

Others start modifying their behavior. They notice that gold free falls for five minutes. Then, all of a sudden it stops, and more than like it reverts to the mean. They’ll start changing their tack and buying after so many minutes have elapsed from the open. They expect that the heavy volume that preceded the selling will eventually return to normal. As people change their behavior, other people respond in kind.

If enough people start selling on the open and then buying on the open five minutes later, you can see that a pattern is forming where one person responds to the actions of another. It’s a self feedback loop where the state that was working for the first couple of days no longer works in the future.

If you can identify a strategy that is able to survive those conditions, and is able to survive conditions where you didn’t do any testing and optimization, you give yourself better odds of succeeding in the future. It means that not very many traders have clued into this trading opportunity that you’ve discovered.

The approach to to walk forward testing is the antidote to the problem known as curve fitting. Curve fitting is the ultimate woulda coulda shoulda strategy.  It’s akin to opening a chart from yesterday and saying I would’ve bought here and I would’ve sold here, already knowing what transpired.

Of course you’re going to “make money” in that situation. You know with perfect information what the market did. In the future, you don’t know the perfect information. The goal of a strategy is to deal with that ambiguity.

Curve fitting means that you’ve fit everything so perfectly to past market conditions that when new situations inevitably arise, sort of akin to the phrase, “history doesn’t repeat itself, but it rhymes,” your strategy does the same thing.

You want a strategy that does well on past performance, but you’re not coming up with a strategy to make money on historical markets. The purpose of developing a strategy is to make money in future markets. When you’re backtesting, you’re trying to strike the balance between solid historical performance and, most importantly, making sure that that historical knowledge extrapolates to future performance. Your goal is to make money.

Rolling Walk Forward Optimization

Rolling walk forward optimization takes the walk forward idea and continuously improves the strategy by exposing it to new data. So let’s say that you have a twenty four month sample period. One way to go about it would be to optimize your strategy for a period of two months, then to walk it forward to the third month. You observe the behavior and you reoptimize for the second and third month, then walk it forward to the fourth month.

By doing so continuously, you eliminate the decay time of the strategy and give it a chance to adapt to ongoing market conditions. It is sort of the redheaded stepchild to machine learning. Experience and losses give the strategy the opportunity to improve and adjust to the market changes through walk forward optimization.

…you eliminate the decay time of the strategy and give it a chance to adapt to ongoing market conditions

Another important consideration for walk forward analysis is the degrees of freedom within a system. For example, let’s say that you are analyzing a moving averaage cross. You’re using two moving averages and use a fixed stoploss and take profit. That would give you four degrees freedom. The fast moving average is the first degree. The slow moving average is the second degree. The third is the stoploss and the fourth is the take profit.

The more degrees of freedom that you allow in a system vastly increases the chances 0f curve fitting your systems to historical data. The absolute best systems maintain twelve degrees of freedom or less. You want to find trading opportunities that have large numbers of trades and that offer performance that you find satisfactory.

Another element to consider in your optimization is what are you optimizing for.  Most people focus on the absolute return.  Returns are great, but most traders care much more about how they make their money instead of how much. Let me give you an example. If I had a system that made $25,000 last year, would you want it? Almost everybody says yes.

If I have a system that made $25,000 last year, but you had to lose to $15,000 before you made any money. Most people don’t want that system. What this means is that you care a lot more about the performance on a day-to-day basis rather than end result. The problem with optimization and even walk forward optimization is that you’re not necessarily focused on what you care about in the real world: the way that you’re making your money.

Most charting packages focused on the net outcome and that can cause some weaknesses in your system. If you’re range trading, what you’ve really done is cherry pick the results that are the least affected by substantial news. In effect, you’ve chosen the settings that have not yet been affected by fat tails.

If you’re trend trading, you’ve done the exact opposite. You intentionally pick the settings that maximize the fat tailes that have happened in the past. With trend trading strategies, you probably aren’t going to find consistent performance. Instead, what you’ll find is that the optimization frequently causes long, ongoing droughts of incessant drawdown. Then suddenly, almost out of nowhere, it finds a mega monster winner that returns several multiples of the drawdown that you experienced. This is fine for a hypothetical backtests, but in the real world where you’re suffering losses on a near daily basis, most traders can’t take the pain.  The weakness I find with most optimizations is that they don’t look at the consistency of performance. A potential substitute for optimizing a strategy would be looking at the linear regression of the equity curve over time. The best equity curve has the strongest linear regression slope.

Popular charting packages that implement rolling walk forward optimization are Amibroker, TradeStation, Multicharts and NinjaTrader.

Walk forward optimization in NinjaTrader

Open the Strategy Analyzer from the Control Center. Click File / New / Strategy Analyzer.

NinjaTrader Strategy Analyzer selection

Open the strategy analyzer in NinjaTrader

  1. Left mouse click on an instrument or instrument list and right mouse click to bring up the right mouse click menu. Select the menu item Walk Forward. You can also click on the “w” icon in the Strategy Analyzer toolbar. If you prefer hot keys, you can also use CTRL + W. Lastly, you can also push the “W” icon at the top left of the Strategy Analyzer.
  2. Select a strategy from the Strategy slide out menu
  3. Set the Walk Forward properties (See the “Understanding Walk Forward properties” section below for property definitions) and press the OK button.
NinjaTrader Walk Forward Optimization

There are many ways to select walk forward optimization in NinjaTrader

The Walk Forward progress will be shown in the Status Bar of the Control Center.

Filed Under: NinjaTrader Tips, Test your concepts historically Tagged With: Amibroker, backtest, curve fitting, fat tails, gold, MultiCharts, ninjatrader, range trading, self feedback loop, short, strategy analyzer, TradeStation, trend, walk forward

NinjaTrader Backtest

December 19, 2012 by Shaun Overton Leave a Comment

Running backtests in NinjaTrader is relatively straight forward, once you learn the ropes. NinjaTrader uses a special window, called the Strategy Analyzer, to run all backtests and optimizations. The first step to finding this window is to click on File \ New \ Strategy Analyzer.

When the Strategy Analyzer opens, the window is divided into 3 vertical panes. The left window allows the user to select the instrument to test. The middle window contains the statistics and backtest information. The final window on the right is not visible; it slides out when you put the mouse over the word “Backtest” in the top right corner.

Finding the symbol that you want to test

It’s important to emphasize that you need a data connection before diving into any of this. NinjaTrader is not a standalone platform. The information that you would like to test is not available by default. Instead, NinjaTrader uses the Account Connection to go out to the broker or data provider to obtain the historical data. Everything below is a moot point if you have not already downloaded historical data and/or do not have an open Account Connection.

The left pane lists all lists that have been created using the Instrument Manager. NinjaTrader supplies lists of the most common instruments available: the DOW, S&P 500 and major forex pairs. Testing an instrument like a junior gold mining stock or something less common requires creating a new list in the Instrument Manager.

One cool feature is that you can test multiple instruments at the same time. If you want to view your strategy’s historical performance on all S&P 500 stocks, then select the list name. It is not necessary to select them individually. Every stock in the list will be included in the analysis.

If this all sounds terribly confusing (Account Connection, Instrument Manager, etc), you’re right. It’s very confusing, which is why the learning curve for NinjaTrader is so steep. It takes a long time to figure out how all of these pieces fit together. Even my staff of professional programmers experienced an ugly few weeks when I started training them. If programmers have trouble figuring out how to use the software, you don’t have to feel bad about your own difficulties.

Strategy options

The right pane opens when the mouse appears over the word “Backtest” on the far right. Within it, the menu contains multiple sections that allows the user to define the strategy. Options near the top under “Parameters” are the inputs or variables that the strategy uses. Common examples include the number of shares to trade, the stop loss distance and others.

The Data series section controls the chart period. Say, for example, that you would live to backtest AAPL on 5 minute charts. The necessary steps are:

  1. Select APPL in the left pane
  2. Choose Last for Price Based on
  3. The type is Minute
  4. The value is 5, which here stands for 5 minute charts

Time Frame controls the period over which the backtest runs. Running an AAPL strategy for 2011 would cause a trade to enter 1/1/2011 for the Start Date and 12/31/2011 for the End Date.

The remaining sections largely do not apply. When they do cause problems, most stem from the Order Handling section. If you want to trade several signals in the same direction, then the option for Entries per direction must change from 1 to a predefined maximum.

Other sections

Anyone with trading experience in other platforms will find the middle pane intuitive, especially TradeStation users. Tabs at the top of the middle pane include the Summary and Graphs. Most of my personal trading analysis concentrates on these two tabs. The others are helpful for more detail oriented folks.

The Strategy Analyzer contains a number of buttons at the top left of the screen. There are four which I find to be useful.

The floppy disc icon stands for saving a file. When the backtest completes, and especially if it takes several minutes to run, then option to save results may save a significant amount of time. If you have multiple backtests and would like to share them, the only way to do so is to send someone all of your backtests. NT stores all of its saved results in a local database. The exact location is Documents\NinjaTrader 7\db\NinjaTrader.sdf. Sending your friends or colleagues this file includes all saved backtests to date. Make sure that the recipient backs up his own NinjaTrader.sdf file before using yours. Otherwise, the information will be lost.

Right clicking in the middle pane allows the user to export the summary grid to an Excel file. Although it does not look as convenient as the NinjaTrader format, the above problem highlights the need for sending and saving individual test results.

NinjaTrader doesn’t give the b, o and w enough visual importance in my opinion. The buttons are tiny, yet they control the most important feature of the backtest – the type of test to run. Do you want to run a backtest, optimization or a walk-forward optimization? These little buttons control the type of test run.

Filed Under: NinjaTrader Tips, Trading strategy ideas Tagged With: backtest, ninjatrader, strategy analyzer

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.