Skip to content

Commit dc5589f

Browse files
Update Day-36
1 parent f46d306 commit dc5589f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

Day-36-StockNewsMonitoring/main.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from dotenv import load_dotenv
2+
import os
3+
import requests
4+
import json
5+
from twilio.rest import Client
6+
from datetime import *
7+
8+
load_dotenv()
9+
STOCK = "TSLA"
10+
COMPANY_NAME = "Tesla Inc"
11+
AV_ENDPOINT = "https://www.alphavantage.co/query"
12+
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"
13+
14+
def send_sms():
15+
with open('text.txt','r') as f:
16+
sms = f.read()
17+
sms = sms.replace('[CHART]','📉')
18+
ARROW = '🔻'
19+
if stockprice_change_percent>0:
20+
ARROW = '🔺'
21+
sms = sms.replace('[ARROW]',ARROW)
22+
account_sid = os.getenv('ACCOUNT_SID')
23+
auth_token = os.getenv('AUTH_TOKEN')
24+
client = Client(account_sid, auth_token)
25+
message = client.messages \
26+
.create(
27+
body=sms,
28+
from_='+14078637908',
29+
to=os.getenv('MY_PHONE')
30+
)
31+
print(message.status)
32+
33+
def get_news():
34+
news_parameters = {
35+
'q': COMPANY_NAME,
36+
'sortBy': 'relevance',
37+
'apiKey': os.getenv('NEWS_API_KEY')
38+
}
39+
response = requests.get(url=NEWS_ENDPOINT, params=news_parameters)
40+
response.raise_for_status()
41+
news_data = response.json()['articles'][:3]
42+
for data in news_data:
43+
with open('text.txt','a') as f:
44+
print(f"Headline: {data['title']}", file=f)
45+
print(f"Brief: {data['description']}", file=f)
46+
47+
av_parameters = {
48+
'function': 'TIME_SERIES_DAILY',
49+
'symbol': STOCK,
50+
'apikey': os.getenv('ALPHA_VANTAGE_API_KEY')
51+
}
52+
53+
response = requests.get(url=AV_ENDPOINT, params=av_parameters)
54+
response.raise_for_status()
55+
data = response.json()['Time Series (Daily)']
56+
57+
today_date = (datetime.now()-timedelta(days=1)).strftime('%Y-%m-%d')
58+
yesterday_date = (datetime.now()-timedelta(days=2)).strftime('%Y-%m-%d')
59+
today_stockprice = float(data[today_date]['4. close'])
60+
yesterday_stockprice = float(data[yesterday_date]['4. close'])
61+
stockprice_change_percent = 100*(today_stockprice - yesterday_stockprice)/today_stockprice
62+
# print(f"{today_stockprice}\n{yesterday_stockprice}\n{stockprice_change_percent}")
63+
64+
if stockprice_change_percent<-5 or stockprice_change_percent>5:
65+
with open('text.txt','w') as f:
66+
print(".\nSTOCK MARKET NEWS MONITOR[CHART]", file=f)
67+
print(f"{STOCK}: [ARROW]{abs(round(stockprice_change_percent))}%", file=f)
68+
get_news()
69+
send_sms()

Day-36-StockNewsMonitoring/text.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.
2+
STOCK MARKET NEWS MONITOR[CHART]
3+
TSLA: [ARROW]1%
4+
Headline: Tesla beats quarterly revenue estimates - Reuters Africa
5+
Brief: Tesla Inc on Wednesday beat Wall Street estimates for fourth-quarter revenue after trouncing 2020 deliveries earlier this month on a steady rise in electric vehicle demand.
6+
Headline: Manufacturer for hire: China's Geely sets out to become a force in electric cars - Reuters India
7+
Brief: Like many others in his industry, Geely Chairman Li Shifu has been irked by skyrocketing valuations for electric car manufacturers such as Tesla Inc and Nio Inc, sources at the Chinese automaker say.
8+
Headline: Tesla says starts delivering Shanghai-made Model Y in China - Reuters
9+
Brief: Tesla Inc said on Monday it has started delivering its Shanghai-made Model Y sports utility vehicles to customers in China.

0 commit comments

Comments
 (0)