|
| 1 | +import requests |
| 2 | + |
| 3 | + |
| 4 | +STOCK_NAME = "TSLA" |
| 5 | +STOCK_ENDPOINT = "https://www.alphavantage.co/query" |
| 6 | +NEWS_ENDPOINT = "https://newsapi.org/v2/everything" |
| 7 | +ALPHA_VANTAGE_APIKEY = "****************" |
| 8 | +NEWS_APIKEY = "********************************" |
| 9 | + |
| 10 | + |
| 11 | +def telegram_bot_send_text(bot_message): |
| 12 | + bot_token = "**********************************************" |
| 13 | + bot_chatID = "**********" |
| 14 | + send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID \ |
| 15 | + + '&parse_mode=Markdown&text=' + bot_message |
| 16 | + bot_response = requests.get(send_text) |
| 17 | + return bot_response.json() |
| 18 | + |
| 19 | + |
| 20 | +def get_change(current, previous): |
| 21 | + if current == previous: |
| 22 | + return 100.0 |
| 23 | + try: |
| 24 | + performance = round(abs(current - previous) / previous, 5) * 100.0 |
| 25 | + print(performance) |
| 26 | + return performance |
| 27 | + except ZeroDivisionError: |
| 28 | + return 0 |
| 29 | + |
| 30 | + |
| 31 | +alpha_vantage_parameters = { |
| 32 | + "function": "TIME_SERIES_DAILY", |
| 33 | + "symbol": STOCK_NAME, |
| 34 | + "apikey": ALPHA_VANTAGE_APIKEY, |
| 35 | +} |
| 36 | + |
| 37 | +news_api_parameters = { |
| 38 | + "q": f"{STOCK_NAME} Market Update", |
| 39 | + "pageSize": 3, |
| 40 | + "apiKey": NEWS_APIKEY, |
| 41 | +} |
| 42 | + |
| 43 | +alpha_vantage_response = requests.get(STOCK_ENDPOINT, params=alpha_vantage_parameters) |
| 44 | +alpha_vantage_response.raise_for_status() |
| 45 | +stock_data = alpha_vantage_response.json() |
| 46 | + |
| 47 | +news_api_response = requests.get(NEWS_ENDPOINT, params=news_api_parameters) |
| 48 | +news_api_response.raise_for_status() |
| 49 | +news_data = news_api_response.json() |
| 50 | + |
| 51 | +closing_prices = [] |
| 52 | +for _, date in zip(range(3), stock_data['Time Series (Daily)']): |
| 53 | + closing_prices.append(stock_data['Time Series (Daily)'][date]['4. close']) |
| 54 | +yd_price = float(closing_prices[0]) |
| 55 | +dyd_price = float(closing_prices[1]) |
| 56 | + |
| 57 | +difference = yd_price - dyd_price |
| 58 | +up_down = None |
| 59 | +if difference > 0: |
| 60 | + up_down = "🔺" |
| 61 | +else: |
| 62 | + up_down = "🔻" |
| 63 | + |
| 64 | +if get_change(yd_price, dyd_price) >= 5: |
| 65 | + for article in range(3): |
| 66 | + market_performance = [] |
| 67 | + news_headlines = [] |
| 68 | + news_description = [] |
| 69 | + market_performance.append(f"{STOCK_NAME}: {up_down} {get_change(yd_price, dyd_price)}% ") |
| 70 | + news_headlines.append(f"Headline: {news_data['articles'][article]['title']}") |
| 71 | + news_description.append(f"Description: {news_data['articles'][article]['description']}") |
| 72 | + news = (market_performance + news_headlines + news_description) |
| 73 | + joined_string = "\n".join(news) |
| 74 | + telegram_bot_send_text(f"{joined_string}") |
0 commit comments