LoginSignup
0
1

More than 3 years have passed since last update.

Bitcoin価格モニターPythonスクリプト

Last updated at Posted at 2020-12-31

概要

Bitcoinの価格が高騰するに伴い欲の皮が突っ張ってきてついついなんどもサイトの価格チャートをリロードしがち。コマンドラインのターミナルで一定間隔でBitcoin表示させる簡単なスクリプト作った。

コード

USDでの価格はCoindeskからJPYでの価格はCoinCheckから引いてきています。この二つのサイトにしたのは特に理由ありません。各サイトのサービスは時の経過とともに中止になったりすると思うのでその時使えるものに適宜変更すればよいかと思います。

8行目で価格情報を取り行く時間間隔を、11行目で自分の持っているBitcoinの量を指定しています。

loop_btc_price.py
btcuser01@ubuntu002:~/Price$ cat -n loop_btc_price.py 
     1  #!/usr/bin/python3
     2  
     3  import requests
     4  import time
     5  import datetime
     6  
     7  # Interval to check btc price in second
     8  interval=60
     9  
    10  # BTC which you have 
    11  btc_you_have=6.04
    12  
    13  COINCHEK_API_URL = 'https://coincheck.com/api/ticker' 
    14  def get_price_jpy_coincheck():
    15    
    16      response = requests.get(COINCHEK_API_URL)
    17      response_json = response.json()
    18      # print(response_json)
    19      return(response_json["last"])
    20    
    21  
    22  COINDESK_API_URL = 'https://api.coindesk.com/v1/bpi/currentprice.json'
    23  def get_price_usd_coindesk():
    24      response = requests.get(COINDESK_API_URL)
    25      response_json = response.json()
    26      # print(response_json)
    27      return(float(response_json["bpi"]["USD"]["rate"].replace(',','')))
    28    
    29  
    30  while True:
    31      dt_now = datetime.datetime.now()
    32      usd_a_btc=get_price_usd_coindesk()
    33      jpy_a_btc=get_price_jpy_coincheck()
    34      # print(dt_now.strftime("%y/%m/%d %H:%M:%S"),usd_a_btc,jpy_a_btc,jpy_a_btc * btc_you_have)
    35      print("{:s}, {:.2f}, {:d}, {:d}".format(dt_now.strftime("%y/%m/%d %H:%M:%S"),
    36          usd_a_btc,
    37          int(jpy_a_btc),
    38          int(jpy_a_btc * btc_you_have)))
    39      time.sleep(interval)
    40  
btcuser01@ubuntu002:~/Price$ 

実行例

btcuser01@ubuntu002:~/Price$ python3 loop_btc_price.py 
20/12/31 09:34:50, 29186.75, 2977781, 17985797
20/12/31 09:35:50, 29148.89, 2980000, 17999200
20/12/31 09:36:50, 29182.07, 2978434, 17989741
20/12/31 09:37:50, 29168.38, 2980612, 18002896
20/12/31 09:38:50, 29232.75, 2980116, 17999900

実行すると各行に
 時刻、USD価格、JPY価格、所有BitcoinのJPYでの評価額
が表示されます。CTRL-Cで停止するまでずっと表示が続きます。

2020/12/31現在非常に値上がりしています。6.04BTCが約1800万円。来年どうなるかなぁ。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1