4
$\begingroup$

I'm trying to plot a DateListPlot and a CandleStickChart together.

Initially I was trying to add a FinancialIndicator by spelunking TradingChart, but it's quite complex, I think I could manage to do it, but it would take some time.

Here's a basic example that doesn't work

data = FinancialData["IBM", "OHLCV", {{2009, 5, 1}, {2010, 4, 30}}];
y = CandlestickChart@data;
x = DateListPlot[{{{2009, 5, 1}, 1}, {{2010, 4, 30}, 40}}];
Show[{x, y}]
$\endgroup$
3
$\begingroup$

The underlying issue is that CandlestickChart is not using date values on its x-axis. With First@y we can see that the x-axis values are index positions (and slight offsets) of the values in data.

You can get the benefits of Show (e.g. retain interactivity) by listing the CandlestickChart first and using ListPlot with values indexed to their respective dates in data.

For example, if listPoints contains the dates and values you want for your ListPlot.

listPoints = {{{2009, 5, 1}, 85}, {{2009, 9, 1}, 100}, {{2010, 4, 1}, 
   110}, {{2010, 4, 30}, 120}};

Then a ListPlot on the same scale as the CandlestickChart can be made by the following.

x1 = ListPlot[
      {First@PositionIndex[data[[All, 1]]][First@#],Last@#} & /@ listPoints, 
      Joined -> True]

Mathematica graphics

Then these can be combined with Show.

Show[y, x1]

Mathematica graphics

The show has all ticks and grid lines of the CandlestickChart since it was listed first and keeps the interactivity of all the charts (here only the candle stick chart has interactivity).

The above assumes that the dates in listPoints are in data. If not then you will need to add some interpolation to assign an x-value between the two data date indices that the listPoints date falls in between.

Hope this helps.

$\endgroup$
3
$\begingroup$
x =
 DateListPlot[{{{2009, 5, 1}, 1}, {{2010, 4, 30}, 40}}, 
   Axes -> False, Frame -> False, ImagePadding -> 20];

y = CandlestickChart[data, ImagePadding -> 20];

Overlay[{x, y}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you, simple and effective ! $\endgroup$ – faysou Dec 27 '15 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.