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

In this tutorial, we will demonstrate how to use the Gate.io spot market API to stream cryptocurrency prices in real-time using Python. Streaming prices is crucial for implementing use cases such as analyzing an incoming stream of price data in real-time for generating trading signals or conducting price analytics. One of the benefits of using Gate.io for streaming prices is that it features many smaller cryptocurrencies that may not be available on larger exchanges like Coinbase or Binance. These smaller, more volatile cryptocurrencies can make for an interesting platform for price analysis and algorithmic trading. Despite being a smaller player in the cryptocurrency exchange market, ranking within the top 20, Gate.io offers a unique set of assets for traders to consider.

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 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
Output exceeds the size limit. Open the full output data in a text editor
{'MKR3S_USDT':        Symbol                  Timestamp             Volume    Price  \
 0  MKR3S_USDT 2021-05-15 15:36:59.449017  240761.4473870418  0.03816   
 1  MKR3S_USDT 2021-05-15 15:37:13.762682  240830.5653870418  0.03820   
 2  MKR3S_USDT 2021-05-15 15:37:27.148195  240830.5653870418  0.03820   
 3  MKR3S_USDT 2021-05-15 15:37:40.406192  240830.5653870418  0.03820   
 
    Price_Delta  Price_Delta_Percent  
 0      0.03816                  1.0  
 1      0.03820                  1.0  
 2      0.00000                  0.0  
 3      0.00000                  0.0  ,
 'BANK_USDT':       Symbol                  Timestamp           Volume   Price  Price_Delta  \
 0  BANK_USDT 2021-05-15 15:36:59.449017  162.21144309298  719.97       719.97   
 1  BANK_USDT 2021-05-15 15:37:13.762682  162.21144309298  720.92       720.92   
 2  BANK_USDT 2021-05-15 15:37:27.148195  162.22547024346  720.92         0.00   
 3  BANK_USDT 2021-05-15 15:37:40.406192  162.22547024346  720.92         0.00   
 
    Price_Delta_Percent  
 0                  1.0  
 1                  1.0  
 2                  0.0  
 3                  0.0  ,
 'COOK_USDT':       Symbol                  Timestamp           Volume    Price  \
 0  COOK_USDT 2021-05-15 15:36:59.449017  5336969.0918761  0.05500   
 1  COOK_USDT 2021-05-15 15:37:13.762682  5338396.2698761  0.05517   
...
    Price_Delta_Percent  
 0                  1.0  
 1                  1.0  
 2                  0.0  
 3                  0.0  }

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 in 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, consider 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.

Sources and Further Reading

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

0 0 votes
Article Rating
Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x