question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch_ohlcv returning strange results

See original GitHub issue

*First of all i understand this is under development

i’m a little bit confused about how the fetch_ohlcv returns data. I have an algo that i drip feed bars every day (or hour etc). For example, every day i want to give a OHLCV daily bar to the algo to process, the algo stores a list of all previous daily candles.

So to clarify, i would have a timer that runs every day, pulls the last full closed daily bar up to the millisecond and feeds it to my algo.

I don’t want a bar in progress. I want the close of the bar to be the current price

  • OS: Windows 10
  • Programming Language: Python 3.5
  • CCXT version: 1.10.413
  • Exchange: bitmex
  • Method: fetch_ohlcv
import ccxt

exchange = ccxt.bitmex()
exchange_markets = exchange.load_markets()
ticker = 'BTC/USD'

print('current exchange time')
print(exchange.iso8601(exchange.milliseconds()))
print()

#the latest close of the ticker - in the following lines i'm expecting the close price of the bar to be this value
print('latest close: ' + str(exchange.fetchTicker(symbol=ticker)['close']))
#this seems to get really old data, 2016 - I would expect the most recent 100 bars by default?
bars = exchange.fetch_ohlcv(ticker,'1d')
print('bars in array: ' + str(len(bars)))
print(bars[-1])
print(exchange.iso8601(bars[-1][0])) 
print()


print('latest close: ' + str(exchange.fetchTicker(symbol=ticker)['close']))
#does this get the latet 1day closed bar?
bars = exchange.fetch_ohlcv(ticker,'1d', since=exchange.milliseconds())
print('bars in array: ' + str(len(bars)))
print('# the -open- is higher than the -high- ?')
print(bars[-1])
print(exchange.iso8601(bars[-1][0])) 
print()


print('latest close: ' + str(exchange.fetchTicker(symbol=ticker)['close']))
#I would expect this to return one full day of ticks in the form of 1 day bar
bars = exchange.fetch_ohlcv(ticker,'1d', since=exchange.milliseconds() - (24 * 60 * 60 * 1000)) # 1 day
print('#returns two bars, but i only put since 1 day of milliseconds')
print('bars in array: ' + str(len(bars)))
print('second second bar in array')
print('is this a bar in progress or a closed bar?')
print(bars[1])
print('# the date is the 2017-12-26 - 1 day in the future, how is this possible')
print(exchange.iso8601(bars[1][0]))
print('first bar in array')
print(bars[0])
print(exchange.iso8601(bars[0][0]))


This is an example output

current exchange time
2017-12-25T04:54:04.260Z

latest close: 14161.0
bars in array: 100
[1451779200000, 435.99, 438, 429.25, 435, 729519]
2016-01-03T00:00:00.000Z

latest close: 14123.5
bars in array: 1
\# the -open- is higher than the -high- ?
[1514246400000, 14227.5, 14185.5, 14123.5, 14136.5, 368345]
2017-12-26T00:00:00.000Z

latest close: 14118.5
\#returns two bars, but i only put since 1 day of milliseconds
bars in array: 2
second second bar in array
is this a bar in progress or a closed bar?
[1514246400000, 14227.5, 14387, 13232, 14108.5, 225518513]
\# the date is the 2017-12-26 - 1 day in the future, how is this possible
2017-12-26T00:00:00.000Z
first bar in array
[1514160000000, 14719.5, 14788.5, 12521.5, 14227.5, 1662515600]
2017-12-26T00:00:00.000Z

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
kroitorcommented, Dec 25, 2017

I tried the last example and it is still fetching the last partial bar, do i need to get the latest build?

What about the first one?

the last bar has a timestamp [1514246400000, 14227.5, 14480, 13232, 14040.5, 315800080] 2017-12-26T00:00:00.000Z

>>> exchange = ccxt.bitmex()
>>> since = exchange.milliseconds() - 86400 * 1000 * 2
>>> limit = 2
>>> result = bitmex.fetch_ohlcv ('BTC/USD', '1d', since, limit)
>>> len(result)
2
>>> [exchange.iso8601(ohlcv[0]) for ohlcv in result]
['2017-12-24T00:00:00.000Z', '2017-12-25T00:00:00.000Z']
>>> limit = None
>>> result = bitmex.fetch_ohlcv ('BTC/USD', '1d', since, limit)
>>> len(result)
3
>>> [exchange.iso8601(ohlcv[0]) for ohlcv in result]
['2017-12-24T00:00:00.000Z', '2017-12-25T00:00:00.000Z', '2017-12-26T00:00:00.000Z']

↑ are you sure you’re doing it right? From the above it should be obvious.

1reaction
kroitorcommented, Dec 26, 2017

@DaneEdwards thanks! We really appreciate it! I’m sure many people will find it useful! You might also want to take a look at our examples/py for built-in rate-limiting, async generators and other interesting use-cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ftx fetchOhlcv returns inconsistent results · Issue #12855 · ccxt ...
the fetch_ohlcv request on ftx returns some strange, inconsistent resutls depending on parameters given. Depending on the time within the candle ...
Read more >
Ccxt binance fetchOHLCV doesn't return last data
To calculate the sar indicator, I am using fetchOHLCV to get last last historical data but the problem is that some times data...
Read more >
ccxt-dev/ccxt - Gitter
how do i know which one to use, TRUNCATE, ROUND , etc ? and DECIMAL_PLACES for each market ? Suppose the market allows...
Read more >
Exchanges — ccxt 2.4.71 documentation
This is only populated when has['fetchOHLCV'] property is true. ... the exchange and returns an exchange-specific JSON result from the API as is,...
Read more >
Anyone use backtrader to do live trading on Bitcoin exchange?
the data is then returned as part of notify_order and forward to a limit ... I noticed something strange with the ccxt fetchohlc()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found