Requesting Crypto Price Data from the Gate.io REST API in Python

This tutorial shows how to use the Gate.io spot market-API for streaming cryptocurrency prices with Python. Streaming prices is essential if you want to implement use cases such as price analytics on an incoming stream of price data, for example, to generate trading signals in real-time.

Despite being one of the smaller players in the crypto exchange market (top 20), Gate.io features many smaller cryptocurrencies that we cannot find on more extensive exchanges, such as Coinbase or Binance. These small-cap cryptos are generally more volatile, making gate.io an exciting platform for price analysis and algorithmic trading.

In the following, we take a quick look at the Gate.io API documentation. Then we will write a short Python script that pulls price information in regular intervals. We store the price data for each cryptocurrency in a Pandas DataFrame, with which you can then continue to work on your projects.

Crypto REST API Python Gate.io Tutorial

Gate.io API provides access to historical prices for a wide range of cryptocurrencies.

About the Gate.io Spotmarket API

Before we look at the market endpoint, it is crucial to understand that Gate.io offers multiple markets for different ways of trading cryptocurrencies. For example, there is a futures market, a spot market, and a margin market. Each of these markets has its HTTP REST API endpoint, which provides different operations. For example, there are operations for submitting buy and sell orders at the marketplace and retrieving price and order data. The official API documentation provides a complete list of these operations.

This tutorial will only work with the spot market API endpoint and the list_tickers operation. Unlike most other functions, the list_tickers operation does not require authentication. So, we don’t need to register as long we only want to retrieve price data. Another advantage is that there is no limit to API requests due to the lack of authentication.

The list_ticker operation returns a list of data fields for cryptocurrency pairs. Examples of price pairs are BTC_USD, BTC_ETH, BTC_ADA, etc. If we limit the returned data to a specific price pair (e.g., BTC_USD), we can pass a single pair as a variable in the API call. However, it is not possible to restrict multiple pairs. So, we either retrieve data for a single pair or all pairs. For each request, the API returns a list with the following data fields:

Crypto REST API Python Gate.io Tutorial: Response returned by the Gate.io API list_tickers operation
The response returned by the Gate.io API list_tickers operation

Implementation: Retrieving Regular Price Ticker Data from Gate.io with Python

Let’s start by implementing a short Python script that will periodically call the gate.io spot market endpoint and return a response. Each time we receive a response, we will process it. We will extract multiple price fields and append them as new records to a set of DataFrames. To do this, we use a separate DataFrame per crypto price pair.

We use a separate DataFrame per price pair to ease working with the data later. An alternative would be to store all price data in a single DataFrame. However, we usually want to work with price data for specific price pairs and do Analytics on them. It is easier to have all the corresponding data in one place and do not have to filter it beforehand.

The code is available on the GitHub repository.

Prerequisites

Also, make sure you install all required packages. In this tutorial, we will be working with the following standard packages: 

In addition, we will be using the gate.io package (package name gate-API) to pull price data from the crypto exchange gate.io.

You can install packages using console commands:

  • pip install <package name>
  • conda install <package name> (if you are using the anaconda packet manager)

Step #1: Connect to the Gate.io API

First, we establish a connection to the API. We do this by creating a new api_client object and then using it as an argument to the spot API function. As a result, the gate_api library returns an api_instance that provides access to several API functions.

import gate_api
from gate_api.exceptions import ApiException, GateApiException
import pandas as pd
import time
import datetime as dt

# Defining the host is optional and defaults to https://api.gateio.ws/api/v4
# See configuration.py for a list of all supported configuration parameters.
configuration = gate_api.Configuration(host = "https://api.gateio.ws/api/v4")
api_client = gate_api.ApiClient(configuration)

# Create an instance of the API class
api_instance = gate_api.SpotApi(api_client)

Step #2: Define Functions for Calling the API and Handling the Response

Next, we create two functions. The first function contains a loop that calls the gate.io spot market list_tickers API. Every time we contact this endpoint, it will return a list with price information. We handle this data in a dictionary that contains a separate DataFrame for each cryptocurrency price pair. Managing the data means iterating through the response and extracting the price data per cryptocurrency pair. Each price data is handed over to our second function, “append_data_to_df,” which stores the data in its respective DataFrame.

The request_data function takes three arguments:

  • runs: this is how often we should call the API.
  • s: the interval in seconds in which we call the API
  • currency_pair: you can alternatively define a specific currency pair for which you want to retrieve price information. If none is specified, the API endpoint will return the complete list of all price pairs (currently ~ 270).
