-
Notifications
You must be signed in to change notification settings - Fork 16
SD-API-Stock-Signalling #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import requests | ||
|
|
||
|
|
||
| # List ofstock symbols that will be monitored | ||
| stock_symbols = ["TSLA", "AAPL", "MSFT", "GOOGL", "NKE"] | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I liked that you have blank lines to separate the different sections in your code. |
||
| # API key and the base URL | ||
| alpha_vantage_api_key = "NZXLRSMAPCX4T3YW" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of variables, so you don't have to write the API Key every time you want to call it. |
||
| base_url = "https://www.alphavantage.co/query" | ||
|
|
||
| # IFTTT Webhook URL | ||
| ifttt_webhook_url = "https://maker.ifttt.com/trigger/stock-price-decrease/json/with/key/bWs2poP5Q2RliJ9-PmAYcbb" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, you could use an environment variable for your webhook key. |
||
|
|
||
| # Dictionary will store the previous stock prices | ||
| previous_prices = {} | ||
| previous_price = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you tell me why the previous price is set to zero? |
||
|
|
||
| def get_stock_price(symbol): | ||
| # Get the daily closing stock price for a given symbol using the Alpha Vantage API. | ||
| params = { | ||
| "function": "TIME_SERIES_DAILY", | ||
| "symbol": symbol, | ||
| "apikey": alpha_vantage_api_key | ||
| } | ||
| for symbol in stock_symbols: | ||
| stock_price = get_stock_price(symbol) | ||
| previous_price = previous_prices.get(symbol, None) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you added none, in case there was no previous price available, it shows that you are thinking of the user experience. |
||
| response = requests.get(base_url, params=params) | ||
| data = response.json() | ||
| return float(data["Time Series (Daily)"][list(data["Time Series (Daily)"].keys())[0]]["4. close"]) | ||
|
|
||
|
|
||
| def send_notification(stock_symbol, stock_price, previous_price): | ||
| # Check if the price decreased by at least £0.25 | ||
| if previous_price - stock_price >= 0.25: | ||
| message = f"{stock_symbol} price decreased by £0.25: {stock_price}" | ||
| data = { | ||
| "value1": message | ||
| } | ||
| response = requests.post(ifttt_webhook_url, json=data) | ||
| if response.status_code == 200: | ||
| print(f"Notification sent for {stock_symbol}") | ||
| else: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the use of the if/else statement, you have accounted for a situation where an error might arise. The notification will inform the user that the action failed. |
||
| print(f"Notification failed for {stock_symbol}") | ||
|
|
||
| # Update the previous price in the dictionary | ||
| previous_prices[symbol] = stock_price | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| for symbol in stock_symbols: | ||
| stock_price = get_stock_price(symbol) | ||
| previous_price = previous_prices.get(symbol, None) | ||
|
|
||
| send_notification(symbol, stock_price, previous_price) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work Suwayda. This is a clear and concise code for monitoring specific stock prices and notifying the user when the price drops by at least £0.25.
Your comments are to the point and help me understand your thought process.
However, I don't see a code for getting the 7 day average, this is something that you could add in future versions of the code, it will result in a better stock monitoring app, as you will have historical data and be better able to track drastic changes in the stock price. This is important as it will help inform the user on when to purchase more stocks.
Otherwise, you followed the syntax in naming variables and functions, your code is very readable, I would be able to understand and continue from where you had stopped if we were handing over. Easy to debug or go back and make changes, because things are clearly named.