Reputation: 318
Using the following example I was able to create a candle stick graph using matplotlib.
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator,\
DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = (2004, 2, 1)
date2 = (2004, 4, 12)
mondays = WeekdayLocator(MONDAY) # major ticks on the mondays
alldays = DayLocator() # minor ticks on the days
weekFormatter = DateFormatter('%b %d') # e.g., Jan 12
dayFormatter = DateFormatter('%d') # e.g., 12
quotes = quotes_historical_yahoo_ohlc('INTC', date1, date2)
if len(quotes) == 0:
raise SystemExit
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)
#plot_day_summary(ax, quotes, ticksize=3)
candlestick_ohlc(ax, quotes, width=0.6)
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
And looking at plot.ly's APIs you can publish matplotlib figures online using plot.ly. I added the following to the above code:
import matplotlib.mlab as mlab
import plotly.plotly as py
py.sign_in('xxx', 'xxxx')
plot_url = py.plot_mpl(fig)
plot.ly produced the following graph, if you zoom in to the graph you can see that the graph does not actually show the body of the candle stick just the upper and lower shadows. Did I import the graph incorrectly? or does plot.ly not support candle stick graphs even if they are generated through matplotlib?
Upvotes: 1
Views: 1000
Reputation: 81
I actually made a request to the plot.ly devs to add candlestick chart types to the stock plot.ly package. Very surprised it still has yet to be included as a default "type"
For this use case, I was able to build my own OHLC candles by hacking together a pandas DataFrame
with a resampled time index using 'ohlc' as the param. Only caveat is you will need a full history of all the asset's trades with timestamps for the DataFrame
index to correctly build this type of chart:
newOHLC_dataframe = old_dataframe['Price'].astype(float).resample('15min', how='ohlc')
where the Price
key is all your y values. This .resample()
will build the candles and figure out max/min/first/last for the given time period all on its own. In my example above, it will give you a new DataFrame
consisting of 15min candles. The .astype(float)
may or may not be necessary depending on your underlying data.
Upvotes: 2
Reputation: 318
Found out as of 5/27/15 Plot.ly does not currently support candle stick.
Upvotes: 2