Joining Futures Contracts for Backtesting

How quarterly index futures contracts become one price history in AmiBroker, and which columns break when real and adjusted prices get mixed up
Published Nov 21, 2026 | Amibroker

A share of Apple is one instrument for its whole life. A futures contract isn't. The E-mini S&P 500 is a series of separate contracts, one per quarter: March, June, September and December, coded H, M, U and Z. The December 2026 contract is ESZ26. The NASDAQ 100, Dow and Russell futures work the same way, and so do the micros.

Each contract is the busy front month for about three months, then it expires on the third Friday of its month. To backtest anything longer than a quarter you have to join the contracts end to end. Where two contracts join, their prices don't match, so every join leaves a step in the chart that nobody traded.

You already know the idea from stock splits. The difference is that a futures roll happens four times a year, every year, and the adjustment is in points, not a ratio. Almost every futures backtesting mistake comes from that step.

Two kinds of price: a raw price is one that actually traded. An adjusted price has been shifted to remove the roll gaps.

Roll on the calendar, not on volume

Pick a fixed calendar rule for when you switch from one contract to the next, for example: the last day on the old contract is 3 trading days before its last trading day. With a third-Friday expiry, that makes the Tuesday of expiry week the last day on the old contract, and the session that opens Tuesday at 17:00 Chicago the first on the new one.

A fixed rule means the backtest can always say which contract it was in on any day, and the live chart can do exactly the same. Rolling "by volume" can't be reproduced: the live platform and the backtest end up in different contracts for a few days every quarter.

The exact number of days matters less than using the same one everywhere: in your data, in the backtest and on the live platform. Two things to check:

  • How days are counted. Some calendars count a day with a short holiday futures session, such as Thanksgiving or Juneteenth, as a trading day. Others don't. When a holiday falls in expiry week, the two counts roll a day apart.
  • When volume moves. Most trading moves to the next contract in the days before expiry, and a fixed rule can switch a day or two after that. It doesn't hurt prices, but it does affect volume columns (see Volume, below).

If you use a vendor's ready-made continuous symbol, find out its roll rule and whether it's adjusted before you trust any column built on it.

The roll gap

At any moment the next contract trades at a different price from the current one. For stock index futures the next contract is normally higher, because buying the index three months later costs interest but gives up the dividends. That's the cost of carry. The gap is a property of the two contracts, not a move in the market, and it grows with the index level and with interest rates.

With interest rates around 4%, the S&P gap at each roll has been tens of points, a good part of a normal day's range. Join the raw contracts and the chart shows a gap that size four times a year that nobody traded.

The examples on this page use a made-up roll gap of +60 points: after the roll, the new contract trades 60 points above where the old one did.

Two ways to join contracts

Unadjusted (raw) Back-adjusted
What it is Each day shows the price of the contract you were in that day, glued end to end. The latest contract is left alone. Every older contract is shifted by the sum of all the gaps since, so each join is smooth.
Prices Every price really traded. Older prices never traded. The further back, the further off.
Differences (ranges, moves in points) Wrong across a roll: a fake jump four times a year. Right everywhere, because shifting both ends of a difference by the same amount cancels out.
Percentages Right, because you divide by a real price. Too small in the past, because the price you divide by is inflated.
Over time Never changes. Every older price moves again at each new roll, unless you anchor it (see below).

The adjustment adds up. After six years of +60 point rolls, a contract from six years ago is shifted up by more than 1,400 points. Here's a smaller case: a day that really closed at 3,000, with 800 points of rolls since then, shows as 3,800 adjusted. A 1% move that day was 30 points. Against the adjusted price it looks like 0.79%.

There's also ratio adjustment, which multiplies instead of adding. It keeps percentages honest but distorts points. If you size stops and targets in points, use additive adjustment. Whichever you choose, one family of measures is distorted, so you need to know which one.

You don't have to choose one series and lose the other. Store the raw prices and the adjustment beside them, and each column can use whichever is right for it.

Storing it in AmiBroker

Make each market one symbol, with raw 1-minute bars: the stored open, high, low and close are prices that really traded. That keeps every fill, stop and profit in the backtest real.

Put the roll adjustment in Aux1, one of AmiBroker's two spare numeric fields. Aux1 is the same on every bar of one contract, and it steps by exactly the roll gap when the contract changes, so Aux1 on its own tells you where every roll was and how big it was. You can import it with the rest of the bar using File > Import ASCII... and a format line like this one:

$FORMAT Ticker,Date_YMD,Time,Open,High,Low,Close,Volume,Aux1

In AFL the adjusted series is the raw price plus the adjustment:

RollAdj = Aux1;
AdjO = O + RollAdj;
AdjH = H + RollAdj;
AdjL = L + RollAdj;
AdjC = C + RollAdj;

