pythonccxt

CCXT - fetch_ohlcv function


is there any way to fetch the ohlcv from active candle in kucoin(current Candle) with ccxt? i want compare current active candle with previous closed candles. know current candle still not closed but i need open ,high and low values from current candle. i used this, But it did not show the information of the last candle. Other functions work the same in ccxt. it's ↓ shows last 20 closed candles but not active candle.

import ccxt
import pandas

ex = ccxt.kucoin()

def ohlcv_info():
    x = ex.fetch_ohlcv('TRX/USDT', '15m', limit=20)

    df = pandas.DataFrame(x, columns=['TIME', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'VOLUME'])
    df['TIME'] = pandas.to_datetime(df['TIME'], unit='ms')
    print(f"{df}\n")
    

ohlcv_info()

Solution

  • After a quick check, it seems like the last row of the df is the active candle as its close price and volume keep changing.

    import ccxt
    import pandas
    import datetime
    import time
    
    ex = ccxt.kucoin()
    
    def ohlcv_info():
        x = ex.fetch_ohlcv('BTC/USDT', '15m', limit=20)
        df = pandas.DataFrame(x, columns=['TIME', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'VOLUME'])
        df['TIME'] = pandas.to_datetime(df['TIME'], unit='ms')
        print(f'*** {datetime.datetime.now()} ***\n {df.tail(1)}\n')
        time.sleep(5)
    
    for i in range(5):
        ohlcv_info()
    
    *** 2023-01-21 17:17:56.808981 ***
                       TIME     OPEN     HIGH      LOW    CLOSE     VOLUME
    19 2023-01-21 09:15:00  22749.1  22761.3  22729.2  22731.2  15.688291
    
    *** 2023-01-21 17:18:01.939278 ***
                       TIME     OPEN     HIGH      LOW    CLOSE     VOLUME
    19 2023-01-21 09:15:00  22749.1  22761.3  22729.2  22731.3  15.688842
    
    *** 2023-01-21 17:18:07.053381 ***
                       TIME     OPEN     HIGH      LOW    CLOSE     VOLUME
    19 2023-01-21 09:15:00  22749.1  22761.3  22729.2  22735.1  15.806622
    
    *** 2023-01-21 17:18:12.148174 ***
                       TIME     OPEN     HIGH      LOW    CLOSE     VOLUME
    19 2023-01-21 09:15:00  22749.1  22761.3  22729.2  22735.1  16.225073
    
    *** 2023-01-21 17:18:17.243179 ***
                       TIME     OPEN     HIGH      LOW    CLOSE     VOLUME
    19 2023-01-21 09:15:00  22749.1  22761.3  22729.2  22735.3  16.340071