0

i am developing a financial graph using candlesticks. i face two different problems with the candels width: — they are too large — they are too small

depending on the amount of data in the interval i have.

i attach two examples: enter image description here enter image description here

i know i can control it with width param:

candlestick_ohlc(ax1, data, width=candel_width, colorup='g', colordown='r')

but i am looking for if there is a parameter that automatically handle the cases.

my code is: fig, ax1 = pyplot.subplots(figsize=(10, 5))

for i in ohlc:
    i['time'] = date2num(datetime.datetime.fromtimestamp(i['time']))
data = []
for i in ohlc:
    sub_lst = i['time'], i['open'], i['high'], i['low'], i['close']
    data.append(sub_lst)
candlestick_ohlc(ax1, data, width=candel_width, colorup='g', colordown='r')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m %H:%M'))
ax1.xaxis.set_major_locator(ticker.MaxNLocator(10))
ax1.grid(True)

pyplot.xlabel('Date')
pyplot.ylabel('Price')
pyplot.title(title)
pyplot.tight_layout()
fig.autofmt_xdate()
ax1.autoscale_view()

pyplot.show()
Scriptable
  • 19,402
  • 5
  • 56
  • 72
91DarioDev
  • 1,612
  • 1
  • 14
  • 31
  • 1
    The width of the candles should probably be something like the difference between two successive data values, i.e. `width=i['time'][1] -i['time'][0]` assuming that the dates are somehow equally spaced. – ImportanceOfBeingErnest Jan 25 '18 at 15:37
  • thank you! @ImportanceOfBeingErnest – 91DarioDev Jan 25 '18 at 15:48
  • @ImportanceOfBeingErnest it actually didn't work because that time is epoch and the second minus the first is like 600s and 600 as width paramater is actually too much – 91DarioDev Jan 25 '18 at 15:55
  • You need to do this with the data *after* `date2num` conversion. – ImportanceOfBeingErnest Jan 25 '18 at 16:06
  • 1
    @ImportanceOfBeingErnest thank you very much it now works. just a tip for who will read the post. the result of your suggestion is all the candles near without neither a little space. so i used `width = (2/3) * (ohlc[1]['time'] - ohlc[0]['time'])` to leave a bit of space between candles thanks – 91DarioDev Jan 25 '18 at 16:26
  • 2
    Maybe you can write an answer yourself, such that this question will not stay unanswered. – ImportanceOfBeingErnest Jan 25 '18 at 19:33

0 Answers0