@@ -2257,55 +2257,84 @@ def create_ohlc(open, high, low, close,
2257
2257
2258
2258
:rtype (dict): returns a representation of an ohlc chart figure.
2259
2259
2260
- Example 1: Plot simple ohlc chart
2260
+ Example 1: Simple OHLC chart from a Pandas DataFrame
2261
2261
```
2262
2262
import plotly.plotly as py
2263
- import plotly.tools as tls
2264
- from plotly.graph_objs import *
2263
+ from plotly.tools import FigureFactory as FF
2265
2264
2266
- # Add data
2267
- open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
2268
- high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
2269
- low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
2270
- close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
2265
+ import pandas.io.data as web
2271
2266
2272
- # Create ohlc
2273
- ohlc = FigureFactory.create_ohlc(open_data, high_data,
2274
- low_data, close_data)
2267
+ df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
2268
+ fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)
2275
2269
2276
- # Plot
2277
- py.plot(ohlc, filename='simple ohlc', validate=False)
2270
+ py.plot(fig, filename='finance/aapl-ohlc')
2278
2271
```
2279
2272
2280
- Example 2: Plot ohlc chart with date labels
2273
+ Example 2: Add text and annotations to the OHLC chart
2281
2274
```
2282
2275
import plotly.plotly as py
2283
- import plotly.tools as tls
2284
- from plotly.graph_objs import *
2276
+ from plotly.tools import FigureFactory as FF
2277
+
2278
+ import pandas.io.data as web
2279
+
2280
+ df = web.DataReader("aapl", 'yahoo', datetime(2008, 8, 15), datetime(2008, 10, 15))
2281
+ fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index)
2282
+
2283
+ # Update the fig - all options here: https://plot.ly/python/reference/#Layout
2284
+ fig['layout'].update({
2285
+ 'title': 'The Great Recession',
2286
+ 'yaxis': {'title': 'AAPL Stock'},
2287
+ 'shapes': [{
2288
+ 'x0': '2008-09-15', 'x1': '2008-09-15', 'type': 'line',
2289
+ 'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
2290
+ 'line': {'color': 'rgb(40,40,40)', 'width': 0.5}
2291
+ }],
2292
+ 'annotations': [{
2293
+ 'text': "the fall of Lehman Brothers",
2294
+ 'x': '2008-09-15', 'y': 1.02,
2295
+ 'xref': 'x', 'yref': 'paper',
2296
+ 'showarrow': False, 'xanchor': 'left'
2297
+ }]
2298
+ })
2299
+
2300
+ py.plot(fig, filename='finance/aapl-recession-ohlc', validate=False)
2301
+ ```
2285
2302
2303
+ Example 3: Customize the OHLC colors
2304
+ ```
2305
+ import plotly.plotly as py
2306
+ from plotly.tools import FigureFactory as FF
2307
+ from plotly.graph_objs import Line, Marker
2286
2308
from datetime import datetime
2287
2309
2288
- # Add data
2289
- open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
2290
- high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
2291
- low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
2292
- close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
2293
- dates = [datetime(year=2013, month=10, day=10),
2294
- datetime(year=2013, month=11, day=10),
2295
- datetime(year=2013, month=12, day=10),
2296
- datetime(year=2014, month=1, day=10),
2297
- datetime(year=2014, month=2, day=10)]
2310
+ import pandas.io.data as web
2298
2311
2299
- # Create ohlc
2300
- ohlc = tls.FigureFactory.create_ohlc(open_data, high_data,
2301
- low_data, close_data,
2302
- dates=dates)
2312
+ df = web.DataReader("aapl", 'yahoo', datetime(2008, 1, 1), datetime(2009, 4, 1))
2313
+
2314
+ # Make increasing ohlc sticks and customize their color and name
2315
+ fig_increasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
2316
+ direction='increasing', name='AAPL',
2317
+ line=Line(color='rgb(150, 200, 250)'))
2318
+
2319
+ # Make decreasing ohlc sticks and customize their color and name
2320
+ fig_decreasing = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index,
2321
+ direction='decreasing',
2322
+ line=Line(color='rgb(128, 128, 128)'))
2323
+
2324
+ # Initialize the figure
2325
+ fig = fig_increasing
2303
2326
2304
- py.plot(ohlc, filename='ohlc with dates', validate=False)
2327
+ # Add decreasing data with .extend()
2328
+ fig['data'].extend(fig_decreasing['data'])
2329
+
2330
+ py.iplot(fig, filename='finance/aapl-ohlc-colors', validate=False)
2305
2331
```
2306
2332
2307
- Example 3: Plot ohlc chart with Title
2333
+ Example 4: OHLC chart with datetime objects
2308
2334
```
2335
+ import plotly.plotly as py
2336
+ from plotly.tools import FigureFactory as FF
2337
+
2309
2338
from datetime import datetime
2310
2339
2311
2340
# Add data
@@ -2320,101 +2349,10 @@ def create_ohlc(open, high, low, close,
2320
2349
datetime(year=2014, month=2, day=10)]
2321
2350
2322
2351
# Create ohlc
2323
- ohlc = tls.FigureFactory.create_ohlc(open_data, high_data,
2324
- low_data, close_data,
2325
- dates=dates)
2326
-
2327
- # Customize layout by using .update()
2328
- fig = ohlc
2329
- fig['layout'].update(title = 'OHLC Chart')
2330
-
2331
- py.plot(fig, filename='ohlc with title', validate=False)
2332
- ```
2333
-
2334
- Example 4: OHLC with increasing vs decreasing keyword arguments
2335
- ```
2336
- from datetime import datetime
2337
-
2338
- # Add Data
2339
- high_data = [33.2, 33.3750, 33.6250, 33.2500, 34.1880, 33.2, 33.3750,
2340
- 33.6250, 33.2500, 34.1880]
2341
- low_data = [32.7, 32.7500, 32.8750, 32.6250, 32.8130, 32.7, 32.7500,
2342
- 32.8750, 32.6250, 32.8130,]
2343
- close_data = [33.1, 32.9380, 33.3750, 33.1880, 33.1880, 33.1, 32.9380,
2344
- 33.3750, 33.1880, 33.1880]
2345
- open_data = [33.0, 33.3125, 33.5000, 33.0625, 34.1250, 33.0, 33.3125,
2346
- 33.5000, 33.0625, 34.1250]
2347
- x = [datetime(year=2013, month=3, day=4),
2348
- datetime(year=2013, month=6, day=5),
2349
- datetime(year=2013, month=9, day=6),
2350
- datetime(year=2014, month=3, day=4),
2351
- datetime(year=2014, month=6, day=5),
2352
- datetime(year=2014, month=9, day=4),
2353
- datetime(year=2015, month=3, day=5),
2354
- datetime(year=2015, month=6, day=6),
2355
- datetime(year=2015, month=9, day=4),
2356
- datetime(year=2016, month=3, day=5)]
2357
-
2358
- # Make increasing ohlc sticks and set kwargs
2359
- ohlc_incr = FigureFactory.create_ohlc(open_data,
2360
- high_data,
2361
- low_data,
2362
- close_data,
2363
- dates=x,
2364
- direction='increasing',
2365
- name='XYZ Increasing',
2366
- line=Line(color='rgb(150, 200,
2367
- 250)'))
2368
-
2369
- # Make decreasing ohlc sticks and set kwargs
2370
- ohlc_decr = FigureFactory.create_ohlc(open_data,
2371
- high_data,
2372
- low_data,
2373
- close_data,
2374
- dates=x,
2375
- direction='decreasing',
2376
- name='XYZ Decreasing',
2377
- showlegend=True,
2378
- line=Line(color='rgb(128, 128,
2379
- 128)'))
2380
-
2381
- # Set figure with increasing data and layout
2382
- fig = ohlc_incr
2383
-
2384
- # Add decreasing data with .extend()
2385
- fig['data'].extend(ohlc_decr['data'])
2386
-
2387
- # Plot!
2388
- py.iplot(fig, filename='ohlc keywords', validate=False)
2389
-
2390
- ```
2391
-
2392
- Example 5: Plot OHLC with Pandas
2393
- ```
2394
- import pandas.io.data as web
2395
- import pandas as pd
2396
- from datetime import datetime
2397
-
2398
- # Get Data
2399
- start = datetime(2010, 1, 1)
2400
- end = datetime(2012, 1, 27)
2401
- df = web.DataReader("aapl", 'yahoo', start, end)
2402
-
2403
- # Get Dates
2404
- datelist = (pd.date_range(datetime(2010, 1, 1),
2405
- periods=len(df.Open)).tolist())
2406
- d=[]
2407
- for i in range(len(datelist)):
2408
- d.append(datelist[i].to_datetime())
2409
-
2410
- # Make OHLC
2411
- ohlc_panda = tls.FigureFactory.create_ohlc(df.Open, df.High,
2412
- df.Low, df.Close,
2413
- dates=d)
2414
-
2415
- # Plot
2416
- py.plot(ohlc_panda, filename='ohlc with pandas', validate=False)
2352
+ fig = FF.create_ohlc(open_data, high_data,
2353
+ low_data, close_data, dates=dates)
2417
2354
2355
+ py.iplot(fig, filename='finance/simple-ohlc', validate=False)
2418
2356
```
2419
2357
"""
2420
2358
if dates :
@@ -2596,85 +2534,96 @@ def create_candlestick(open, high, low, close,
2596
2534
2597
2535
:rtype (dict): returns a representation of candlestick chart figure.
2598
2536
2599
- Example 1: Plot candlestick chart
2537
+ Example 1: Simple candlestick chart from a Pandas DataFrame
2600
2538
```
2601
2539
import plotly.plotly as py
2602
- import plotly.tools as tls
2603
- from plotly.graph_objs import *
2540
+ from plotly.tools import FigureFactory as FF
2541
+ from datetime import datetime
2604
2542
2605
- # Add data
2606
- high_data = [34.20, 34.37, 33.62, 34.25, 35.18, 33.25, 35.37, 34.62]
2607
- low_data = [31.70, 30.75, 32.87, 31.62, 30.81, 32.75, 32.75, 32.87]
2608
- close_data = [34.10, 31.93, 33.37, 33.18, 31.18, 33.10, 32.93, 33.70]
2609
- open_data = [33.01, 33.31, 33.50, 32.06, 34.12, 33.05, 33.31, 33.50]
2610
-
2611
- # Make candlestick
2612
- candle = FigureFactory.create_candlestick(open_data,
2613
- high_data,
2614
- low_data,
2615
- close_data)
2616
-
2617
- # Plot!
2618
- py.iplot(candle, filename='candle', validate=False, overwrite=True)
2543
+ import pandas.io.data as web
2544
+
2545
+ df = web.DataReader("aapl", 'yahoo', datetime(2007, 10, 1), datetime(2009, 4, 1))
2546
+ fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
2547
+ py.plot(fig, filename='finance/aapl-candlestick', validate=False)
2548
+ ```
2549
+
2550
+ Example 2: Add text and annotations to the candlestick chart
2551
+ ```
2552
+ fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
2553
+ # Update the fig - all options here: https://plot.ly/python/reference/#Layout
2554
+ fig['layout'].update({
2555
+ 'title': 'The Great Recession',
2556
+ 'yaxis': {'title': 'AAPL Stock'},
2557
+ 'shapes': [{
2558
+ 'x0': '2007-12-01', 'x1': '2007-12-01',
2559
+ 'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
2560
+ 'line': {'color': 'rgb(30,30,30)', 'width': 1}
2561
+ }],
2562
+ 'annotations': [{
2563
+ 'x': '2007-12-01', 'y': 0.05, 'xref': 'x', 'yref': 'paper',
2564
+ 'showarrow': False, 'xanchor': 'left',
2565
+ 'text': 'Official start of the recession'
2566
+ }]
2567
+ })
2568
+ py.plot(fig, filename='finance/aapl-recession-candlestick', validate=False)
2619
2569
```
2620
2570
2621
- Example 2: Plot candlestick chart with dates and change trace colors
2571
+ Example 3: Customize the candlestick colors
2622
2572
```
2573
+ import plotly.plotly as py
2574
+ from plotly.tools import FigureFactory as FF
2575
+ from plotly.graph_objs import Line, Marker
2623
2576
from datetime import datetime
2624
2577
2625
- # Add Data
2626
- open_data = [33.01, 33.31, 33.50, 32.06, 34.12,
2627
- 33.05, 33.31, 33.50, 32.62]
2628
- high_data = [34.20, 34.37, 33.62, 34.25, 35.18,
2629
- 33.25, 35.37, 34.62, 34.25]
2630
- low_data = [31.70, 30.75, 32.87, 31.62, 30.81,
2631
- 32.75, 32.75, 32.87, 32.62]
2632
- close_data = [34.10, 31.93, 33.37, 33.18, 31.18,
2633
- 33.10, 32.93, 33.70, 33.18]
2634
-
2635
- x = [datetime(year=2013, month=3, day=4),
2636
- datetime(year=2013, month=6, day=5),
2637
- datetime(year=2013, month=9, day=6),
2638
- datetime(year=2013, month=12, day=4),
2639
- datetime(year=2014, month=3, day=5),
2640
- datetime(year=2014, month=6, day=6),
2641
- datetime(year=2014, month=9, day=4),
2642
- datetime(year=2014, month=12, day=5),
2643
- datetime(year=2015, month=3, day=6)]
2644
-
2645
- c_inc = FigureFactory.create_candlestick(open_data,
2646
- high_data,
2647
- low_data,
2648
- close_data,
2649
- dates=x,
2650
- direction='increasing',
2651
- line=Line(color='rgb(204,
2652
- 229,
2653
- 255)',
2654
- width=4),
2655
- marker=Marker(color='rgb(204,
2656
- 229,
2657
- 255)')
2658
- )
2659
-
2660
- c_dec = FigureFactory.create_candlestick(open_data,
2661
- high_data,
2662
- low_data,
2663
- close_data,
2664
- dates=x,
2665
- direction='decreasing',
2666
- line=Line(color='rgb(160,
2667
- 160,
2668
- 160)',
2669
- width=4),
2670
- marker=Marker(color='rgb(160,
2671
- 160,
2672
- 160)'),
2673
- )
2674
- fig = c=_inc
2675
- fig['data'].extend(c_dec['data'])
2676
-
2677
- py.iplot(fig, filename='candle', validate=False, overwrite=True)
2578
+ import pandas.io.data as web
2579
+
2580
+ df = web.DataReader("aapl", 'yahoo', datetime(2008, 1, 1), datetime(2009, 4, 1))
2581
+ fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
2582
+
2583
+ # Make increasing ohlc sticks and customize their color and name
2584
+ fig_increasing = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index,
2585
+ direction='increasing', name='AAPL',
2586
+ marker=Marker(color='rgb(150, 200, 250)'),
2587
+ line=Line(color='rgb(150, 200, 250)'))
2588
+
2589
+ # Make decreasing ohlc sticks and customize their color and name
2590
+ fig_decreasing = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index,
2591
+ direction='decreasing',
2592
+ marker=Marker(color='rgb(128, 128, 128)'),
2593
+ line=Line(color='rgb(128, 128, 128)'))
2594
+
2595
+ # Initialize the figure
2596
+ fig = fig_increasing
2597
+
2598
+ # Add decreasing data with .extend()
2599
+ fig['data'].extend(fig_decreasing['data'])
2600
+
2601
+ py.iplot(fig, filename='finance/aapl-candlestick-custom', validate=False)
2602
+ ```
2603
+
2604
+ Example 4: Candlestick chart with datetime objects
2605
+ ```
2606
+ import plotly.plotly as py
2607
+ from plotly.tools import FigureFactory as FF
2608
+
2609
+ from datetime import datetime
2610
+
2611
+ # Add data
2612
+ open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
2613
+ high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
2614
+ low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
2615
+ close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
2616
+ dates = [datetime(year=2013, month=10, day=10),
2617
+ datetime(year=2013, month=11, day=10),
2618
+ datetime(year=2013, month=12, day=10),
2619
+ datetime(year=2014, month=1, day=10),
2620
+ datetime(year=2014, month=2, day=10)]
2621
+
2622
+ # Create ohlc
2623
+ fig = FF.create_candlestick(open_data, high_data,
2624
+ low_data, close_data, dates=dates)
2625
+
2626
+ py.iplot(fig, filename='finance/simple-candlestick', validate=False)
2678
2627
```
2679
2628
"""
2680
2629
FigureFactory .validate_ohlc (open , high , low , close , direction ,
0 commit comments