Adding a new line to a candlestick plot

Hi all,

The TimeSeries.jl package allows the construction of a candlestick plot. This works very nicely for me. However, sometimes I’d like to add another line to overlay the candlestick plot. I’ve been messing around for an hour or so now and can’t work out a way to do this.

As a test case, I just tried to add a line that passes through the open price on the candlestick plot. I tried this:

using Plots, MarketData, TimeSeries
gr()
ta = yahoo(:GOOG, YahooOpt(period1 = now() - Month(1)))
p = plot(ta, seriestype = :candlestick)
plot!(p, ta[:Open], seriestype = :line)

But this seems to squish the candles up on the left of the plot and the line up on the right.

I also tried extracting the dates and values of the open price separately into, say, dt, and x, and then use:

plot!(p, dt, x)

but this had the same issue.

Does anyone have any ideas if it is possible to add a line to an existing candlestick plot?

Cheers,

Colin

It looks like the candlestick recipe is tricking you by basically just putting bars centered at 0.5:1:nrow(ta) and then overwriting the xticks with the dates. You can do:

plot!(0.5:1:20.5, values(ta.Open))

image

I think ideally they would just use the actual Dates as x locations, but there might be reasons I’m unaware of why they had to do it this way. Maybe worth filing an issue?

Yeah open an issue and ping mkborregaard - I wrote that recipe but can’t for the life of me remember why the x axis was implemented in that way

1 Like

Ah! I figured it would be something like this but I couldn’t quite parse the source effectively enough to spot it. I did experiment with whether the recipe was parsing the strings before creating the plot, but I didn’t quite get to the extra step of working out the type was changed to float range.

My guess is that it was done as a hack to get all the bars to be the same width. If you just used the dates then a non-synchronous date input could lead to drastically different bar widths across the plot.

Anyway, thanks so much for this. That’s the key piece of info I was looking for.

And I’ll file an issue.

Cheers,

Colin

I’ll file an issue for this. However, my guess is that you did it in order to guarantee fixed bar widths across the plot. If you didn’t do this and they user input non-synchronous date inputs then I think you would end up with large variation in bar widths.

So I’ll file the issue, but I don’t think the solution will be straightforward.

Cheers and thanks for your work on TimeSeries.jl,

Colin

Ah yes, that makes a lot of sense actually - you don’t want either very wide bars or gaps in the plot on non-trading days. Not immediately sure how one would do that, but fortunately Michael knows Plots a lot better than me :slight_smile:

Issue filed (and I pinged you in it)

Yeah, I’ve filed the issue, but I can’t think of any general solution that one would call “elegant”. :slight_smile:

Hopefully Michael can come up with a better idea.