From 8be54fc17b9e89297a960dcbee2b7e6a3aafd08b Mon Sep 17 00:00:00 2001 From: Sabina Pun Date: Tue, 22 Aug 2023 22:26:52 +0100 Subject: [PATCH 1/4] created script that returns latest stock prices. --- main.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..5e73253 --- /dev/null +++ b/main.py @@ -0,0 +1,31 @@ +import requests + +api_key = "2f20ea0a2f9b4a27bd2cd6b2464d6d60" + +def get_stock_price(symbol): + + params = { + "symbol": symbol, + "apikey": api_key + } + + response = requests.get("https://api.twelvedata.com/price", params=params) + data = response.json() + + if "price" in data: + return float(data["price"]) + + else: + return None + +if __name__ == "__main__": + stock_symbols = ["AAPL", "GOOGL", "MSFT" , "NKE", "TSLA"] + + for symbol in stock_symbols: + stock_price = get_stock_price(symbol) + + if stock_price is not None: + print(f"The latest price of {symbol} is ${stock_price:.2f}") + + else: + print(f"Couldn't retrieve latest price for {symbol}") \ No newline at end of file From 5e4ff90cc59afd680d791f771829ca8baec8690d Mon Sep 17 00:00:00 2001 From: Sabina Pun Date: Wed, 23 Aug 2023 16:52:04 +0100 Subject: [PATCH 2/4] created function that sends IFTTT notification --- main.py | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 5e73253..01b5e15 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,23 @@ -import requests +import requests +import time -api_key = "2f20ea0a2f9b4a27bd2cd6b2464d6d60" +#Twelve Data API Key +API_KEY = "2f20ea0a2f9b4a27bd2cd6b2464d6d60" + +WEBHOOKS_KEY = "b-gntgVdhCa1F8DgkWXRvT" + + +PRICE_FALL_THRESHOLD = 0.25 + + + +#function that returns latest stock prices def get_stock_price(symbol): params = { "symbol": symbol, - "apikey": api_key + "apikey": API_KEY } response = requests.get("https://api.twelvedata.com/price", params=params) @@ -18,14 +29,35 @@ def get_stock_price(symbol): else: return None +#function that sends IFTTT notification + +def send_notification(stock_symbol, price): + ifttt_url = "https://maker.ifttt.com/trigger/notify/json/with/key/b-gntgVdhCa1F8DgkWXRvT" + + data = { + "value1" : stock_symbol, + "value2" : price + } + + response = requests.post(ifttt_url, json=data) + + if response.status_code == 200: + print(f"Notification has been sent for {stock_symbol}.") + + else: + print(f"Failed to send notification for {stock_symbol}.") + + + + if __name__ == "__main__": stock_symbols = ["AAPL", "GOOGL", "MSFT" , "NKE", "TSLA"] for symbol in stock_symbols: - stock_price = get_stock_price(symbol) + current_stock_price = get_stock_price(symbol) - if stock_price is not None: - print(f"The latest price of {symbol} is ${stock_price:.2f}") + if current_stock_price is not None: + print(f"The latest price of {symbol} is ${current_stock_price:.2f}") else: print(f"Couldn't retrieve latest price for {symbol}") \ No newline at end of file From c83ff91703a8f4754e2fe4eb7d337d98e1e9a8cc Mon Sep 17 00:00:00 2001 From: Sabina Pun Date: Thu, 24 Aug 2023 21:46:56 +0100 Subject: [PATCH 3/4] added IF condition to main function and .env files! --- .gitignore | 1 + main.py | 78 +++++++++++++++++++++++++++--------------------------- 2 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/main.py b/main.py index 01b5e15..055a217 100644 --- a/main.py +++ b/main.py @@ -1,63 +1,63 @@ import requests import time +import os -#Twelve Data API Key -API_KEY = "2f20ea0a2f9b4a27bd2cd6b2464d6d60" +from dotenv import load_dotenv +load_dotenv() -WEBHOOKS_KEY = "b-gntgVdhCa1F8DgkWXRvT" +API_KEY = os.getenv('API_KEY') +WEBHOOKS_KEY = os.getenv('WEBHOOKS_KEY') -PRICE_FALL_THRESHOLD = 0.25 +IFTTT_URL = f"https://maker.ifttt.com/trigger/notify/with/key/{WEBHOOKS_KEY}" -#function that returns latest stock prices -def get_stock_price(symbol): - - params = { - "symbol": symbol, - "apikey": API_KEY - } - response = requests.get("https://api.twelvedata.com/price", params=params) - data = response.json() - if "price" in data: - return float(data["price"]) +PRICE_DROP_THRESHOLD = 0.25 - else: - return None +stock_symbols = ["AAPL", "GOOGL", "MSFT" , "NKE", "TSLA"] -#function that sends IFTTT notification +previous_prices = {} -def send_notification(stock_symbol, price): - ifttt_url = "https://maker.ifttt.com/trigger/notify/json/with/key/b-gntgVdhCa1F8DgkWXRvT" - data = { - "value1" : stock_symbol, - "value2" : price - } - response = requests.post(ifttt_url, json=data) +#function that returns latest stock prices +def get_stock_price(symbol): + url = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={API_KEY}" + response = requests.get(url) + data = response.json() + return data['latestPrice'] + - if response.status_code == 200: - print(f"Notification has been sent for {stock_symbol}.") +#function that sends IFTTT notification +def send_notification(symbol, price): - else: - print(f"Failed to send notification for {stock_symbol}.") - + payload = { + "value1" : symbol, + "value2" : str(price) + } + requests.post(IFTTT_URL, json=payload) + if __name__ == "__main__": - stock_symbols = ["AAPL", "GOOGL", "MSFT" , "NKE", "TSLA"] - - for symbol in stock_symbols: - current_stock_price = get_stock_price(symbol) - - if current_stock_price is not None: - print(f"The latest price of {symbol} is ${current_stock_price:.2f}") + while True: + try: + for symbol in stock_symbols: + current_stock_price = get_stock_price(symbol) + print(f"The latest price of {symbol} is {current_stock_price:} GBP") + + if current_stock_price <= (get_stock_price(symbol) - PRICE_DROP_THRESHOLD): + send_notification(symbol, current_stock_price) + print(f"{symbol} price dropped by at least £0.25 GBP.") + + + + time.sleep(300) - else: - print(f"Couldn't retrieve latest price for {symbol}") \ No newline at end of file + except Exception as e: + print("ERROR!", e) From 67d17145e355c3b0439a46e2a3154e09de105687 Mon Sep 17 00:00:00 2001 From: Sabina Pun Date: Thu, 24 Aug 2023 22:01:27 +0100 Subject: [PATCH 4/4] added documentation --- main.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 055a217..5aba200 100644 --- a/main.py +++ b/main.py @@ -6,25 +6,23 @@ load_dotenv() -API_KEY = os.getenv('API_KEY') +API_KEY = os.getenv('API_KEY') #IEX CLOUD API -WEBHOOKS_KEY = os.getenv('WEBHOOKS_KEY') +WEBHOOKS_KEY = os.getenv('WEBHOOKS_KEY') #WEBHOOKS KEY -IFTTT_URL = f"https://maker.ifttt.com/trigger/notify/with/key/{WEBHOOKS_KEY}" +IFTTT_URL = f"https://maker.ifttt.com/trigger/notify/with/key/{WEBHOOKS_KEY}" #IFTTT URL -PRICE_DROP_THRESHOLD = 0.25 +PRICE_DROP_THRESHOLD = 0.25 #THRESHOLD FOR PRICE DROP stock_symbols = ["AAPL", "GOOGL", "MSFT" , "NKE", "TSLA"] -previous_prices = {} - -#function that returns latest stock prices +#FUNCTION THAT RETURNS LATEST PRICE STOCKS def get_stock_price(symbol): url = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={API_KEY}" response = requests.get(url) @@ -32,7 +30,7 @@ def get_stock_price(symbol): return data['latestPrice'] -#function that sends IFTTT notification +#FUNCTION THAT SENDS IFTTT NOTIFICATION def send_notification(symbol, price): payload = { @@ -57,7 +55,7 @@ def send_notification(symbol, price): - time.sleep(300) + time.sleep(300) #CHECKING PRICE AGAIN EVERY FIVE MINS except Exception as e: - print("ERROR!", e) + print("AN ERROR OCCURED!", e)