Skip to content
Open
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
57 changes: 57 additions & 0 deletions Stock-signalling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import requests

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.



# List ofstock symbols that will be monitored
stock_symbols = ["TSLA", "AAPL", "MSFT", "GOOGL", "NKE"]

Choose a reason for hiding this comment

The 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.
For consistency, I suggest choosing either one or two blank lines and applying it across the whole code.
For example - there is one blank line separating the codes here, but in line 2 and 3 there are two lines, for the majority of the code there is one blank line, but in line 31 and 32 there are two blank lines.

# API key and the base URL
alpha_vantage_api_key = "NZXLRSMAPCX4T3YW"

Choose a reason for hiding this comment

The 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.
I just learned about environment variables (.env files), they are good for storing sensitive information in a secure way.
I suggest storing your API key in an environment variable for added security.

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"

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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:

Choose a reason for hiding this comment

The 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)