Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kucoin fetch_trades ignores SINCE param, but it works for BINANCE #22187

Open
germanrp1 opened this issue Apr 17, 2024 · 1 comment
Open

Kucoin fetch_trades ignores SINCE param, but it works for BINANCE #22187

germanrp1 opened this issue Apr 17, 2024 · 1 comment
Assignees

Comments

@germanrp1
Copy link

germanrp1 commented Apr 17, 2024

Operating System

windows

Programming Languages

Python

CCXT Version

4.2.86

Description

I am trying to get all the trades in Kucoin SINCE a timestamp.

I have tried my code with Binance and it works perfectly, but when I try with Kucoin, it gets only the last trades, and it ignores the SiNCE param.

I am using this function to get the number of trades made in each on of the last 10 candlestick in a 1 minute timeframe.

In my code, I show you the execution os this idea twice, first one for Binance exchange (it works ok, using SINCE param), and the second one for Kucoin exchange (it only get 100 trades, ignoring SINCE param).

NOTE: For Binance you must use a valid APIKEY and APISECRET
For Kucoin, this both vars can be empty

If there is another approach to get the same information for kucoin exchange, any help will be appreciated.

Thanks in advance

Code

import ccxt
import os
import sys
from datetime import datetime
from pandas import to_datetime
from pprint import pprint
import APIS


def calculaNTrades10Velas (trades, trades_per_candle1):
    
    j = 1
    for trade in trades:
        candle_timestamp = minutosYMicrosegundosACero(trade['timestamp'])
        if candle_timestamp in trades_per_candle1:
            trades_per_candle1[candle_timestamp] += 1
        else:
            trades_per_candle1[candle_timestamp] = 1
        
        j += 1
    
    return
        

def minutosYMicrosegundosACero(tiempo1):
    tiempo1dt = to_datetime(tiempo1, unit='ms').replace(second=0, microsecond=0) #Quitamos segundos y quitamos ms
    t2 = tiempo1dt.timestamp() * 1000
    return t2
        
    
    
def tiempoTiempoEnHHMMSSMS(timestamp1):
    t1dt = datetime.fromtimestamp(timestamp1/1000)
    t1dt_format = t1dt.strftime('%H:%M:%S.%f')
    return t1dt_format


def mainLoop(exchangeNombre):


    #exchangeNombre = 'K'        # If exchangeNombre = 'K' it doesn´t work
    #exchangeNombre = 'B'        # If exchangeNombre = 'B' it works OK
    # exchangeNombre Will be a parameter so in Main, this function will be executed twice:
    #   - once with param exchangeNombre = 'B' for Binance, it WORKS
    #   - once with param exchangeNombre = 'K' for Kucoin, it DOESN't WORK

    if (exchangeNombre == 'K'):
        print('EXCHANGE: Kucoin')
        exchange_name = 'kucoin'
        apiKey = APIS.apiKey_K          # Use your APIKEY for Kucoin
        apiSecret = APIS.apiSecret_K    # Use your APISECRET for Kucoin
        symbol = 'BTC/USDT'  # change for your symbol
        
    elif (exchangeNombre == 'B'):
        print('EXCHANGE: Binance')
        exchange_name = 'binance'
        apiKey = APIS.apiKey_B          # Use your APIKEY for Binance
        apiSecret = APIS.apiSecret_B    # Use your APISECRET for Binance
        symbol = 'BTC/USDT'  # change for your symbol
        
    
    exchange = getattr (ccxt, exchange_name) ({
            'apiKey': apiKey,
            'secret': apiSecret,
            'enableRateLimit': True,
            'options': {
                'fetchTradesMethod': 'publicGetHistoricalTrades',
            },
    })
            
    all_trades = []
    tiempoExchange = exchange.milliseconds ()
    since =  tiempoExchange - (10 * 60 * 1000)  # TIme to Begin the Candles: 10 minutes before now
    last_trade = {}
    
    nIteracion = 1

    while since < tiempoExchange:

        #print('Tiempo Since: ', to_datetime(since, unit='ms'), ' NO (UTC)')
        
        limit = 1000  # change for your limit
        trades = exchange.fetch_trades(symbol, since, limit)
        # HERE IS THE FAIL WITH THE SINCE PARAMETER for KUCOIN
                    
        if len(trades):
            since = trades[len(trades) - 1]['timestamp'] + 1
            all_trades += trades
        else:
            # No more trades, we go out of the while loop
            break

        nIteracion += 1

    #Calculate number of trades for each one of the 10 candlesticks before now
    trades_per_candle = {}

    calculaNTrades10Velas(all_trades, trades_per_candle) 

    sorted_candles = sorted(trades_per_candle.items())

    j = 1
    for candle_timestamp, num_trades in sorted_candles:
        print(f'Candle ({j}) - TimeStamp: ', tiempoTiempoEnHHMMSSMS(candle_timestamp), ' - Nº Trades: ', num_trades)
        j += 1

def main():
    print ('First Exchange: BINANCE')
    mainLoop('B')
    print('\n\n')
    print ('Second Exchange: KUCOIN')
    mainLoop('K')
    
main()


@germanrp1 germanrp1 changed the title Kucoin fetch_trades ignores SINCE param Kucoin fetch_trades ignores SINCE param, but it works for BINANCE Apr 17, 2024
@samgermain samgermain self-assigned this Apr 18, 2024
@samgermain
Copy link
Member

Yeah it looks like the api endpoint always just returns the last 100 trades

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants