Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
61 changes: 61 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import requests
import time
import os

from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv('API_KEY') #IEX CLOUD API

WEBHOOKS_KEY = os.getenv('WEBHOOKS_KEY') #WEBHOOKS KEY



IFTTT_URL = f"https://maker.ifttt.com/trigger/notify/with/key/{WEBHOOKS_KEY}" #IFTTT URL



PRICE_DROP_THRESHOLD = 0.25 #THRESHOLD FOR PRICE DROP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good use of variables to avoid magic numbers- falls within good coding standards


stock_symbols = ["AAPL", "GOOGL", "MSFT" , "NKE", "TSLA"]



#FUNCTION THAT RETURNS LATEST PRICE STOCKS

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of comments to clarify code

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']


#FUNCTION THAT SENDS IFTTT NOTIFICATION
def send_notification(symbol, price):

payload = {
"value1" : symbol,
"value2" : str(price)
}

requests.post(IFTTT_URL, json=payload)



if __name__ == "__main__":
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) #CHECKING PRICE AGAIN EVERY FIVE MINS

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frequency of every 5 mins aligns with the rate at which the source updates its stock prices, and so could have explained that in separate documentation


except Exception as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have used exceptions more to avoid disruptions that come from all the inputs

print("AN ERROR OCCURED!", e)