Skip to content

Commit 4726a4e

Browse files
committed
ohlc notebook
1 parent 19d1798 commit 4726a4e

File tree

1 file changed

+51
-127
lines changed

1 file changed

+51
-127
lines changed

notebooks/ohlc-charts.md

Lines changed: 51 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -10,146 +10,96 @@ jupyter:
1010
display_name: Python 3
1111
language: python
1212
name: python3
13-
plotly:
14-
description: How to make interactive OHLC charts in Python with Plotly. Six examples
15-
of OHLC charts with Pandas, time series, and yahoo finance data.
16-
display_as: financial
17-
has_thumbnail: true
18-
ipynb: ~notebook_demo/53
19-
language: python
20-
layout: user-guide
21-
name: OHLC Charts
22-
order: 1
23-
page_type: example_index
24-
permalink: python/ohlc-charts/
25-
thumbnail: thumbnail/ohlc.jpg
2613
---
2714

28-
#### New to Plotly?
29-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
30-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
31-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
32-
#### Version Check
33-
Plotly's Python API is updated frequently. Run `pip install plotly --upgrade` to update your Plotly version.
15+
The [OHLC](https://en.wikipedia.org/wiki/Open-high-low-close_chart) chart (for open, high, low and close) is a style of financial chart describing open, high, low and close values for a given `x` coordinate (most likely time). The tip of the lines represent the `low` and `high` values and the horizontal segments represent the `open` and `close` values. Sample points where the close value is higher (lower) then the open value are called increasing (decreasing). By default, increasing items are drawn in green whereas decreasing are drawn in red.
3416

35-
```python
36-
import plotly
37-
plotly.__version__
38-
```
17+
See also [Candlestick Charts](https://plot.ly/python/candlestick-charts/) and [other financial charts](https://plot.ly/python/#financial-charts).
3918

40-
##### Simple OHLC Chart with Pandas
19+
#### Simple OHLC Chart with Pandas
4120

4221
```python
43-
import plotly.plotly as py
44-
import plotly.graph_objs as go
45-
22+
import plotly.graph_objects as go
4623
import pandas as pd
47-
from datetime import datetime
4824

4925
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
5026

51-
trace = go.Ohlc(x=df['Date'],
52-
open=df['AAPL.Open'],
53-
high=df['AAPL.High'],
54-
low=df['AAPL.Low'],
55-
close=df['AAPL.Close'])
56-
data = [trace]
57-
py.iplot(data, filename='simple_ohlc')
27+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
28+
open=df['AAPL.Open'],
29+
high=df['AAPL.High'],
30+
low=df['AAPL.Low'],
31+
close=df['AAPL.Close']))
32+
fig.show()
5833
```
5934

60-
##### OHLC Chart without Rangeslider
35+
#### OHLC Chart without Rangeslider
6136

6237
```python
63-
import plotly.plotly as py
64-
import plotly.graph_objs as go
38+
import plotly.graph_objects as go
6539

6640
import pandas as pd
67-
from datetime import datetime
41+
6842

6943
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
7044

71-
trace = go.Ohlc(x=df['Date'],
45+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
7246
open=df['AAPL.Open'],
7347
high=df['AAPL.High'],
7448
low=df['AAPL.Low'],
75-
close=df['AAPL.Close'])
76-
77-
layout = go.Layout(
78-
xaxis = dict(
79-
rangeslider = dict(
80-
visible = False
81-
)
82-
)
83-
)
84-
85-
data = [trace]
86-
87-
fig = go.Figure(data=data, layout=layout)
88-
py.iplot(fig, filename='OHLC without Rangeslider')
49+
close=df['AAPL.Close']))
50+
fig.update(layout_xaxis_rangeslider_visible=False)
51+
fig.show()
8952
```
9053

9154
#### Adding Customized Text and Annotations
9255

9356
```python
94-
import plotly.plotly as py
95-
import plotly.graph_objs as go
96-
97-
from datetime import datetime
57+
import plotly.graph_objects as go
9858
import pandas as pd
9959

10060
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
10161

102-
trace = go.Ohlc(x=df['Date'],
62+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
10363
open=df['AAPL.Open'],
10464
high=df['AAPL.High'],
10565
low=df['AAPL.Low'],
106-
close=df['AAPL.Close'])
107-
data = [trace]
108-
layout = {
109-
'title': 'The Great Recession',
110-
'yaxis': {'title': 'AAPL Stock'},
111-
'shapes': [{
112-
'x0': '2016-12-09', 'x1': '2016-12-09',
113-
'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
114-
'line': {'color': 'rgb(30,30,30)', 'width': 1}
115-
}],
116-
'annotations': [{
117-
'x': '2016-12-09', 'y': 0.05, 'xref': 'x', 'yref': 'paper',
118-
'showarrow': False, 'xanchor': 'left',
119-
'text': 'Increase Period Begins'
120-
}]
121-
}
122-
fig = dict(data=data, layout=layout)
123-
py.iplot(fig, filename='aapl-recession-ohlc')
66+
close=df['AAPL.Close']))
67+
68+
fig.update_layout(
69+
title='The Great Recession',
70+
yaxis_title='AAPL Stock',
71+
shapes = [dict(
72+
x0='2016-12-09', x1='2016-12-09', y0=0, y1=1, xref='x', yref='paper',
73+
line_width=2)],
74+
annotations=[dict(
75+
x='2016-12-09', y=0.05, xref='x', yref='paper',
76+
showarrow=False, xanchor='left', text='Increase Period Begins')]
77+
)
78+
79+
fig.show()
12480
```
12581

12682
#### Custom OHLC Colors
12783

12884
```python
129-
import plotly.plotly as py
130-
import plotly.graph_objs as go
131-
85+
import plotly.graph_objects as go
13286
import pandas as pd
133-
from datetime import datetime
13487

13588
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
13689

137-
trace = go.Ohlc(x=df['Date'],
138-
open=df['AAPL.Open'],
139-
high=df['AAPL.High'],
140-
low=df['AAPL.Low'],
141-
close=df['AAPL.Close'],
142-
increasing=dict(line=dict(color= '#17BECF')),
143-
decreasing=dict(line=dict(color= '#7F7F7F')))
144-
data = [trace]
145-
py.iplot(data, filename='styled_ohlc')
90+
fig = go.Figure(data=[go.Ohlc(
91+
x=df['Date'],
92+
open=df['AAPL.Open'], high=df['AAPL.High'],
93+
low=df['AAPL.Low'], close=df['AAPL.Close'],
94+
increasing_line_color= 'cyan', decreasing_line_color= 'gray'
95+
)])
96+
fig.show()
14697
```
14798

148-
##### Simple OHLC with `datetime` Objects
99+
#### Simple OHLC with `datetime` Objects
149100

150101
```python
151-
import plotly.plotly as py
152-
import plotly.graph_objs as go
102+
import plotly.graph_objects as go
153103

154104
from datetime import datetime
155105

@@ -163,20 +113,16 @@ dates = [datetime(year=2013, month=10, day=10),
163113
datetime(year=2014, month=1, day=10),
164114
datetime(year=2014, month=2, day=10)]
165115

166-
trace = go.Ohlc(x=dates,
167-
open=open_data,
168-
high=high_data,
169-
low=low_data,
170-
close=close_data)
171-
data = [trace]
172-
py.iplot(data, filename='ohlc_datetime')
116+
fig = go.Figure(data=[go.Ohlc(x=dates,
117+
open=open_data, high=high_data,
118+
low=low_data, close=close_data)])
119+
fig.show()
173120
```
174121

175122
### Custom Hovertext
176123

177124
```python
178-
import plotly.plotly as py
179-
import plotly.graph_objs as go
125+
import plotly.graph_objects as go
180126

181127
import pandas as pd
182128
from datetime import datetime
@@ -187,38 +133,16 @@ for i in range(len(df['AAPL.Open'])):
187133

188134
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
189135

190-
trace = go.Ohlc(x=df['Date'],
136+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
191137
open=df['AAPL.Open'],
192138
high=df['AAPL.High'],
193139
low=df['AAPL.Low'],
194140
close=df['AAPL.Close'],
195141
text=hovertext,
196-
hoverinfo='text')
197-
data = [trace]
198-
py.iplot(data, filename='ohlc_custom_hover')
142+
hoverinfo='text'))
143+
fig.show()
199144
```
200145

201146
#### Reference
202147
For more information on candlestick attributes, see: https://plot.ly/python/reference/#ohlc
203148

204-
```python
205-
from IPython.display import display, HTML
206-
207-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
208-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
209-
210-
!pip install git+https://github.com/plotly/publisher.git --upgrade
211-
import publisher
212-
publisher.publish(
213-
'ohlc-charts.ipynb', 'python/ohlc-charts/', 'Python OHLC Charts | plotly',
214-
'How to make interactive OHLC charts in Python with Plotly. '
215-
'Six examples of OHLC charts with Pandas, time series, and yahoo finance data.',
216-
name = 'OHLC Charts',
217-
thumbnail='thumbnail/ohlc.jpg', language='python',
218-
page_type='example_index', has_thumbnail='true', display_as='financial', order=1,
219-
ipynb= '~notebook_demo/53')
220-
```
221-
222-
```python
223-
224-
```

0 commit comments

Comments
 (0)