Ruby 或 Python 中的金融图表/图形

发布于 2024-07-25 20:06:52 字数 341 浏览 8 评论 0原文

使用 Ruby 或 Python 等高级语言创建金融开盘价-最高价-最低价-收盘价 (OHLC) 图表的最佳选择是什么? 虽然似乎有很多图表选项,但我还没有看到任何带有这种图表的宝石或鸡蛋。

http://en.wikipedia.org/wiki/Open-high-low-close_chart (但我不需要移动平均线或布林线)

JFreeChart 可以在 Java 中做到这一点,但我想让我的代码库尽可能小和简单。

谢谢!

What are my best options for creating a financial open-high-low-close (OHLC) chart in a high level language like Ruby or Python? While there seem to be a lot of options for graphing, I haven't seen any gems or eggs with this kind of chart.

http://en.wikipedia.org/wiki/Open-high-low-close_chart (but I don't need the moving average or Bollinger bands)

JFreeChart can do this in Java, but I'd like to make my codebase as small and simple as possible.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

暮年 2024-08-01 20:06:52

您可以使用 matplotlibmatplotlib.pyplot.bar。 然后,您可以使用线 plot 来指示开盘价和收盘价:

例如:

#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import lines

import random


deltas = [4, 6, 13, 18, 15, 14, 10, 13, 9, 6, 15, 9, 6, 1, 1, 2, 4, 4, 4, 4, 10, 11, 16, 17, 12, 10, 12, 15, 17, 16, 11, 10, 9, 9, 7, 10, 7, 16, 8, 12, 10, 14, 10, 15, 15, 16, 12, 8, 15, 16]
bases = [46, 49, 45, 45, 44, 49, 51, 52, 56, 58, 53, 57, 62, 63, 68, 66, 65, 66, 63, 63, 62, 61, 61, 57, 61, 64, 63, 58, 56, 56, 56, 60, 59, 54, 57, 54, 54, 50, 53, 51, 48, 43, 42, 38, 37, 39, 44, 49, 47, 43]


def rand_pt(bases, deltas):
    return [random.randint(base, base + delta) for base, delta in zip(bases, deltas)]

# randomly assign opening and closing prices 
openings = rand_pt(bases, deltas)
closings = rand_pt(bases, deltas)

# First we draw the bars which show the high and low prices
# bottom holds the low price while deltas holds the difference 
# between high and low.
width = 0
ax = plt.axes()
rects1 = ax.bar(np.arange(50), deltas, width, color='r', bottom=bases)

# Now draw the ticks indicating the opening and closing price
for opening, closing, bar in zip(openings, closings, rects1):
    x, w = bar.get_x(), 0.2

    args = {
    }

    ax.plot((x - w, x), (opening, opening), **args)
    ax.plot((x, x + w), (closing, closing), **args)


plt.show()

创建如下图:

在此处输入图像描述

显然,您希望将其打包在一个使用 (open, close, min, max) 元组绘制绘图的函数中(并且您可能不会想要随机分配您的开盘价和收盘价)。

You can use matplotlib and the the optional bottom parameter of matplotlib.pyplot.bar. You can then use line plot to indicate the opening and closing prices:

For example:

#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import lines

import random


deltas = [4, 6, 13, 18, 15, 14, 10, 13, 9, 6, 15, 9, 6, 1, 1, 2, 4, 4, 4, 4, 10, 11, 16, 17, 12, 10, 12, 15, 17, 16, 11, 10, 9, 9, 7, 10, 7, 16, 8, 12, 10, 14, 10, 15, 15, 16, 12, 8, 15, 16]
bases = [46, 49, 45, 45, 44, 49, 51, 52, 56, 58, 53, 57, 62, 63, 68, 66, 65, 66, 63, 63, 62, 61, 61, 57, 61, 64, 63, 58, 56, 56, 56, 60, 59, 54, 57, 54, 54, 50, 53, 51, 48, 43, 42, 38, 37, 39, 44, 49, 47, 43]


def rand_pt(bases, deltas):
    return [random.randint(base, base + delta) for base, delta in zip(bases, deltas)]

# randomly assign opening and closing prices 
openings = rand_pt(bases, deltas)
closings = rand_pt(bases, deltas)

# First we draw the bars which show the high and low prices
# bottom holds the low price while deltas holds the difference 
# between high and low.
width = 0
ax = plt.axes()
rects1 = ax.bar(np.arange(50), deltas, width, color='r', bottom=bases)

# Now draw the ticks indicating the opening and closing price
for opening, closing, bar in zip(openings, closings, rects1):
    x, w = bar.get_x(), 0.2

    args = {
    }

    ax.plot((x - w, x), (opening, opening), **args)
    ax.plot((x, x + w), (closing, closing), **args)


plt.show()

creates a plot like this:

enter image description here

Obviously, you'd want to package this up in a function that drew the plot using (open, close, min, max) tuples (and you probably wouldn't want to randomly assign your opening and closing prices).

给妤﹃绝世温柔 2024-08-01 20:06:52

您可以将 Pylab (matplotlib.finance) 与 Python 结合使用。 以下是一些示例:http://matplotlib.sourceforge.net/examples/pylab_examples/plotfile_demo。 htmlBeginning Python Visualization 中有一些专门讨论此问题的好材料。

更新:我认为您可以使用 matplotlib.finance.candlestick 来实现日本烛台效果。

