From feed4d0bba33c4887a41291debeccaad24852404 Mon Sep 17 00:00:00 2001 From: Virginia Helmreich Date: Thu, 12 Oct 2023 20:56:44 +0100 Subject: [PATCH 1/5] Added main file --- main.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..e1f5fb0 --- /dev/null +++ b/main.py @@ -0,0 +1,54 @@ +import yfinance as yf +import requests +import time +import pandas as pd + +IFTTT_EVENT_NAME = 'stock_has_dropped' +IFTTT_KEY = 'b8wRX9xgqi7o9u43AAlmwA' + +# Calculate the 7-day average of stock prices +def calculate_7_day_average(ticker_symbol): + today = pd.Timestamp.now() + seven_days_ago = today - pd.DateOffset(days=7) + + data = yf.download(ticker_symbol, start=seven_days_ago, end=today) + return data['Close'].mean() + +# Get the current stock price +def get_stock_price(ticker_symbol): + stock_data = yf.Ticker(ticker_symbol) + return stock_data.history(period='1d')['Close'].iloc[0] + +# Main function +def main(): + ticker_symbol = {'AAPL': 0, + 'TSLA': 0, + 'MSFT': 0, + 'GOOGL': 0, + 'NKE': 0} + previous_price = None + current_price = None + + while True: + for stock in ticker_symbol: + previous_price = ticker_symbol[stock] + current_price = get_stock_price(stock) + seven_day_average = calculate_7_day_average(stock) + ticker_symbol[stock] = current_price + + price_difference = previous_price - current_price + + if price_difference >= 0.25 or current_price < seven_day_average: + # Trigger IFTTT applet using Webhooks service + payload = {'value1': current_price, 'value2': seven_day_average} + requests.post(f'https://maker.ifttt.com/trigger/{IFTTT_EVENT_NAME}/json/with/key/{IFTTT_KEY}', json=payload) + + payload = {'value1': current_price, 'value2': seven_day_average} + requests.post(f'https://maker.ifttt.com/trigger/{IFTTT_EVENT_NAME}/json/with/key/{IFTTT_KEY}', json=payload) + + previous_price = current_price + # Check every 5 minutes + time.sleep(300) + +if __name__ == "__main__": + main() \ No newline at end of file From c97a704f72cb196f410e91e256f283cae65f3530 Mon Sep 17 00:00:00 2001 From: Virginie Helmreich <122171893+virginiacodes@users.noreply.github.com> Date: Fri, 13 Oct 2023 08:28:49 +0100 Subject: [PATCH 2/5] Removed test notification event --- main.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index e1f5fb0..7248f08 100644 --- a/main.py +++ b/main.py @@ -43,12 +43,10 @@ def main(): payload = {'value1': current_price, 'value2': seven_day_average} requests.post(f'https://maker.ifttt.com/trigger/{IFTTT_EVENT_NAME}/json/with/key/{IFTTT_KEY}', json=payload) - payload = {'value1': current_price, 'value2': seven_day_average} - requests.post(f'https://maker.ifttt.com/trigger/{IFTTT_EVENT_NAME}/json/with/key/{IFTTT_KEY}', json=payload) - + previous_price = current_price # Check every 5 minutes time.sleep(300) if __name__ == "__main__": - main() \ No newline at end of file + main() From d796ca4fe205a5072a78eb330e2d0ca611863a23 Mon Sep 17 00:00:00 2001 From: Virginie Helmreich <122171893+virginiacodes@users.noreply.github.com> Date: Fri, 13 Oct 2023 08:35:25 +0100 Subject: [PATCH 3/5] Added comments --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index 7248f08..5b4fb92 100644 --- a/main.py +++ b/main.py @@ -29,6 +29,7 @@ def main(): previous_price = None current_price = None + # Getting new current price, updating previous price and seven day average while True: for stock in ticker_symbol: previous_price = ticker_symbol[stock] From a33043be388adfa464ce901d9db06c14ee9df805 Mon Sep 17 00:00:00 2001 From: Virginie Helmreich <122171893+virginiacodes@users.noreply.github.com> Date: Fri, 13 Oct 2023 11:00:07 +0100 Subject: [PATCH 4/5] Changed 0.25 GBP to 0.30 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stock data are in dollars, so the £0.25 needed to be changed to $0.30 to reflect that. --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 5b4fb92..7f816b7 100644 --- a/main.py +++ b/main.py @@ -39,7 +39,7 @@ def main(): price_difference = previous_price - current_price - if price_difference >= 0.25 or current_price < seven_day_average: + if price_difference >= 0.30 or current_price < seven_day_average: # Trigger IFTTT applet using Webhooks service payload = {'value1': current_price, 'value2': seven_day_average} requests.post(f'https://maker.ifttt.com/trigger/{IFTTT_EVENT_NAME}/json/with/key/{IFTTT_KEY}', json=payload) From 77c766bb01b1289b07b9072d5560b20be89f02ce Mon Sep 17 00:00:00 2001 From: Virginie Helmreich <122171893+virginiacodes@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:02:07 +0100 Subject: [PATCH 5/5] Removed API key --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 7f816b7..138567c 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import pandas as pd IFTTT_EVENT_NAME = 'stock_has_dropped' -IFTTT_KEY = 'b8wRX9xgqi7o9u43AAlmwA' +IFTTT_KEY = '' # Calculate the 7-day average of stock prices def calculate_7_day_average(ticker_symbol):