Get Stock Update
Tags: #yahoofinance #usdinr #plotly
Author: Megha Gupta
Description: With this template you will get INR USD rate visualized on a chart

Input

Import Libraries

1
import naas
2
from naas_drivers import yahoofinance, plotly
3
import markdown2
4
from IPython.display import Markdown as md
Copied!

Setup Yahoo parameters

👉 Here you can input:
  • yahoo ticker : get tickers here
  • date from
  • date to
1
TICKER = 'INR=X'
2
date_from = -30
3
date_to = 'today'
Copied!

Setup your 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 the data from yahoo finance using naas drivers

1
#data cleaning
2
df = yahoofinance.get(TICKER, date_from=date_from, date_to = date_to)
3
df = df.dropna()# drop the na values from the dataframe
4
df.reset_index(drop=True)
5
df = df.sort_values("Date", ascending=False).reset_index(drop=True)
6
df.head()
Copied!

Extract value from data

1
LASTOPEN = round(df.loc[0, "Open"], 2)
2
LASTCLOSE = round(df.loc[0, "Close"], 2)
3
YESTERDAYOPEN = round(df.loc[1, "Open"], 2)
4
YESTERDAYCLOSE = round(df.loc[1, "Close"], 2)
5
MAXRATE = round(df['Open'].max(),2)
6
MXDATEOPEN = df.loc[df['Open'].idxmax(), "Date"].strftime("%Y-%m-%d")
7
MINRATE = round(df['Open'].min(),2)
8
MNDATEOPEN = df.loc[df['Open'].idxmin(), "Date"].strftime("%Y-%m-%d")
Copied!

Plot the data

1
last_date = df.loc[df.index[0], "Date"].strftime("%Y-%m-%d")
2
3
output = plotly.linechart(df,
4
x="Date",
5
y=['Open','Close'],
6
title=f"<b>INR USD rates of last month</b><br><span style='font-size: 13px;'>Last value as of {last_date}: Open={LASTOPEN}, Close={LASTCLOSE}</span>")
Copied!

Output

Save the dataset in csv

1
df.to_csv(f"{TICKER}_LastMonth.csv", index=False)
Copied!

Create markdown template

1
%%writefile message.md
2
Hello world,
3
4
The **TICKER** price is Open LASTOPEN and Close LASTCLOSE right now. <br>
5
**Yesterday Open**: YESTERDAYOPEN <br>
6
**Yesterday Close**: YESTERDAYCLOSE <br>
7
The Max Open rate of **TICKER** was on MXDATEOPEN which was MAXRATE. <br>
8
The Min Open rate of **TICKER** was on MNDATEOPEN which was MINRATE. <br>
9
10
Attached is the excel file for your reference. <br>
11
12
Have a nice day.
13
<br>
14
15
PS: You can [send the email again](link_webhook) if you need a fresh update.<br>
16
<div><strong>Full Name</strong></div>
17
<div>Open source lover | <a href="http://www.naas.ai/" target="_blank">Naas</a></div>
18
<div>+ 33 1 23 45 67 89</div>
19
<div><small>This is an automated email from my Naas account</small></div>
Copied!

Add email template as dependency

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

Replace values in template

1
markdown_file = "message.md"
2
content = open(markdown_file, "r").read()
3
md = markdown2.markdown(content)
4
md
Copied!
1
post = md.replace("LASTOPEN", str(LASTOPEN))
2
post = post.replace("LASTCLOSE", str(LASTCLOSE))
3
post = post.replace("YESTERDAYOPEN", str(YESTERDAYOPEN))
4
post = post.replace("YESTERDAYCLOSE", str(YESTERDAYCLOSE))
5
post = post.replace("MXDATEOPEN", str(MXDATEOPEN))
6
post = post.replace("MAXRATE", str(MAXRATE))
7
post = post.replace("MNDATEOPEN", str(MNDATEOPEN))
8
post = post.replace("MINRATE", str(MINRATE))
9
post = post.replace("TICKER", str(TICKER))
10
post
Copied!

Add webhook to run your notebook again

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

Send by email

1
subject = f"📈 {TICKER} Open and close rates as of today"
2
content = post
3
files = [f"{TICKER}_LastMonth.csv"]
4
5
naas.notification.send(email_to=email_to,
6
subject=subject,
7
html=content,
8
email_from=email_from,
9
files=files)
Copied!

Schedule your notebook

Please uncomment and run the cell below to schedule your notebook everyday at 8:00 during business days
1
# import naas
2
# naas.scheduler.add("0 8 1-5 * *")
Copied!
Copy link
Edit on GitHub