You can use Pylab (matplotlib.finance) with Python. Here are some examples: http://matplotlib.sourceforge.net/examples/pylab_examples/plotfile_demo.html . There is some good material specifically on this problem in Beginning Python Visualization.

Update: I think you can use matplotlib.finance.candlestick for the Japanese candlestick effect.

风启觞 2024-08-01 20:06:52

您是否考虑过使用 R 和 quantmod 包? 它可能正是您所需要的。

Have you considered using R and the quantmod package? It likely provides exactly what you need.

为你鎻心 2024-08-01 20:06:52

有关使用 matplotlib 的财务图 (OHLC) 的一些示例可以在此处找到:

  • 财务演示

    #!/usr/bin/env python 
      从 pylab 导入 * 
      从 matplotlib.dates 导入 DateFormatter、WeekdayLocator、HourLocator、\ 
           DayLocator,星期一 
      从 matplotlib.finance 导入quotes_historical_yahoo,烛台,\ 
           情节_日_摘要,烛台2 
    
      # (年、月、日)元组足以作为quotes_historical_yahoo 的参数 
      日期 1 = ( 2004, 2, 1) 
      日期2 = ( 2004, 4, 12 ) 
    
    
      mondays = WeekdayLocator(MONDAY) # 周一的主要价格变动 
      alldays = DayLocator() # 这些天的小刻度 
      weekFormatter = DateFormatter('%b %d') # 例如,1 月 12 日 
      dayFormatter = DateFormatter('%d') # 例如,12 
    
      报价 = quote_historical_yahoo('INTC', date1, date2) 
      如果 len(引号) == 0: 
          引发系统退出 
    
      图=图() 
      图.subplots_调整(底部=0.2) 
      斧头 = 图.add_subplot(111) 
      ax.xaxis.set_major_locator(星期一) 
      ax.xaxis.set_minor_locator(全天) 
      ax.xaxis.set_major_formatter(weekFormatter) 
      #ax.xaxis.set_minor_formatter(dayFormatter) 
    
      #plot_day_summary(斧头,引号,刻度大小= 3) 
      烛台(斧头,引号,宽度=0.6) 
    
      ax.xaxis_date() 
      ax.autoscale_view() 
      setp( gca().get_xticklabels(), 旋转=45, 水平对齐='右') 
    
      展示() 
      

在此处输入图像描述

Some examples about financial plots (OHLC) using matplotlib can be found here:

  • finance demo

    #!/usr/bin/env python
    from pylab import *
    from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
         DayLocator, MONDAY
    from matplotlib.finance import quotes_historical_yahoo, candlestick,\
         plot_day_summary, candlestick2
    
    # (Year, month, day) tuples suffice as args for quotes_historical_yahoo
    date1 = ( 2004, 2, 1)
    date2 = ( 2004, 4, 12 )
    
    
    mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
    alldays    = DayLocator()              # minor ticks on the days
    weekFormatter = DateFormatter('%b %d')  # Eg, Jan 12
    dayFormatter = DateFormatter('%d')      # Eg, 12
    
    quotes = quotes_historical_yahoo('INTC', date1, date2)
    if len(quotes) == 0:
        raise SystemExit
    
    fig = figure()
    fig.subplots_adjust(bottom=0.2)
    ax = fig.add_subplot(111)
    ax.xaxis.set_major_locator(mondays)
    ax.xaxis.set_minor_locator(alldays)
    ax.xaxis.set_major_formatter(weekFormatter)
    #ax.xaxis.set_minor_formatter(dayFormatter)
    
    #plot_day_summary(ax, quotes, ticksize=3)
    candlestick(ax, quotes, width=0.6)
    
    ax.xaxis_date()
    ax.autoscale_view()
    setp( gca().get_xticklabels(), rotation=45, horizontalalignment='right')
    
    show()
    

enter image description here

智商已欠费 2024-08-01 20:06:52

您可以自由地使用 JRuby 而不是 Ruby 吗? 这样您就可以使用 JFreeChart,而且您的代码仍将使用 Ruby

Are you free to use JRuby instead of Ruby? That'd let you use JFreeChart, plus your code would still be in Ruby

少跟Wǒ拽 2024-08-01 20:06:52

请查看 WHIFF 的 Open Flash Chart 嵌入
http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1600。打开FlashCharts
蜡烛图的示例位于顶部。 这将是特别
适合嵌入网页。

Please look at the Open Flash Chart embedding for WHIFF
http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1600.openFlashCharts
An example of a candle chart is right at the top. This would be especially
good for embedding in web pages.

旧时光的容颜 2024-08-01 20:06:52

如果您喜欢示例的外观,Open Flash Chart 是不错的选择。 我已经转移到 JavaScript/Canvas 库,例如用于 HTML 嵌入图表的 Flot更具可定制性,无需太多黑客攻击即可获得所需的效果(http://itprolife.worona.eu/2009/08/scatter-chart-library-moving-to-flot.html)。

Open Flash Chart is nice choice if you like the look of examples. I've moved to JavaScript/Canvas library like Flot for HTML embedded charts, as it is more customizable and I get desired effect without much hacking (http://itprolife.worona.eu/2009/08/scatter-chart-library-moving-to-flot.html).

忱杏 2024-08-01 20:06:52

这是我几天前使用 Matplotlib 绘制的股票图表,我也发布了源代码,供您参考: StockChart_Matplotlib

This is the stock chart I draw just days ago using Matplotlib, I've posted the source too, for your reference: StockChart_Matplotlib

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文