DEVGRU

プログラミングと競馬予想について書きます

Binance の手持ちの草コインの価格上昇・下落がぱっと分かるようにした

昨日の続き。

team-6.hatenablog.jp

昨晩は全般的にかなり下落していたようで、反発したコインを売りたくなった。

いちいち購入履歴と付きあわせてどのくらい上がったかを計算するのはアレなので、 昨日のスクリプトを改良して以下のように表示されるようにした。

f:id:katoken-0215:20180512094316p:plain

ソースコードはこんな感じ(Python3)。

$ pip install python-binance
import requests

from binance.client import Client

API_KEY = 'Your API key'
API_SECRET = 'Your API secret'

def get_price(client, coin):
    recent_trade = client.get_recent_trades(symbol=f'{coin}BTC', limit=1)
    return float(recent_trade[0]['price'])

def get_current_position(client, coin):
    orders = client.get_all_orders(symbol=f'{coin}BTC')
    position = 0
    amount = 0
    for order in orders:
        if order['side'] == 'BUY' and float(order['executedQty']) > 0:
            price = float(order['price'])
            executed_qty = float(order['executedQty'])
            position = (amount * position + price * executed_qty) / (amount + executed_qty)
            amount += executed_qty
        elif order['side'] == 'SELL':
            amount -= executed_qty
    return position

if __name__ == '__main__':
    client = Client(API_KEY, API_SECRET)
    account = client.get_account()
    btc_total = 0
    for balance in account['balances']:
        amount = float(balance['free']) + float(balance['locked'])
        coin = balance['asset']
        if amount > 0 and coin != 'BTC':
            price = get_price(client, coin)
            btc_amount = price * amount
            if btc_amount > 0.0001:
                position = get_current_position(client, coin)
                ratio = ((price - position) / position) * 100
            else:
                position = 0
                ratio = 0
            print(f'{coin}:\t{btc_amount:.10f}\t{amount:8.2f}\t{position:.10f}\t({price:.10f} {ratio:5.2f}%)')
            btc_total += btc_amount
        elif coin == 'BTC':
            print(f'{coin}:\t{amount}')
            btc_total += amount
    print('Total:', btc_total)
    response = requests.get('https://api.cryptowat.ch/markets/bitflyer/btcjpy/price')
    btcjpy_price = response.json()['result']['price']
    print('Total(JPY):', btc_total * btcjpy_price)

仮想通貨の総まとめ

仮想通貨の総まとめ

いま仕込んでおくべき10倍通貨教えます!

いま仕込んでおくべき10倍通貨教えます!

アルトコイン投資術: 仮想通貨で億り人になる

アルトコイン投資術: 仮想通貨で億り人になる