Stock market price alerter
- 라임 샹큼
- Apr 7
- 2 min read
Lately I’ve been getting API data from existing data and using it to get actual live data. This is done by importing requests.
This was easier than other projects because the things I needed to do were limited and fixed. The code I needed to write was all laid out and this is probably not the best indication of my improvement. The code written here was nearly all my writing except for one line that uses the list comprehension to get list items and the values so I didn’t need to use the date time module. Being able to use live data is very interesting and fun. I was supposed to send the stock increase as a text message using twilio but found the process of signing in tedious and also didn’t like I was limited to a specific amount of messages. So I just wrote the other parts of the code and excluded the text part. This really made me realize the importance of list comprehensins and api keys and wonder if I'll be able to make my own apis in the future.
import requests
import json
import datetime
STOCK = "TSLA"
COMPANY_NAME = "Tesla Inc"
STOCK_ENDPOINT = "https://www.alphavantage.co/query"
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"
news_api_key =
stock_api_key =
parameter={
'q':[COMPANY_NAME,STOCK],
'from':'2025-04-06',
'language':'en',
'sortBy':'relevancy',
'apiKey':news_api_key,
}
def get_news_info():
data = requests.get(url=NEWS_ENDPOINT,params=parameter)
json_data = data.json()['articles'][:3]
news_names = [json_data[n]['source']['name'] for n in range(3)]
news_titles = [json_data[n]['title'] for n in range(3)]
news_authors = [json_data[n]['author'] for n in range(3)]
news_descriptions = [json_data[n]['description'] for n in range(3)]
if increase < 0:
point = '🔻'
else:
point = '🔺'
for n in range(3):
print(f'{STOCK}: {point}{int(abs(increase))}%')
print(f'\'{news_titles[n]}\' from {news_names[n]} by {news_authors[n]}\n{news_descriptions[n]}\n')
stock_parameter={
'apikey':stock_api_key,
'function':'TIME_SERIES_DAILY',
'symbol':STOCK,
}
stock_data = requests.get(url=STOCK_ENDPOINT,params=stock_parameter)
json_stock_data = stock_data.json()['Time Series (Daily)']
value_list = [value for (key,value) in json_stock_data.items()]
yesterday_close = float(value_list[1]['4. close'])
before_yesterday_close = float(value_list[2]['4. close'])
increase = round((yesterday_close-before_yesterday_close)/before_yesterday_close * 100,2)
if abs(increase) >= 5:
get_news_info()
print(json_stock_data)
Comments