Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - Matplotlib candlestick in minutes

Good afternoon,

I would like to see if any of you could help me make a candle chart in minutes. I have managed to graph them in days but I do not know how to do them in minutes.

Attached code.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates, ticker
import matplotlib as mpl
from mpl_finance import candlestick_ohlc

mpl.style.use('default')

data = [('2017-01-02 02:00:00', '1.05155', '1.05197', '1.05155', '1.0519'),
    ('2017-01-02 02:01:00', '1.05209', '1.05209', '1.05177', '1.05179'),
    ('2017-01-02 02:02:00', '1.05177', '1.05198', '1.05177', '1.05178'),
    ('2017-01-02 02:03:00', '1.05188', '1.052', '1.05188', '1.052'),
    ('2017-01-02 02:04:00', '1.05196', '1.05204', '1.05196', '1.05203'),
    ('2017-01-02 02:06:00', '1.05196', '1.05204', '1.05196', '1.05204'),
    ('2017-01-02 02:07:00', '1.05205', '1.0521', '1.05205', '1.05209'),
    ('2017-01-02 02:08:00', '1.0521', '1.0521', '1.05209', '1.05209'),
    ('2017-01-02 02:09:00', '1.05208', '1.05209', '1.05208', '1.05209'),
    ('2017-01-02 02:10:00', '1.05208', '1.05211', '1.05207', '1.05209')]

ohlc_data = []

for line in data:
    ohlc_data.append((dates.datestr2num(line[0]), np.float64(line[1]), np.float64(line[2]), np.float64(line[3]), np.float64(line[4])))

fig, ax1 = plt.subplots()
candlestick_ohlc(ax1, ohlc_data, width = 0.5, colorup = 'g', colordown = 'r', alpha = 0.8)

ax1.xaxis.set_major_formatter(dates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.xaxis.set_major_locator(ticker.MaxNLocator(10))

plt.xticks(rotation = 30)
plt.grid()
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Historical Data EURUSD')
plt.tight_layout()
plt.show()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So close, but only trial and error will get you any further. Isn't crappy documentation great?

Simply divide width by the number of minutes in a day. Full code for your copy & paste pleasure below, but all I've done is change width = 0.5 to width = 0.5/(24*60).

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates, ticker
import matplotlib as mpl
from mpl_finance import candlestick_ohlc

mpl.style.use('default')

data = [('2017-01-02 02:00:00', '1.05155', '1.05197', '1.05155', '1.0519'),
    ('2017-01-02 02:01:00', '1.05209', '1.05209', '1.05177', '1.05179'),
    ('2017-01-02 02:02:00', '1.05177', '1.05198', '1.05177', '1.05178'),
    ('2017-01-02 02:03:00', '1.05188', '1.052', '1.05188', '1.052'),
    ('2017-01-02 02:04:00', '1.05196', '1.05204', '1.05196', '1.05203'),
    ('2017-01-02 02:06:00', '1.05196', '1.05204', '1.05196', '1.05204'),
    ('2017-01-02 02:07:00', '1.05205', '1.0521', '1.05205', '1.05209'),
    ('2017-01-02 02:08:00', '1.0521', '1.0521', '1.05209', '1.05209'),
    ('2017-01-02 02:09:00', '1.05208', '1.05209', '1.05208', '1.05209'),
    ('2017-01-02 02:10:00', '1.05208', '1.05211', '1.05207', '1.05209')]

ohlc_data = []

for line in data:
    ohlc_data.append((dates.datestr2num(line[0]), np.float64(line[1]), np.float64(line[2]), np.float64(line[3]), np.float64(line[4])))

fig, ax1 = plt.subplots()
candlestick_ohlc(ax1, ohlc_data, width = 0.5/(24*60), colorup = 'g', colordown = 'r', alpha = 0.8)

ax1.xaxis.set_major_formatter(dates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.xaxis.set_major_locator(ticker.MaxNLocator(10))

plt.xticks(rotation = 30)
plt.grid()
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Historical Data EURUSD')
plt.tight_layout()
plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...