2
votes

I just started out with Python, so bit of a noob here. I managed to figure out how to get data from an API to retrieve stock data. after reindexing it columns come out like this: [date, 1. open, 2. close, 3. high, 4. low, 5. volume]

I used MPL_finance and matplotlib and now I got a nice looking candlestick in a chart, succes! :) But I have been cracking my head how to add the volume in the SAME figure for saving. volume is in the dataframe onder the 5. volume column. Can anyone help me out? thanks!


Code:

import matplotlib.pyplot as plt

from mpl_finance import candlestick_ohlc

import pandas as pd

import matplotlib.dates as mpl_dates

stock_names=['AAPL']
ts = TimeSeries(key='xxx',output_format='pandas')
data, meta_data = ts.get_daily(symbol=stock_names, outputsize='compact')

data = df.reset_index()

#slice to make the chart look better

data = data[:-50]

plt.style.use('ggplot')

#Extracting Data for plotting

ohlc = data.loc[:, ['date', '1. open', '2. high', '3. low', '4. close']]
ohlc['date'] = pd.to_datetime(ohlc['date'])
ohlc['date'] = ohlc['date'].apply(mpl_dates.date2num)
ohlc = ohlc.astype(float)

#Creating Subplots

fig, ax = plt.subplots()

candlestick_ohlc(ax, ohlc.values, width=0.6, colorup='green', colordown='red', alpha=0.8)

#Setting labels & titles

ax.set_xlabel('Date')
ax.set_ylabel('Price')
fig.suptitle('Daily Candlestick Chart')

#Formatting Date

date_format = mpl_dates.DateFormatter('%d-%m-%Y')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()

plt.show()
1

1 Answers

1
votes

Try the new version of matplotlib finance. It handles DataFrames and Volume automatically. Documentation and package here:

https://pypi.org/project/mplfinance/

Install with: pip install --upgrade mplfinance

NOTE: The package name no longer has the dash or underscore: It is now mplfinance (not mpl-finance, nor mpl_finance).