在Ruby或Python中的财务图表/图形

12

我应该选择哪种最佳选项来使用高级编程语言(如Ruby或Python)创建财务开高低收(OHLC)图表?尽管似乎有许多绘图选项,但我没有看到任何可以生成这种类型图表的宝石(Ruby)或Egg(Python)。

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

JFreeChart可以在Java中完成此操作,但我想让我的代码库尽可能小和简单。

谢谢!

8个回答

18
你可以使用matplotlibmatplotlib.pyplot.bar的可选参数bottom。然后,你可以使用线图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()

生成如下图所示的绘图:

enter image description here

显然,你需要将这个过程封装成一个函数,使用 (open, close, min, max) 元组绘制图形(并且你可能不希望随机分配开盘和收盘价格)。


这种图表不包括开盘价或收盘价。 - Eric the Red
@Eric 我添加了代码来绘制开盘和收盘价的刻度。当我查看维基百科页面上的图表时,我没有看到它们(因为我不是财务人员,所以不知道它们应该在哪里 :))。 - Aaron Maenpaa
已经为您修复了图片 :-) - Ivo Flipse

8

很遗憾,这些图表都不是我要找的类型。 - Eric the Red

4

在这里可以找到一些使用matplotlib绘制财务图表(OHLC)的示例:

  • 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()
    

输入图像描述


如果代码示例很短,请在您的答案中包含它们,以防链接失效,这样人们就不必点击。 - agf
添加了第一个例子,我认为第二个太长了。 - karlacio

4

你是否考虑过使用R语言和quantmod包?这可能提供了你所需的功能。


这看起来很有前途!我会尽快查看。谢谢! - Eric the Red

1

你可以自由地使用JRuby来代替Ruby吗?这样可以让你使用JFreeChart,同时你的代码仍然是用Ruby编写的。


1
同样适用于Jython,如果您更倾向于使用Python。 - Autoplectic
在这种情况下,使用Jython相比Python有什么优势? - Eric the Red
Eric:和 Ruby 一样 - 使用 Jython 将使他可以访问 Java 类,因此他可以使用 JFreeChart。 - Dave

0

问题要求一个在 rubypython 中的图表工具... 你提到的解决方案都是用 Flash 或 JavaScript 实现的... - user1055604

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

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接