2

次のような OHLC チャート (始値、高値、安値、終値) を作成したい株式データがたくさんあります。

ここに画像の説明を入力

このために、matplotlib.financeのローソク足関数を利用します。私は分単位のデータを持っているので、日中チャートも作成するために、この別のスタックオーバーフロー スレッドを使用しました。これは、すべてのローソク足を均等に配置して、日の間のギャップを回避します (午後 5 時 30 分と午前 9 時の間にデータがないため)。 )。これは少量のデータではかなりうまく機能しますが、残念ながら大量のデータがあります (1 日あたり 510 分 * 10 年 ≈ 100 万分)。このデータから、概要を示すチャートを作成したいと考えていますが、ズームインして、履歴の特定の日の個々の分を確認することもできます。

これを行うために、単純に巨大な (非常に幅の広い) 図形を作成して保存し、任意の画像ビューアーで簡単に拡大することを考えました。

他のスレッドからコピーして少し調整したコードがいくつかあります。コードでは、最初にランダムなサンプル データをいくつか作成します (他のスレッドの元のサンプル データが削除されたため)。このデータから、固定のローソク足の幅と、個々のローソク足の幅 (len(data)*candlestickWidth) を分数倍した数値の幅を持つ図を作成します。コードと結果の画像を以下に示します。これは、1 日 2 日と 15 分 (合計 30 分) しかありません。

私の質問は次のとおりです。ローソク足を配置して、スティック間にスペースがないようにし、画像の幅がスティックの量に依存するようにして、分を追加すると幅が広くなるようにするにはどうすればよいですか?

すべてのヒントは大歓迎です!

import numpy as np
import matplotlib.pyplot as plt
import datetime
import random

from matplotlib.finance import candlestick
from matplotlib.dates import num2date, date2num

# Create sample data for 5 days. Five columns: time, opening, close, high, low
jaar = 2007
maand = 05
data = np.array([[1.0,1.0,1.0,1.0,1.0]])
quotes = [(5, 6, 7, 4), (6, 9, 9, 6), (9, 8, 10, 8), (8, 6, 9, 5), (8, 11, 13, 7)]

for dag in range(5, 7):
    for uur in range(9, 10):
        for minuut in range(15):
            numdatumtijd = date2num(datetime.datetime(jaar, maand, dag, uur, minuut))
            koersdata = quotes[random.randint(0,4)]
            data = np.append(data, [[numdatumtijd, koersdata[0], koersdata[1], koersdata[2], koersdata[3], ]], axis=0)

data = np.delete(data, 0, 0)
print('Ready with building sample data')

# determine number of days and create a list of those days
ndays = np.unique(np.trunc(data[:,0]), return_index=True)
xdays =  []
for n in np.arange(len(ndays[0])):
    xdays.append(datetime.date.isoformat(num2date(data[ndays[1],0][n])))

# creation of new data by replacing the time array with equally spaced values.
# this will allow to remove the gap between the days, when plotting the data
data2 = np.hstack([np.arange(data[:,0].size)[:, np.newaxis], data[:,1:]])

# plot the data
candlestickWidth = 0.2
figWidth = len(data) * candlestickWidth
fig = plt.figure(figsize=(figWidth, 5))
ax = fig.add_axes([0.05, 0.1, 0.9, 0.9])
# customization of the axis
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.tick_params(axis='both', direction='out', width=2, length=8, labelsize=12, pad=8)
ax.spines['left'].set_linewidth(2)
ax.spines['bottom'].set_linewidth(2)

# set the ticks of the x axis only when starting a new day
ax.set_xticks(data2[ndays[1],0]) ## (Also write the code to set a tick for every whole hour)
ax.set_xticklabels(xdays, rotation=45, horizontalalignment='right')

ax.set_ylabel('Quotes', size=20)
# Set limits to the high and low of the data set
ax.set_ylim([min(data[:,4]), max(data[:,3])])
# Create the candle sticks
candlestick(ax, data2, width=candlestickWidth, colorup='g', colordown='r')

plt.show()

ここに画像の説明を入力

4

0 に答える 0