Macumbaomuerte
Macumbaomuerte

Reputation: 2247

Weird random width on matplotlib candlestick_ohlc function

Im using candlestick_ohlc from matplotlib to draw short candlestick patterns, I have the settings for width as 0.01. and always drawing 6 candles.

Most of the times the bars look as expected, but 1 out of 5 of each images (more or less) are drawn with a way bigger width that breaks the graph.

graph exemple

Here's an example of how I use my function:

candlestick_ohlc(ax1, candles, width=0.01,colorup='green', colordown='red')

and an example of the data being processed:

[[7.37151000e+05 1.30622000e+00 1.30658000e+00 1.30597000e+00  1.30610000e+00]
 [7.37151010e+05 1.30609000e+00 1.30619000e+00 1.30500000e+00  1.30595000e+00]
 [7.37151021e+05 1.30594000e+00 1.30621000e+00 1.30575000e+00  1.30602000e+00]
 [7.37151031e+05 1.30603000e+00 1.30644000e+00 1.30603000e+00  1.30632000e+00]
 [7.37151042e+05 1.30633000e+00 1.30648000e+00 1.30622000e+00  1.30648000e+00]
 [7.37151052e+05 1.30644000e+00 1.30661000e+00 1.30551000e+00  1.30553000e+00]]

[[7.37151010e+05 1.30609000e+00 1.30619000e+00 1.30500000e+00  1.30595000e+00]
 [7.37151021e+05 1.30594000e+00 1.30621000e+00 1.30575000e+00  1.30602000e+00]
 [7.37151031e+05 1.30603000e+00 1.30644000e+00 1.30603000e+00  1.30632000e+00]
 [7.37151042e+05 1.30633000e+00 1.30648000e+00 1.30622000e+00  1.30648000e+00]
 [7.37151052e+05 1.30644000e+00 1.30661000e+00 1.30551000e+00  1.30553000e+00]
 [7.37151062e+05 1.30553000e+00 1.30590000e+00 1.30529000e+00  1.30573000e+00]]

[[7.37151060e+05 1.30561000e+00 1.30561000e+00 1.30549000e+00  1.30551000e+00]
 [7.37151060e+05 1.30550000e+00 1.30568000e+00 1.30550000e+00  1.30568000e+00]
 [7.37151061e+05 1.30565000e+00 1.30580000e+00 1.30562000e+00  1.30580000e+00]
 [7.37151062e+05 1.30580000e+00 1.30583000e+00 1.30578000e+00  1.30578000e+00]
 [7.37151062e+05 1.30579000e+00 1.30590000e+00 1.30566000e+00  1.30573000e+00]
 [7.37151063e+05 1.30573000e+00 1.30574000e+00 1.30550000e+00  1.30561000e+00]]

Upvotes: 1

Views: 297

Answers (1)

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

As ImportanceOfBeingErnest said "In those cases where it doesn't work out, you need to further reduce the width. More precisely, the width should never be larger than the difference between two successive x data points."

That said, there is a new version of matplotlib finance that handles this automatically for you. You can find the new version, along with documentation, 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)

Upvotes: 1

Related Questions