Create Candlestick
Tags: #plotly #chart #candlestick #group #dataviz #snippet
Author: Jeremy Ravenel

Input

Import libraries

1
import naas
2
from naas_drivers import yahoofinance
3
import plotly.graph_objects as go
4
import pandas as pd
Copied!

Variables

1
title = "Candlestick"
2
3
# Output paths
4
output_image = f"{title}.png"
5
output_html = f"{title}.html"
Copied!

Get data

1
date_from = -360 # Date can be number or date or today
2
date_to = "today"
3
df = yahoofinance.get("TSLA", date_from=date_from, date_to=date_to)
4
df
Copied!

Model

Create Candlestick

1
fig = go.Figure()
2
fig = go.Figure(data=[go.Candlestick(x=df['Date'],
3
open=df['Open'],
4
high=df['High'],
5
low=df['Low'],
6
close=df['Close'])])
7
8
fig.update_layout(
9
title=title,
10
plot_bgcolor="#ffffff",
11
width=1200,
12
height=800,
13
xaxis_tickfont_size=14,
14
yaxis=dict(
15
title='Price in #x27;,
16
titlefont_size=16,
17
tickfont_size=14,
18
)
19
)
20
config = {'displayModeBar': False}
21
fig.show(config=config)
Copied!

Output

Export in PNG and HTML

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

Generate shareable assets

1
link_image = naas.asset.add(output_image)
2
link_html = naas.asset.add(output_html, {"inline":True})
3
4
#-> Uncomment the line below to remove your assets
5
# naas.asset.delete(output_image)
6
# naas.asset.delete(output_html)
Copied!
Copy link
Edit on GitHub