Ruby或Python中的财务图表/图表 [英] Financial Charts / Graphs in Ruby or Python

查看:260
本文介绍了Ruby或Python中的财务图表/图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby或Python这样的高级语言中创建财务开放 - 高低 - 接近(OHLC)图表时,我最好的选择是什么?虽然似乎有很多图形选项,我没有看到任何宝石或鸡蛋与这种图表。



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



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

谢谢!

解决方案

您可以使用 matplotlib 的可选底部 /matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.barrel =nofollow noreferrer> matplotlib.pyplot.bar 。然后,您可以使用 plot 表示开盘价和收盘价:



例如:

 #!/ usr / bin / env python 
import numpy as np
import matplotlib.pyplot as plt
从matplotlib导入行

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(例如,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, ,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,61,62,63,68,66,65,66,63,63,62,61,61,57,61,64,63,58, 59,54,57,54,54,50,53,51,48,43,42,38,37,39,44,49,47,43]


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

#随机分配开盘价和收盘价
openings = rand_pt(bases,deltas)
closings = rand_pt(bases,deltas)

#首先我们绘制显示高低价格的柱状图
#bottom持有低价,而deltas持有高和低之间的差额
#。
width = 0
ax = plt.axes()
rects1 = ax.bar(np.arange(50),deltas,width,color ='r',bottom = bases)

#现在绘制指示开盘价和收盘价的价格
用于开盘,收盘,拉链(开仓,收盘,rects1):
x,w = bar.get_x ,0.2

args = {
}

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


plt.show()

创建如下图:





显然,你想把这个包装在一个函数中使用(打开,关闭,最小,最大)绘制图形(您可能不想随机分配开盘价和收盘价)。


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!

解决方案

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:

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

这篇关于Ruby或Python中的财务图表/图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