A simple Bitcoin price tracker with Python

This script tracks of Bitcoin's current USD price and displays it along with the price's "trend" since the script started running. The main part of the idea was borrowed heavily from this Stack Exchange article. It takes advantage of the coindesk API for getting current price information. Sure, it's not a fancy line graph, but it's got ASCII-art up/down arrows! Here's an example screenshot: And, the code: ```python import random import json import urllib.request import time original concept/code borrowed from user polka at https://codereview.stackexchange.com/questions/116272/get-bitcoin-price-and-advice delaySeconds = 60 currencyType = "USD" def bitcoin(currency, amt = 1): url = "https://api.coindesk.com/v1/bpi/currentprice.json" response = urllib.request.urlopen(url) thepage = response.read() data = json.loads(thepage) conversion = data['bpi']['USD']['rate_float'] return conversion oldval = bitcoin(currencyType,1) initialprice = oldval print("Initial value: ${0:,.2f}".format(oldval)) # print an initial value (starting point) while True: time.sleep(delaySeconds) newval = bitcoin(currencyType,1) diff = newval - oldval if diff < 0: diff = diff * -1 # make the diff a positive number if newval > oldval: trend = "\u2191" # are we trending up or down else: trend = "\u2193" oldval = newval print("${0:,.2f} ({1} ${2:,.2f})|({4} ${3:,.2f})".format( newval, trend, diff, newval - initialprice, "\u2191" if newval>initialprice else "\u2193")) ```

Add new comment

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.