Log in to see Cloud of Tags

Wealth-Lab Wiki

General | The Dangers of Looking Ahead (Peeking) in Trading System Development

Original tutorial by Dion Kurczek can be found here.

Imagine if you could step one day into the future. You could casually flip on CNBC and quickly see how your favorite stocks did. Then, go back to the "present" and trade based on this information.

The scenario described above is possible in WealthScript code. What's more, it's not that hard to make the mistake without noticing it. The results can be costly on several levels (including the embarrassment involved after you've published and promoted your killer script). But please don't be too hard on yourselves if this happens, because it's happened to the best of us.

I've gone through this more times than I care to mention. Sometimes it was the fault of the software I was using, but most of the time it was my mistake. The general pattern is that you come across a set of rules that perform beautifully in most markets. You're absolutely amazed at the nice smooth slope of your equity curve, and the low drawdown.

When you start to get feelings like this, be extra careful and scrutinize your code with a fine-toothed comb. You may accidentally be peeking into the future.

A Simple Example

The script below has very simple entry and exit rules. It buys when the 10 day EMA turns up and sells when it turns down.

using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;

namespace WealthLab.Strategies { public class BluntlyPeeks : WealthScript { protected override void Execute() { // Plot Indicators EMA ema = EMA.Series( Close,10,EMACalculation.Modern ); PlotSeries( PricePane, ema, Color.Purple, LineStyle.Solid, 2 ); for(int bar = 30; bar < Bars.Count; bar++) { if (IsLastPositionActive) { if( LastPosition.PositionType == PositionType.Long ) if( TurnDown( bar, ema ) ) SellAtMarket( bar, LastPosition ); } else { if( TurnUp( bar, ema ) ) BuyAtMarket( bar ); } } } } }

If you run this script on any stock I'll guarantee that you'll get outstanding results.

But, before you quit your day job and log into your online broker, read on.

No Peeking!

The trouble with the Strategy are in the BuyAtMarket and SellAtMarket functions. The first parameter of BuyAtMarket and SellAtMarket tell the function what bar you wish to place the order on. In this case, we use the "bar" variable, which is defined in our main trading system loop.

The problem is that the entry and exit rules are based on the EMA turning up/down on that same bar. We wouldn't know if the EMA turned up or down until after the close! The system is using future information that it would have no way of knowing about in reality.

This is why we stress to always use bar + 1 when using the Market orders. There are cases when you'd use Bar when opening your position. This includes buying at the close (by that time all the price info is in), and if you're using stop/limit order based on prices from the previous day. The point is that WealthScript is very flexible in what it'll allow you to do, so you have to be extra careful for gotchas like this.

Back to Reality

You might not think that such a minor problem would amount to much in terms of the system's performance. But even a small bug like this can have an enormous impact. Fix the script by replacing the buy/sell bars with "bar + 1" and rerun the system ...

Further reading

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.

ScrewTurn Wiki. Some of the icons created by FamFamFam.