EUR/USD historical candlestick chart produced with Python

Blog ·

How to Download EUR/USD Historical Data in Python (Step by Step)

EUR/USD is the most traded currency pair in the world, and its history is the starting point for countless strategies. This tutorial shows how to download EUR/USD historical data in Python, save it as CSV and analyse it with pandas.

1. Install the libraries

pip install requests pandas

2. Download daily candles

import os, requests, pandas as pd

API = "https://api.marketapi.app/v1"
H = {"Authorization": "Bearer " + os.environ["API_KEY"]}

def bars(symbol, timeframe, start, end):
    params = {"symbol": symbol, "timeframe": timeframe, "from": start, "to": end, "limit": 10000}
    rows = []
    while True:
        r = requests.get(f"{API}/bars", params=params, headers=H, timeout=30)
        r.raise_for_status()
        j = r.json()
        rows += j["data"]
        if not j.get("next_cursor"):
            return rows
        params["cursor"] = j["next_cursor"]

data = bars("EURUSD", "D1", "2024-01-01T00:00:00Z", "2026-01-01T00:00:00Z")
df = pd.DataFrame(data)
df["time"] = pd.to_datetime(df["time"])
df = df.set_index("time")[["open", "high", "low", "close", "tick_volume"]].astype(float)
df.to_csv("eurusd_d1.csv")
print(df.tail())

3. Quick analysis

df["return"] = df["close"].pct_change()
print("Annualised volatility:", df["return"].std() * (252 ** 0.5))
print(df["close"].rolling(50).mean().tail())

Tips

  • Use H1 or M15 for intraday studies; the same function works.
  • Store the CSV and fetch only new candles next time.
  • Prices come as strings to preserve precision; convert explicitly as above.

Plotting the candles

With the DataFrame from step 2, a candlestick chart takes a few lines using the mplfinance library: install it with pip, then call mplfinance.plot(df.tail(120), type="candle"). Daily candles over the last six months give a clear picture of trend and volatility.

Resampling to other timeframes

You can request any timeframe directly from the API, which is the most accurate option. If you already have hourly data, pandas can resample it: df.resample("4h").agg({"open": "first", "high": "max", "low": "min", "close": "last"}). Mind the trading-day convention when resampling to daily candles.

Keeping the dataset up to date

Store the last timestamp you saved and request only newer candles each day. A small scheduled job keeps a local EUR/USD database current at minimal cost.

Can I download GBP/USD or gold the same way?

Yes: change the symbol to GBPUSD, XAUUSD or any symbol from the ticker list.

Next: learn the difference between tick data and OHLC bars, or get your API key.

Try the market price API

1000 free credits after sign-up.

Get a free API key

More articles