# Adding a new line to a candlestick plot

**URL:** <https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [January 15, 2021, 7:17am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375 "2021-01-15T07:17:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 15, 2021, 7:17am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/1 "2021-01-15T07:17:41Z")

</div>

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:

```julia
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:

```julia
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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 15, 2021, 7:56am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/2 "2021-01-15T07:56:54Z")

</div>

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:

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

```

![image](https://global.discourse-cdn.com/julialang/original/3X/2/0/2020cb4d1123100c7ef42c11df177a895af0fced.png)

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?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 15, 2021, 7:59am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/3 "2021-01-15T07:59:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 15, 2021, 10:02am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/4 "2021-01-15T10:02:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 15, 2021, 10:03am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/5 "2021-01-15T10:03:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 15, 2021, 10:08am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/6 "2021-01-15T10:08:32Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 15, 2021, 10:17am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/7 "2021-01-15T10:17:30Z")

</div>

[Issue filed](https://github.com/JuliaStats/TimeSeries.jl/issues/476) (and I pinged you in it)

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 15, 2021, 10:19am UTC](https://discourse.julialang.org/t/adding-a-new-line-to-a-candlestick-plot/53375/8 "2021-01-15T10:19:29Z")

</div>

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

Hopefully Michael can come up with a better idea.
