Python Forum
How would I alter this to a single api request?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would I alter this to a single api request?
#1
I wrote this code that will fetch data from different currencies from a crypto exchange and saves the data into a file:

import krakenex
import pandas as pd

def fetch_and_save_ohlc_data(currency_pairs):
    # Initialize the Kraken API client
    api = krakenex.API()

    for pair in currency_pairs:
        # Fetch OHLC data for the current currency pair and 5-minute time frame
        ohlc_data = api.query_public('OHLC', {'pair': pair, 'interval': '5'})

        # Check if the request was successful
        if 'result' not in ohlc_data or pair not in ohlc_data['result']:
            print(f"Error fetching OHLC data for {pair} and 5-minute time frame")
            continue

        # Convert the data to a pandas DataFrame
        pair_data = ohlc_data['result'][pair]
        df = pd.DataFrame(pair_data, columns=['time', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count'])
        df = df[['time', 'vwap']]
        df['time'] = pd.to_datetime(df['time'], unit='s')
        df.set_index('time', inplace=True)

        # Save the DataFrame to a CSV file
        filename = f"{pair.lower()}_data.csv"
        df.to_csv(filename)
        print(f"OHLC data for {pair} saved to {filename}")

# Define the currency pairs for which you want to fetch and save OHLC data
currency_pairs = ['XXBTZUSD', 'ZEURZUSD', 'XETHXXBT', 'XXBTZEUR', 'XXBTZGBP', 'XETHZEUR', 'XETHZGBP', 'XETHZUSD', 'EURGBP', 'ZEURZUSD', 'ZGBPZUSD', 'PAXGXBT', 'PAXGETH', 'PAXGEUR', 'PAXGUSD', 'XXRPXXBT', 'XRPETH', 'XXRPZEUR', 'XRPGBP', 'XXRPZUSD']  # Add more currency pairs as needed

fetch_and_save_ohlc_data(currency_pairs)
The problem is that each time it cycles through the data, it makes another api request. This means that I get close to the api limit. 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?
Reply
#2
(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 pairs could be retrieved in a single API call, since the argument is a dictof 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how can I correct the Bad Request error on my curl request tomtom 8 5,090 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Inserting multiple rows in a single request. swaroop 2 2,917 Jan-11-2021, 01:34 PM
Last Post: swaroop
  Fetching data from multiple tables in a single request. swaroop 0 1,907 Jan-09-2021, 04:23 PM
Last Post: swaroop
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,947 Jun-18-2020, 08:07 AM
Last Post: buran
  Can someone help me alter/complete my python 3.7 code kathyadventure94 1 2,509 Nov-22-2018, 04:12 PM
Last Post: ichabod801
  need to alter static picture to video file mika 1 2,759 Feb-23-2018, 02:11 PM
Last Post: ka06059

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020