# this function starts calling the Gate.io API
# the response will contains price data for multiple cryptocurrencies
# the price information of each cryptocurrency will be stored in its own dataframe
def request_data(runs, currency_pair, s):
    currency_dfs = {}
    for t in range(runs):

        try:
            api_response = api_instance.list_tickers(currency_pair=currency_pair)
        except GateApiException as ex:
            print("Gate api exception, label: %s, message: %s\n" % (ex.label, ex.message))
        except ApiException as e:
            print("Exception when calling SpotApi->list_tickers: %s\n" % e)

        ts = dt.datetime.now()
        currency_response_dict = {resp.currency_pair: resp for resp in api_response
                                if "USDT" in resp.currency_pair and "BEAR" not in resp.currency_pair}
                  
        for currency_name, response in currency_response_dict.items():
            try:
                currency_dfs[currency_name]
            except KeyError:
                # Create new dataframe if currency does not have one yet
                currency_dfs[currency_name] = pd.DataFrame(columns=[
                    'Symbol', 
                    'Timestamp', 
                    'Volume', 
                    'Price', 
                    'Price_Delta', 
                    'Price_Delta_Percent'])
            
            # get the price of the currency at the last price point
            if len(currency_dfs[currency_name]) > 1:
                #print(currency_dfs[currency_name])
                price_before = currency_dfs[currency_name]['Price'].iloc[-1]
            else:
                price_before = 0
            
            # append a new record the dataframe of this currency
            new_data_as_dict = append_data_to_df(price_before, response, ts)
            
            # add this dataframe to the list of currency_dataframe. there are separate dfs per currency.
            currency_dfs[currency_name] = currency_dfs[currency_name].append(new_data_as_dict, ignore_index=True)
                
        # wait s seconds until the next request
        time.sleep(s)
    return currency_dfs

# this function is called for each cryptocurrency and everytime the gate.io API returns price data
# the function extracts price information from a single API response and adds it to a dataframe 
# example: the API response contains data for 270 cryptocurrency price pairs -> the function is called 270 time per API response
def append_data_to_df(price_before, data, ts):
    volume = data.base_volume
    price = pd.to_numeric(data.last)
    price_delta = price - price_before
    
    if price > 0:
        price_delta_p = price_delta / price 
    else:
        price_delta_p = 0
    
    new_record = {
                  'Symbol': data.currency_pair, 
                  'Timestamp': ts, 
                  'Volume': volume, 
                  'Price': price,
                  'Price_Delta': price_delta,
                  'Price_Delta_Percent': price_delta_p
                 }
    return new_record

Step #3: Start Calling the Gate.io Market API

Once we have defined the functions to request and handle the price data, we can start calling the API. In the code below, we use a request interval of 10 seconds. We stop calling the API for test purposes stop after four runs. Afterward, we print the list of DataFrames to inspect the collected data.

s = 10 # API request interval in seconds
currency_pair = '' # currency pair (optional)
runs = 4 # number of data points to fetch

df_list = request_data(runs, currency_pair, s)

df_list # list that contains one dataframe per currency
gate.io Tutorial: cryptocurrency price data from the gate.io API
Excerpt of the list of DataFrames

As shown above, we have created a list of DataFrames. It contains one DataFrame with price points per cryptocurrency price pair. You can now use these historical price data to conduct analytics or visualize price movements in real-time.

Summary

This tutorial demonstrated how to query cryptocurrency price data via the Gate.io API. We have queried the API and stored historical crypto prices into a DataFrame. Now that you are familiar with the concepts of the Gate.io API, you can tackle exciting projects. For example, you could use the data to display price information on a website or create analytics applications.

In another relataly tutorial, we trained a crypto trading bot that acts upon automated trading signals. If you are interested in developing trading bots, you might want to look at one of my recent tutorials on Gate.io crypto trading bots.

Of course, Gate.io is not the only crypto API in the market. So if you are looking for an API for crypto market data, you might also consider the Coinmarketcap API. I have recently covered it in a separate coinmarket API tutorial.

I hope you liked this post. If you have questions, let me know in the comments.

If you are interested in stock-market Prediction, check out the following articles:

Author

  • Florian Follonier

    Hi, I am Florian, a Zurich-based consultant for AI and Data. Since the completion of my Ph.D. in 2017, I have been working on the design and implementation of ML use cases in the Swiss financial sector. I started this blog in 2020 with the goal in mind to share my experiences and create a place where you can find key concepts of machine learning and materials that will allow you to kick-start your own Python projects.

Leave a Comment