![見出し画像](https://assets.st-note.com/production/uploads/images/123675810/rectangle_large_type_2_4c00771f189f5d7cfc1c042019684220.png?width=1200)
zaif APIで仮想通貨BTCの価格推移をグラフ表示する。サンプルソース
グラフ表示のサンプル・pythonソース・コードです。
動作確認済です。
以下をそのまま実行して下さい。
pip install zaifapi コマンドプロンプトにて投入する
from zaifapi import ZaifPublicApi
zaif_public = ZaifPublicApi()
import time
import threading
import tkinter
import requests
import json
loop = 1260
hei = 680
count = -1
root = tkinter.Tk()
root.geometry("1300x715")
ca = tkinter.Canvas(width=loop, height=hei, bg="white")
ca.pack()
me = tkinter.Message(root, text="銭の生る樹", font=("Times New ZRoman", 16), width=500)
me.place(x=20, y=100)
me2 = tkinter.Message(root, text="countがloopに成るとコイン価格推移グラフが表示されます", font=("Times New ZRoman", 16), width=550)
me2.place(x=20, y=130)
flag = "off"
histry1s = []
market = "bitflyer"
def func1():
global flag
global loop
global ca
global count
global loop
global histry1s
while True:
time.sleep(0.001)
count = count + 1
if market == "bitflyer":
# bitflyer
#res = requests.get('https://api.zaif.jp/api/1/last_price/btc_jpy')
res = requests.get('https://api.bitflyer.jp/v1/getboard?product_code=BTC_JPY')
if res.status_code != 200:
raise Exception('return status code is {}'.format(res.status_code))
price = json.loads(res.text)
graph_y = int(price["mid_price"])
else:
# zaif
res = zaif_public.last_price('btc_jpy')
graph_y = int(res["last_price"])
me["text"] = "count = {}".format(count),str(graph_y)
if count < loop:
histry1s.append(graph_y)
else:
for i in range(1,loop):
histry1s[i-1] = histry1s[i]
histry1s[loop - 1] = graph_y
max_price = int(max(histry1s)) / 100 + 10
min_price = int(min(histry1s)) / 100 - 10
#print("max_price = {}".format(max_price))
hi_range = max_price - min_price
bai = hei / hi_range
ca.delete("all")
index = len(histry1s)
print("index = {}".format(index))
bar_x = 10
j = -1
for i in range(loop):
if i > loop - index:
j = j + 1
hi = max_price - int(histry1s[j] /100)
bar_y = hi * bai
ca.create_line(bar_x, bar_y, bar_x + 1, bar_y + 1, fill="black",width=2)
bar_x = bar_x + 1
if flag == "on":
flag = "off"
break
print("loop end")
def btn_on2():
global flag
global bu2
bu2["text"] = "ループ終了"
flag = "on"
bu2 = tkinter.Button(root, text="ループ終了", font=("Times New ZRoman", 16), command=btn_on2)
bu2.place(x=20, y=50)
thread_1 = threading.Thread(target=func1)
thread_1.start()
root.mainloop()