awfulnico
awfulnico

Reputation: 1

Is there a better way to find price data using Kraken's API?

I am trying to find how to pluck the price data of a coin from Kraken's crypto exchange. Here is my code:

def populateArray():
    priceChange = ['/', '/', '/', '/', '/', '/', '/', '/', '/', '/']
    prices = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

    while True:       
        for i in range(10):
            priceChange[9 - i] = priceChange[8 - i]
        for i in range(10):
            prices[9 - i] = prices[8 - i]
        resp = requests.get('https://api.kraken.com/0/public/Ticker?pair=XDGUSD').text
        prices[0] = resp[38:49:1] 

        while(float(prices[0]) == float(prices[1])):
            resp = requests.get('https://api.kraken.com/0/public/Ticker?pair=XDGUSD').text
            prices[0] = resp[38:49:1] 

        if float(prices[0]) < float(prices[1]):
            priceChange[0] = '-'
        elif float(prices[0]) > float(prices[1]):
            priceChange[0] = '+'
        time.sleep(1)
        print(prices)
        print(priceChange)

This is very simple, all it does is check if a price change is detected and then log it into an array. However, I am not a big fan of having to use a substring to find the information from the Response object. I am using .text to output the text of the ticker information, but I feel like there may be a better way than using a substring of the .text output. Any help is appreciated, thank you in advance!!

resp = requests.get('https://api.kraken.com/0/public/Ticker?pair=XDGUSD').text
prices[0] = resp[38:49:1] 

This is the part that finds the substring.

Upvotes: 0

Views: 1295

Answers (1)

holdenweb
holdenweb

Reputation: 37023

You say your answer is simple, and yet there are much simpler ways to approach it. Welcome to the world of programming! The better way is, as @Klaus_D said in the comments, to have your code recognise the data format it is working with.

https://api.kraken.com/0/public/Ticker?pair=XDGUSD returns data n the well-known JSON format. Since you are already using the excellent requests library you will find that the response object has a json() method whose return value is a Python dictionary.

Playing with that should help you to understand the simpler way to approach your problem.

Upvotes: 1

Related Questions