Created
February 7, 2017 09:22
-
-
Save lanfon72/07f9e9ca7f7d043be8c70e9bdb09b9a7 to your computer and use it in GitHub Desktop.
OHLC chart with XlsxWriter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # extended from http://xlsxwriter.readthedocs.io/example_chart_stock.html | |
| from datetime import datetime | |
| import xlsxwriter | |
| workbook = xlsxwriter.Workbook('chart_ohlc_stock.xlsx') | |
| worksheet = workbook.add_worksheet() | |
| bold = workbook.add_format({'bold': 1}) | |
| date_format = workbook.add_format({'num_format': 'dd/mm/yyyy'}) | |
| chart = workbook.add_chart({'type': 'stock'}) | |
| # Add the worksheet data that the charts will refer to. | |
| headings = ['Date', 'Open', 'High', 'Low', 'Close'] | |
| data = [ | |
| ['2007-01-01', '2007-01-02', '2007-01-03', '2007-01-04', '2007-01-05'], | |
| [24, 22, 18, 18.5, 17], | |
| [27.2, 25.03, 19.05, 20.34, 18.5], | |
| [23.49, 19.55, 15.12, 17.84, 16.34], | |
| [25.45, 23.05, 17.32, 20.45, 17.34], | |
| ] | |
| worksheet.write_row('A1', headings, bold) | |
| for row in range(len(headings)): | |
| date = datetime.strptime(data[0][row], "%Y-%m-%d") | |
| worksheet.write(row + 1, 0, date, date_format) | |
| worksheet.write(row + 1, 1, data[1][row]) | |
| worksheet.write(row + 1, 2, data[2][row]) | |
| worksheet.write(row + 1, 3, data[3][row]) | |
| worksheet.write(row + 1, 4, data[4][row]) | |
| worksheet.set_column('A:E', 11) | |
| # Add a series for each of the High-Low-Close columns. | |
| chart.add_series({ | |
| 'categories': '=Sheet1!$A$2:$A$6', | |
| 'values': '=Sheet1!$B$2:$B$6', | |
| 'marker': {'type': 'none'} | |
| }) | |
| chart.add_series({ | |
| 'categories': '=Sheet1!$A$2:$A$6', | |
| 'values': '=Sheet1!$C$2:$C$6', | |
| }) | |
| chart.add_series({ | |
| 'categories': '=Sheet1!$A$2:$A$6', | |
| 'values': '=Sheet1!$D$2:$D$6', | |
| }) | |
| chart.add_series({ | |
| 'categories': '=Sheet1!$A$2:$A$6', | |
| 'values': '=Sheet1!$E$2:$E$6', | |
| 'line': {'none': True}, | |
| 'marker': {'type': 'none'} | |
| }) | |
| # Add a chart title and some axis labels. | |
| chart.set_title({'name': 'Open-High-Low-Close'}) | |
| chart.set_x_axis({'name': 'Date'}) | |
| chart.set_y_axis({'name': 'Share price'}) | |
| chart.set_up_down_bars() | |
| worksheet.insert_chart('E9', chart) | |
| workbook.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment