与python的每周stockchart

时间:2016-02-22 15:50:48

标签: python matplotlib finance candlestick-chart

我试图制作一些带技术指标的每周股票图表(移动平均线,成交量移动平均线等,我目前使用ta-lib)。我用matplotlib编写了日常图表(下面的代码)。我没有设法生成每周图表,其中5欧姆蜡烛组合成一周蜡烛。因此,我们非常感谢您的帮助。

如果首选matplotlib,我愿意接受替代解决方案。但是,它们应该在python中。

import matplotlib.pyplot as plt
import matplotlib.finance as mpf
import numpy as np
ticker = ('AAPL')
start = (2016, 1, 1)
end = (2016, 2, 12)

quotes = np.array(mpf.quotes_historical_yahoo_ohlc(ticker, start, end))

y = np.linspace(90, 105, len(quotes))
fig, (ax1, ax2) = plt.subplots(2, sharex=True, figsize=(8, 6))
mpf.candlestick_ohlc(ax1, quotes, width=0.6, colorup='g', colordown='r')
ax1.set_title('aapl')
ax1.set_ylabel('index level')
ax1.grid(True)
ax1.xaxis_date()
plt.bar(quotes[:, 0] - 0.25, quotes[:, 5], width=0.5)
ax2.set_ylabel('volume')
ax2.grid(True)
ax2.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=30)

1 个答案:

答案 0 :(得分:0)

编写一个函数,返回用于绘制蜡烛棒(OHLC)的每个变量的每周平均值。这些方面的东西:

closePrice = [2.0, 2.3, 2.6, 2.5, 2.2, 2.1, 1.8, 1.9, 2.2, 2.5, 3.7, 3.8, 3.4]

def weeklyFromDaily(values):
    weeklyData = []
    numberOfdays = len(values)
    print('numberOfDays', numberOfdays)
    numberOfWeeks = int(numberOfdays/5)
    print('NumberOfWeeks', numberOfWeeks)
    weekNum = 0
    while weekNum < numberOfWeeks:
        oneWeekAvg = (values[0+(5*weekNum)]+values[1+(5*weekNum)]+values[2+(5*weekNum)]+values[3+(5*weekNum)]+values[4+(5*weekNum)]) / 5.0
        weeklyData.append(oneWeekAvg)
        weekNum += 1

    return weeklyData

x = weeklyFromDaily(closePrice)
print('Weekly Data Set', x)