0
$\begingroup$

I'm trying to get some old Mathematica code working on MMA 12. I found the code at this link. Here's the relevant code:

Portfolio = {"AAPL", "BA", "IBM", "BMW.DE", "DIS", "R", "PEP", "BRBY.L", "AXP", "BTI"};
data = FinancialData[#, "Price", {{2016}, {2020}, "Month"}]["Path"][[All, 2]] & /@ Portfolio;
ListLogPlot[data, PlotRange -> All, Joined -> True]
Returns = Differences[Log[data[[#]]]] & /@ Range[10];
ListLinePlot[Returns, PlotRange -> All, ImageSize -> Medium]

The second plot is empty. When I look at Returns, it has this mess:

{{Log[Quantity[24.172500610351562, "USDollars"]] - Log[Quantity[24.334999084472656, "USDollars"]], -Log[Quantity[24.172500610351562, "USDollars"]] + Log[Quantity[27.247499465942383, "USDollars"]], Log[Quantity[23.434999465942383, "USDollars"]] - Log[Quantity[27.247499465942383, "USDollars"]], -Log[Quantity[23.434999465942383, "USDollars"]] + Log[Quantity[24.96500015258789, "USDollars"]], Log[Quantity[23.899999618530273, "USDollars"]] - Log[Quantity[24.96500015258789, "USDollars"]], -Log[Quantity[23.899999618530273, "USDollars"]] + Log[Quantity[26.052499771118164, "USDollars"]], -Log[Quantity[26.052499771118164, "USDollars"]] + Log[Quantity[26.524999618530273, "USDollars"]], -Log[Quantity[26.524999618530273, "USDollars"]] + Log[Quantity[28.262500762939453, "USDollars"]], -Log[Quantity[28.262500762939453, "USDollars"]] + Log[Quantity[28.385000228881836, "USDollars"]], Log[Quantity[27.6299991607666, "USDollars"]] - Log[Quantity[28.385000228881836, "USDollars"]], -Log[Quantity[27.6299991607666, "USDollars"]] + Log[Quantity[28.954999923706055, "USDollars"]], -Log[Quantity[28.954999923706055, "USDollars"]] + Log[Quantity[30.337499618530273, "USDollars"]],

Theoretically, this is correct, but I want MMA to evaluate these expressions and plot the answer. If I remove the Log function in the formula for the Returns, I get the expected results, but not on a logarithmic scale.

What is wrong with the Returns formula? Why won't this version evaluate and plot?

$\endgroup$
  • $\begingroup$ Use Log @ Ratios[data[[#]]] instead of Differences[Log[data[[#]]]]. $\endgroup$ – Carl Woll Sep 16 at 1:21
1
$\begingroup$

You can use the 'QuantityMagnitude' function

Returns = 
  Differences[Log[QuantityMagnitude[data[[#]]]]] & /@ Range[10];

It produces valid figure on my computer (MMA 12.0)

| improve this answer | |
$\endgroup$
0
$\begingroup$

When using old code for FinancialData, the Method->"Legacy" option solves most of the problems with the code you mentioned. Add the legacy option to FinancialData wherever it's used, for example,

data = FinancialData[#, "Price", {{2016}, {2020}, "Month"}, Method->"Legacy"][[All, 2]] & /@ Portfolio;

The Method option restores legacy output, i.e., real numbers and lists of date-value pairs, etc., in place of Quantity objects and TimeSeries.

If you prefer to update the old code to use the new features of FinancialData, use QuantityMagnitude to convert Quantity objects (such as currency values) to real numbers, and use the real number values wherever they're required. Here's how you might use the Log function without the Method->"Legacy" option:

dataValues = QuantityMagnitude[data];
Returns = Differences[Log[dataValues[[#]]]] & /@ Range[10];
| improve this answer | |
$\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.