Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Last active February 2, 2023 04:57
Show Gist options
  • Save yuyasugano/fd5ab6749ed90a39e9464f4b5447a8b0 to your computer and use it in GitHub Desktop.
Save yuyasugano/fd5ab6749ed90a39e9464f4b5447a8b0 to your computer and use it in GitHub Desktop.
ccxt library in Python to fetch OHLCV data test
#!/usr/bin/python
import ccxt
import calendar
from datetime import datetim
binance = ccxt.binance()
now = datetime.utcnow()
unixtime = calendar.timegm(now.utctimetuple())
since = (unixtime - 60*60) * 1000 # UTC timestamp in milliseconds
ohlcv = binance.fetch_ohlcv(symbol='ETH/BTC', timeframe='5m', since=since, limit=12)
start_dt = datetime.fromtimestamp(ohlcv[0][0]/1000)
end_dt = datetime.fromtimestamp(ohlcv[-1][0]/1000)
# convert it into Pandas DataFrame
import pandas as pd
df = pd.DataFrame(ohlcv, columns = ['Time', 'Open', 'High', 'Low', 'Close', 'Volume'])
df['Time'] = [datetime.fromtimestamp(float(time)/1000) for time in df['Time']]
df.set_index('Time', inplace=True)
@yuyasugano
Copy link
Author

@avatar-lavventura Time is set as Index in this Pandas DataFrame object. That's why it shows like this. As this is not visualizing purpose, there is no way to control how it shows in the library in my understanding. Or you may find something in the official document of Pandas. Thanks.

@mablue
Copy link

mablue commented Feb 2, 2023

take it easy

import pandas as pd
import ccxt
from datetime import datetime


exchange = ccxt.kucoin()

df = pd.DataFrame(
    exchange.fetch_ohlcv(
        symbol='BTC/USDT', 
        timeframe='5m', 
        # since=since, 
        limit=12
    ),
    columns = ['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
)

df['Time'] = [datetime.fromtimestamp(float(time)/1000) for time in df['Time']]
df.set_index('Time', inplace=True)
print(df)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment