Skip to content

Commit ebf94e2

Browse files
authored
Create main.py
1 parent e6f9389 commit ebf94e2

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

  • python_projects/36_stocks_alert_sms
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import requests
2+
from twilio.rest import Client
3+
4+
STOCK_NAME = "TSLA"
5+
COMPANY_NAME = "Tesla Inc"
6+
7+
STOCK_ENDPOINT = "https://www.alphavantage.co/query"
8+
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"
9+
10+
STOCK_API_KEY = "L2Y4FINQF8P3S1U6"
11+
NEWS_API_KEY = "32c3f0dfb669425193057fdc87fc5733"
12+
13+
TWILIO_ACCOUNT_SID = "ACf225f87a311e49354524c4f56b8df5c4"
14+
TWILIO_AUTH_TOKEN = "bea638d09ab50b3fc1f63564ab78cc44"
15+
16+
stock_params = {
17+
"function" : "TIME_SERIES_WEEKLY",
18+
"symbol" : STOCK_NAME,
19+
"apikey" : STOCK_API_KEY,
20+
}
21+
## Use https://www.alphavantage.co/documentation/#daily
22+
23+
24+
#Get yesterday's closing stock price. Hint: You can perform list comprehensions on Python dictionaries. e.g. [new_value for (key, value) in dictionary.items()]
25+
response = requests.get(STOCK_ENDPOINT, params=stock_params)
26+
data = response.json()["Weekly Time Series"]
27+
data_list = [value for (key, value) in data.items()]
28+
last_week = data_list[0]
29+
last_week_closing_price = last_week["4. close"]
30+
31+
# Get the day before yesterday's closing stock price
32+
second_last_week = data_list[1]
33+
second_last_week_closing_price = second_last_week["4. close"]
34+
35+
#Find the positive difference between 1 and 2. e.g. 40 - 20 = -20, but the positive difference is 20. Hint: https://www.w3schools.com/python/ref_func_abs.asp
36+
difference = float(last_week_closing_price) - float(second_last_week_closing_price)
37+
if difference > 0:
38+
up_down = "⬆️"
39+
else:
40+
up_down = "⬇️"
41+
#Work out the percentage difference in price between closing price yesterday and closing price the day before yesterday.
42+
diff_percentage = round(float(difference) / float(last_week_closing_price) * 100)
43+
print(diff_percentage)
44+
45+
if diff_percentage <2:
46+
news_params = {
47+
"q" : COMPANY_NAME,
48+
"apiKey" : NEWS_API_KEY,
49+
50+
}
51+
response = requests.get(NEWS_ENDPOINT, params=news_params)
52+
news_article = response.json()["articles"]
53+
54+
#Use Python slice operator to create a list that contains the first 3 articles. Hint: https://stackoverflow.com/questions/509211/understanding-slice-notation
55+
last_three_articles = news_article[:3]
56+
57+
#Create a new list of the first 3 article's headline and description using list comprehension.
58+
formatted_articles = [f"Stock: {STOCK_NAME}{up_down}\nHeadline: {article['title']}. \nBrief:{article['description']}" for article in last_three_articles]
59+
print(formatted_articles)
60+
#Send each article as a separate message via Twilio.
61+
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
62+
for article in formatted_articles:
63+
message = client.messages.create(
64+
body=article,
65+
from_="+17602538114",
66+
to="+919347589813"
67+
)
68+
69+

0 commit comments

Comments
 (0)