0

Hi I've made a bitcoin price checker that sends me alerts to tell me Bitcoins current price. It also lets me know when Bitcoin has moved into a certain price range. Its working good so far, but I'm having trouble figuring out how to keep my while loop ('run_loop') running continuously. I want the code to run on a timed interval. I want it to loop and tell me the price after a set amount of time without me re-running the code manually.

import smtplib
import os
import requests
import json
import time

email_user = os.environ.get('EMAIL_USER')  # set your own environment variables
email_password = os.environ.get('APP_PASS')
phone_number = os.environ.get('USER_PHONE')

target_price = 20000   # set your target prices in USD
target_price_2 = 30000

current_price = 0 # declare current price

def send_email(current_price):

    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()

        smtp.login(email_user, email_password)

        subject = 'Bitcoin Alert: '
        body = 'Bitcoins price is ' + str(current_price) + "!"

        if current_price > target_price:
            body = "Bitcoin is higher than your target price at " + str(current_price) + "."
        elif target_price <= current_price <= target_price_2:
            body = "Bitcoin is in your target price range at " + str(current_price) + "!"
        elif current_price < target_price:
            body = "Bitcoin is below your target price range at " + str(current_price) + "!"

        msg = f'Subject: {subject}\n\n{body}'

        smtp.sendmail(email_user, phone_number, msg)

def check_price(current_price):

    run_loop = True
    print("Enter 'q' to exit the loop")

    while run_loop:

        response = requests.get("https://api.coinbase.com/v2/prices/BTC-USD/spot")
        data = response.json()

        currency = data["data"]["base"]
        current_price = data["data"]["amount"]
        current_price = int(float(current_price))  # converts string float to int

        if current_price:
            send_email(current_price)

        print(f'Bitcoins current price is {current_price}')

        stop_loop = input()
        if stop_loop == 'q':
            run_loop = False
        
        time.sleep(10)


check_price(current_price) #Make call to current_price which ATM is 0


I tried removing this if statement:

        if current_price:
            send_email(current_price)

It didn't work. I would appreciate if someone could help me solve this. Thank you.

Edit: Still looking for some input on this one.

Jamesyt60
  • 57
  • 6
  • What do you think calling `input()` does? – Scott Hunter Feb 04 '21 at 18:03
  • Does this answer your question? [How to kill a while loop with a keystroke?](https://stackoverflow.com/questions/13180941/how-to-kill-a-while-loop-with-a-keystroke) – G. Anderson Feb 04 '21 at 18:06
  • Do you mean the input() I use to exit my loop? I'm not sure what you mean exactly sorry. I should note I'm not trying to break out of the loop. I want to keep it running and keep the price check alerts coming. Im trying figure out how to keep the loop continuous. – Jamesyt60 Feb 04 '21 at 18:07

0 Answers0