Skip to content

Commit 1a5a679

Browse files
committed
complete stock trading alert sms
1 parent c2b84b5 commit 1a5a679

1 file changed

Lines changed: 58 additions & 13 deletions

File tree

StockTradingAlertSMS/main.py

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from twilio.rest import Client
3+
from datetime import date, timedelta
34
import requests
45

56
STOCK = "TSLA"
@@ -12,25 +13,57 @@
1213

1314
## STEP 1: Use https://www.alphavantage.co
1415
# When STOCK price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News").
15-
alphavantage_response = requests.get(f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={STOCK}&apikey={alphavantage_api_key}")
16-
print(f"alphavantage response {alphavantage_response.status_code}")
16+
def get_stock_price() -> dict:
17+
alphavantage_response = requests.get(
18+
f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={STOCK}&apikey={alphavantage_api_key}")
19+
data_alphavantage = alphavantage_response.json()
20+
return data_alphavantage
21+
22+
23+
def get_fluctuation_stock(data_stock: dict) -> float:
24+
yesterday = date.today() - timedelta(days=1)
25+
before_yesterday = date.today() - timedelta(days=2)
26+
before_yesterday_str = before_yesterday.strftime("%Y-%m-%d")
27+
yesterday_str = yesterday.strftime("%Y-%m-%d")
28+
yesterday_close = data_stock["Time Series (Daily)"][yesterday_str]["4. close"]
29+
before_yesterday_close = data_stock["Time Series (Daily)"][before_yesterday_str]["4. close"]
30+
31+
difference = yesterday_close - before_yesterday_close
32+
percentage = round(difference / before_yesterday_close * 100)
33+
if percentage >= 5 or percentage <= -5:
34+
return percentage
35+
else:
36+
return 0
37+
1738

1839
## STEP 2: Use https://newsapi.org
1940
# Instead of printing ("Get News"), actually get the first 3 news pieces for the COMPANY_NAME.
20-
news_response = requests.get(f"https://newsapi.org/v2/top-headlines?q=tesla&apiKey={news_api_key}")
21-
print(f"news response: {news_response.status_code}")
41+
def get_last_three_news(fluctuation: int) -> list[dict]:
42+
list_news = []
43+
news_response = requests.get(f"https://newsapi.org/v2/top-headlines?q=tesla&apiKey={news_api_key}")
44+
data_news = news_response.json()
45+
for news in data_news["articles"][:3]:
46+
list_news.append({
47+
"fluctuation": fluctuation,
48+
"title": news["title"],
49+
"description": news["description"]
50+
})
51+
return list_news
52+
2253

2354
## STEP 3: Use https://www.twilio.com
2455
# Send a seperate message with the percentage change and each article's title and description to your phone number.
25-
client = Client(account_sid, auth_token)
26-
# message = client.messages.create(
27-
# from_='+',
28-
# body='Hi there',
29-
# to='+'
30-
# )
31-
# print(message.sid)
32-
33-
#Optional: Format the SMS message like this:
56+
def send_sms(news: dict):
57+
client = Client(account_sid, auth_token)
58+
message = client.messages.create(
59+
from_='+',
60+
body=f'{STOCK}: {news["fluctuation"]}\nHeadline: {news["title"]}\nBrief: {news["description"]}',
61+
to='+'
62+
)
63+
print(message.sid)
64+
65+
66+
# Optional: Format the SMS message like this:
3467
"""
3568
TSLA: 🔺2%
3669
Headline: Were Hedge Funds Right About Piling Into Tesla Inc. (TSLA)?.
@@ -41,3 +74,15 @@
4174
Brief: We at Insider Monkey have gone over 821 13F filings that hedge funds and prominent investors are required to file by the SEC The 13F filings show the funds' and investors' portfolio positions as of March 31st, near the height of the coronavirus market crash.
4275
"""
4376

77+
78+
def main():
79+
stock_prices = get_stock_price()
80+
# fluctuation = get_fluctuation_stock(stock_prices)
81+
fluctuation = 5
82+
if fluctuation != 0:
83+
last_three_news = get_last_three_news(fluctuation)
84+
for news in last_three_news:
85+
send_sms(news)
86+
87+
88+
main()

0 commit comments

Comments
 (0)