-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
fetch_longer_ohlcv_through_multiple_calls_and_save_to_csv
github-actions[bot] edited this page Jun 11, 2026
·
3 revisions
# -*- coding: utf-8 -*-
import os
import sys
import ccxt # noqa: E402
import numpy as np
from datetime import datetime
print('CCXT Version:', ccxt.__version__)
# data
exchange_name = 'okx'
symbol = "BTC/USD"
max_candles = 5000
timeframe = '1h'
start = 1609459200000 # Jan 1, 2021
ms_per_candle = {
'1m': 60000,
'5m': 300000,
'15m': 900000,
'30m': 1800000,
'1h': 3600000,
'2h': 7200000,
'4h': 14400000,
'8h': 28800000,
'12h': 57600000,
'1d': 86400000,
}
now = int(datetime.now().timestamp() * 1000)
outfile = f"{symbol.replace('/', '-')}_{timeframe}_{exchange_name}_{start}-{now}.csv"
# setup
exchange = ccxt.okx()
exchange.load_markets()
ohlcv = []
# make requests for candle data
while start < now:
candles = exchange.fetch_ohlcv(symbol, timeframe, start, max_candles)
ohlcv += candles
start = start + (ms_per_candle[timeframe] * max_candles)
# write to csv
np.savetxt(
outfile,
ohlcv,
delimiter=",",
fmt='%d,%s,%s,%s,%s,%s'
)(If the page is not being rendered for you, you can refer to the mirror at https://docs.ccxt.com/)
- Install
- Examples
- Manual
- CCXT Pro
- Contributing
- Supported Exchanges
- Exchanges By Country
- API Spec By Method
- FAQ
- Changelog
- Awesome
- API Spec by Exchange
- fetchCurrencies
- alpaca
- apex
- ascendex
- aster
- backpack
- bigone
- binance
- bingx
- bit2c
- bitbank
- bitbns
- bitfinex
- bitflyer
- bitget
- bithumb
- bitmart
- bitmex
- bitopro
- bitrue
- bitso
- bitstamp
- bitteam
- bittrade
- bitvavo
- blockchaincom
- blofin
- btcbox
- btcmarkets
- btcturk
- bullish
- bybit
- bydfi
- cex
- coinbase
- coinbaseexchange
- coinbaseinternational
- coincheck
- coinex
- coinmate
- coinmetro
- coinone
- coinsph
- coinspot
- cryptocom
- cryptomus
- deepcoin
- delta
- deribit
- derive
- digifinex
- dydx
- exmo
- extended
- foxbit
- gate
- gemini
- grvt
- hashkey
- hibachi
- hitbtc
- hollaex
- htx
- hyperliquid
- independentreserve
- indodax
- kraken
- krakenfutures
- kucoin
- fetchBidsAsks
- latoken
- lbank
- lighter
- luno
- mercado
- mexc
- modetrade
- ndax
- novadax
- okx
- onetrading
- p2b
- pacifica
- paradex
- paymium
- phemex
- poloniex
- tokocrypto
- toobit
- upbit
- weex
- whitebit
- woo
- woofipro
- xt
- zaif
- fetchStatus