Matplotlib Tutorial 14 – candlestick OHLC graphs




[ad_1]

In this Matplotlib tutorial, we’re going to cover how to create open, high, low, close (OHLC) candlestick charts within Matplotlib. These graphs are used to display time-series stock price information in a condensed form.

sample code: http://pythonprogramming.net
http://hkinsley.com
https://twitter.com/sentdex
http://sentdex.com
http://seaofbtc.com

Source


[ad_2]

Comment List

  • sentdex
    December 14, 2020

    Can you please a make a tutorial regarding drawing data from interactive brokers API and creating live candlestick chart

  • sentdex
    December 14, 2020

    How can I use range variable 'https://pythonprogramming.net/yahoo_finance_replacement' in this url(range =1 month) tried it but not work…plz suggest …

  • sentdex
    December 14, 2020

    This works in 2020:
    import matplotlib.pyplot as plt
    import csv
    import numpy as np
    import urllib.request
    import matplotlib.dates as mdates
    import matplotlib.ticker as mticker
    import mplfinance
    from mplfinance.original_flavor import candlestick_ohlc
    def bytespdate2num(fmt, encoding='utf-8' ):
    strconverter= mdates.strpdate2num(fmt)
    def bytesconverter(b):
    s = b.decode(encoding)
    return strconverter(s)
    return bytesconverter
    def graph_data(stock):
    fig = plt.figure()
    ax1= plt.subplot2grid((1, 1), (0,0))
    stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement'
    source_code = urllib.request.urlopen(stock_price_url).read().decode()
    stock_data = []
    split_source = source_code.split('n')
    for line in split_source:
    split_line = line.split(',')
    if len(split_line) == 7:
    if 'Volume' not in line and 'labels' not in line:
    stock_data.append(line)
    date, openp, highp, lowp, closep, adjustedp, volume = np.loadtxt(stock_data,
    delimiter=',',
    unpack=True,
    converters={0: bytespdate2num('%Y-%m-%d')})
    x = 0
    y = len(date)
    ohlc = []
    while x < y:
    append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x]
    ohlc.append(append_me)
    x+=1
    mplfinance.original_flavor.candlestick_ohlc(ax1, ohlc, width=0.4, colorup='g', colordown= 'r')
    for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    ax1.xaxis.set_major_locator(mticker.MaxNLocator(6))

    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.title('TSLA')
    plt.legend()
    plt.subplots_adjust(left = 0.09, bottom = 0.18, right = 0.94, top = 0.95, wspace=0.2, hspace=0)
    plt.show()
    graph_data('TSLA')

  • sentdex
    December 14, 2020

    The series since he started giving finance link is not working. you can't follow things not workable.

  • sentdex
    December 14, 2020

    How to plot volume to each candle. Volume ask right and volume bid left

  • sentdex
    December 14, 2020

    I love coding with you via the videos. Some modules or code you put there may be deprecated but I was able to google StackOverflow to fix the code or install the updated modules. It is a learning curve and I learned a lot from you.

  • sentdex
    December 14, 2020

    Fyi there is now a NEW, easier to use, VERSION of the matplotlib mplfinance package, with tutorials, that can be found here: https://pypi.org/project/mplfinance/

  • sentdex
    December 14, 2020

    could you tell me how to draw a line that shows the maximum value and minimum ?

  • sentdex
    December 14, 2020

    There is a NEW, easier to use VERSION of the matplotlib mplfinance package, that can be found here: https://pypi.org/project/mplfinance/

  • sentdex
    December 14, 2020

    I'm loving this series but could you do a whole vid on your interesting profanities. Goshdarnit anyone working with code needs a rich library of invective and you have a rich new one 😂…… Killing me smalls 😂😂

  • sentdex
    December 14, 2020

    It may be worth noting that since it is deprecated, you'll need to install mpl_finance with Anaconda Python terminal, not regular windows CMD. It wouldn't work for me with just the windows edition, had to do it in Anaconda.

  • sentdex
    December 14, 2020

    does anyone know how to do it with quandl dataframe, i mean getting a quandl dataframe and then plotting a candlestick graph from it.

  • sentdex
    December 14, 2020

    im having so trouble in making it happen can u help me with that

  • sentdex
    December 14, 2020

    hey how can i connect to my local database to get data from and to plot candlestick graph

  • sentdex
    December 14, 2020

    I found some bitcoin data with the open,close, high and low and decided it was a neat opportunity to graph a candlestick graph. It brings a bunch of errors hoping someone here might know the solution. Posted the code on stackoverflow https://stackoverflow.com/questions/51521733/matplotlib-plotting-candlestick-ohlc-error-invalid-data-types

  • sentdex
    December 14, 2020

    Matplotlib.finance has been depreciated for some time. I installed mpl_finance but I can't seem to use the module. Is it possible for you to do a video about installing and using mpl_finance?

  • sentdex
    December 14, 2020

    your killin me smalls

  • sentdex
    December 14, 2020

    Again, this my by wrongheaded hairsplitting. I think this video title should be "Matplotlib Tutorial 14 – Candlestick OHLC graphs".

  • sentdex
    December 14, 2020

    You're videos are unbelieveble! I think there have been some downs in people who watch them because of the video where you introduce us to internet data… sorry about that. But I can't stop watching these <3

  • sentdex
    December 14, 2020

    I am having this warning message and my graph isnt showing up.

    MatplotlibDeprecationWarning: The finance module has been deprecated in mpl 2.0 and will be removed in mpl 2.2. Please use the module mpl_finance instead.
    warnings.warn(message, mplDeprecation, stacklevel=1)

    Love your tutorial but this yahoo finance api being shutdown or whatever kinda ruins the fun.

  • sentdex
    December 14, 2020

    How could i add other stuff to the same plot? Like horizontal/vertical lines at arbitrary positions? it seems i cannot simply do plt.plot(date) after the candlestick_ohlc fucntion is called. thanks!

  • sentdex
    December 14, 2020

    Why isn't the ohlc graph complete. As in whats the reason for the gaps between candlesticks. Is there a way to fill the gaps.

  • sentdex
    December 14, 2020

    some might get an import error among other issues if you're using a different version of matplotlib or python. I had to make the following changes…

    remove the "_ohlc":

    from matplotlib.finance import candlestick

    change the order of the ohlc data:

    while x < y:
    append_me = date[x], openp[x], closep[x], highp[x], lowp[x]

  • sentdex
    December 14, 2020

    How would you get rid of the weekend gaps while still keeping dates as x-ticks? So far the only thing I can come up with is make each week its own subplot, which seems inefficient.

  • sentdex
    December 14, 2020

    Why not use a zip for creating ohlc? If you need a list instead of a generator, just wrap the whole thing in list() when you're making it.

    ohlc = zip(date, openp, highp, lowp, closep, volume)

  • sentdex
    December 14, 2020

    What is the required format for times rather than dates?

  • sentdex
    December 14, 2020

    Thank you Sentdex for your tutorials.I was trying to use a candlestick chart but for some reason the candlesticks are all green. I assume that closep<openp, then the candlesticks should be red. Any reason why?Or my assumption is wrong?

  • sentdex
    December 14, 2020

    wouldn't hurt to use "for x,y in enumerate(date)" or at least "for x in range(len(date))" instead of an ugly while loop for the ohlc data. i guess you did that for not to overcomplicate things for beginners, but i think being confronted with pythonic stuff like this will benefit them greatly. for more advanced python programmers, that kind of while loop is just irritating 😉
    anyway, thanks for this relatively quick overview of matplotlib. regarding the styling of labels and such, i learned a few things that i didn't know about from just browsing the wiki.

Write a comment