BinanceからAPI経由で全ての仮想通貨(暗号通貨)の価格情報を取得する
行1ではPandasのDataFrameから列「symbol」の値を取得して変数「symbols」に格納しています。
行3-13ではBinanceからAPI経由で仮想通貨(暗号通貨)の価格を取得しています。
この関数からはPandasのDataFrameが戻り値として返されます。
行25では1日「1d」の価格変動率のグラフを作成することを指定しています。
1分間のときは「1m」、1時間なら「1h」、1週間なら「1w」を設定します。
行28-57では関数getminutedata()を呼び出してBinanceから仮想通貨(暗号通貨)の価格を取得しています。
行46-49では価格変動率を計算してDataFrameに格納しています。
行64-67ではDataFrameに仮想通貨(暗号通貨)の価格変動率を取り込んでいます。
行68-71では価格変動率が「プラス」か「マイナス」をチェックしています。
プラスのときは「True」を、マイナスのときは「False」を格納します。
行73では列「coin_name」にindexを設定しています。
symbols = df['symbol'].values
def getminutedata(symbol, interval, lookback):
res = client.get_historical_klines(symbol, interval, lookback)
frame = pd.DataFrame(res)
if frame.shape[0] == 2:
frame = frame.iloc[:,:6]
frame.columns = ['Date','Open','High','Low','Close','Volume']
frame = frame.set_index('Date')
frame.index = pd.to_datetime(frame.index, unit='ms')
frame.index = frame.index + datetime.timedelta(hours=9)
frame = frame.astype(float)
return frame
coin_name_list = [] # BTC, ETH, LTC,...
coin_currency_list = [] # USDT, USDT, USDT,...
price_list = []
percent_change_1m_list = []
percent_change_1h_list = []
percent_change_1d_list = []
percent_change_1w_list = []
#percent_timeframe = '1m'
#percent_timeframe = '1h'
percent_timeframe = '1d'
#percent_timeframe = '1w'
for symbol in symbols:
if percent_timeframe == '1m':
df = getminutedata(symbol, '1m', '2 min ago UTC')
elif percent_timeframe == '1h':
df = getminutedata(symbol, '1h', '2 hour ago UTC')
elif percent_timeframe == '1d':
df = getminutedata(symbol, '1d', '2 day ago UTC')
else:
df = getminutedata(symbol, '1w', '2 week ago UTC')
rows = df.shape[0]
if rows == 2:
# Reverse String replace()
symbol_coin = ''.join(symbol.rsplit('USDT', 1))
#symbol_coin = re.sub(r'(.*)USDT', r'\1', symbol)
symbol_currency = 'USDT'
coin_name = [symbol_coin] * rows
coin_currency = [symbol_currency] * rows
df['pct_changed_1m'] = df['Close'].pct_change()
df['pct_changed_1h'] = df['Close'].pct_change()
df['pct_changed_1d'] = df['Close'].pct_change()
df['pct_changed_1w'] = df['Close'].pct_change()
coin_name_list.append(coin_name[1])
coin_currency_list.append(coin_currency[1])
price_list.append(df['Close'].values[1])
percent_change_1m_list.append(df['pct_changed_1m'].values[1])
percent_change_1h_list.append(df['pct_changed_1h'].values[1])
percent_change_1d_list.append(df['pct_changed_1d'].values[1])
percent_change_1w_list.append(df['pct_changed_1w'].values[1])
df = pd.DataFrame(columns=['coin_name', 'coin_currency', 'price',
'percent_change_1m','percent_change_1h','percent_change_1d', 'percent_change_1w'])
df['coin_name'] = coin_name_list
df['coin_currency'] = coin_currency_list
df['price'] = price_list
df['percent_change_1m'] = percent_change_1m_list
df['percent_change_1h'] = percent_change_1h_list
df['percent_change_1d'] = percent_change_1d_list
df['percent_change_1w'] = percent_change_1w_list
df['positive_percent_change_1m'] = df['percent_change_1m'] > 0
df['positive_percent_change_1h'] = df['percent_change_1h'] > 0
df['positive_percent_change_1d'] = df['percent_change_1d'] > 0
df['positive_percent_change_1w'] = df['percent_change_1w'] > 0
df = df.set_index('coin_name')
図2-1はVisual Studio Code(VSC)の画面です。
ここでは行1の変数「symbols」の内容を表示しています。
図2-2ではPandasのDataFrameの列名を表示しています。
図2-3ではPandasのDataFrameの内容を表示しています。
ここで解説したコードをまとめて掲載
最後にここで解説したすべてのコードをまとめて掲載しましたので参考にしてください。
# Import the necessary libraries
import os
import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mpl_dates
import matplotlib.style as style
import numpy as np
import re
from binance import Client # pip install python-binance
import warnings
from pandas.core.base import DataError
warnings.simplefilter('ignore')
# Get Binance API-key / Secret Key
api_key = os.environ.get('BINANCE_API_KEY')
api_secret = os.environ.get('BINANCE_SECRET_KEY')
client = Client(api_key, api_secret, tld='us') # tld='us' , testnet=True
# Get all symbol prices
prices = client.get_all_tickers()
# Save all tickers
df = pd.DataFrame(prices)
csv_file = 'data/csv/binance/tickers.csv'
df.to_csv(csv_file, index=False)
# Save only USDT currency
filter_by = df['symbol'].str.endswith('USDT')
dfx = df[filter_by]
csv_file = 'data/csv/binance/tickers(USDT).csv'
dfx.to_csv(csv_file, index=False)
df = pd.read_csv(csv_file)
# %%
symbols = df['symbol'].values
#symbols = ['BTCUSDT','ETHUSDT','LTCUSDT']
# symbols = ['BTCUSDT', 'ETHUSDT', 'XRPUSDT' *, 'BCHUSDT', 'LTCUSDT', 'BNBUSDT',
# 'ADAUSDT', 'BATUSDT', 'ETCUSDT', 'XLMUSDT', 'ZRXUSDT', 'DOGEUSDT',
# 'ATOMUSDT', 'NEOUSDT', 'VETUSDT', 'QTUMUSDT', 'ONTUSDT', 'KNCUSDT',
# 'VTHOUSDT', 'COMPUSDT', 'HNTUSDT', 'MKRUSDT', 'ONEUSDT',
# 'BANDUSDT', 'STORJUSDT', 'BUSDUSDT', 'UNIUSDT', 'SOLUSDT',
# 'EGLDUSDT', 'PAXGUSDT', 'OXTUSDT', 'ZENUSDT', 'FILUSDT',
# 'AAVEUSDT', 'SHIBUSDT', 'CRVUSDT', 'AXSUSDT' *]
def getminutedata(symbol, interval, lookback):
res = client.get_historical_klines(symbol, interval, lookback)
#res = client.get_historical_klines(symbol, interval, lookback + ' min ago UTC')
frame = pd.DataFrame(res)
if frame.shape[0] == 2:
frame = frame.iloc[:,:6]
frame.columns = ['Date','Open','High','Low','Close','Volume']
frame = frame.set_index('Date')
frame.index = pd.to_datetime(frame.index, unit='ms')
frame.index = frame.index + datetime.timedelta(hours=9)
frame = frame.astype(float)
return frame
coin_name_list = [] # BTC, ETH, LTC,...
coin_currency_list = [] # USDT, USDT, USDT
price_list = []
percent_change_1m_list = []
percent_change_1h_list = []
percent_change_1d_list = []
percent_change_1w_list = []
#percent_timeframe = '1m'
#percent_timeframe = '1h'
percent_timeframe = '1d'
#percent_timeframe = '1w'
for symbol in symbols:
#symbol = 'BTCUSDT'
#symbol = 'ETHUSDT'
#symbol = 'LTCUSDT'
if percent_timeframe == '1m':
df = getminutedata(symbol, '1m', '2 min ago UTC')
elif percent_timeframe == '1h':
df = getminutedata(symbol, '1h', '2 hour ago UTC')
elif percent_timeframe == '1d':
df = getminutedata(symbol, '1d', '2 day ago UTC')
else:
df = getminutedata(symbol, '1w', '2 week ago UTC')
#df = getminutedata('BTCUSDT', '1m', '1 day ago UTC')
#df = getminutedata('BTCUSDT', '1d', '8 day ago UTC')
#df = getminutedata('BTCUSDT', '1d', '30 day ago UTC')
#df = getminutedata('BTCUSDT', '1d', '100 day ago UTC')
# # m -> minutes; h -> hours; d -> days; w -> weeks; M -> months
# # 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
rows = df.shape[0]
if rows == 2:
# Reverse String replace() : USDxUSD => USDx
symbol_coin = ''.join(symbol.rsplit('USDT', 1))
#symbol_coin = re.sub(r'(.*)USDT', r'\1', symbol)
#symbol_coin = symbol.replace('USDT', '') # BUG BUG BUG
symbol_currency = 'USDT'
coin_name = [symbol_coin] * rows
coin_currency = [symbol_currency] * rows
df['pct_changed_1m'] = df['Close'].pct_change()
df['pct_changed_1h'] = df['Close'].pct_change()
df['pct_changed_1d'] = df['Close'].pct_change()
df['pct_changed_1w'] = df['Close'].pct_change()
coin_name_list.append(coin_name[1])
coin_currency_list.append(coin_currency[1])
price_list.append(df['Close'].values[1])
percent_change_1m_list.append(df['pct_changed_1m'].values[1])
percent_change_1h_list.append(df['pct_changed_1h'].values[1])
percent_change_1d_list.append(df['pct_changed_1d'].values[1])
percent_change_1w_list.append(df['pct_changed_1w'].values[1])
df = pd.DataFrame(columns=['coin_name', 'coin_currency', 'price',
'percent_change_1m','percent_change_1h','percent_change_1d', 'percent_change_1w'])
df['coin_name'] = coin_name_list
df['coin_currency'] = coin_currency_list
df['price'] = price_list
df['percent_change_1m'] = percent_change_1m_list
df['percent_change_1h'] = percent_change_1h_list
df['percent_change_1d'] = percent_change_1d_list
df['percent_change_1w'] = percent_change_1w_list
df['positive_percent_change_1m'] = df['percent_change_1m'] > 0
df['positive_percent_change_1h'] = df['percent_change_1h'] > 0
df['positive_percent_change_1d'] = df['percent_change_1d'] > 0
df['positive_percent_change_1w'] = df['percent_change_1w'] > 0
df = df.set_index('coin_name')
# %%
# Set the font to support Japanese
plt.rcParams['font.family'] = 'Meiryo' # Meiryo, Yu Gothic
plt.style.use('fivethirtyeight') # 'fivethirtyeight' 'ggplot'
plt.figure(figsize=(10,12)) # 10, 12
if percent_timeframe == '1m':
df.sort_values('percent_change_1m', ascending=True, inplace=True)
df['percent_change_1m'].plot(kind='barh', color=df.positive_percent_change_1m.map({True: 'g', False: 'r'}))
elif percent_timeframe == '1h':
df.sort_values('percent_change_1h', ascending=True, inplace=True)
df['percent_change_1h'].plot(kind='barh', color=df.positive_percent_change_1h.map({True: 'g', False: 'r'}))
elif percent_timeframe == '1d':
df.sort_values('percent_change_1d', ascending=True, inplace=True)
df['percent_change_1d'].plot(kind='barh', color=df.positive_percent_change_1d.map({True: 'g', False: 'r'}))
else:
df.sort_values('percent_change_1w', ascending=True, inplace=True)
df['percent_change_1w'].plot(kind='barh', color=df.positive_percent_change_1w.map({True: 'g', False: 'r'}))
plt.legend()
plt.grid(False)
plt.ylabel('Crypto Symbols')
plt.title(f'Binance Crypto Price Change\n(USDT)')
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()
# %%