0

I wrote a simple program that gets Bitcoin data from Kraken and writes it to a CSV file every 10 seconds. I timestamped the data so I know when it's coming in. The issue I'm having is that here and there the program will cut out for an hour or two and then resume normally. I want to know why this is happening. Is it Kraken's service, something wrong with the code, or something else I'm not considering?

Here's the code:

import requests
import time
from time import sleep
import csv
from datetime import datetime



#Functions below return data from Kraken BTC exchange. Data is packed into nested dictionaries and lists. Functions unpack nested items and convert desired string into float object 


#parse the data into a list, which can be used each time the functions are called
def parse_data(request):
        json = request.json()
        values = json.values()
        values_list = [*values]
        XBT = values_list[1]
        XBT_values = XBT.values()
        XBT_values_list = [*XBT_values]
        data_dict = XBT_values_list[0]
        data_dict_values = data_dict.values()
        data_list = [*data_dict_values]
        return data_list


#Get and return BTC ask price from Kraken
#Return as float 
def ask_price(data_list):        
        return float(data_list[0][0])
                
#Get and return BTC bid price from Kraken
#Return as float
def bid_price(data_list):     
        return float(data_list[1][0])

                
#Get and return BTC last trade closed from Kraken
def last_trade_closed(data_list):
        return float(data_list[2][0])                
                
#Get and return BTC volume today from Kraken
#Return as float
def volume_today(data_list):       
        return float(data_list[3][0])
                
#Get and return BTC 24 hour volume from Kraken
#Return as float
def volume_24(data_list):
        return float(data_list[3][1])
        
                
#Get and return BTC price today from Kraken
#Return as float
def volume_price_today(data_list):      
        return float(data_list[4][0])                
                
#Get and return BTC 24 hour price from Kraken
#Return as float
def volume_price_24(data_list):      
        return float(data_list[4][1])        
                
#Get and return BTC number of trades today from Kraken
#Return as float
def trade_numbers_today(data_list):
        return float(data_list[5][0])                
        
        
#Get and return BTC 24 hour number of trades from Kraken
#Return as float
def trade_numbers_24(data_list): 
        return float(data_list[5][1])
                
                
#Get and return BTC low today from Kraken
#Return as float
def low_today(data_list):
        return float(data_list[6][0])


#Get and return BTC 24 hour low from Kraken
#Return as float
def low_24(data_list):
        return float(data_list[6][1])
                
        
#Get and return BTC high today from Kraken
def high_today(data_list):
        return float(data_list[7][0])
                
                
#Get and return BTC 24 hour highfrom Kraken
#Return as float
def high_24(data_list):    
        return float(data_list[7][1])

        
#write results to csv continuously
def write_to_csv():
        #establishing column headers 
        columns = ['Ask Price', 'Bid Price', 'Last Trade Closed', 'Volume Today', 'Volume Last 24 Hours', 'Price Today', 'Price Last 24 Hours', \
                   'Number of Trades Today', 'Number of Trades Last 24 Hours', 'Low Today', 'Low Last 24 Hours', 'High Today', 'High Last 24 Hours', 'Time']

        #write headers
        with open('BTC.csv', 'a') as file:
                writer = csv.writer(file)
                writer.writerow(columns)

                while True:
                        #make new request every 10 seconds, save the result of requested data as a list
                        request = requests.get('https://api.kraken.com/0/public/Ticker?pair=XBTUSD')
                        data_list = parse_data(request)

                        #putting function output data into list so I can write to CSV
                        final_data = [ask_price(data_list), bid_price(data_list), last_trade_closed(data_list), volume_today(data_list), volume_24(data_list), volume_price_today(data_list),volume_price_24(data_list), \
                                        trade_numbers_today(data_list), trade_numbers_24(data_list), low_today(data_list), low_24(data_list), high_today(data_list), high_24(data_list), str(datetime.now())]

                        #write function data output data to CSV
                        writer.writerow(final_data)
             
                        time.sleep(10)
                   


write_to_csv()

Here's Kraken API documentation for anyone curious: https://www.kraken.com/features/api

And here's what the timestamps look like on the CSV

Any thoughts/suggestions would be helpful, thanks!

jcm42
  • 1
  • Are you getting rate limited? The kraken API says it has a rate limit but I didn't read too much into it. It seems like that could be a possibility here. – Brandon.G Apr 21 '21 at 20:34
  • Really can't imagine it's a rate limit issue. Per Kraken: "Every user of our API has a 'call counter' which starts at 0. Ledger/trade history calls increase the counter by 2. Place/cancel order calls do not affect the counter. All other API calls increase the counter by 1. The user's counter is reduced every couple of seconds, and if the counter exceeds the user's maximum API access is suspended for 15 minutes. Starter verified users have a maximum of 15 and their count gets reduced by 1 every 3 seconds" I set my call frequency at 10 seconds so I wouldn't run into limit issues. – jcm42 Apr 21 '21 at 23:02
  • I am unable to replicate your problem. How long does it usually take until the problem starts, and are you certain no other processes are pausing Python? – Tim Stack Jun 11 '21 at 11:54

0 Answers0