Skip to content

Commit 950e03f

Browse files
authored
Merge pull request #24 from plotly/ohlc
Ohlc
2 parents 5a1f9ed + d60dd02 commit 950e03f

File tree

1 file changed

+53
-115
lines changed

1 file changed

+53
-115
lines changed

notebooks/ohlc-charts.md

Lines changed: 53 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -24,133 +24,98 @@ jupyter:
2424
page_type: example_index
2525
permalink: python/ohlc-charts/
2626
thumbnail: thumbnail/ohlc.jpg
27+
v4upgrade: true
28+
2729
---
2830

29-
#### New to Plotly?
30-
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/).
31-
<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).
32-
<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!
33-
#### Version Check
34-
Plotly's Python API is updated frequently. Run `pip install plotly --upgrade` to update your Plotly version.
31+
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.
3532

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

41-
##### Simple OHLC Chart with Pandas
35+
#### Simple OHLC Chart with Pandas
4236

4337
```python
44-
import plotly.plotly as py
45-
import plotly.graph_objs as go
46-
38+
import plotly.graph_objects as go
4739
import pandas as pd
48-
from datetime import datetime
4940

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

52-
trace = go.Ohlc(x=df['Date'],
53-
open=df['AAPL.Open'],
54-
high=df['AAPL.High'],
55-
low=df['AAPL.Low'],
56-
close=df['AAPL.Close'])
57-
data = [trace]
58-
py.iplot(data, filename='simple_ohlc')
43+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
44+
open=df['AAPL.Open'],
45+
high=df['AAPL.High'],
46+
low=df['AAPL.Low'],
47+
close=df['AAPL.Close']))
48+
fig.show()
5949
```
6050

61-
##### OHLC Chart without Rangeslider
51+
#### OHLC Chart without Rangeslider
6252

6353
```python
64-
import plotly.plotly as py
65-
import plotly.graph_objs as go
54+
import plotly.graph_objects as go
6655

6756
import pandas as pd
68-
from datetime import datetime
57+
6958

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

72-
trace = go.Ohlc(x=df['Date'],
61+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
7362
open=df['AAPL.Open'],
7463
high=df['AAPL.High'],
7564
low=df['AAPL.Low'],
76-
close=df['AAPL.Close'])
77-
78-
layout = go.Layout(
79-
xaxis = dict(
80-
rangeslider = dict(
81-
visible = False
82-
)
83-
)
84-
)
85-
86-
data = [trace]
87-
88-
fig = go.Figure(data=data, layout=layout)
89-
py.iplot(fig, filename='OHLC without Rangeslider')
65+
close=df['AAPL.Close']))
66+
fig.update(layout_xaxis_rangeslider_visible=False)
67+
fig.show()
9068
```
9169

9270
#### Adding Customized Text and Annotations
9371

9472
```python
95-
import plotly.plotly as py
96-
import plotly.graph_objs as go
97-
98-
from datetime import datetime
73+
import plotly.graph_objects as go
9974
import pandas as pd
10075

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

103-
trace = go.Ohlc(x=df['Date'],
78+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
10479
open=df['AAPL.Open'],
10580
high=df['AAPL.High'],
10681
low=df['AAPL.Low'],
107-
close=df['AAPL.Close'])
108-
data = [trace]
109-
layout = {
110-
'title': 'The Great Recession',
111-
'yaxis': {'title': 'AAPL Stock'},
112-
'shapes': [{
113-
'x0': '2016-12-09', 'x1': '2016-12-09',
114-
'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
115-
'line': {'color': 'rgb(30,30,30)', 'width': 1}
116-
}],
117-
'annotations': [{
118-
'x': '2016-12-09', 'y': 0.05, 'xref': 'x', 'yref': 'paper',
119-
'showarrow': False, 'xanchor': 'left',
120-
'text': 'Increase Period Begins'
121-
}]
122-
}
123-
fig = dict(data=data, layout=layout)
124-
py.iplot(fig, filename='aapl-recession-ohlc')
82+
close=df['AAPL.Close']))
83+
84+
fig.update_layout(
85+
title='The Great Recession',
86+
yaxis_title='AAPL Stock',
87+
shapes = [dict(
88+
x0='2016-12-09', x1='2016-12-09', y0=0, y1=1, xref='x', yref='paper',
89+
line_width=2)],
90+
annotations=[dict(
91+
x='2016-12-09', y=0.05, xref='x', yref='paper',
92+
showarrow=False, xanchor='left', text='Increase Period Begins')]
93+
)
94+
95+
fig.show()
12596
```
12697

