-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (79 loc) · 3.74 KB
/
main.py
File metadata and controls
97 lines (79 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import requests
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
STOCK_NAME = "TSLA"
COMPANY_NAME = "Tesla Inc"
STOCK_ENDPOINT = "https://www.alphavantage.co/query"
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"
# Retrieve sensitive information from environment variables
news_api_key = os.getenv('NEWS_API_KEY')
stock_api_key = os.getenv('STOCK_API_KEY')
EMAIL_SENDER = os.getenv('EMAIL_SENDER')
EMAIL_RECEIVER = os.getenv('EMAIL_RECEIVER')
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
# Threshold for percentage change
percentage_threshold = 5
# Fetching Stock Data
parameters = {
"function": "TIME_SERIES_INTRADAY",
"symbol": STOCK_NAME,
"interval": "5min", # You can use "1min", "5min", "15min", "30min", or "60min"
"apikey": stock_api_key
}
response = requests.get(STOCK_ENDPOINT, params=parameters)
response.raise_for_status()
stock_data = response.json()
# Ensure the 'Time Series (5min)' key is present
if "Time Series (5min)" in stock_data:
time_series = stock_data["Time Series (5min)"]
# Get and sort timestamps
timestamps = list(time_series.keys())
timestamps.sort(reverse=True) # Sort in descending order to get the latest times first
# Check if there are at least two timestamps
if len(timestamps) >= 2:
latest_time = timestamps[0]
previous_time = timestamps[1]
latest_closing_price = time_series[latest_time]["4. close"]
previous_closing_price = time_series[previous_time]["4. close"]
print(f"Latest closing price: {latest_closing_price} at {latest_time}")
print(f"Previous closing price: {previous_closing_price} at {previous_time}")
# Calculate the percentage change
difference = abs(float(latest_closing_price) - float(previous_closing_price))
percentage = (difference / float(latest_closing_price)) * 100
# Check if the percentage change exceeds the threshold value
if percentage > percentage_threshold * 100: # Convert percentage_threshold to percentage
parameters1 = {
"q": "Tesla",
"apiKey": news_api_key,
}
response = requests.get(NEWS_ENDPOINT, params=parameters1)
response.raise_for_status()
news_data = response.json()
articles = news_data["articles"]
# Prepare email content
news_content = "News Articles:\n"
for article in articles:
news_content += f"Title: {article['title']}\n"
news_content += f"Description: {article['description']}\n"
news_content += f"URL: {article['url']}\n\n"
# Create email content for testing
msg = MIMEMultipart()
msg['From'] = EMAIL_SENDER
msg['To'] = EMAIL_RECEIVER
msg['Subject'] = f"Stock Price Alert: {STOCK_NAME}"
body = f"Stock price for {COMPANY_NAME} has changed by more than {percentage_threshold * 100}%.\n\n"
body += f"Latest closing price: {latest_closing_price} at {latest_time}\n"
body += f"Previous closing price: {previous_closing_price} at {previous_time}\n\n"
body += news_content
msg.attach(MIMEText(body, 'plain'))
# Print the email content for testing
print("\n--- Email Content ---")
print(msg.as_string())
print("----------------------")
else:
print(f"Percentage change is within the acceptable range of {percentage_threshold * 100}%.")
else:
print("Not enough data to compare latest and previous closing prices.")
else:
print("No 'Time Series (5min)' data found.")