python想弄个K线图,折腾了一上午没有简单的办法
- wsyx87930想获得特定股票特定期间(比如从2020年3月7日至今)的K线图
之前都是登录股票软件,点K线图,右键拖选目前区间,然后放大,然后导出图片再编辑
想把上述步骤自动化一下,没折腾成功:
1、想绕开股票软件,否则在别人电脑上运行就太麻烦了,还要安装这个股票软件登录账号密码;
2、财经网站上的K线图很难精确调整日期区间;
3、获得交易数据后自己画K线图太麻烦了,没有现成的库,看了下网上现成的案例,画出来的K线图都太丑了。。 - mijuu百度良心echart
- cookiefj前排学习! iOS 13.3 fly ~
- wsyx87930感谢关键词,我来学习一下
- fqxufo
- 花菊mark iOS fly ~
- opensesame百度图表echartHiPDA·NG
- hjkl0001记号,记号 iOS fly ~
- easygooder必须要备份。
- dqfish这个要mark
- 天地遥昭这个要mark一下。。。
- 数不清学习了
- guangjian学习一下
- zeroxia学js nodejs 看哪本书?
- wsyx87930这个能直接生成K线图吗?
我对交易数据要求很低,深交所都能直接爬到,现在就是搞不到漂亮的图。。 - lovemu纷纷学习。。 iOS fly ~
- yaohoo
- 马背上的鱼马克一下 iOS fly ~
- 谁家丢的西瓜皮标记一下 iOS fly ~
- shehzb进来学习的 iOS fly ~
- ionpick这个是我以前写的用python画K线图的代码,供你参考,输出格式是SVG,原理就是把股票每天开盘,收盘,最高,最低这些数据转化成SVG代码就行了,很简单。SVG是一种矢量图形格式,可以在浏览器里面显示。复制代码
- def DrawStockCandlestickChart(self, code, halfyear, data=[]):
- def get_y(price):
- height = 156 # 180 * 0.8
- y = 25 + (1 - (price - price_lowest) / price_range) * height if price_range else 25
- return int(y)
- def get_vol_y(vol):
- height = 30
- # y = 168 + (1 - (vol - volume_lowest) / volume_range) * height
- y = 168 + (1 - vol / volume_highest if volume_highest else vol) * height
- return int(y)
- def DrawChart(g, d, i):
- x = int(startx + (daycount - i - 2) * ratio)
- up = True if d["open"] <= d["price"] else False
- # Draw middle line
- y1 = get_y(d["high"])
- y2 = get_y(d["low"])
- color = g_StockChartPriceFillColorUp if up else g_StockChartPriceColorDown
- outline_color = g_StockChartPriceOutlineColorUp if d["open"] <= d["price"] else g_StockChartPriceColorDown
- if halfyear in ["category", "halfyear"]: bias = 1
- else: bias = 3 if up else 2
- g += '<line x1="{:d}" y1="{:d}" x2="{:d}" y2="{:d}" stroke-width="1" style="stroke:{};" />\n'.format(x+bias, y1, x+bias, y2, outline_color)
- # g.line((x+bias, y1, x+bias, y2), fill=outline_color)
- # Draw volume
- if halfyear in ["category", "halfyear"]: stickwidth = 2
- else: stickwidth = 6 if up else 5
- y1 = get_vol_y(d["volume"])
- y2 = 199
- color = g_StockChartVolumeColorUp if d["open"] <= d["price"] else g_StockChartVolumeColorDown
- g += '<rect x="{:d}" y="{:d}" width="{:d}" height="{:d}" stroke-width="1" style="stroke:{}; fill:{}"/>\n'.format(x, y1, stickwidth, y2-y1, color, color)
- # Draw stick
- color = g_StockChartPriceFillColorUp if up else g_StockChartPriceColorDown
- y1 = get_y(d["open"])
- y2 = get_y(d["price"])
- if up: y1, y2 = y2, y1
- if y2 == y1:
- g += '<line x1="{:d}" y1="{:d}" x2="{:d}" y2="{:d}" stroke-width="1" style="stroke:{};" />\n'.format(x, y1, x+stickwidth+1, y2, outline_color)
- else:
- g += '<rect x="{:d}" y="{:d}" width="{:d}" height="{:d}" stroke-width="1" style="stroke:{}; fill:{}"/>\n'.format(x, y1, stickwidth, y2-y1, outline_color, color)
- return g
- startx = 10
- daycount = 0
- preferdays = 118 if halfyear in ["category", "halfyear"] else 53
- # Calculate number of days
- if len(data) > 1: g, d, price_lowest, price_range, volume_highest, daycount = data
- else:
- datelist = self.GetAvailableDateList(code, None, preferdays)
- for i in datelist:
- d = self.QueryStockData(code, i)
- if d: daycount += 1
- if halfyear in ["category", "halfyear"]:
- if daycount and daycount < 50: ratio = 473.0 / ((daycount-1) if daycount > 2 else 2)
- else:
- daycount = preferdays
- ratio = 4
- else:
- if daycount and daycount < 50: ratio = 473.0 / ((daycount-1) if daycount > 2 else 2)
- else:
- daycount = preferdays
- ratio = 9
- if not self.InHoliday():
- daycount -= 1 # Move the chart left
- t, _ = GetTodayDate(False)
- if len(data) == 1 and t in datelist: del datelist[-1]
- if len(data) > 1:
- # DrawChart(g, d, -1)
- g = ""
- return DrawChart(g, d, -1).encode()
- g = '\n<svg version="1.1" baseProfile="full" width="510" height="220" xmlns="http://www.w3.org/2000/svg">\n\n'
- # Draw Outline
- g += '<g style="font-family: Simsun; font-size: 12px; stroke-width: 1px; shape-rendering: crispEdges;">\n\n'
- g += '<rect x="{:d}" y="{:d}" width="{:d}" height="{:d}" stroke-width="1" style="stroke:{}; fill:{}"/>\n'.format(7, 20, 471, 180, g_StockChartOutlineColor, g_StockChartBackgroundColor)
- g += '<rect x="{:d}" y="{:d}" width="{:d}" height="{:d}" stroke-width="1" style="stroke:{}; fill:{}"/>\n'.format(8, 21, 469, 14, "#E5E5F7", "#E5E5F7")
- # Draw Coordinate
- for i in range(1, 7):
- y = 20 + i*26
- g += '<line x1="{:d}" y1="{:d}" x2="{:d}" y2="{:d}" stroke-width="1" style="stroke:{};" />\n'.format(8, y, 477, y, g_StockChartCoordinateColor)
- for i in range(1, 4):
- x = 7 + i*120
- g += '<line x1="{:d}" y1="{:d}" x2="{:d}" y2="{:d}" stroke-width="1" style="stroke:{};" />\n'.format(x, 21, x, 199, g_StockChartCoordinateColor)
- # Draw title
- text = "{} ({})".format(self.StockCodeToName(code), code)
- g += '<text x="62" y="14">{}</text>\n'.format(text)
- g += '<text x="234" y="14">日 线</text>\n'
- # Get highest, lowest
- price_highest = 0
- price_lowest = 1000
- volume_highest = 0
- volume_lowest = 100000
- # datelist = self.GetAvailableDateList(code, None, daycount)
- datelist.reverse()
- for i in datelist:
- d = self.QueryStockData(code, i)
- if not d or (price_highest and d["low"] / price_highest > 1.15): continue
- volume = d["volume"]
- if price_highest < d["high"]: price_highest = d["high"]
- if price_lowest > d["low"]: price_lowest = d["low"]
- if volume_highest < volume: volume_highest = volume
- if volume_lowest > volume: volume_lowest = volume
- price_range = price_highest - price_lowest
- volume_range = volume_highest - volume_lowest
- # Draw Price
- for i in range(7):
- x = 483
- y = 13 + i*26
- priceformat = "{:.1f}" if price_lowest + price_range > 10 else "{:.2f}"
- price = priceformat.format(price_lowest + price_range * (6-i) / 6)
- g += '<text x="{:d}" y="{:d}">{}</text>\n'.format(x, y+11, price)
- # Draw Date
- idate, _ = GetTodayDate(True)
- x = 474
- for i in range(3):
- idate = self.GetDateDelta(-daycount // 4, idate)
- text = idate[-5:]
- x -= 120
- y = 213
- g += '<text x="{:d}" y="{:d}">{}</text>\n'.format(x, y, text)
- # Draw chart
- count = -1
- for i in datelist:
- d = self.QueryStockData(code, i)
- if d or i not in self.StockHoliday: count += 1
- if not d: continue
- g = DrawChart(g, d, count)
- if count > daycount-3: break
- g += '</g>\n'
- g += '\n</svg>\n\n'
- content = g.encode()
- return content
- def DrawStockCandlestickChart(self, code, halfyear, data=[]):
- 之一线mark。mark
- cookite_li学习。
- zsdicky学习一下
- kww9812bokeh也可以
plt也可以
聚宽上看看一堆堆