Last active
February 2, 2023 04:57
-
-
Save yuyasugano/fd5ab6749ed90a39e9464f4b5447a8b0 to your computer and use it in GitHub Desktop.
ccxt library in Python to fetch OHLCV data test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
@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.
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
Thanks for this nice example. I realize
Time
andOpen High Low Close Volume
are not on the same line, is it possible to make them on the same line for better visualization?ex: