Create Open-High-Low-Close(OHLC) Stock Chart
Possible Usage Scenarios
The Open-High-Low-Close (OHLC) chart uses five columns of data, in order: category, open, high, low, and close. The range of prices in each category is again indicated by a vertical line, while the range between open and close is given by a wider floating bar; if the price increases in the category (close is higher than open), the bar is filled with one color, while if the price decreases, the bar is filled with another. This type of chart is often called a candlestick chart.
Visibility improvements in the chart
We often use colors rather than black and white to indicate increasing and decreasing prices. In the first set of candlesticks below, red shows increasing nad green decreasing prices.
Sample Code
The following sample code loads the sample Excel file and generates the output Excel file.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType, LegendPositionType | |
from aspose.cells.drawing import FillType | |
from aspose.pydrawing import Color | |
# Create an instance of Workbook | |
workbook = Workbook("Open-High-Low-Close.xlsx") | |
# Access the first worksheet. | |
worksheet = workbook.worksheets[0] | |
# Create High-Low-Close-Stock Chart | |
pieIdx = worksheet.charts.add(ChartType.STOCK_OPEN_HIGH_LOW_CLOSE, 5, 6, 20, 12) | |
# Retrieve the Chart object | |
chart = worksheet.charts[pieIdx] | |
# Set the legend can be showed | |
chart.show_legend = True | |
# Set the chart title name | |
chart.title.text = "OPen-High-Low-Close Stock" | |
# Set the Legend at the bottom of the chart area | |
chart.legend.position = LegendPositionType.BOTTOM | |
# Set data range | |
chart.set_chart_data_range("A1:E9", True) | |
# Set category data | |
chart.n_series.category_data = "A2:A9" | |
# Set the DownBars and UpBars with different color | |
chart.n_series[0].down_bars.area.foreground_color = Color.green | |
chart.n_series[0].up_bars.area.foreground_color = Color.red | |
# Fill the PlotArea area with nothing | |
chart.plot_area.area.fill_format.fill_type = FillType.NONE | |
# Save the Excel file | |
workbook.save("out.xlsx") |