bitFlyer用オーダー発注ソフトをPython+Tkinterで作ってみた

ども。@sj_cryptoです。今回は前回BitMEX用に作ったアレを間に合せで移植したものです。

公式ページの残高周りの表記がMEXより親切なので、今回は省いております。それにともない清算ボタンが上に来ているので注意。一応色は変えたし大丈夫なはず…。

説明に関しては前回のnoteを参照して下さい。ほぼ同じです。表示されている価格は現物価格なので注意。

公式ページから発注すると注文の確認画面が出るためワンテンポ遅れてしまいますが、こいつを使えば秒スキャも注文連打もお手の物です。ただしAPIの呼び出し回数制限(200回/分)の範囲内ですが…。

そして、移植版な関係でいらんコードが残ってたりするかもしれないし連番が飛んでたりしますが大目に見てください。

様々なサイト様を参考にしている関係で例によって無料で読めますが、100円に設定しておくのでもしよければコーヒー代下さいw

万が一不具合で損失を出しても補償はできかねますので自己責任にて。

不具合報告、要望等あればTwitterブログまで。

以下、本体です。

import tkinter
from tkinter import *
from tkinter import font
from tkinter import ttk
import ccxt
import time

try:
    bitflyer=ccxt.bitflyer({
        'apiKey':'ゆあえーぴーあいきー',
        'secret':'ゆあえーぴーあいしーくれっと'
    })    
except Exception as e:
    print(e)    

