-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
fetch ohlcv on new candle
github-actions[bot] edited this page Jun 11, 2026
·
2 revisions
# -*- coding: utf-8 -*-
import os
import sys
import time
import ccxt # noqa: E402
from ccxt.base.decimal_to_precision import ROUND_UP # noqa: E402
# common constants
msec = 1000
minute = 60 * msec
hold = 30
exchange = ccxt.binance()
limit = 500
timeframe = "5m"
interval = exchange.parse_timeframe(timeframe) * 1000
while True:
try:
print(exchange.milliseconds(), 'Fetching candles')
since = exchange.round_timeframe(timeframe, exchange.milliseconds(), ROUND_UP) - (limit * interval)
ohlcv = exchange.fetch_ohlcv('ETH/BTC', timeframe, since=since, limit=limit)
print(exchange.milliseconds(), 'Fetched', len(ohlcv), 'candles')
first = ohlcv[0][0]
last = ohlcv[-1][0]
print('First candle epoch', first, exchange.iso8601(first))
print('Last candle epoch', last, exchange.iso8601(last))
# Calculate time to next candle and sleep for that many seconds
sleeptime = (exchange.round_timeframe(timeframe, last, ROUND_UP) - exchange.milliseconds()) / 1000
print('sleeping for: ', sleeptime, 's', sleeptime // 60, 'min')
time.sleep(sleeptime)
except (ccxt.ExchangeError, ccxt.AuthenticationError, ccxt.ExchangeNotAvailable, ccxt.RequestTimeout) as error:
print('Got an error', type(error).__name__, error.args, ', retrying in', hold, 'seconds...')
time.sleep(hold)(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