![見出し画像](https://assets.st-note.com/production/uploads/images/124217933/rectangle_large_type_2_20644f10d8350a3d027b454c1097056b.png?width=1200)
暗号資産・販売所ザイフ(zaif) 自動売買システム開発サンプル ドラゴン・シグナル
BOT開発の参考資料です。
複数の移動平均を基に売買のタイミングを検出します。
天に昇りきったドラゴンが頭下げて下り始める様を売りのタイミングとし、下がりきったドラゴンが頭上げて登り始める様を買いタイミングとする方法です。
from zaifapi import ZaifPublicApi
zaif_public = ZaifPublicApi()
import time
import threading
import tkinter
import math
import requests
import json
loop = 1260
hei = 680
count = -1
root = tkinter.Tk()
root.geometry("1300x715") #note = 1210x710" desk = 1600x830
ca = tkinter.Canvas(width=loop, height=hei, bg="white") #note = height=680 desk = height=800
ca.pack()
me = tkinter.Message(root, text="銭の生る樹", font=("Times New ZRoman", 16), width=500)
me.place(x=20, y=100)
flag = "off"
histry1s = []
histry10s = []
histry1m = []
histry2m = []
histry4m = []
histry8m = []
histry16m = []
histry32m = []
histry64m = []
stance = [None] * loop
market = "bitflyer" # "zaif" "bitflyer"
entry = "ok"
def func1():
global flag
global ca
global count
global loop
global histry1s
global histry10s
global histry1m
global histry2m
global histry4m
global histry8m
global histry16m
global histry32m
global histry64m
global stance
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)
index = len(histry1s)
if index < loop:
histry1s.append(graph_y)
stance.append(0)
stance[index] = 0
else:
for i in range(1,loop):
histry1s[i-1] = histry1s[i]
stance[i-1] = stance[i]
histry1s[loop - 1] = graph_y
stance[loop - 1] = 0
if count >= 10:
total = 0
for s in range(len(histry1s) - 10,len(histry1s)):
total = total + histry1s[s]
heikin10s = total // 10
if len(histry10s) < loop:
histry10s.append(heikin10s)
else:
for i in range(1,loop):
histry10s[i-1] = histry10s[i]
histry10s[loop - 1] = heikin10s
if count >= 60:
total = 0
for s in range(len(histry1s) - 60,len(histry1s)):
total = total + histry1s[s]
heikin1m = total // 60
if len(histry1m) < loop:
histry1m.append(heikin1m)
else:
for i in range(1,loop):
histry1m[i-1] = histry1m[i]
histry1m[loop - 1] = heikin1m
if count >= 120:
total = 0
for s in range(len(histry1s) - 120,len(histry1s)):
total = total + histry1s[s]
heikin2m = total // 120
if len(histry2m) < loop:
histry2m.append(heikin2m)
else:
for i in range(1,loop):
histry2m[i-1] = histry2m[i]
histry2m[loop - 1] = heikin2m
if count >= 240:
total = 0
for s in range(len(histry1s) - 240,len(histry1s)):
total = total + histry1s[s]
heikin4m = total // 240
if len(histry4m) < loop:
histry4m.append(heikin4m)
else:
for i in range(1,loop):
histry4m[i-1] = histry4m[i]
histry4m[loop - 1] = heikin4m
if count >= 480:
total = 0
for s in range(len(histry1s) - 480,len(histry1s)):
total = total + histry1s[s]
heikin8m = total // 480
if len(histry8m) < loop:
histry8m.append(heikin8m)
else:
for i in range(1,loop):
histry8m[i-1] = histry8m[i]
histry8m[loop - 1] = heikin8m
if count >= 960:
total = 0
for s in range(len(histry1s) - 960,len(histry1s)):
total = total + histry1s[s]
heikin16m = total // 960
if len(histry16m) < loop:
histry16m.append(heikin16m)
else:
for i in range(1,loop):
histry16m[i-1] = histry16m[i]
histry16m[loop - 1] = heikin16m
if count >= 1920:
total = 0
for s in range(len(histry1s) - 1920,len(histry1s)):
total = total + histry1s[s]
heikin32m = total // 1920
if len(histry32m) < loop:
histry32m.append(heikin32m)
else:
for i in range(1,loop):
histry32m[i-1] = histry32m[i]
histry32m[loop - 1] = heikin32m
if count >= 3840:
total = 0
for s in range(len(histry1s) - 3840,len(histry1s)):
total = total + histry1s[s]
heikin64m = total // 3840
if len(histry64m) < loop:
histry64m.append(heikin64m)
else:
for i in range(1,loop):
histry64m[i-1] = histry64m[i]
histry64m[loop - 1] = heikin64m
#########
if count >= 3840:
price1s = histry1s[len(histry1s) - 1]
price10s = histry10s[len(histry10s) - 1]
price1m = histry1m[len(histry1m) - 1]
price2m = histry2m[len(histry2m) - 1]
price4m = histry4m[len(histry4m) - 1]
price8m = histry8m[len(histry8m) - 1]
price16m = histry16m[len(histry16m) - 1]
price32m = histry32m[len(histry32m) - 1]
price64m = histry64m[len(histry64m) - 1]
if histry1m[len(histry1m) - 2] < histry1m[len(histry1m) - 1]:
hi_low1 = 1
if histry1m[len(histry1m) - 2] == histry1m[len(histry1m) - 1]:
hi_low1 = 0
if histry1m[len(histry1m) - 2] > histry1m[len(histry1m) - 1]:
hi_low1 = -1
if histry2m[len(histry2m) - 2] < histry2m[len(histry2m) - 1]:
hi_low2 = 1
if histry2m[len(histry2m) - 2] == histry2m[len(histry2m) - 1]:
hi_low2 = 0
if histry2m[len(histry2m) - 2] > histry2m[len(histry2m) - 1]:
hi_low2 = -1
if histry4m[len(histry4m) - 2] < histry4m[len(histry4m) - 1]:
hi_low4 = 1
if histry4m[len(histry4m) - 2] == histry4m[len(histry4m) - 1]:
hi_low4 = 0
if histry4m[len(histry4m) - 2] > histry4m[len(histry4m) - 1]:
hi_low4 = -1
if histry8m[len(histry8m) - 2] < histry8m[len(histry8m) - 1]:
hi_low8 = 1
if histry8m[len(histry8m) - 2] == histry8m[len(histry8m) - 1]:
hi_low8 = 0
if histry8m[len(histry8m) - 2] > histry8m[len(histry8m) - 1]:
hi_low8 = -1
if histry16m[len(histry16m) - 2] < histry16m[len(histry16m) - 1]:
hi_low16 = 1
if histry16m[len(histry16m) - 2] == histry16m[len(histry16m) - 1]:
hi_low16 = 0
if histry16m[len(histry16m) - 2] > histry16m[len(histry16m) - 1]:
hi_low16 = -1
if histry32m[len(histry32m) - 2] < histry32m[len(histry32m) - 1]:
hi_low32 = 1
if histry32m[len(histry32m) - 2] == histry32m[len(histry32m) - 1]:
hi_low32 = 0
if histry32m[len(histry32m) - 2] > histry32m[len(histry32m) - 1]:
hi_low32 = -1
if histry64m[len(histry64m) - 2] < histry64m[len(histry64m) - 1]:
hi_low64 = 1
if histry64m[len(histry64m) - 2] == histry64m[len(histry64m) - 1]:
hi_low64 = 0
if histry64m[len(histry64m) - 2] > histry64m[len(histry64m) - 1]:
hi_low64 = -1
#print("histry1s={}".format(str(price1s)))
#print("histry1m={}".format(str(price1m)))
#print("histry2m={}".format(str(price2m)))
#print("histry4m={}".format(str(price4m)))
#print("histry8m={}".format(str(price8m)))
#print("histry16m={}".format(str(price16m)))
#print("histry32m={}".format(str(price32m)))
#print("histry64m={}".format(str(price64m)))
stance[len(stance) - 1] = 0
if count > len(stance) + stack:
#avr1m["text"] = "1m: {}".format(str(price1m)) #1m
#avr2m["text"] = "2m: {}".format(str(price2m)) #2m
#avr4m["text"] = "4m: {}".format(str(price4m)) #4m
#avr8m["text"] = "8m: {}".format(str(price8m)) #8m
#avr16m["text"] = "16m: {}".format(str(price16m)) #16m
#avr32m["text"] = "32m: {}".format(str(price32m)) #32m
#avr64m["text"] = "64m: {}".format(str(price64m)) #64m
if (price64m > price32m and hi_low64 == -1 and hi_low32 == -1) :
#print("64 > 32",end=" ")
if (price32m > price16m and hi_low32 == -1 and hi_low16 == -1) :
#print("32 > 16",end=" ")
if (price16m > price8m and hi_low16 == -1 and hi_low8 == -1) :
#print("16 > 8",end=" ")
if (price8m > price4m and hi_low8 == -1 and hi_low4 == -1) :
#print("8 > 4",end=" ")
if (price4m > price2m and hi_low4 == -1 and hi_low2 == -1) :
#print("4 > 2",end=" ")
if (price2m < price1m and hi_low2 == -1 and hi_low1 == 1) :
#print("2 < 1",end=" ")
if price1m < price10s:
print("2 < 1 買え{}".format(price10s))
stance[len(stance) - 1] = 1
action["text"] = "買え"
else: #price1m >= price1s
print("2 < 1 まだ下がる")
stance[len(stance) - 1] = 0
action["text"] = "下降中"
else: #price1m == price1s
print("2 > 1 下降中")
stance[len(stance) - 1] = 0
action["text"] = "下降中"
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
elif (price64m < price32m and hi_low64 == 1 and hi_low32 == 1) :
#print("64 < 32",end=" ")
if (price32m < price16m and hi_low32 == 1 and hi_low16 == 1) :
#print("32 < 16",end=" ")
if (price16m < price8m and hi_low16 == 1 and hi_low8 == 1) :
#print("16 < 8",end=" ")
if (price8m < price4m and hi_low8 == 1 and hi_low4 == 1) :
#print("8 < 4",end=" ")
if (price4m < price2m and hi_low4 == 1 and hi_low2 == 1) :
#print("4 < 2",end=" ")
if (price2m > price1m and hi_low2 == 1 and hi_low1 == -1) :
#print("2 > 1",end=" ")
if price1m > price10s:
print("2 > 1 売れ{}".format(price10s))
stance[len(stance) - 1] = -1
action["text"] = "売れ"
else: #price1m <= price1s
print("2 > 1 まだ上がる")
stance[len(stance) - 1] = 0
action["text"] = "上昇中"
else: #price1m == price1s
print("2 < 1 上昇中")
stance[len(stance) - 1] = 0
action["text"] = "上昇中"
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
else:
#print("fin")
stance[len(stance) - 1] = 0
action["text"] = ""
else:
#print("64 == 32")
stance[len(stance) - 1] = 0
action["text"] = ""
#print("stance = {}".format(stance[loop - 1]))
#########
if count < loop + 10:
min_price = int(min(histry1s) / 100)
max_price = int(max(histry1s) / 100)
if count > loop + 10:
min_price = int(min(min(histry1s, histry10s, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, key=max)) / 100)
if count > loop + 60:
min_price = int(min(min(histry1s, histry10s, histry1m, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, histry1m, key=max)) / 100)
if count > loop + 120:
min_price = int(min(min(histry1s, histry10s, histry1m, histry2m, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, histry1m, histry2m, key=max)) / 100)
if count > loop + 240:
min_price = int(min(min(histry1s, histry10s, histry1m, histry2m, histry4m, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, histry1m, histry2m, histry4m, key=max)) / 100)
if count > loop + 480:
min_price = int(min(min(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, key=max)) / 100)
if count > loop + 960:
min_price = int(min(min(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, histry16m, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, histry16m, key=max)) / 100)
if count > loop + 1920:
min_price = int(min(min(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, histry16m, histry32m, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, histry16m, histry32m, key=max)) / 100)
if count > loop + 3840:
min_price = int(min(min(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, histry16m, histry32m, histry64m, key=min)) / 100)
max_price = int(max(max(histry1s, histry10s, histry1m, histry2m, histry4m, histry8m, histry16m, histry32m, histry64m, key=max)) / 100)
min_price = min_price - 1
max_price = max_price + 1
hi_range = max_price - min_price
bai = hei / hi_range
ca.delete("all")
base_y = hei - 20
line10s_x = 800
line1m_x = 700
line2m_x = 600
line4m_x = 500
line8m_x = 400
line16m_x = 300
line32m_x = 200
line64m_x = 100
if count > 10:
hi_10s = max_price - int(histry10s[len(histry10s) - 1] /100)
barB_y = hi_10s * bai
ca.create_oval(line10s_x, barB_y,line10s_x + 15, barB_y + 15, fill="black",width=1)
if count > 60:
hi_1m = max_price - int(histry1m[len(histry1m) - 1] /100)
barC_y = hi_1m * bai
ca.create_oval(line1m_x, barC_y, line1m_x + 15, barC_y + 15, fill="coral",width=1)
if count > 120:
hi_2m = max_price - int(histry2m[len(histry2m) - 1] /100)
barD_y = hi_2m * bai
ca.create_oval(line2m_x, barD_y, line2m_x + 15, barD_y + 15, fill="deeppink",width=1)
if count > 240:
hi_4m = max_price - int(histry4m[len(histry4m) - 1] /100)
barM_y = hi_4m * bai
ca.create_oval(line4m_x, barM_y, line4m_x + 15, barM_y + 15, fill="mediumblue",width=1)
if count > 480:
hi_8m = max_price - int(histry8m[len(histry8m) - 1] /100)
barL_y = hi_8m * bai
ca.create_oval(line8m_x, barL_y, line8m_x + 15, barL_y + 15, fill="lime",width=1)
if count > 960:
hi_16m = max_price - int(histry16m[len(histry16m) - 1] /100)
barD_y = hi_16m * bai
ca.create_oval(line16m_x, barD_y, line16m_x + 15, barD_y + 15, fill="deeppink",width=1)
if count > 1920:
hi_32m = max_price - int(histry32m[len(histry32m) - 1] /100)
barM_y = hi_32m * bai
ca.create_oval(line32m_x, barM_y, line32m_x + 15, barM_y + 15, fill="mediumblue",width=1)
if count > 3840:
hi_64m = max_price - int(histry64m[len(histry64m) - 1] /100)
barL_y = hi_64m * bai
ca.create_oval(line64m_x, barL_y, line64m_x + 15, barL_y + 15, fill="lime",width=1)
barA_x = 10
j10s = -1
j1m = -1
j2m = -1
j4m = -1
j8m = -1
j16m = -1
j32m = -1
j64m = -1
for i in range(loop):
if len(histry10s) > 0:
if i > loop - len(histry10s):
j10s = j10s + 1
hi_10s = max_price - int(histry10s[j10s] /100)
barB_y = hi_10s * bai
ca.create_line(barA_x, barB_y, barA_x + 1, barB_y + 1, fill="black",width=2)
if len(histry1m) > 0:
if i > loop - len(histry1m):
j1m = j1m + 1
hi_1m = max_price - int(histry1m[j1m] /100)
barC_y = hi_1m * bai
ca.create_line(barA_x, barC_y, barA_x + 1, barC_y + 1, fill="coral",width=2)
if len(histry2m) > 0:
if i > loop - len(histry2m):
j2m = j2m + 1
hi_2m = max_price - int(histry2m[j2m] /100)
barD_y = hi_2m * bai
ca.create_line(barA_x, barD_y, barA_x + 1, barD_y + 1, fill="deeppink",width=2)
if len(histry4m) > 0:
if i > loop - len(histry4m):
j4m = j4m + 1
hi_4m = max_price - int(histry4m[j4m] /100)
barM_y = hi_4m * bai
ca.create_line(barA_x, barM_y, barA_x + 1, barM_y + 1, fill="mediumblue",width=2)
if len(histry8m) > 0:
if i > loop - len(histry8m):
j8m = j8m + 1
hi_8m = max_price - int(histry8m[j8m] /100)
barL_y = hi_8m * bai
ca.create_line(barA_x, barL_y, barA_x + 1, barL_y + 1, fill="lime",width=2)
if len(histry16m) > 0:
if i > loop - len(histry16m):
j16m = j16m + 1
hi_16m = max_price - int(histry16m[j16m] /100)
barL_y = hi_16m * bai
ca.create_line(barA_x, barL_y, barA_x + 1, barL_y + 1, fill="lime",width=2)
if len(histry32m) > 0:
if i > loop - len(histry32m):
j32m = j32m + 1
hi_32m = max_price - int(histry32m[j32m] /100)
barL_y = hi_32m * bai
ca.create_line(barA_x, barL_y, barA_x + 1, barL_y + 1, fill="lime",width=2)
if len(histry64m) > 0:
if i > loop - len(histry64m):
j64m = j64m + 1
hi_64m = max_price - int(histry64m[j64m] /100)
barL_y = hi_64m * bai
ca.create_line(barA_x, barL_y, barA_x + 1, barL_y + 1, fill="lime",width=2)
if stance[i] == 1:
ca.create_line(barA_x, 0, barA_x + 1, hei, fill="blue",width=1)
if stance[i] == -1:
ca.create_line(barA_x, 0, barA_x + 1, hei, fill="red", width=1)
barA_x = barA_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)
action = tkinter.Message(root, text="状態", font=("Times New ZRoman", 16), width=150)
action.place(x=150, y=50)
thread_1 = threading.Thread(target=func1)
thread_1.start()
root.mainloop()