Send daily prediction to Email
With this template, you can create daily email prediction bot on any ticker available in Yahoo finance.
Tags: #yahoofinance #trading #markdown #prediction #plotly #slack #naas_drivers #scheduler #notification #asset #webhook #dependency #naas
Author: Jeremy Ravenel

Input

Import libraries

1
import naas
2
from naas_drivers import prediction, yahoofinance, plotly, slack
3
import markdown2
Copied!

Input ticker and dates

👉 Here you can change the ticker and timeframe
1
ticker = "TSLA"
2
date_from = -100 # 1OO days max to feed the naas_driver for prediction
3
date_to = "today"
4
data_point = 20
5
6
# Output paths image and html
7
output_image = f"{ticker}.png"
8
output_html = f"{ticker}.html"
Copied!

Input email parameters

👉 Here you can input your sender email and destination email
Note: emails are sent from [email protected] by default
1
email_to = ["[email protected]"]
2
email_from = None
Copied!

Model

Get dataset from Yahoo Finance

1
df_yahoo = yahoofinance.get(ticker, date_from=date_from, date_to=date_to)
2
3
# clean df
4
df_yahoo = df_yahoo.dropna()
5
df_yahoo.reset_index(drop=True)
6
df_yahoo.head()
Copied!

Add prediction columns

1
df_predict = prediction.get(dataset=df_yahoo,
2
date_column='Date',
3
column="Close",
4
data_points=data_point,
5
prediction_type="all")
Copied!
1
df_predict = df_predict.sort_values("Date", ascending=False).reset_index(drop=True)
2
df_predict.head(30)
Copied!

Build chart

1
chart = plotly.linechart(df_predict,
2
x="Date",
3
y=["Close", "ARIMA", "SVR", "LINEAR", "COMPOUND"],
4
showlegend=True,
5
title=f"{ticker} predictions as of today, for next {data_point} days.")
Copied!

Set daily variations values

1
df_yahoo = df_yahoo.sort_values("Date", ascending=False).reset_index(drop=True)
Copied!
1
DATANOW = df_yahoo.loc[0, "Close"]
2
DATANOW
Copied!
1
DATAYESTERDAY = df_yahoo.loc[1, "Close"]
2
DATAYESTERDAY
Copied!
1
VARV = DATANOW - DATAYESTERDAY
2
VARV = "{:+,.2f}".format(VARV)
3
VARV
Copied!
1
VARP = ((DATANOW - DATAYESTERDAY) / DATANOW)*100
2
VARP = "{:+,.2f}".format(VARP)
3
VARP
Copied!

Format values

1
ARIMA = df_predict.loc[0, "ARIMA"]
2
ARIMA = round(ARIMA, 1)
3
ARIMA = "${:,.2f}".format(ARIMA)
4
ARIMA
Copied!
1
SVR = df_predict.loc[0, "SVR"]
2
SVR = round(SVR, 1)
3
SVR = "${:,.2f}".format(SVR)
4
SVR
Copied!
1
LINEAR = df_predict.loc[0, "LINEAR"]
2
LINEAR = round(LINEAR, 1)
3
LINEAR = "${:,.2f}".format(LINEAR)
4
LINEAR
Copied!
1
COMPOUND = df_predict.loc[0, "COMPOUND"]
2
COMPOUND = round(COMPOUND, 1)
3
COMPOUND = "${:,.2f}".format(COMPOUND)
4
COMPOUND
Copied!
1
DATANOW = round(DATANOW, 1)
2
DATANOW = "${:,.2f}".format(DATANOW)
3
DATANOW
Copied!
1
DATAYESTERDAY = round(DATAYESTERDAY, 1)
2
DATAYESTERDAY = "${:,.2f}".format(DATAYESTERDAY)
3
DATAYESTERDAY
Copied!

Output

Save data in Excel

1
df_predict.to_excel(f"{ticker}_TODAY.xlsx")
Copied!

Save chart in png and html

1
chart.write_image(output_image, width=1200)
2
chart.write_html(output_html)
Copied!

Expose chart

1
link_image = naas.asset.add(output_image)
2
link_html = naas.asset.add(output_html, {"inline":True})
Copied!

Add webhook to run your notebook again

1
link_webhook = naas.webhook.add()
Copied!

Create markdown template

1
%%writefile message.md
2
Hello world,
3
4
The **TICKER** price is **DATANOW** right now, VARV vs yesterday (VARP%).<br>
5
Yesterday close : DATAYESTERDAY
6
7
In +20 days, basic ML models predict the following prices:
8
9
- **arima**: ARIMA
10
- **svr**: SVR
11
- **linear**: LINEAR
12
- **compound**: COMPOUND
13
14
<img href=link_html target="_blank" src=link_image style="width:640px; height:360px;" /><br>
15
[Open dynamic chart](link_html)<br>
16
17
Please find attached the data in Excel.<br>
18
19
Have a nice day.
20
<br>
21
22
PS: You can [send the email again](link_webhook) if you need a fresh update.<br>
23
<div><strong>Full Name</strong></div>
24
<div>Open source lover | <a href="http://www.naas.ai/" target="_blank">Naas</a></div>
25
<div>+ 33 1 23 45 67 89</div>
26
<div><small>This is an automated email from my Naas account</small></div>
Copied!
1
markdown_file = "message.md"
2
content = open(markdown_file, "r").read()
3
md = markdown2.markdown(content)
4
md
Copied!

Replace values in template

1
post = md.replace("DATANOW", str(DATANOW))
2
post = post.replace("TICKER", str(ticker))
3
post = post.replace("DATAYESTERDAY", str(DATAYESTERDAY))
4
post = post.replace("VARV", str(VARV))
5
post = post.replace("VARP", str(VARP))
6
post = post.replace("LINEAR", str(LINEAR))
7
post = post.replace("SVR", str(SVR))
8
post = post.replace("COMPOUND", str(COMPOUND))
9
post = post.replace("ARIMA", str(ARIMA))
10
post = post.replace("link_image", str(link_image))
11
post = post.replace("link_html", str(link_html))
12
post = post.replace("link_webhook", str(link_webhook))
13
post
Copied!

Send by Email

1
subject = f"📈 {ticker} predictions as of today"
2
content = post
3
files = [f"{ticker}_TODAY.xlsx"]
4
5
naas.notification.send(email_to=email_to,
6
subject=subject,
7
html=content,
8
files=files,
9
email_from=email_from)
Copied!

Add email template as a dependency

1
naas.dependency.add("message.md")
Copied!

Schedule every day

1
naas.scheduler.add(cron="0 9 * * *")
2
3
# naas.scheduler.delete() #if you want to delete the scheduler
Copied!
Copy link
Edit on GitHub