From 1dc9bb479b799866640b4cbebfb0de2cc21677e7 Mon Sep 17 00:00:00 2001 From: Shireen Pajnigar Date: Thu, 24 Aug 2023 16:35:53 +0100 Subject: [PATCH 1/2] final project --- main.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..31cc322 --- /dev/null +++ b/main.py @@ -0,0 +1,64 @@ +import requests +import time + +# API key with IEX Cloud +api_key = "pk_c2257f70a6584faca4bb653bd4618478" + +# Stocks to monitor +stocks = ["TSLA", "AAPL", "MSFT", "GOOGL", "NKE"] + +# IFTTT Webhook URL +IFTTT_webhook_url = "https://maker.ifttt.com/trigger/stock_price_falls/with/key/hKArh7xueLrGZEGnVP-IB4nYOdWZ3bR-hiMDYmTt283" + +# Create a dictionary to store previous prices for each week +previous_prices = {} + +# Function to fetch 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 float(data["latestPrice"]) + +""" +# Function to fetch 7-day average price +def get_7day_average(symbol): + url = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={api_key}" + response = requests.get(url) + data = response.json() + prices = [float(entry["close"]) for entry in data["values"]] + average = sum(prices) / len(prices) + return average +""" + + +# Function to send IFTTT notification +def send_notification(stock_symbol, price): + payload = { + "value1": stock_symbol, + "value2": str(price) + } + requests.post(IFTTT_webhook_url, json=payload) + +# Main monitoring loop +while True: + print("Monitoring stock prices...") + + for stock_symbol in stocks: + current_price = get_stock_price(stock_symbol) + + if stock_symbol in previous_prices: + price_change = previous_prices[stock_symbol] - current_price + + if price_change >= 0.25: + send_notification(stock_symbol, current_price) + print(f"{stock_symbol} price fell by £0.25 or more. Sending notification.") + else: + print(f"{stock_symbol} price change: £{price_change: .2f}") + + previous_prices[stock_symbol] = current_price + + +# Check every 1 minute +time.sleep(60) + From ef9307342d9b47d163368232cd9901840410a97b Mon Sep 17 00:00:00 2001 From: Shireen Pajnigar Date: Fri, 25 Aug 2023 11:43:11 +0100 Subject: [PATCH 2/2] final project --- main.py | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 31cc322..1e0aaaf 100644 --- a/main.py +++ b/main.py @@ -7,9 +7,12 @@ # Stocks to monitor stocks = ["TSLA", "AAPL", "MSFT", "GOOGL", "NKE"] -# IFTTT Webhook URL +# IFTTT Webhook URL for £0.25 price drop IFTTT_webhook_url = "https://maker.ifttt.com/trigger/stock_price_falls/with/key/hKArh7xueLrGZEGnVP-IB4nYOdWZ3bR-hiMDYmTt283" +# IFTTT Webhook URL for 7-day average fall +IFTTT_7day_average_webhook_url = "https://maker.ifttt.com/trigger/7day_average/with/key/hKArh7xueLrGZEGnVP-IB4nYOdWZ3bR-hiMDYmTt283" + # Create a dictionary to store previous prices for each week previous_prices = {} @@ -18,21 +21,23 @@ 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 float(data["latestPrice"]) + return float(data["latestPrice"]) -""" # Function to fetch 7-day average price def get_7day_average(symbol): - url = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={api_key}" + url = f"https://cloud.iexapis.com/stable/stock/{symbol}/chart/1w?token={api_key}" response = requests.get(url) data = response.json() - prices = [float(entry["close"]) for entry in data["values"]] - average = sum(prices) / len(prices) - return average -""" - + + if data: + prices = [float(entry["close"]) for entry in data] + average = sum(prices) / len(prices) + return average + else: + print(f"Error fetching 7-day average for {symbol}: {data}") + return None -# Function to send IFTTT notification +# Function to send IFTTT notification for £0.25 price drop def send_notification(stock_symbol, price): payload = { "value1": stock_symbol, @@ -40,25 +45,37 @@ def send_notification(stock_symbol, price): } requests.post(IFTTT_webhook_url, json=payload) +# Function to send IFTTT notification for 7-day average price drop +def send_7day_average_notification(stock_symbol): + payload = { + "value1": stock_symbol, + "value3": "Today's price is lower than 7-day average." + } + requests.post(IFTTT_7day_average_webhook_url, json=payload) + # Main monitoring loop while True: print("Monitoring stock prices...") for stock_symbol in stocks: current_price = get_stock_price(stock_symbol) + average_7day = get_7day_average(stock_symbol) if stock_symbol in previous_prices: price_change = previous_prices[stock_symbol] - current_price if price_change >= 0.25: - send_notification(stock_symbol, current_price) + send_notification(stock_symbol, f"Fell by £{price_change:.2f} or more.") print(f"{stock_symbol} price fell by £0.25 or more. Sending notification.") - else: - print(f"{stock_symbol} price change: £{price_change: .2f}") + + if average_7day is not None and current_price < average_7day: + send_7day_average_notification(stock_symbol) + print(f"{stock_symbol} price is lower than 7-day average. Sending notification.") previous_prices[stock_symbol] = current_price + # Check every 1 minute + time.sleep(60) + -# Check every 1 minute -time.sleep(60)