Is there a method for building Kline trading data using live ticker information from an exchanges' API?

1.5k Views Asked by At

I have built a Python spot trading bot and intend to use it with KuCoin exchange. The bot requires historical OHLCV data for each coin on a 5m interval for the last 50 datapoints. My intention is retrieve this kline (candlestick) data for each coin (~1000 coins on the exchange) as fast as possible and preferably within a 5 minute window for adequate trading.

The API provides a client method for retrieving kline data, my current implementation, however it heavily restricts the frequency of this request. Setting a sleep time of 2 seconds will still yield a frequency violation. Furthermore, there is a note within the documentation that reads as follows:

Klines should not be polled frequently. If you need real-time information, use the trade and book endpoints along with the websocket feed.

The code below is my current implementation:

#Import Modules
import os
import pickle
import datetime as dt
import time
import threading
import random
from kucoin.client import Market

def get_symbols():
    
    """
        This function retrieves all tradable symbols from
        the exchange.
        
        Parameters:
        -----------
        None
        
        Returns:
        -----------
        symbols: list(str)
            list of trading symbols
        
    """
    
    #Get list of possible symbols
    all_symbols = client.get_symbol_list()
    
    #For all tickers
    symbols = []
    for ticker in all_symbols:
        
        #Pull data
        symbol = ticker["symbol"]
        trading = ticker["enableTrading"]
        
        #keep if trading allowed
        if trading:
            symbols.append(symbol)
        
    return symbols

def update_kline(symbol,source_dict):
    
    #Points to pull
    DATA_PTS = 50
    
    #Get kline data
    klines = client.get_kline(symbol,'5min')
    klines = klines[:DATA_PTS]
    
    #Get the lastest epoch time from kline dataset
    latest_time = int(klines[0][0])
    
    #If kline data is not recent (info is bugged), skip
    if abs(end-latest_time) > 5*60:
        return
    
    #Make sure data has 50 full datapoints
    if len(klines) == DATA_PTS:
        source_dict[symbol] = klines    
  
def update_klines(source_dict,symbols):
    
    """
        This function continously updates kline data
        for all given symbols to a destination dictionary
        "source_dict".
        
        Parameters:
        -----------
        source_dict: dict
            where keys are symbol names and value is
            kline 2D array/list data
            
        symbols: list(str)
            symbols to pull data from
            
        Returns:
        -----------
        None
        
    """
    
    #Forever
    while True:
        
        #For each symbol
        for i,symbol in enumerate(symbols):

            #Update dictionary with kline data for symbol
            update_kline(symbol,source_dict)
            
            #Sleep for some time to avoid IP freeze
            time.sleep(random.uniform(2,3))

def main():
    
    """
        Main function to run bot.
        
        Parameters:
        -----------
        None
        
        Returns:
        -----------
        None
        
    """
    
    #Initialize client
    client = Market(url='https://api.kucoin.com')
    
    #Get symbols
    symbols = get_symbols()
    
    #Source dict which will hold kline data
    source_dict = {}
    
    #Set thread to continuously update kline data
    t = threading.Thread(target = update_klines,args=(source_dict,symbols))
    t.start()
    
#Run main function in script
if __name__ == "__main__":
    main()

The above execution will eventually yield a return error code indicating there are too many requests. The first viable solution I can think of is to manually build the kline data using a different method. The API mentions using book and trade endpoints with websockets (not familiar with websockets). However, I am not familiar with how a kline data frame could be assembled using such data.

The second potential solution I can think of is to use an alternate exchange's API, such as Binance or even data from the yfinance library which could have less stringent kline frequency limitations. However I imagine the kline data will be somewhat different than the exchange I intend to trade on (KuCoin) and thus be problematic. However I am using the historical kline data to predict buy-in candidates. Therefore, this solution would involve using a different API/library to acquire frequent kline data and once a viable candidate is found for a buy-opportunity, request the single kline data for the symbol of interest from KuCoin to confirm its candidacy. This, as you can imagine, will have accuracy and delay drawbacks.

KuCoin Official API Reference: https://docs.kucoin.com/#market-data

KuCoin Python API Reference: https://github.com/Kucoin/kucoin-python-sdk

0

There are 0 best solutions below