gpt4 book ai didi

python - Ruby 或 Python 中的财务图表/图形

转载 作者:太空狗 更新时间:2023-10-29 17:37:19 26 4
gpt4 key购买 nike

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

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

JFreeChart 可以在 Java 中执行此操作,但我希望我的代码库尽可能小和简单。

谢谢!

最佳答案

您可以使用 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) 元组绘制绘图的函数中(并且您可能不想随机分配您的开盘价和收盘价)。

关于python - Ruby 或 Python 中的财务图表/图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1089307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com