Python third-party library OPENPYXL (one) stock Charts (stock chart)
The data in a column or row arranged in a specific order on a worksheet can be plotted in a stock chart. As its name implies, stock charts are often used to illustrate the volatility of stock prices. However, this chart can also be used for scientific data. For example, you can use a stock chart to represent daily or yearly temperature fluctuations. You must organize your data in the correct order to create a stock chart.
It is important to organize the stock chart data in the worksheet. For example, in order to create a simple stock chart with high and low closing prices, you should arrange your data in this order as a column header in the form of highs, lows, and approaches.
Although stock charts are a unique type, the various types are just shortcuts to specific formatting options:
1.high-low-close is essentially a line chart with no lines and is marked as XYZ. It also causes HiLoLines to be set to True
2.open-high-low-close is a high-low-close chart, with each data point labeled Xzz and Updownline.
You can increase the volume by combining the stock chart with the volume's Bar chart.
fromDatetimeImportDate fromOpenpyxlImportWorkbook fromOpenpyxl.chartImport(Barchart, Stockchart, Reference, Series,) fromOpenpyxl.chart.axisImportDateaxis, Chartlines fromOpenpyxl.chart.updown_barsImportUPDOWNBARSWB=Workbook () WS=wb.activerows= [ ['Date','Volume','Open',' High',' Low','Close'], ['2015-01-01', 20000, 26.2, 27.20, 23.49, 25.45, ], ['2015-01-02', 10000, 25.45, 25.03, 19.55, 23.05, ], ['2015-01-03', 15000, 23.05, 24.46, 20.03, 22.42, ], ['2015-01-04', 2000, 22.42, 23.97, 20.07, 21.90, ], ['2015-01-05', 12000, 21.9, 23.65, 19.50, 21.51, ],] forRowinchrows:ws.append (Row)#High-low-closeC1 =Stockchart () labels= Reference (WS, Min_col=1, min_row=2, max_row=6) Data= Reference (WS, Min_col=4, Max_col=6, Min_row=1, max_row=6) c1.add_data (data, Titles_from_data=True) c1.set_categories (labels) forSinchC1.series:s.graphicalproperties.line.nofill=True#marker for closeS.marker.symbol ="Dot"s.marker.size= 5C1.title="High-low-close"C1.hilowlines=Chartlines ()#Excel is broken and needs a caches of values in order to display HiLoLines:-/ fromOpenpyxl.chart.data_sourceImportNumdata, Numvalpts= [Numval (idx=i) forIinchRange (len (data)-1)]cache= Numdata (pt=pts) c1.series[-1].val.numref.numcache =Cachews.add_chart (C1,"A10")#Open-high-low-closeC2 =Stockchart () data= Reference (WS, Min_col=3, Max_col=6, Min_row=1, max_row=6) c2.add_data (data, Titles_from_data=True) c2.set_categories (labels) forSinchC2.series:s.graphicalproperties.line.nofill=Truec2.hilowlines=chartlines () c2.updownbars=updownbars () c2.title="Open-high-low-close"#Add Dummy CacheC2.series[-1].val.numref.numcache =Cachews.add_chart (C2,"G10")#Create Bar chart for volumeBar=barchart () data= Reference (WS, min_col=2, Min_row=1, max_row=6) bar.add_data (data, Titles_from_data=True) bar.set_categories (labels) fromCopyImportdeepcopy#Volume-high-low-closeB1 =deepcopy (bar) C3=deepcopy (C1) C3.y_axis.majorGridlines=Nonec3.y_axis.title=" Price"b1.y_axis.axId= 20B1.z_axis=c3.y_axisb1.y_axis.crosses="Max"B1+=C3c3.title="High low close volume"Ws.add_chart (B1,"A27")## Volume-open-high-low-closeB2 =deepcopy (bar) C4=deepcopy (C2) C4.y_axis.majorGridlines=Nonec4.y_axis.title=" Price"b2.y_axis.axId= 20B2.z_axis=c4.y_axisb2.y_axis.crosses="Max"B2+=C4ws.add_chart (B2,"G27") Wb.save ("stock.xlsx")
View Code
Note : Due to the shortcomings of Excel high-low lines, it is only displayed if at least one data series has some dummy values. This can be done through the following attacks:
from Import for in range (len (data)-1= Numdata (pt=pts) c1.series[-1]. Val.numRef.numCache = cache
Run results
Python third-party library OPENPYXL (11)