Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get OHLCV until a date #392

Closed
werewere opened this issue Oct 24, 2017 · 3 comments
Closed

How to get OHLCV until a date #392

werewere opened this issue Oct 24, 2017 · 3 comments
Assignees
Labels

Comments

@werewere
Copy link

Hi, I'm trying to get data not only with a starting date, but also with a finish date. I mean, something like this:

since=int(datetime.datetime.strptime('2017-1-15 00:00:00', '%Y-%m-%d %H:%M:%S').strftime("%s"))
until=int(datetime.datetime.strptime('2017-1-17 00:00:00', '%Y-%m-%d %H:%M:%S').strftime("%s"))
poloniex.fetch_ohlcv("ETH/BTC", '5m', since, until)

I know that fetch_ohlcv doesn't allow "until" parameter, and I wonder if its planned to be implemented, or what would be the suggested way to get a range. I wouldn't want to get a full range, from some date until today, but I'd want to get data for specific moments in time, like in my example (a month from the past year, or a day in the past or something like that)

I found the ticket #215 but after checking the provided example, I'm not sure that it is doing the correct result. In the example, I changed the initial data with this:

from_datetime = '2017-10-22 00:00:00'
from_timestamp = exchange.parse8601(from_datetime)

#now = exchange.milliseconds()
until = '2017-10-23 00:00:00'
now=exchange.parse8601(until)

But then when I print "data", the results are not in that range:

(1508887746600, 'Fetching candles starting from', '2017-10-22T00:00:00.000Z')
(1508887754233, 'Fetched', 142, 'candles')
('First candle epoch', 1508632200000, '2017-10-22T00:30:00.000Z')
('Last candle epoch', 1508886000000, '2017-10-24T23:00:00.000Z')
(1508887754233, 'Fetching candles starting from', '2017-10-22T02:22:00.000Z')
(1508887757689, 'Fetched', 138, 'candles')
('First candle epoch', 1508639400000, '2017-10-22T02:30:00.000Z')
('Last candle epoch', 1508886000000, '2017-10-24T23:00:00.000Z')
(1508887757689, 'Fetching candles starting from', '2017-10-22T04:40:00.000Z')
(1508887761784, 'Fetched', 133, 'candles')
('First candle epoch', 1508648400000, '2017-10-22T05:00:00.000Z')
('Last candle epoch', 1508886000000, '2017-10-24T23:00:00.000Z')

I'm not sure to understand how to do this...

@kroitor kroitor changed the title How to get ohlcv until a date (Question) How to get OHLCV until a date Oct 24, 2017
@kroitor kroitor self-assigned this Oct 24, 2017
kroitor added a commit that referenced this issue Oct 24, 2017
kroitor added a commit that referenced this issue Oct 24, 2017
kroitor added a commit that referenced this issue Oct 25, 2017
@kroitor
Copy link
Member

kroitor commented Oct 25, 2017

Hi!

I know that fetch_ohlcv doesn't allow "until" parameter, and I wonder if its planned to be implemented, or what would be the suggested way to get a range.

The fetchOHLCV method doesn't have an "until" argument, but it has a "limit" argument instead, that is the number of candles you want back. The "limit" argument is more common than "until", because most of exchanges accept a number of candles, not the end date of the range.


Example A

So, let's say, you are fetching 2 days of 5m timeframe:

(1440 minutes in one day * 2 days) / 5 minutes = 576 candles

Therefore we need a limit of 576 candles. As of ccxt v1.9.252, you can do this:

# datetimes are UTC
import ccxt
poloniex = ccxt.poloniex()
ohlcv = poloniex.fetch_ohlcv("ETH/BTC", '5m', poloniex.parse8601('2017-01-15 00:00:00'), 576)
for entry in ohlcv:
    print('Example A', poloniex.iso8601(entry[0]), entry[1:5])

This will give you exactly the amount of candles starting from 2017-01-15 00:00:00 to 2017-01-17 00:00:00, inclusively.


Example B

An alternative way, that does work with earlier versions of ccxt as well is to pass the extra end param (Poloniex demands it to be in seconds):

import ccxt
poloniex = ccxt.poloniex()
start = poloniex.parse8601('2017-01-15 00:00:00')
end = poloniex.parse8601('2017-01-17 00:00:00')  # inclusive
ohlcv = poloniex.fetch_ohlcv("ETH/BTC", '5m', start, None, { 'end': int(end/1000)})
for entry in ohlcv:
    print('Example B', poloniex.iso8601(entry[0]), entry[1:5])

Both of the above examples should output the same result. Don't forget to upgrade to the most recent version of the library.

Thx!

P.S Let us know if it helped you or not, plz.

@kroitor kroitor closed this as completed Oct 25, 2017
@werewere
Copy link
Author

If I use the first example it gives me the correct candles, but the second example (the one for earlier versions of ccxt) wasn't working and it gave me candles since the date until today.

But anyway, the first example works perfectly so I'm fine with that :)

thanks!!

(by the way, do you sleep some day?? The speed of answering questions and development of this project is insane!)

@kroitor
Copy link
Member

kroitor commented Oct 25, 2017

giphy-downsized-it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants