-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_ohlcv_1d.py
34 lines (28 loc) · 975 Bytes
/
fetch_ohlcv_1d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import ccxt
import pprint
import pandas as pd
with open("./upbit.key") as f:
lines = f.readlines()
api_key = lines[0].strip()
api_secret = lines[1].strip()
exchange = ccxt.upbit(config={
'apiKey': api_key,
'secret': api_secret,
'enableRateLimit': True
})
ohlcv = exchange.fetch_ohlcv(symbol="BTC/KRW", timeframe="1d")
df = pd.DataFrame(ohlcv, columns=['datetime', 'open', 'high', 'low', 'close', 'volume'])
pd_ts = pd.to_datetime(df['datetime'], utc=True, unit='ms') # unix timestamp to pandas Timestamp
pd_ts = pd_ts.dt.tz_convert("Asia/Seoul") # convert timezone
pd_ts = pd_ts.dt.tz_localize(None)
df.set_index(pd_ts, inplace=True)
df = df[['open', 'high', 'low', 'close', 'volume']]
print(df)
# pandas Timestamp
# last_min = df.index[-1]
# print(last_min)
# print(last_min.tzinfo)
# print(last_min.year, last_min.month, last_min.day, last_min.hour, last_min.minute)
# excel
df.index = df.index.tz_localize(None)
df.to_excel("btc-krw-1d.xlsx")