-1

Using this API call I want to get a complete BTCUSD data set for 5 minute OHLC data.

I tried the following code in python but the API isn't returning the right data:

import requests
import pandas as pd

r = requests.post('https://api.bitfinex.com/v2/candles/trade:5m:tBTCUSD/hist', data={'start': 1434764470000, 'end': 1497922870000})
data = r.json()

Can anyone offer any help ?

David Hancock
  • 453
  • 1
  • 11
  • 24

1 Answers1

1

David!

That Bitfinex v2 endpoint is actually meant to be accessed via HTTP GET, not POST.

The params should be appended to the URL query like so:

https://api.bitfinex.com/v2/candles/trade:5m:tBTCUSD/hist?start=1434764470000&end=1497922870000

Also, you should be more specific on what you mean by the right data. If you don't get the answer at all - it may be due to a malformed request. If the prices are not corresponding to what you expect for requested period of history - you may want to ensure your timestamps are UTC time.

If you don't pass start and end filters in the HTTP GET URL querystring, you always get last 100 candles, as if there was no start/end filtering at all.

import requests
url = 'https://api.bitfinex.com/v2/candles/trade:5m:tBTCUSD/hist'
params = { 'start': 1434764470000, 'end': 1497922870000 }
r = requests.get(url, params = params)
data = r.json()
print(data)
Igor Kroitor
  • 1,548
  • 13
  • 16
  • When I run this exact same code, I get an empty list returned back to me. – Mustard Tiger Nov 03 '17 at 01:18
  • @abcla, when I execute it with python, I get this: `>>> print(data) [[1497922800000, 2607.4, 2607.5, 2609.9, 2607.2, 14.19067283], ...` – Igor Kroitor Nov 03 '17 at 06:37
  • Hi, sorry I made a mistake in what I wrote. the code does return a result but it only return at most 1000 ochl bars, is there a way for it to return all bars within a given time period? – Mustard Tiger Nov 04 '17 at 03:31
  • @abcla, with [ccxt](https://github.com/ccxt/ccxt) you can do that like shown in this example: https://github.com/ccxt/ccxt/blob/master/examples/py/fetch-bitfinex-ohlcv-history.py – Igor Kroitor Nov 04 '17 at 18:45