使用Ruby或Python等高級語言創建金融開放高低收盤價(OHLC)圖表的最佳選擇是什麼?雖然似乎有很多圖表的選項,但我還沒有看到這種圖表的任何寶石或雞蛋。Ruby或Python中的金融圖表/圖表
http://en.wikipedia.org/wiki/Open-high-low-close_chart(但我不需要均線或布林通道)
的JFreeChart可以做到這一點在Java中,但我想使我的代碼庫小而簡單越好。
謝謝!
使用Ruby或Python等高級語言創建金融開放高低收盤價(OHLC)圖表的最佳選擇是什麼?雖然似乎有很多圖表的選項,但我還沒有看到這種圖表的任何寶石或雞蛋。Ruby或Python中的金融圖表/圖表
http://en.wikipedia.org/wiki/Open-high-low-close_chart(但我不需要均線或布林通道)
的JFreeChart可以做到這一點在Java中,但我想使我的代碼庫小而簡單越好。
謝謝!
你可以使用matplotlib和matplotlib.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()
創建這樣一個情節:
很明顯,你會想使用(open, close, min, max)
元組將函數打包到一個函數中(並且您可能不想隨機分配您的開盤價和收盤價)。
您是否考慮過使用R和quantmod包?它可能提供你所需要的。
您可以在Python中使用Pylab(matplotlib.finance
)。這裏有一些例子:http://matplotlib.sourceforge.net/examples/pylab_examples/plotfile_demo.html。在Beginning Python Visualization中有一些關於這個問題的很好的材料。
更新:我認爲你可以使用matplotlib.finance.candlestick作爲日本的燭臺效果。
你可以自由使用JRuby而不是Ruby嗎?這可以讓你使用JFreeChart,加上你的代碼仍然在Ruby中
這同樣適用於jython,如果你更傾向於使用python。 – Autoplectic 2009-07-08 19:05:30
在這種情況下使用python比python有什麼優勢? – 2009-07-09 01:06:26
Eric:與Ruby相同 - 使用Jython會讓他訪問Java類,所以他可以使用JFreeChart – 2009-07-09 22:09:20
請看WHIFF的開放式閃存卡嵌入 http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1600.openFlashCharts 蠟燭圖的例子就在頂部。這將是特別適合嵌入網頁的 。
如果您喜歡示例的外觀,打開Flash圖表是不錯的選擇。我已經將HTML嵌入式圖表移動到了JavaScript/Canvas庫,如Flot,因爲它更具可定製性,並且在沒有太多黑客入侵的情況下獲得所需效果(http://itprolife.worona.eu/2009/08/scatter-chart-library-moving-to-flot.html)。
這是使用Matplotlib股票圖表我畫就在幾天前,我已經發布了源太,供大家參考:StockChart_Matplotlib
一些使用matplotlib對金融地塊的例子(OHLC)可以在這裏找到:
#!/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()
這看起來很有希望!我會盡快檢查。謝謝! – 2009-07-06 22:22:10