MCPcopy Create free account
hub / github.com/ccxt/ccxt / fetch_ohlcv

Method fetch_ohlcv

python/ccxt/binance.py:4669–4787  ·  view source on GitHub ↗

fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#klinecandlestick-data https://developers.binance.com/docs/derivatives/opt

(self, symbol: str, timeframe: str = '1m', since: Int = None, limit: Int = None, params={})

Source from the content-addressed store, hash-verified

4667 ]
4668
4669 def fetch_ohlcv(self, symbol: str, timeframe: str = '1m', since: Int = None, limit: Int = None, params={}) -> List[list]:
4670 """
4671 fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market
4672
4673 https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#klinecandlestick-data
4674 https://developers.binance.com/docs/derivatives/option/market-data/Kline-Candlestick-Data
4675 https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Kline-Candlestick-Data
4676 https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Index-Price-Kline-Candlestick-Data
4677 https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Mark-Price-Kline-Candlestick-Data
4678 https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Premium-Index-Kline-Data
4679 https://developers.binance.com/docs/derivatives/coin-margined-futures/market-data/rest-api/Kline-Candlestick-Data
4680 https://developers.binance.com/docs/derivatives/coin-margined-futures/market-data/rest-api/Index-Price-Kline-Candlestick-Data
4681 https://developers.binance.com/docs/derivatives/coin-margined-futures/market-data/rest-api/Mark-Price-Kline-Candlestick-Data
4682 https://developers.binance.com/docs/derivatives/coin-margined-futures/market-data/rest-api/Premium-Index-Kline-Data
4683
4684 :param str symbol: unified symbol of the market to fetch OHLCV data for
4685 :param str timeframe: the length of time each candle represents
4686 :param int [since]: timestamp in ms of the earliest candle to fetch
4687 :param int [limit]: the maximum amount of candles to fetch
4688 :param dict [params]: extra parameters specific to the exchange API endpoint
4689 :param str [params.price]: "mark" or "index" for mark price and index price candles
4690 :param int [params.until]: timestamp in ms of the latest candle to fetch
4691 :param boolean [params.paginate]: default False, when True will automatically paginate by calling self endpoint multiple times. See in the docs all the [available parameters](https://github.com/ccxt/ccxt/wiki/Manual#pagination-params)
4692 :returns int[][]: A list of candles ordered, open, high, low, close, volume
4693 """
4694 self.load_markets()
4695 paginate = False
4696 paginate, params = self.handle_option_and_params(params, 'fetchOHLCV', 'paginate', False)
4697 if paginate:
4698 return self.fetch_paginated_call_deterministic('fetchOHLCV', symbol, since, limit, timeframe, params, 1000)
4699 market = self.market(symbol)
4700 # binance docs say that the default limit 500, max 1500 for futures, max 1000 for spot markets
4701 # the reality is that the time range wider than 500 candles won't work right
4702 defaultLimit = 500
4703 maxLimit = 1000
4704 price = self.safe_string(params, 'price')
4705 until = self.safe_integer(params, 'until')
4706 params = self.omit(params, ['price', 'until'])
4707 if since is not None and until is not None and limit is None:
4708 limit = maxLimit
4709 limit = defaultLimit if (limit is None) else min(limit, maxLimit)
4710 request = {
4711 'interval': self.safe_string(self.timeframes, timeframe, timeframe),
4712 'limit': limit,
4713 }
4714 marketId = market['id']
4715 if price == 'index':
4716 parts = marketId.split('_')
4717 pair = self.safe_string(parts, 0)
4718 request['pair'] = pair # Index price takes self argument instead of symbol
4719 else:
4720 request['symbol'] = marketId
4721 # duration = self.parse_timeframe(timeframe)
4722 if since is not None:
4723 request['startTime'] = since
4724 #
4725 # It didn't work before without the endTime
4726 # https://github.com/ccxt/ccxt/issues/8454

Callers

nothing calls this directly

Tested by

no test coverage detected