4
$\begingroup$

I am making a Tradingchart with data returned from Financialdata. I find I cannot add a line to the chart by giving the option Epilog. (For example, to draw a line from the 1st day to the 50th day.) So which coordinate shall I tell Epilog?

data = FinancialData["SP500", "OHLCV", {2014, 1, 1}];
TradingChart[data,
  {Style[FinancialIndicator["SimpleMovingAverage", 5], Blue],
   Style[FinancialIndicator["SimpleMovingAverage", 20], Red],
   Style[FinancialIndicator["Volume"], Black],
   Style[FinancialIndicator["MACD"], {Green, Purple}]}, 
  Epilog -> Line[{data[[1]], data[[50]]}]]

When I tried this, it doesn't work. Anybody have some ideas?

$\endgroup$
  • $\begingroup$ That's a challenging task. The difficulty is that you have not one, but at least two plots, stacked on top of each other with each execution of TradingChart. So, the plot coordinates are not the same as the final coordinates which is where Epilog operates. I'm not sure if there is a straightforward way to accomplish this. $\endgroup$ – rcollyer Apr 1 '15 at 13:06
  • $\begingroup$ Trading Chart is a dynamic beast prone to change its internals in future versions. I don't think you can do it without messing with specific characteristics that will surely impair its future portability $\endgroup$ – Dr. belisarius Apr 1 '15 at 13:41
1
$\begingroup$

It looks like TradingChart rescales the chart from 1 to 100, so in order to add Epilog features, you must first know the range of your dates and then rescale the values. Here is a clunky way to do so.

Clear[tchartx]

tchartx[val_, limits_: data[[{1, -1}, 1]]] := 
 Rescale[AbsoluteTime[val], AbsoluteTime /@ limits, {1, 100}]

data = FinancialData["SP500", "OHLCV", {2014, 1, 1}];
TradingChart[data, {Style[
   FinancialIndicator["SimpleMovingAverage", 5], Blue], 
  Style[FinancialIndicator["SimpleMovingAverage", 20], Red], 
  Style[FinancialIndicator["Volume"], Black], 
  Style[FinancialIndicator["MACD"], {Green, Purple}]},
 Epilog -> {Thick, Red, Line[{
     {tchartx[data[[1, 1]]], data[[1, 2, 3]]},
     {tchartx[data[[250, 1]]], data[[50, 2, 3]]}
     }]}

 ]

Mathematica graphics

I am not sure what line you actually wanted to draw, so I arbitrarily chose a data point for demonstration purposes only.

$\endgroup$

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.