Search code examples
pythonbitcoin

How to store a value and change it when it is updated in python?


So I'm trying to create a bitcoin price tracker and with it, it will tell you whether the price went up or down. For example, when you start it, it shows the price and an increase of $0.00, then when it increases or decreases it shows that. The problem is after it tests again, it reverts back to 0, not staying at the increase or decrease amount.

Heres what I have tried and everything works but when it changes, it only shows it until it tests for a change, after one second.

###############
import requests
import time
import os
#############

#######
bct = 0.0
bctChange = 0
errorLevel = 0
################
os.system('cls')
################

#print('The current price of Bitcoin is $10,000.00')
#print('Connected: True')
#print('Increase of $2.50') #use abs() for absolute value
#print('ErrorLevel: 0')
#print('ErrorLevel is the amount of request errors\nthere have been to the bitcoin api')

########################
def place_value(number): 
    return ("{:,}".format(number)) 
#####################################################################
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
bctPriceStart = place_value(round(r.json()['bpi']['USD']['rate_float'], 2))
###########################################################################

while True:
  try:
    #############
    bctLast = bct
    #####################################################################
    r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
    bct = round(r.json()['bpi']['USD']['rate_float'], 2)
    ####################################################
    if (bctChange != bctChange):
      bctChange = bctLast - bct
    ###########################

    ################
    os.system('cls')
    print('The current price of Bitcoin is $' + place_value(bct))
    #############################################################

    #################
    if (bctLast > 0):
      print('Increase of $' + place_value(abs(round(bctChange, 2))))
      time.sleep(1)
    ###############

    ###################
    elif (bctLast < 0):
      print('Decrease of $' + place_value(abs(round(bctChange, 2))))
      time.sleep(1)
    ###############


  except requests.ConnectionError or requests.ConnectTimeout or requests.HTTPError or requests.NullHandler or requests.ReadTimeout or requests.RequestException or requests.RequestsDependencyWarning or requests.Timeout or requests.TooManyRedirects:
    #Do error function
    os.system('cls')
    print('There was an error...')

Solution

  • If I understand what you want to do, replacing your script with this should work:

    import requests
    import time
    
    bct = 0.0
    bctChange = 0
    
    def place_value(number): 
        return ("{:,}".format(number)) 
    
    r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
    bct = round(r.json()['bpi']['USD']['rate_float'], 2)
    print('The starting price of Bitcoin is $' + place_value(bct))
    
    while True:
        try:
            bctLast = bct
            r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
            bct = round(r.json()['bpi']['USD']['rate_float'], 2)
            bctChange = bctLast - bct
            if bctChange > 0:
                print('The current price of Bitcoin is $' + place_value(bct))
                print('Increase of $' + place_value(abs(round(bctChange, 2))))
            elif bctChange < 0:
                print('The current price of Bitcoin is $' + place_value(bct))
                print('Decrease of $' + place_value(abs(round(bctChange, 2))))
            time.sleep(1)
    
        except requests.ConnectionError or requests.ConnectTimeout or requests.HTTPError or requests.NullHandler or requests.ReadTimeout or requests.RequestException or requests.RequestsDependencyWarning or requests.Timeout or requests.TooManyRedirects:
          print('There was an error...')
    

    The problem is your if...elif statements were based on btcLast not btcChange, and you don't need the if statement around bctChange = bctLast - bct. You were also calling os.system('cls') which was clearing your printed output rather than printing it persistently.