@@ -24,133 +24,98 @@ jupyter:
24
24
page_type : example_index
25
25
permalink : python/ohlc-charts/
26
26
thumbnail : thumbnail/ohlc.jpg
27
+ v4upgrade : true
28
+
27
29
---
28
30
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.
35
32
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 ) .
40
34
41
- ##### Simple OHLC Chart with Pandas
35
+ #### Simple OHLC Chart with Pandas
42
36
43
37
``` python
44
- import plotly.plotly as py
45
- import plotly.graph_objs as go
46
-
38
+ import plotly.graph_objects as go
47
39
import pandas as pd
48
- from datetime import datetime
49
40
50
41
df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' )
51
42
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()
59
49
```
60
50
61
- ##### OHLC Chart without Rangeslider
51
+ #### OHLC Chart without Rangeslider
62
52
63
53
``` python
64
- import plotly.plotly as py
65
- import plotly.graph_objs as go
54
+ import plotly.graph_objects as go
66
55
67
56
import pandas as pd
68
- from datetime import datetime
57
+
69
58
70
59
df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' )
71
60
72
- trace = go.Ohlc(x = df[' Date' ],
61
+ fig = go.Figure( data = go.Ohlc(x = df[' Date' ],
73
62
open = df[' AAPL.Open' ],
74
63
high = df[' AAPL.High' ],
75
64
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()
90
68
```
91
69
92
70
#### Adding Customized Text and Annotations
93
71
94
72
``` 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
99
74
import pandas as pd
100
75
101
76
df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' )
102
77
103
- trace = go.Ohlc(x = df[' Date' ],
78
+ fig = go.Figure( data = go.Ohlc(x = df[' Date' ],
104
79
open = df[' AAPL.Open' ],
105
80
high = df[' AAPL.High' ],
106
81
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()
125
96
```
126
97
127
98
#### Custom OHLC Colors
128
99
129
100
``` python
130
- import plotly.plotly as py
131
- import plotly.graph_objs as go
132
-
101
+ import plotly.graph_objects as go
133
102
import pandas as pd
134
- from datetime import datetime
135
103
136
104
df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' )
137
105
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()
147
113
```
148
114
149
- ##### Simple OHLC with ` datetime ` Objects
115
+ #### Simple OHLC with ` datetime ` Objects
150
116
151
117
``` python
152
- import plotly.plotly as py
153
- import plotly.graph_objs as go
118
+ import plotly.graph_objects as go
154
119
155
120
from datetime import datetime
156
121
@@ -164,20 +129,16 @@ dates = [datetime(year=2013, month=10, day=10),
164
129
datetime(year = 2014 , month = 1 , day = 10 ),
165
130
datetime(year = 2014 , month = 2 , day = 10 )]
166
131
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()
174
136
```
175
137
176
138
### Custom Hovertext
177
139
178
140
``` python
179
- import plotly.plotly as py
180
- import plotly.graph_objs as go
141
+ import plotly.graph_objects as go
181
142
182
143
import pandas as pd
183
144
from datetime import datetime
@@ -188,38 +149,15 @@ for i in range(len(df['AAPL.Open'])):
188
149
189
150
df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' )
190
151
191
- trace = go.Ohlc(x = df[' Date' ],
152
+ fig = go.Figure( data = go.Ohlc(x = df[' Date' ],
192
153
open = df[' AAPL.Open' ],
193
154
high = df[' AAPL.High' ],
194
155
low = df[' AAPL.Low' ],
195
156
close = df[' AAPL.Close' ],
196
157
text = hovertext,
197
- hoverinfo = ' text' )
198
- data = [trace]
199
- py.iplot(data, filename = ' ohlc_custom_hover' )
158
+ hoverinfo = ' text' ))
159
+ fig.show()
200
160
```
201
161
202
162
#### Reference
203
163
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