Early access is open. Free sample datasets are live now. Download a sample

Learn

How to Backtest a Strategy with 1-Minute Data in Python

8 min read · Updated 19 November 2026 · By the marketdata.co data team

Minute bars let you test intraday ideas that daily data can't see. They also make it much easier to fool yourself, because small timing errors and missing costs add up across hundreds of trades. This walk-through runs a simple strategy on real 1-minute bars and shows where the result changes once you do it properly.

Details

The setup

We'll use our free BTC-USDT file: 500 one-minute bars from 15:40 to 23:59 UTC on 31 October 2026. Each row has symbol, interval, timestamp, open, high, low, close, volume, quote_volume, vwap, trade_count and taker_buy_volume. The first rows look like this:

timestamp open high low close volume trade_count
2026-10-31T15:40:00Z 77598.76 77598.76 77598.75 77598.76 6.02156 479
2026-10-31T15:41:00Z 77598.75 77628.53 77598.75 77628.53 8.09918 1773
2026-10-31T15:42:00Z 77628.53 77628.53 77576.34 77576.35 7.45285 1863

Source: Binance. All rows have symbol = BTC-USDT and interval = 1m; other columns omitted here.

The strategy is deliberately plain: go long when the 20-bar simple moving average of the close crosses above the 60-bar average, exit when it crosses back below. Long only, one position at a time.

Two rules make the backtest honest:

  1. Signal on the close, fill on the next open. The bar's close is only known after the bar ends. You can't also trade at it.
  2. Charge costs on every fill. We use 0.04% per side as an example taker fee plus slippage.

Code

The code

"""Example: SMA crossover on 1-minute bars with next-bar fills and costs."""
import pandas as pd

df = pd.read_csv("btc-usdt-1m-bars.csv", parse_dates=["timestamp"])
df = df.set_index("timestamp")

df["fast"] = df["close"].rolling(20).mean()
df["slow"] = df["close"].rolling(60).mean()
df["signal"] = (df["fast"] > df["slow"]).astype(int)

df["position"] = df["signal"].shift(1).fillna(0)  # act on the next bar

df["ret"] = df["open"].shift(-1) / df["open"] - 1  # open-to-open return
df["gross"] = df["position"] * df["ret"]

cost = 0.0004
df["trades"] = df["position"].diff().abs().fillna(0)
df["net"] = df["gross"] - df["trades"] * cost

print("round trips:", int(df["trades"].sum() // 2))
print("gross return %:", round(df["gross"].sum() * 100, 3))
print("net return %:", round(df["net"].sum() * 100, 3))

Reading the result

When we ran an equivalent loop on this file (enter and exit at the next bar's open, first signal after bar 60), the strategy made four round trips. Before costs it was almost exactly flat: -0.006%. After 0.04% per side it lost 0.326%. Each trade's net result was negative, between -0.028% and -0.134%.

Holding BTC from the first tradeable bar to the end of the file returned about -0.04%, so the market itself barely moved. What killed the strategy was cost. Four round trips at 0.08% each is 0.32%, which is the whole loss. That is the most common story in minute-bar backtests: the edge before costs is small, and fees and spread take all of it.

This file is 500 bars, which is far too short to judge any strategy. It is a good size to check that your code does what you think before you run it on years of data.

Watch out

Common mistakes

Filling at the signal bar's close. It looks harmless, but on 1-minute data it adds up to a large, fake edge. Shift the position by one bar, as in the code above.

Leaving out costs. At minute frequency a strategy can trade dozens of times a day. Even 0.02% per side matters. Run the test at zero cost and at a realistic cost, and only trust a result that survives the second.

Ignoring sessions. Crypto trades all day, but US stocks have a 9:30 to 16:00 ET regular session with pre-market and after-hours bars that behave very differently. Filter on the session field in our stock bars, and don't let a moving average span the overnight gap without thinking about it.

Testing on one quiet stretch. Minute bars make files big, so people test a week. Test across several months that include fast and slow markets.

Using unadjusted stock prices across a split. A 4-for-1 split in the middle of your file looks like a 75% crash. Use split adjusted bars, and check the adjusted flag.

Details

Get the data

Free 1-minute bars to test your code. 500 rows each for BTC-USDT, ETH-USDT and SOL-USDT, the last 500 minutes of October 2026.

FAQ

Frequently asked questions

How much 1-minute data do I need for a backtest?

Enough to cover different market conditions, which usually means at least a year and ideally several. A US stock has about 390 regular-session bars a day, so one year is roughly 98,000 bars per symbol. Short samples are good for debugging code, not for judging a strategy.

Should I use the close or the next open for fills?

Use the next bar's open, or later. The close of the signal bar is the price you used to make the decision, so you can't also trade at it.

Is pandas fast enough for minute-bar backtests?

Yes, for vectorised strategies like the one above, even on millions of rows. Strategies with path-dependent logic, such as trailing stops, may need a loop with numba or an event-driven backtester.

Why does my strategy look great with no costs and lose with costs?

Because minute strategies trade often and capture small moves. If the average gain per trade is smaller than fees plus spread, costs turn it negative. Look at gross return per trade before anything else.

Keep reading