昨日の続き。
昨晩は全般的にかなり下落していたようで、反発したコインを売りたくなった。
いちいち購入履歴と付きあわせてどのくらい上がったかを計算するのはアレなので、 昨日のスクリプトを改良して以下のように表示されるようにした。
ソースコードはこんな感じ(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)
- 作者: 暗号通貨情報局
- 発売日: 2018/01/16
- メディア: Kindle版
- この商品を含むブログを見る
- 作者: 佐藤正和
- 発売日: 2018/03/10
- メディア: Kindle版
- この商品を含むブログを見る
- 作者: かどやたつひこ
- 出版社/メーカー: CRAFTec Art
- 発売日: 2018/01/15
- メディア: Kindle版
- この商品を含むブログを見る
- 作者: フィアット出版
- 発売日: 2018/01/29
- メディア: Kindle版
- この商品を含むブログを見る