python想弄个K线图,折腾了一上午没有简单的办法

  • w
    wsyx87930
    想获得特定股票特定期间(比如从2020年3月7日至今)的K线图

    之前都是登录股票软件,点K线图,右键拖选目前区间,然后放大,然后导出图片再编辑

    想把上述步骤自动化一下,没折腾成功:
    1、想绕开股票软件,否则在别人电脑上运行就太麻烦了,还要安装这个股票软件登录账号密码;
    2、财经网站上的K线图很难精确调整日期区间;
    3、获得交易数据后自己画K线图太麻烦了,没有现成的库,看了下网上现成的案例,画出来的K线图都太丑了。。
  • m
    mijuu
    百度良心echart
  • c
    cookiefj
    前排学习! iOS 13.3 fly ~
  • w
    wsyx87930
    感谢关键词,我来学习一下
  • f
    fqxufo
  • 花菊
    mark iOS fly ~
  • o
    opensesame
    百度图表echartHiPDA·NG
  • h
    hjkl0001
    记号,记号 iOS fly ~
  • e
    easygooder
    必须要备份。
  • d
    dqfish
    这个要mark
  • 天地遥昭
    这个要mark一下。。。
  • 数不清
    学习了
  • g
    guangjian
    学习一下
  • z
    zeroxia
    学js nodejs 看哪本书?
  • w
    wsyx87930
    这个能直接生成K线图吗?
    我对交易数据要求很低,深交所都能直接爬到,现在就是搞不到漂亮的图。。
  • l
    lovemu
    纷纷学习。。 iOS fly ~
  • y
    yaohoo
  • 马背上的鱼
    马克一下 iOS fly ~
  • 谁家丢的西瓜皮
    标记一下 iOS fly ~
  • s
    shehzb
    进来学习的 iOS fly ~
  • i
    ionpick
    这个是我以前写的用python画K线图的代码,供你参考,输出格式是SVG,原理就是把股票每天开盘,收盘,最高,最低这些数据转化成SVG代码就行了,很简单。SVG是一种矢量图形格式,可以在浏览器里面显示。

    1. def DrawStockCandlestickChart(self, code, halfyear, data=[]):
    2. def get_y(price):
    3. height = 156 # 180 * 0.8
    4. y = 25 + (1 - (price - price_lowest) / price_range) * height if price_range else 25

    5. return int(y)

    6. def get_vol_y(vol):
    7. height = 30
    8. # y = 168 + (1 - (vol - volume_lowest) / volume_range) * height
    9. y = 168 + (1 - vol / volume_highest if volume_highest else vol) * height

    10. return int(y)

    11. def DrawChart(g, d, i):
    12. x = int(startx + (daycount - i - 2) * ratio)
    13. up = True if d["open"] <= d["price"] else False

    14. # Draw middle line
    15. y1 = get_y(d["high"])
    16. y2 = get_y(d["low"])
    17. color = g_StockChartPriceFillColorUp if up else g_StockChartPriceColorDown
    18. outline_color = g_StockChartPriceOutlineColorUp if d["open"] <= d["price"] else g_StockChartPriceColorDown

    19. if halfyear in ["category", "halfyear"]: bias = 1
    20. else: bias = 3 if up else 2

    21. 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)
    22. # g.line((x+bias, y1, x+bias, y2), fill=outline_color)

    23. # Draw volume
    24. if halfyear in ["category", "halfyear"]: stickwidth = 2
    25. else: stickwidth = 6 if up else 5

    26. y1 = get_vol_y(d["volume"])
    27. y2 = 199
    28. color = g_StockChartVolumeColorUp if d["open"] <= d["price"] else g_StockChartVolumeColorDown

    29. 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)

    30. # Draw stick
    31. color = g_StockChartPriceFillColorUp if up else g_StockChartPriceColorDown
    32. y1 = get_y(d["open"])
    33. y2 = get_y(d["price"])
    34. if up: y1, y2 = y2, y1

    35. if y2 == y1:
    36. 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)
    37. else:
    38. 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)

    39. return g

    40. startx = 10
    41. daycount = 0
    42. preferdays = 118 if halfyear in ["category", "halfyear"] else 53

    43. # Calculate number of days
    44. if len(data) > 1: g, d, price_lowest, price_range, volume_highest, daycount = data
    45. else:
    46. datelist = self.GetAvailableDateList(code, None, preferdays)

    47. for i in datelist:
    48. d = self.QueryStockData(code, i)
    49. if d: daycount += 1

    50. if halfyear in ["category", "halfyear"]:
    51. if daycount and daycount < 50: ratio = 473.0 / ((daycount-1) if daycount > 2 else 2)
    52. else:
    53. daycount = preferdays
    54. ratio = 4
    55. else:
    56. if daycount and daycount < 50: ratio = 473.0 / ((daycount-1) if daycount > 2 else 2)
    57. else:
    58. daycount = preferdays
    59. ratio = 9

    60. if not self.InHoliday():
    61. daycount -= 1 # Move the chart left
    62. t, _ = GetTodayDate(False)
    63. if len(data) == 1 and t in datelist: del datelist[-1]

    64. if len(data) > 1:
    65. # DrawChart(g, d, -1)
    66. g = ""
    67. return DrawChart(g, d, -1).encode()

    68. g = '\n<svg version="1.1" baseProfile="full" width="510" height="220" xmlns="http://www.w3.org/2000/svg">\n\n'

    69. # Draw Outline
    70. g += '<g style="font-family: Simsun; font-size: 12px; stroke-width: 1px; shape-rendering: crispEdges;">\n\n'

    71. 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)
    72. g += '<rect x="{:d}" y="{:d}" width="{:d}" height="{:d}" stroke-width="1" style="stroke:{}; fill:{}"/>\n'.format(8, 21, 469, 14, "#E5E5F7", "#E5E5F7")

    73. # Draw Coordinate
    74. for i in range(1, 7):
    75. y = 20 + i*26
    76. g += '<line x1="{:d}" y1="{:d}" x2="{:d}" y2="{:d}" stroke-width="1" style="stroke:{};" />\n'.format(8, y, 477, y, g_StockChartCoordinateColor)

    77. for i in range(1, 4):
    78. x = 7 + i*120
    79. g += '<line x1="{:d}" y1="{:d}" x2="{:d}" y2="{:d}" stroke-width="1" style="stroke:{};" />\n'.format(x, 21, x, 199, g_StockChartCoordinateColor)

    80. # Draw title

    81. text = "{} ({})".format(self.StockCodeToName(code), code)

    82. g += '<text x="62" y="14">{}</text>\n'.format(text)
    83. g += '<text x="234" y="14">日 线</text>\n'

    84. # Get highest, lowest
    85. price_highest = 0
    86. price_lowest = 1000
    87. volume_highest = 0
    88. volume_lowest = 100000

    89. # datelist = self.GetAvailableDateList(code, None, daycount)
    90. datelist.reverse()

    91. for i in datelist:
    92. d = self.QueryStockData(code, i)
    93. if not d or (price_highest and d["low"] / price_highest > 1.15): continue

    94. volume = d["volume"]
    95. if price_highest < d["high"]: price_highest = d["high"]
    96. if price_lowest > d["low"]: price_lowest = d["low"]
    97. if volume_highest < volume: volume_highest = volume
    98. if volume_lowest > volume: volume_lowest = volume

    99. price_range = price_highest - price_lowest
    100. volume_range = volume_highest - volume_lowest

    101. # Draw Price

    102. for i in range(7):
    103. x = 483
    104. y = 13 + i*26
    105. priceformat = "{:.1f}" if price_lowest + price_range > 10 else "{:.2f}"
    106. price = priceformat.format(price_lowest + price_range * (6-i) / 6)
    107. g += '<text x="{:d}" y="{:d}">{}</text>\n'.format(x, y+11, price)

    108. # Draw Date
    109. idate, _ = GetTodayDate(True)
    110. x = 474

    111. for i in range(3):
    112. idate = self.GetDateDelta(-daycount // 4, idate)
    113. text = idate[-5:]
    114. x -= 120
    115. y = 213
    116. g += '<text x="{:d}" y="{:d}">{}</text>\n'.format(x, y, text)

    117. # Draw chart
    118. count = -1

    119. for i in datelist:
    120. d = self.QueryStockData(code, i)
    121. if d or i not in self.StockHoliday: count += 1
    122. if not d: continue
    123. g = DrawChart(g, d, count)
    124. if count > daycount-3: break

    125. g += '</g>\n'
    126. g += '\n</svg>\n\n'
    127. content = g.encode()

    128. return content
    复制代码
  • 之一线
    mark。mark
  • c
    cookite_li
    学习。
  • z
    zsdicky
    学习一下
  • k
    kww9812
    bokeh也可以
    plt也可以
    聚宽上看看一堆堆