Skip to content

Instantly share code, notes, and snippets.

@Jithender5913
Created February 24, 2022 05:09
Show Gist options
  • Save Jithender5913/b28e0c2218ec87d5c2bfc7a0d57792db to your computer and use it in GitHub Desktop.
Save Jithender5913/b28e0c2218ec87d5c2bfc7a0d57792db to your computer and use it in GitHub Desktop.
Stock Trading News Alert Project using Python News and Twilio APIs
import requests
from twilio.rest import Client
STOCK_NAME = "TSLA"
COMPANY_NAME = "Tesla Inc"
STOCK_ENDPOINT = "https://www.alphavantage.co/query"
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"
API_KEY = "xxx"
# # STEP 1: Use https://www.alphavantage.co/documentation/#daily
# When stock price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News").
# TODO 1. - 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()]
stock_parameters = {
"function": "TIME_SERIES_DAILY",
"symbol": STOCK_NAME,
"datatype": "json",
"apikey": API_KEY,
}
response = requests.get(STOCK_ENDPOINT, params=stock_parameters)
response.raise_for_status()
data = response.json()
time_series_data = data["Time Series (Daily)"]
print(time_series_data.items())
close_price = [value for (key, value) in time_series_data.items()]
yesterday_close_price = close_price[0]["4. close"]
print(yesterday_close_price)
# TODO 2. - Get the day before yesterday's closing stock price
previous_day_close = close_price[1]["4. close"]
print(previous_day_close)
# TODO 3. - 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
difference = float(yesterday_close_price) - float(previous_day_close)
up_down = None
if difference > 0:
up_down = "🔺"
else:
up_down = "🔻"
# TODO 4. - Work out the percentage difference in price between closing price yesterday and closing price the day
# before yesterday.
diff_percent = round((difference / float(yesterday_close_price)) * 100)
print(diff_percent)
# TODO 5. - If TODO4 percentage is greater than 5 then print("Get News").
if abs(diff_percent) > 5:
# # STEP 2: https://newsapi.org/
# Instead of printing ("Get News"), actually get the first 3 news pieces for the COMPANY_NAME.
# TODO 6. - Instead of printing ("Get News"), use the News API to get articles related to the COMPANY_NAME.
NEWS_API_KEY = "xxx"
NEWS_API = "https://newsapi.org/v2/everything"
news_parameters = {
"qInTitle": COMPANY_NAME,
"language": "en",
"apikey": NEWS_API_KEY,
}
# TODO 7. - Use Python slice operator to create a list that contains the first 3 articles. Hint:
# https://stackoverflow.com/questions/509211/understanding-slice-notation
response_news = requests.get(NEWS_API, params=news_parameters)
news_data = response_news.json()
data_news_articles = news_data["articles"]
three_articles = data_news_articles[0:3]
# # STEP 3: Use twilio.com/docs/sms/quickstart/python
# to send a separate message with each article's title and description to your phone number.
# TODO 8. - Create a new list of the first 3 article's headline and description using list comprehension.
formatted_articles_list = [f"{STOCK_NAME}: {up_down}{diff_percent} {article['title']}. \n{article['description']}" for article in three_articles]
print(formatted_articles_list)
# TODO 9. - Send each article as a separate message via Twilio.
account_sid = "xxx"
auth_token = "xxx"
client = Client(account_sid, auth_token)
for article in formatted_articles_list:
message = client.messages \
.create(
body=article,
from_='+xx',
to='+xxx'
)
print(message.status)
else:
print(f"{COMPANY_NAME}:{up_down} and {diff_percent}%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment