3
$\begingroup$

I'm new to Mathematica and am trying to plot log-returns of a stock as a timeseries object. However, the following code gives me back elements of the form "Log[151.23$]" unevaluated. What am I doing wrong?

Differences[Log[FinancialData["GE", {2018, 1, 1}, "All"]]]

I know you can get it to work if you use the "values" of the time series, but this removes the dates and I need them for a DateListPlot.

$\endgroup$

2 Answers 2

5
$\begingroup$

The QuantityMagnitude command does the work.

ts = Differences[Log[QuantityMagnitude[FinancialData["GE", {2018, 1, 1}, "All"]]]]
DateListPlot[ts]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you so much! $\endgroup$ Commented Dec 11, 2021 at 9:43
  • $\begingroup$ As returns, technically they should be plotted as a histogram. By contrast, a time series (index) makes sense as a line plot. $\endgroup$ Commented Dec 11, 2021 at 20:01
  • $\begingroup$ It's interesting how your answer is working with time series. Some unobvious semantics going on, such as Log knows not to operate on the dates. Seems quite specific and a bit black box. $\endgroup$ Commented Dec 11, 2021 at 20:17
3
$\begingroup$
a = FinancialData["GE", {2018, 1, 1}, "All"];
b = Normal[a];

DateListPlot[Transpose[{b[[2 ;;, 1]],
   Differences@Log[b[[All, 2, 1]]]}], Joined -> False, Filling -> Axis]

enter image description here

alternatively

a = FinancialData["GE", {2018, 1, 1}, "All"];
b = Normal[a];

c = Rest@b[[All, 1, 1, ;; 3]];
d = Differences@Log[b[[All, 2, 1]]];
DateListPlot[Transpose[{c, d}], Joined -> False]

note

b[[All, 2, 1]] == QuantityMagnitude@b[[All, 2]]

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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