(Jun-19-2023, 04:00 PM)SuchUmami Wrote: I was wondering if there's a way to write the code that will create the request just once and fetch the full amount of data in a single api request?
The reason this hasn't been answered, I suspect, is that it's really a question about the
krakenex
API, that can only be answered by those familiar with that API — it's not really a Python question at all.With the interface you're showing us, where the API calls take the form of...
api.query_public('OHLC', {'pair': pair, 'interval': '5'})it's hard to see how multiple
pair
s could be retrieved in a single API call, since the argument is a dict
of parameters, which wouldn't seem conducive to combining requests.According to the exchange's API docs, though, the only query limit for the public endpoints is a rate limit that triggers when an IP is making more than 1 request per second. So it may be easier. and would seem to be sufficient, if you just ensure requests are made at least 1 second apart:
import time import krakenex import pandas as pd class RateLimitedRequester: """krakenex.API.query_public interface with rate limiting.""" def __init__(self, interval=1.0): self._last_request_time = -1 self._interval = interval self._api = krakenex.API() def query_public(self, *args): """Make a query_public API call, possibly after a delay.""" _elapsed = time.monotonic() - self._last_request_time if _elapsed < self._interval: # Sleep until _last_request_time + _interval time.sleep(self._interval - _elapsed) result = self._api.query_public(*args) self._last_request_time = time.monotonic() return result def fetch_and_save_ohlc_data(currency_pairs): # Initialize the API client with a 1-second minimum interval req = RateLimitedRequester(1.0) for pair in currency_pairs: # Fetch OHLC data for the current currency pair and 5-minute time frame ohlc_data = req.query_public('OHLC', {'pair': pair, 'interval': '5'}) [...rest unchanged...]The code will stay comfortably within the rate limits, since the execution time of the Python calls themselves will pad things out by a few extra milliseconds per call. For some extra insurance,
_last_request_time
is only updated after krakenex.API.query_public()
returns. That way, the rate-limiting is maximally pessimistic about how quickly requests can be made, and will never be thrown off by fluctuations in communication speed or API server performance.The whole thing will take at least
len(currency_pairs) - 1
seconds to run, but you won't get rate-limited no matter how many requests you make.