-
Notifications
You must be signed in to change notification settings - Fork 391
/
email_stock (1).py
65 lines (45 loc) · 1.3 KB
/
email_stock (1).py
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
import os
import smtplib
import imghdr
from email.message import EmailMessage
import yfinance as yf
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import time
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
msg = EmailMessage()
yf.pdr_override()
start =dt.datetime(2018,12,1)
now = dt.datetime.now()
stock="QQQ"
TargetPrice = 180
msg['Subject'] = 'Alert on '+ stock+'!'
msg['From'] = ''
msg['To'] = ''
alerted=False
while 1:
df = pdr.get_data_yahoo(stock, start, now)
currentClose=df["Adj Close"][-1]
condition=currentClose>TargetPrice
if(condition and alerted==False):
alerted=True
message=stock +" Has activated the alert price of "+ str(TargetPrice) +\
"\nCurrent Price: "+ str(currentClose)
print(message)
msg.set_content(message)
files=[r""]
for file in files:
with open(file,'rb') as f:
file_data=f.read()
file_name=""
msg.add_attachment(file_data, maintype="application",
subtype='ocetet-stream', filename=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
print("completed")
else:
print("No new alerts")
time.sleep(60)