4

I would like to create a candlestick chart on Matplotlib. I have found many examples on the web but so far they are all using connections to yahoo finance or other kind of data, while not explaining well how to get the same result when you have a list of tuples containing the date, the open, the close, the high and the low prices. Often, indeed, it happens that you already have historical values, or estimated values, or more in general you just don't want to use the numbers coming from a provider such as Yahoo Finance. What I would like to know is the very basic code to make something like create a candlestick chart with your own list of values. Assume that I have a list of tuples with all the data I need for two days:

Prices = [('01/01/2010', 1.123 (open), 1.212 (close), 1.463 (high), 1.056(low)),
          ('02/01/2010', 1.121 (open), 1.216 (close), 1.498 (high), 1.002(low))] 

What should I code exactly to get a candlestick plot (that means a plot where every element of the list "Prices" is creating a candlestick) with these two data points? I can of course manipulate the data (example the date string to be converted in a float day etc.), but I cannot get the simple commands to create the chart. Anyone can help?

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • What's wrong with matplotlib's own example on `candlestick` http://matplotlib.org/examples/pylab_examples/finance_demo.html? You just need to adapt it to your data (for which we have btw no idea what you want plotted either). – Benjamin Bannier Jan 14 '14 at 11:17
  • Just wasn't understanding how to structure my data because it was not explicit: `quotes = quotes_historical_yahoo('INTC', date1, date2)`. But now I've found out [here](http://doc.astro-wise.org/matplotlib.finance.html) that it must be "quotes is a sequence of (time, open, close, high, low, ...) sequences". So now it works, mine was just a generic question cause I've seen that in the web it's hard to find a very basic example and many times the user prefer to use their own data rather than yahoo finance ones. Thanks anyway. – Matteo NNZ Jan 14 '14 at 11:28
  • Your question leaves out important details for it to be useful as a future reference. Maybe you could write down what information you would like your candlestick graph to display and what exactly the different fields in `Prices` stand for. – Benjamin Bannier Jan 14 '14 at 11:54
  • You're right. I solved anyway, so I'm gonna write the question + the answer in a clearer form to be useful as future reference :) – Matteo NNZ Jan 14 '14 at 12:14

1 Answers1

5

Following the matplotlib example I have got to the following solution:

from pylab import *
import matplotlib.pyplot as plt
from datetime import datetime
import time
from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
     DayLocator, MONDAY
from matplotlib.finance import candlestick,\
     plot_day_summary, candlestick2


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

#starting from dates expressed as strings...
Date1 = '01/01/2010'
Date2 = '02/01/2010'
#...you convert them in float numbers....
Date1 = date2num(datetime.strptime(Date1, "%d/%m/%Y"))
Date2 = date2num(datetime.strptime(Date2, "%d/%m/%Y"))
#so redefining the Prices list of tuples...
Prices = [(Date1, 1.123, 1.212, 1.463, 1.056), (Date2,1.121, 1.216, 1.498, 1.002)]
#and then following the official example. 
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)
candlestick(ax, Prices, width=0.6)

ax.xaxis_date()
ax.autoscale_view()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

plt.show()
Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • congratulations on solving your own problem! Please remember to accept your own answer when it will let you (I think it is like a day delay). – tacaswell Jan 14 '14 at 14:27
  • 1
    1. The finance sub-module is not being maintained, and is depracated. 2. candlestick needs to be changed to _candlestick to work. 3. When I tested it, I removed the imports of plot_day_summary and candlestick2; they were not used in the code. 4. I don't know another way to plot candlesticks or ohlc bars in matplotlib. Ideally someone should write a new, very basic set of special bars for that. Multiple colors would be nice (the plot I generated was pure black on white). – Bit Chaser Oct 07 '17 at 15:08
  • 1
    @bitchaser write it as a new answer please, this thread is very old (january 2014), it's probably true what you say so new users should be informed – Matteo NNZ Oct 07 '17 at 22:28
  • @MatteoNNZ I can't answer a closed question. The function is now available as candlestick_ohlc according to https://pythonprogramming.net/candlestick-ohlc-graph-matplotlib-tutorial/ . That program works with python 3.6, but has lots of data so I suggest limiting it if you want to test it. I don't wish to pursue a new question, but feel free to do so if you like. – Bit Chaser Oct 08 '17 at 19:25