Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

python - Running my Bitcoin price checker continuously

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}

{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.

question from:https://stackoverflow.com/questions/66051275/running-my-bitcoin-price-checker-continuously

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...