|
1 | 1 | import os |
2 | 2 | from twilio.rest import Client |
| 3 | +from datetime import date, timedelta |
3 | 4 | import requests |
4 | 5 |
|
5 | 6 | STOCK = "TSLA" |
|
12 | 13 |
|
13 | 14 | ## STEP 1: Use https://www.alphavantage.co |
14 | 15 | # 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 | + |
17 | 38 |
|
18 | 39 | ## STEP 2: Use https://newsapi.org |
19 | 40 | # 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 | + |
22 | 53 |
|
23 | 54 | ## STEP 3: Use https://www.twilio.com |
24 | 55 | # 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: |
34 | 67 | """ |
35 | 68 | TSLA: 🔺2% |
36 | 69 | Headline: Were Hedge Funds Right About Piling Into Tesla Inc. (TSLA)?. |
|
41 | 74 | 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. |
42 | 75 | """ |
43 | 76 |
|
| 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