def main():
    
    def button1_clicked():
        lot = 0
        price = 0
        price_entry = entry1.get()
        size_entry = entry2.get()
        if not price_entry:
            order_type='market'
        else:
            price = price_entry
            order_type = 'limit'
            price = float(entry1.get()) 
        if not size_entry:
            pass
        else:
            size = size_entry
            lot = entry2.get()          
        try:
            order = bitflyer.create_order(symbol='FX_BTC_JPY', type=order_type, side='buy', amount=lot, price=price)
            order_id = order.get('id')
            text1.configure(state='normal')
            text1.insert('end', 'success '+order_id+'\n')
            text1.yview(END)
            text1.configure(state='disabled')
        except Exception as e:
            print(e)    
    def button2_clicked():
        lot = 0
        price = 0
        price_entry = entry1.get()
        size_entry = entry2.get()
        if not price_entry:
            order_type='market'
        else:
            price = price_entry
            order_type = 'limit'
            price = float(entry1.get()) 
        if not size_entry:
            pass
        else:
            size = size_entry
            lot = entry2.get()          
        try:
            order = bitflyer.create_order(symbol='FX_BTC_JPY', type=order_type, side='sell', amount=lot, price=price)
            order_id = order.get('id')
            text1.configure(state='normal')
            text1.insert('end', 'success '+order_id+'\n')
            text1.yview(END)
            text1.configure(state='disabled')
        except Exception as e:
            print(e)

    def button4_clicked():
        entry1.delete(0,tkinter.END)
        entry2.delete(0,tkinter.END) 
    def button5_clicked():
        orders = bitflyer.fetch_open_orders(symbol='FX_BTC_JPY')
        for o in orders:
            cancel = bitflyer.cancel_order(id=o['id'], symbol='FX_BTC_JPY')
            text1.configure(state='normal')
            text1.insert('end','canceled\n')
            text1.yview(END)
            text1.configure(state='disabled') 
    def button6_clicked():
        text1.configure(state='normal')
        text1.delete(1.0,tkinter.END)   
        text1.configure(state='disabled') 
    def button7_clicked():
        size=[]
        price=[]
        pos = bitflyer.private_get_getpositions(params={'product_code':'FX_BTC_JPY'})
        if not pos:
            pass
        for p in pos:
            size.append(p['size'])
            price.append(p['price'])
            side=p['side']    
            average_price = round(sum(price[i]*size[i] for i in range(len(price)))/sum(size))
            sum_size = round(sum(size),2)
            text1.configure(state='normal')
            text1.insert('end', 'Open interest:{}\nAverage price:{}JPY\nSize:{}BTC\nSide:{}\n'.format(len(price),average_price,sum_size,side))
            text1.yview(END)
            text1.configure(state='disabled')
        return average_price,sum_size,side    
        if openpos > 0:
            order = bitflyer.create_order(symbol='FX_BTC_JPY', type='market', side='sell', amount=openpos)
            order_id = order.get('id')
            text1.configure(state='normal')
            text1.insert('end', 'liquidated long '+order_id+'\n')
            text1.yview(END)
            text1.configure(state='disabled')
        elif openpos < 0:
            order = bitflyer.create_order('FX_BTC_JPY', type='market', side='buy', amount=abs(openpos))    
            order_id = order.get('id')
            text1.configure(state='normal')
            text1.insert('end', 'liquidated short '+order_id+'\n')
            text1.yview(END)
            text1.configure(state='disabled')
        else:
            text1.configure(state='normal')
            text1.insert('end', 'no position.\n')
            text1.yview(END)
            text1.configure(state='disabled')                
            
    root = tkinter.Tk()
    root.title('bitFlyer Oeder Client v1.0')
    root.geometry('320x280')
    fetch_balance = bitflyer.fetch_balance()
    balance = fetch_balance.get('info')[0].get('amount')
    balance_free = fetch_balance.get('info')[0].get('available')
    balance_used = balance-balance_free
    balance_use_rate = balance_used/balance*100

    text1 = tkinter.Text(height=5,width=5) 
    text1.grid(row=9,column=0,ipadx=100,sticky='nsew',columnspan=2) 
    text1.configure(state='disabled')
    scrollbar = ttk.Scrollbar(
        root, 
        orient=VERTICAL, 
        command=text1.yview)
    text1['yscrollcommand'] = scrollbar.set
    scrollbar.grid(row=9,column=2,sticky='ns')

    ticker = bitflyer.fetch_ticker(symbol='BTC/JPY')
    ltp = ticker.get('last')
    size=[]
    price=[]
    pos = bitflyer.private_get_getpositions(params={'product_code':'FX_BTC_JPY'})
    if not pos:
        pass
    for p in pos:
        size.append(p['size'])
        price.append(p['price'])
        side=p['side']    
        average_price = round(sum(price[i]*size[i] for i in range(len(price)))/sum(size))
        sum_size = round(sum(size),2)
        text1.configure(state='normal')
        text1.insert('end', 'Open interest:{}\nAverage price:{}JPY\nSize:{}BTC\nSide:{}'.format(len(price),average_price,sum_size,side))
        text1.yview(END)
        text1.configure(state='disabled')

    label4 = tkinter.Label(root, text='Price') 
    label4.grid(row=4, column=0)
    label5 = tkinter.Label(root, text='Size') 
    label5.grid(row=4, column=1)    
    label7 = tkinter.Label(root, text='BTC/JPY:'+str(ltp))   
    label7.grid(row=2, column=0, sticky='w') 
    entry1 = tkinter.Entry(root,font=('',12),justify='center',width=15)
    entry1.grid(row=5, column=0)
    entry2 = tkinter.Entry(root,font=('',12),justify='center',width=15)
    entry2.grid(row=5, column=1)   
    button1 = tkinter.Button(text='LONG',command=button1_clicked,width=20,height=5,fg='green')   
    button1.grid(row=6,column=0,sticky='ns') 
    button2 = tkinter.Button(text='SHORT',command=button2_clicked,width=20,height=5,fg='red')   
    button2.grid(row=6,column=1,sticky='ns') 
    button4 = tkinter.Button(text='Clear all text',command=button4_clicked) 
    button4.grid(row=7,column=1,sticky='ns') 
    button5 = tkinter.Button(text='Cancel all orders',command=button5_clicked) 
    button5.grid(row=8,column=0,sticky='ns')
    button6 = tkinter.Button(text='Clear logs',command=button6_clicked) 
    button6.grid(row=8,column=1,sticky='ns')
    button7 = tkinter.Button(text='Liquidation', command=button7_clicked, fg='violet')
    button7.grid(row=7, column=0)

    root.mainloop() 

if __name__ == '__main__':
    main()    


ここから先は

30字

¥ 100

期間限定!PayPayで支払うと抽選でお得

この記事が気に入ったらチップで応援してみませんか?