12798
#### Custom OHLC Colors
12899

129100
```python
130-
import plotly.plotly as py
131-
import plotly.graph_objs as go
132-
101+
import plotly.graph_objects as go
133102
import pandas as pd
134-
from datetime import datetime
135103

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

138-
trace = go.Ohlc(x=df['Date'],
139-
open=df['AAPL.Open'],
140-
high=df['AAPL.High'],
141-
low=df['AAPL.Low'],
142-
close=df['AAPL.Close'],
143-
increasing=dict(line=dict(color= '#17BECF')),
144-
decreasing=dict(line=dict(color= '#7F7F7F')))
145-
data = [trace]
146-
py.iplot(data, filename='styled_ohlc')
106+
fig = go.Figure(data=[go.Ohlc(
107+
x=df['Date'],
108+
open=df['AAPL.Open'], high=df['AAPL.High'],
109+
low=df['AAPL.Low'], close=df['AAPL.Close'],
110+
increasing_line_color= 'cyan', decreasing_line_color= 'gray'
111+
)])
112+
fig.show()
147113
```
148114

149-
##### Simple OHLC with `datetime` Objects
115+
#### Simple OHLC with `datetime` Objects
150116

151117
```python
152-
import plotly.plotly as py
153-
import plotly.graph_objs as go
118+
import plotly.graph_objects as go
154119

155120
from datetime import datetime
156121

@@ -164,20 +129,16 @@ dates = [datetime(year=2013, month=10, day=10),
164129
datetime(year=2014, month=1, day=10),
165130
datetime(year=2014, month=2, day=10)]
166131

167-
trace = go.Ohlc(x=dates,
168-
open=open_data,
169-
high=high_data,
170-
low=low_data,
171-
close=close_data)
172-
data = [trace]
173-
py.iplot(data, filename='ohlc_datetime')
132+
fig = go.Figure(data=[go.Ohlc(x=dates,
133+
open=open_data, high=high_data,
134+
low=low_data, close=close_data)])
135+
fig.show()
174136
```
175137

176138
### Custom Hovertext
177139

178140
```python
179-
import plotly.plotly as py
180-
import plotly.graph_objs as go
141+
import plotly.graph_objects as go
181142

182143
import pandas as pd
183144
from datetime import datetime
@@ -188,38 +149,15 @@ for i in range(len(df['AAPL.Open'])):
188149

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

191-
trace = go.Ohlc(x=df['Date'],
152+
fig = go.Figure(data=go.Ohlc(x=df['Date'],
192153
open=df['AAPL.Open'],
193154
high=df['AAPL.High'],
194155
low=df['AAPL.Low'],
195156
close=df['AAPL.Close'],
196157
text=hovertext,
197-
hoverinfo='text')
198-
data = [trace]
199-
py.iplot(data, filename='ohlc_custom_hover')
158+
hoverinfo='text'))
159+
fig.show()
200160
```
201161

202162
#### Reference
203163
For more information on candlestick attributes, see: https://plot.ly/python/reference/#ohlc
204-
205-
```python
206-
from IPython.display import display, HTML
207-
208-
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" />'))
209-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
210-
211-
!pip install git+https://github.com/plotly/publisher.git --upgrade
212-
import publisher
213-
publisher.publish(
214-
'ohlc-charts.ipynb', 'python/ohlc-charts/', 'Python OHLC Charts | plotly',
215-
'How to make interactive OHLC charts in Python with Plotly. '
216-
'Six examples of OHLC charts with Pandas, time series, and yahoo finance data.',
217-
name = 'OHLC Charts',
218-
thumbnail='thumbnail/ohlc.jpg', language='python',
219-
page_type='example_index', has_thumbnail='true', display_as='financial', order=1,
220-
ipynb= '~notebook_demo/53')
221-
```
222-
223-
```python
224-
225-
```

0 commit comments

Comments
 (0)