Jun-19-2023, 04:00 PM
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?
