python 獲得數字貨幣平臺 BTC合約收盤價數據並用matplotlib製作成簡易圖表

最近公司要用btc合約套利 就練練手把以前學的matplotlib回憶一下 好記性不如爛筆頭 記好筆記很重要啊

#導入平臺 api

import okex.swap_api as swap
list = []
list1 = []
d= []
swapAPI = swap.SwapAPI(api_key, seceret_key, passphrase, True)

#獲得合約半小時K線數據
S = swapAPI.get_kline(granularity=43200,instrument_id='BTC-USD-SWAP',start =None,end=None)
S1 = swapAPI.get_kline(granularity=43200,instrument_id='BTC-USDT-SWAP',start =None,end=None)

#對數據進行處理排序等
for i in range(len(S)):
    d.append(i+1)
    list.append(round(float(S[i][-3])-float(S1[i][-3]),2))
for i in range(1,len(list)+1):
    list1.append(list[-i])
from matplotlib import pyplot as plt

#設置x,y軸數據,x軸時間,y軸點差

x = d
y = list1
#顯示文字不然標題中的中文會顯示成空心四邊形
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure()
graph = fig.add_subplot(111)
#k線樣式
graph.plot(x,y,linewidth=1.0,linestyle='-')
#總圖表標題和x,y軸標題
plt.title(u'btc-usd')
graph.set_ylabel(u"usd-usdt點差")
graph.set_xlabel(u"時間")
#去黑邊框
graph.spines['top'].set_visible(False)
graph.spines['right'].set_visible(False)
#定義x軸數據及樣式
plt.xticks(x[::10],rotation=450)#600左斜 300右斜
plt.show()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章