Freeze the anchor

The usual way to back-adjust sets the newest contract to 0 and pushes every older contract up. That means every bar in your history changes at every roll, and a backtest you ran last quarter can't be reproduced.

Instead, pick one contract as the anchor and never move it. Older contracts carry a positive adjustment, and each new contract carries a negative one: the next contract is minus its roll gap, the one after that minus both gaps, and so on. Nothing already in the database ever changes. The adjusted series stays in the anchor contract's price level, and that's fine, because every correct use of it either takes a difference or shifts back to today's level.

You don't need to store the contract month. All the bars with the same Aux1 belong to one contract, and each step in Aux1 is a roll, so Aux1 plus your roll calendar tells you which contract any bar came from.

One session, one date

The index futures session runs from 17:00 to 16:00 Chicago, so it starts the evening before its trade date. If AmiBroker builds daily bars at midnight, each "day" is the end of one session glued to the start of the next, and every daily column built on it is wrong.

Decide how a session maps to a date before you import anything. One way is to shift every bar's time so the whole session lands on its trade date. Then a new day in AmiBroker means a new session, and daily bars come out right. The cost is that the chart clock no longer matches the exchange clock.

Make the roll happen at a session open too. Then a roll session is one contract from start to finish, and RollAdj is constant through every session.

The one rule for columns

Points come from the adjusted series. Percentages divide by a raw price. Any level you compare with today's price has to be shifted to today's price level first.

Ask three questions of every column:

  1. Is it a difference? A range, ATR, distance moved or stop size. Build it from the adjusted series. The adjustment cancels out, and so does the roll gap.
  2. Is it a ratio? A percentage move or a slope in percent. The top of the fraction is a difference (adjusted), and the bottom must be a real price (raw).
  3. Is it a level compared with price? A moving average, an N-day high, yesterday's close. Build it on the adjusted series, then subtract today's RollAdj before you compare it with the close.

Inside one session none of this matters. The adjustment doesn't change during the session, so intraday highs, lows, VWAP and minute ranges are the same on either series. The trouble starts when a calculation looks back across a session boundary that might be a roll. A 20-day lookback crosses a roll on about a third of all trading days.

Column pitfalls, case by case

The examples below use daily bars. On an intraday chart, get the daily values with TimeFrameCompress() and TimeFrameExpand(), as in the yesterday's close example below.

Moving averages

On raw prices: the day after the roll, 19 of the last 20 closes are from the old contract, about 60 points lower. Price suddenly looks 57 points above its 20-day average. A trend or "extended" filter fires for no reason, then fades over 20 days as the old closes drop out. A 200-day average on raw prices is never clean, because it always spans several contracts.

On adjusted prices, compared with the raw close: every bar is off by the whole adjustment, hundreds of points in the early years. This one is easy to miss, because the column still moves sensibly.

Right: average the adjusted closes, subtract today's RollAdj, then compare with the close.

MA20 = MA( AdjC, 20 ) - RollAdj;
AboveMA = C > MA20;

Daily range, true range and ATR

On raw prices: the roll day's true range runs from the old contract's close to the new contract's high or low, so the whole gap lands in it. With a +60 point gap and a quiet day, true range comes out 60 points too big. That adds about 4 points to a 14-day ATR, and the excess takes weeks to fade. Every ATR-sized stop, target or entry threshold is off for weeks after each roll, four times a year.

AmiBroker's ATR() uses the stored high, low and close, so on raw bars it has this problem.

Right: build true range and ATR on the adjusted series. No shift is needed, because it's a difference.

TR = Max( AdjH, Ref( AdjC, -1 ) ) - Min( AdjL, Ref( AdjC, -1 ) );
ATR14 = Wilders( TR, 14 );

N-day highs, lows and breakouts

On raw prices: after the roll, the old contract's highs are 60 points too low, so the first days on the new contract can break a 20-day high they never really broke. When a roll gap is negative, you get missed breakouts and false breakdowns instead.

Right: take the highest adjusted high, then subtract today's RollAdj.

High20 = Ref( HHV( AdjH, 20 ), -1 ) - RollAdj;
Breakout = H > High20;

Yesterday's close, the overnight gap and pivots

On raw prices: on the roll session, yesterday belongs to the other contract, so the opening gap includes the 60-point roll gap. Gap filters, gap-fill targets, change-from-yesterday columns and pivot levels are all off by the roll gap on roll days.

Right: take yesterday's value from the adjusted daily series, then shift it to today's level:

DailyAdjC = TimeFrameCompress( AdjC, inDaily );
YesterdayClose = TimeFrameExpand( Ref( DailyAdjC, -1 ), inDaily, expandFirst ) - RollAdj;
PriorClose5 = TimeFrameExpand( Ref( DailyAdjC, -5 ), inDaily, expandFirst ) - RollAdj;

