Graham Brown
Graham Brown

Reputation: 21

ImportError: cannot import name 'candlestick_ohlc' from 'mplfinance' - NG.L Stock Candlestick Chart.py

In June 2021 I ran a Python stock script taking data from the yahoo finance website to display a Japanese candlestick chart for the stock National Grid plc.

Unfortunately the program has now stopped working when I tried to run it on 10 July 2021, but I have changed mpl_finance to mplfinance.

The mpl_finance and mplfinance packages have been upgraded but I now get this error:

ImportError: cannot import name 'candlestick_ohlc' from 'mplfinance'

import datetime as dt
import pandas_datareader as web
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from mplfinance import candlestick_ohlc

# Define Time Frame
start = dt.datetime(2021,6,2) # National Grid plc shares were bought on 02/06/2021
end = dt.datetime.now() # Current Time

# Load Data from yahoo API
ticker = 'NG.L'
data = web.DataReader(ticker, 'yahoo', start, end)
print(data.columns)

# Stock Market Prices during work day
data = data[['Open', 'High', 'Low', 'Close']]
data.reset_index(inplace=True)
data['Date'] = data['Date'].map(mdates.date2num)

# Visualization of candlestick chart
ax = plt.subplot()
ax.grid(True)
ax.set_axisbelow(True)
ax.set_title('{}  - NATIONAL GRID PLC - JUNE 2021 - PRESENT'.format(ticker), color='black')
ax.set_facecolor('white')
ax.figure.set_facecolor('lightgray')
ax.tick_params(axis='x', colors='black')
ax.tick_params(axis='y', colors='black')
ax.xaxis_date()
candlestick_ohlc(ax, data.values, width=0.5, colorup='forestgreen', colordown='orangered') # Colours for Japanese candlesticks
plt.xlabel('TIMELINE OF NG.L STOCK', color='black')
plt.ylabel('STOCK PRICE IN BRITISH POUND STERLING', color='black')
plt.show()

I've had no problem running similar scripts like this until recently which is frustrating.

I installed Anaconda3 (Python 3.8.8 64-bit) and Python 3.9.6 (64-bit). The IDEs I use are PyScripter and Visual Studio Code.

Any help is appreciated to help me fix this problem.

Upvotes: 1

Views: 828

Answers (1)

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

To use the old candlestick_ohlc API, you have to change the import in your code above from:

from mplfinance import candlestick_ohlc

to

from mplfinance.original_flavor import candlestick_ohlc

Upvotes: 1

Related Questions