![見出し画像](https://assets.st-note.com/production/uploads/images/123912191/rectangle_large_type_2_fb77250b862b63f0fd9d7662fd816065.png?width=1200)
暗号資産・販売所ザイフ(zaif)zaifAPIでzaifとbitflyerのBITJPY価格をグラフ表示する。pythonプログラムサンプル
仮想通貨・取引所のzaifとbitflyerのBITコイン価格推移をグラフ表示します。python でのプログラムサンプルです。
BOT開発の参考にして下さい。
動作確認済
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_flyer = tkinter.Message(root, text="銭の生る樹", font=("Times New ZRoman", 16), width=500)
me_flyer.place(x=20, y=100)
me_zaif = tkinter.Message(root, text="銭の生る樹", font=("Times New ZRoman", 16), width=550)
me_zaif.place(x=20, y=130)
flag = "off"
histry1s_flyer = []
histry1s_zaif = []
histry10s_flyer = []
histry10s_zaif = []
market = "bitflyer"
def func1():
global flag
global loop
global ca
global count
global loop
global histry1s_flyer
global histry1s_zaif
global histry10s_flyer
global histry10s_zaif
while True:
time.sleep(0.001)
count = count + 1
# 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_flyer = int(price["mid_price"])
# zaif
res = zaif_public.last_price('btc_jpy')
graph_zaif = int(res["last_price"])
me_flyer["text"] = "flyer = {}".format(count),str(graph_flyer)
me_zaif["text"] = "zaif = {}".format(count),str(graph_zaif)
index = len(histry1s_flyer)
if count < loop:
histry1s_flyer.append(graph_flyer)
histry1s_zaif.append(graph_zaif)
else:
for i in range(1,loop):
histry1s_flyer[i-1] = histry1s_flyer[i]
histry1s_zaif[i-1] = histry1s_zaif[i]
histry1s_flyer[loop - 1] = graph_flyer
histry1s_zaif[loop - 1] = graph_zaif
if count >= 10:
total = 0
for s in range(len(histry1s_flyer) - 10,len(histry1s_flyer)):
total = total + histry1s_flyer[s]
heikin10s = total // 10
if len(histry10s_flyer) < loop:
histry10s_flyer.append(heikin10s)
else:
for i in range(1,loop):
histry10s_flyer[i-1] = histry10s_flyer[i]
histry10s_flyer[loop - 1] = heikin10s
total = 0
for s in range(len(histry1s_zaif) - 10,len(histry1s_zaif)):
total = total + histry1s_zaif[s]
heikin10s = total // 10
if len(histry10s_zaif) < loop:
histry10s_zaif.append(heikin10s)
else:
for i in range(1,loop):
histry10s_zaif[i-1] = histry10s_zaif[i]
histry10s_zaif[loop - 1] = heikin10s
if count < loop + 10:
min_price = int(min(histry1s_flyer) / 100)
max_price = int(max(histry1s_flyer) / 100)
if count > loop + 10:
max_price = int(max(max(histry10s_flyer, histry10s_zaif, key=max))) / 100 + 10
min_price = int(min(min(histry10s_flyer, histry10s_zaif, key=min))) / 100 - 10
#print("max_price = {} min_price = {} ".format(max_price,min_price))
hi_range = (max_price + 10) - (min_price - 10)
bai = hei / hi_range
ca.delete("all")
index = len(histry10s_flyer) # and len(histry10s_zaif)
#print("index = {}".format(index))
bar_x = 10
j = -1
for i in range(loop):
if len(histry10s_flyer) > 0:
if i > loop - index:
j = j + 1
hi = max_price - int(histry10s_flyer[j] /100)
bar_y = hi * bai
ca.create_line(bar_x, bar_y, bar_x + 1, bar_y + 1, fill="black",width=2)
hi = max_price - int(histry10s_zaif[j] /100)
bar_y = hi * bai
ca.create_line(bar_x, bar_y, bar_x + 1, bar_y + 1, fill="red",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()