expandFirst makes yesterday's close available from the first bar of today, which is when you'd really know it. See Look-Ahead Bias in Backtesting for why the expand mode matters.

Watch for one AmiBroker trap. If you copy yesterday's close forward with ValueWhen() and then pick it up several sessions later, you get it at the price level of the session it came from. Apply the shift to a value taken from the adjusted daily series, as above, not to an old copy.

Percentage moves and slopes

Dividing by an adjusted price: the move in points is right, but the bottom of the fraction is too big, so the percentage is too small. The further back, the worse. With 800 points of adjustment on a 3,000 index, a real 10% crash day reads as about 7.9%. A filter such as "skip days down more than 3%" gives different answers on the two series, and they disagree on exactly the days that matter most.

The error also shrinks over time, as there are fewer rolls between a bar and the anchor. So a percentage threshold chosen on the whole history means something different in the early years than in the recent ones.

Right: the top of the fraction from the adjusted series, the bottom from a real price. For an older bar, take off that bar's own adjustment, not today's:

ChangePct = ( AdjC - Ref( AdjC, -1 ) ) / Ref( C, -1 ) * 100;

AdjEMA = EMA( AdjC, 20 );
SlopePct = ( AdjEMA - Ref( AdjEMA, -10 ) ) / Ref( AdjEMA - RollAdj, -10 ) * 100;

Ratios of two differences

Columns such as where the close sits in the day's range (0 to 100%) or the size of a wick compared with the range divide a difference by a difference. The adjustment cancels twice, so they're safe on either series, as long as both parts come from the same day.

Volume

Volume is never adjusted, but the roll affects it in a different way. In the last few days on the old contract, more and more of the trading has already moved to the next one. Relative-volume and average-volume columns read low in those days, then jump at the switch. Treat volume filters on the days around a roll with suspicion, and consider skipping them.

Long lookbacks

A 252-day change in points is fine on the adjusted series. A 252-day change in percent needs a decision: the real price a year ago is that day's raw close. Neither series gives you the return of actually holding and rolling the contract, which also includes the carry. Write down which one a column means.

Fills, stops and profit

Entries and exits fill at the stored raw prices, so profit in points and dollars is real. Stop and target distances are differences, so you can size them from adjusted measures such as the ATR above.

Adjusted charts on your trading platform

Most platforms that show a continuous futures chart have a setting for how the contracts are joined. It's called back-adjusted, equalized or difference-adjusted, against unadjusted or raw. Today's prices are the same either way. Only history changes.

The platform's built-in studies, like ATR, moving averages and N-day highs, are calculated on whichever series the chart shows. On an unadjusted chart, a daily ATR takes in every roll gap, just like the raw case above. So the live chart has to be set up the same way as the backtest, or the live system trades on different numbers from the ones you tested.

Check two settings before you trust any number from the platform:

  • The roll rule. Set it to a fixed number of days before expiry that gives the same switch day as your backtest, never "by volume". Check how the platform counts: some count calendar days to the expiry, some count trading days, so the same number can mean different days.
  • The adjustment setting. Confirm it's still what you chose, especially after an update. One way to check: on old data, the difference between the platform's prices and your backtest's prices should be constant within each contract. If it wanders around, the two disagree about which bars or which contracts they hold.

Other data traps that look like roll problems

  • Time zones. The exchange runs on Chicago time. If your data is recorded in another time zone, there are a few weeks each spring and fall when the two change their clocks on different dates. Convert with real time zone rules, never a fixed offset, and check that every day's bars land in the right session.
  • Thin back months. Before the roll, the next contract can go minutes without a trade. Gaps in its minute bars are normal. Don't fill them, and don't measure a roll gap from a stale price.
  • Holidays and early closes. A short session is real. A missing session is a hole in your data. Check the bar count of every session, and stop an import that skips one.
  • Two vendors, two answers. Two data sources can agree on every high and low but disagree on a lot of closes, usually by a tick, depending on which trade lands in which minute. Compare highs and lows when you check one source against another.
  • The calendar. Holidays move expiry weeks around. Check each roll date against the exchange calendar instead of trusting a formula.

Checklist for a new futures column

  1. Write down whether it's a level, a difference or a ratio.
  2. Difference: adjusted series. Ratio: real price on the bottom, using that bar's own adjustment. Level compared with price: adjusted series, then subtract today's RollAdj.
  3. Does its lookback cross a session boundary? If not, the series doesn't matter.
  4. Print it on a roll session and the 20 sessions after one. A jump the size of the roll gap on the roll day means it's on the wrong series.
  5. Check a percentage column by hand against real traded prices on an early date, where the adjustment is largest.
  6. If the live platform calculates it too, confirm the chart's roll and adjustment settings match the backtest.