Trader logo

Getting historical coin pair price from KuCoin using Python

by Ayman Negm 6 days ago in fintech

Find the price of your favourite coin on KuCoin exchange back in time without looking at a chart.

Photo by Behnam Norouzi on Unsplash

Hello Crypto fans. Have you ever visited a website that displays the weekly, monthly, and annual change in a coin price and wished if you could have this info on your machine without having to check a website? In this article I will show you how you can do that. I am going to explain how to use Python code to retrieve a historical price of a certain coin pair on KuCoin exchange without looking at any charts. You can use this method to retrieve a price a week ago, a month ago, a year ago, or any other timestamp of your choice.

KuCoin is one of the most famous crypto exchanges that has been in the market for around 4 years now. KuCoin is characterized by a wide variety of coins in addition to its very efficient API interface.

When you need to find a coin pair price back in time, you may watch charts over a certain period of time to check the price on a specific date. However, this is a tedious job that may require zooming in and out of charts till you reach the point of interest, and you may still not get accurate values.

Instead, I am going to show a short code that allows you to get the price exactly at the time instant that you want without using charts at all.

The Recipe

For this task, we will need these tools: “requests” library for setting a connection to the KuCoin API server, JSON decoding to decode JSON response from the server, and datetime variables to set the desired date for price retrieval. More details about requests library can be found here, more details about JSON can be found here, and more info about the datetime variable can be found here.

requests” is a core library in Python for setting up connections to severs and getting the response. The URL we are going to connect to is “https://api.kucoin.com/api/v1/market/candles”. This location allows you to access market candle data for any coin listed on KuCoin. In addition to the URL, we will insert some parameters with the request, which are: the coin symbol of interest, the candle duration, starting timestamp, and ending timestamp. The format for the coin symbol is something like this: ‘BTC-USDT’; the two symbols are separated by a hyphen.

Since we are interested in the price of only one timestamp, we do not need to retrieve more than a single price, so we set the candle duration to 1 minute, and set the difference between the starting and ending timestamps to 1 minute. This way only one price is received and the request will not take much time to complete. Let’s implement this in Python:

#==========================================

import requests

from datetime import datetime,timedelta

base_url = 'https://api.kucoin.com/api/v1/market/candles'

start_timestmp = datetime.strptime("2021-11-11T11:11:11","%Y-%m-%dT%H:%M:%S")

# END TIMESTAMP IS 1 MIN LATER THAN START TIMESTAMP

end_timestmp = start_timestmp + timedelta(minutes=1)

req_params = {'symbol':'BTC-USDT',

'type':'1min',

'startAt':int(datetime.timestamp(start_timestmp)),

'endAt': int(datetime.timestamp(end_timestmp))}

# FETCH THE DATA FROM SERVER

url_response = requests.get(url=base_url, params=req_params)

# PUT THE RESPONSE IN JSON FORMAT

response_json = url_response.json()

#==============================================

If you run the code, you will get the response as:

{'code': '200000', 'data': [['1636647120', '64892.9', '64867.9', '64895.3', '64867.9', '2.73602919', '177526.053725391']]}

The retrieved data is sorted in the following order: time, opening price, closing price, highest price, lowest price, volume, turnover. If we want to extract the closing price, we use the following command:

closing_price = float(response_json['data'][0][2])

where the ‘2’ here corresponds to the third item in the data array, which gives us 64867.9.

Now you can change the pair (I used BTC-USDT as an example) or change the timestamp date to retrieve the price of your choice.

In summary, I showed here a simple method to get the price of a coin pair at a certain historical timestamp using Python code. I hope you found this tutorial useful.

fintech

Ayman Negm

Read next: Analgesics Market Is Projected to Grow at an Exponential Rate over 2020 to 2030

Find us on social media

Miscellaneous links

  • Explore
  • Contact
  • Privacy Policy
  • Terms of Use
  • Support

© 2022 Creatd, Inc. All Rights Reserved.