Djalil lounis
Djalil lounis

Reputation: 121

Issue when trying to create candlestick chart using candlestick_ohlc

I am trying to build a candlestick from data collected in an API below a simple of the data it is list that hase date Open High low close and volume :

[[1502928000000, 4261.48, 4485.39, 4200.74, 4285.08, 795.150377], 
[1503014400000, 4285.08, 4371.52, 3938.77, 4108.37, 1199.888264], 
[1503100800000, 4108.37, 4184.69, 3850.0, 4139.98, 381.309763], 
[1503187200000, 4120.98, 4211.08, 4032.62, 4086.29, 467.083022], 
[1503273600000, 4069.13, 4119.62, 3911.79, 4016.0, 691.74306], 
[1503360000000, 4016.0, 4104.82, 3400.0, 4040.0, 966.684858], 
[1503446400000, 4040.0, 4265.8, 4013.89, 4114.01, 1001.136565], 
[1503532800000, 4147.0, 4371.68, 4085.01, 4316.01, 787.418753]]

I have noticed that I have 3 extra 0 at the end of the timestamp, so I created this code to remove them and convert them to time :

for i in ohlc:
Date = str(i[0])
last=len(Date)-3
Date =Date[:last]
Date=float(Date)
Date = datetime.datetime.fromtimestamp(Date).strftime('%d-%m-%y %H:%M:%S')
i[0] =Date
print(i[0])

the date now is this format : 13-03-18 01:00:00 now the problem when I try to use matplotlib to plot with this code :

fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
candlestick_ohlc(ax1,ohlc,width=0.002)

date_fmt = '%d-%m-%y %H:%M:%S'
fig.subplots_adjust(bottom=0.2)

ax1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d'))

#ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(symbol)
#plt.legend()
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()

I am having an error below I think that the problem is with the Dates. Maybe it it is worth mentioning that I tried to use the list without converting the timestamp but it did not work error :

Traceback (most recent call last):
File "chartingV2.py", line 84, in <module>
candlestick_ohlc(ax1,ohlc,width=0.002)
File "/usr/local/lib/python3.5/dist-packages/mpl_finance-0.10.0-
py3.5.egg/mpl_finance.py", line 236, in candlestick_ohlc
File "/usr/local/lib/python3.5/dist-packages/mpl_finance-0.10.0-
py3.5.egg/mpl_finance.py", line 302, in _candlestick
TypeError: unsupported operand type(s) for -: 'str' and 'float'

Upvotes: 0

Views: 825

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339250

Since mpl_finance.candlestick_xxx plots cannot plot strings, you need to use a numerical representation of the dates. In order for matplotlib to be able to interprete those numbers you may use the matplotlib.dates.dates2num() function.

Hence replace the for loop by

for row in ohlc:
    row[0] = mdates.date2num(datetime.datetime.fromtimestamp(row[0]/1000.))

Complete code:

import datetime
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates

ohlc = [[1502928000000, 4261.48, 4485.39, 4200.74, 4285.08, 795.150377], 
        [1503014400000, 4285.08, 4371.52, 3938.77, 4108.37, 1199.888264], 
        [1503100800000, 4108.37, 4184.69, 3850.0, 4139.98, 381.309763], 
        [1503187200000, 4120.98, 4211.08, 4032.62, 4086.29, 467.083022], 
        [1503273600000, 4069.13, 4119.62, 3911.79, 4016.0, 691.74306], 
        [1503360000000, 4016.0, 4104.82, 3400.0, 4040.0, 966.684858], 
        [1503446400000, 4040.0, 4265.8, 4013.89, 4114.01, 1001.136565], 
        [1503532800000, 4147.0, 4371.68, 4085.01, 4316.01, 787.418753]]

for row in ohlc:
    row[0] = mdates.date2num(datetime.datetime.fromtimestamp(row[0]/1000.))

fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
candlestick_ohlc(ax1,ohlc,width=0.2)

date_fmt = '%d-%m-%y %H:%M:%S'
fig.subplots_adjust(bottom=0.2)

ax1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d'))

for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

enter image description here

Upvotes: 1

Related Questions