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

Input

Import libraries

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

Input ticker and dates

1
ticker = "ABNB"
2
date_from = -900 # 1OO days max to feed the naas_driver for prediction
3
date_to = "today"
4
data_point = 20
Copied!

Input Slack token and channel

1
token = "your_token_number"
2
channel = "your_channel_name"
Copied!

Model

Get the data 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!

Make prediction chart

Predict datapoints
1
df_predict = prediction.get(dataset=df_yahoo,
2
date_column='Date',
3
data_points=20,
4
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!
Display predicted 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!

Calculate daily variations

1
df_yahoo = df_yahoo.sort_values("Date", ascending=False).reset_index(drop=True)
Copied!
Data now
1
DATA_NOW = df_yahoo.loc[0, "Close"]
2
DATA_NOW
Copied!
Data yesterday
1
DATA_YESTERDAY = df_yahoo.loc[1, "Close"]
2
DATA_YESTERDAY
Copied!
Calculate daily variations
1
VARV = DATA_NOW - DATA_YESTERDAY
2
VARV = "{:+,.2f}".format(VARV)
3
VARV
Copied!
1
VARP = ((DATA_NOW - DATA_YESTERDAY) / DATA_NOW)*100
2
VARP = "{:+,.2f}".format(VARP)
3
VARP
Copied!
Display current values
1
DATA_NOW = round(DATA_NOW, 1)
2
DATA_NOW = "${:,.2f}".format(DATA_NOW)
3
DATA_NOW
Copied!
1
DATA_YESTERDAY = round(DATA_YESTERDAY, 1)
2
DATA_YESTERDAY = "${:,.2f}".format(DATA_YESTERDAY)
3
DATA_YESTERDAY
Copied!

Output

Save chart as png and html

1
chart.write_html(f"{ticker}.html")
2
chart.write_image(f"{ticker}.png", width=1200)
Copied!

Expose chart

1
link_image = naas.asset.add(f"{ticker}.png")
2
link_html = naas.asset.add(f"{ticker}.html", {"inline":True})
Copied!

Create markdown template

1
%%writefile message.md
2
Hey <!here>
3
4
The *TICKER* price is *DATA_NOW* right now, VARV vs yesterday (VARP%).
5
Yesterday close : DATA_YESTERDAY
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
<link_html |Open dynamic chart>
Copied!
1
markdown_file = "message.md"
2
md = open(markdown_file, "r").read()
3
md
Copied!

Replace values in template

1
post = md.replace("DATA_NOW", str(DATA_NOW))
2
post = post.replace("TICKER", str(ticker))
3
post = post.replace("DATA_YESTERDAY", str(DATA_YESTERDAY))
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_html", str(link_html))
11
post
Copied!

Post on Slack

1
message = post
2
image = link_image
3
slack.connect(token).send(channel, post, link_image)
Copied!

Add email template as a dependency

1
## add as a dependency
2
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