1
$\begingroup$

I have a list of database id and stock indicies which I use to withdraw the related price data.

lis = {{4470, "^AEX"}, {4471, "^AFLI"}, {4472, "^AORD"}}

I use the following function to get the price data:

Map[
 FinancialData[#, "OHLC", {{2015, 1, 1}, {2015, 1, 5}}] &,
 lis[[All, 2]]]

As a result I get:

{{{{2015, 1, 2}, {425.89, 428.09, 420.35, 422.28}}, {{2015, 1, 5}, {420.06, 424.58, 410.49, 410.94}}}, {{{2015, 1, 2}, {5563.4, 5587.7, 5535., 5586.3}}, {{2015, 1, 5}, {5586.3, 5622.1, 5578.9, 5598.3}}}, {{{2015, 1, 2}, {5388.6, 5415., 5366.4, 5415.}}, {{2015, 1, 5}, {5415., 5447.6, 5409.4, 5429.5}}}}

Now I would like to append the related database id to each data set (to enter the data in a mySQL database). I would like to get a list as follows (append the database id - eg. 4470):

{{{{2015, 1, 2}, {425.89, 428.09, 420.35, 422.28, 4470}}

For performance reasons I don´t like to use a loop function.

$\endgroup$

3 Answers 3

1
$\begingroup$

Why not just do it on-the-fly:

MapThread[
 Replace[FinancialData[#2, "OHLC", {{2015, 1, 1}, {2015, 1, 5}}], {d : {__}, p : {__}} :> {d,
         Append[p, #1]}, {1}] &, Transpose@lis]
$\endgroup$
1
$\begingroup$

One way:

MapAt[Append[4470], data, {All, All, 2}]

By the way updating all of the rows and columns with a known value is likely to be redundant. If you could put that number into the code that inserts the data into the database somehow it would likely be faster, because you will be able to skip this step completely.

$\endgroup$
1
  • $\begingroup$ Thanks for the quick answer. The problem is, that FinancialData[] provides a list with price data of several indices. I I use your function like this Map[ MapAt[ Append[#], pricelis, {All, All, 2}] &, lis[[All, 1]]], all data sets are updated with all database id. That means the function needs to append the first db id to {{},{dataset11}{dataset12}}, second id to {{},{dataset21}{dataset22}}, aso. How can I implement such a selective append? $\endgroup$
    – Phadreus
    Mar 25, 2015 at 22:01
1
$\begingroup$

This pre-pends to each result set retrieved by Financial Data (data=Map[Financial......) the db ids. For appending swap the arguments in Transpose

Transpose[{lis[[All, 1]], data}] // ((f \[Function] Flatten[f, 2]) /@ # & )

Thread works as well, here with the arguments swopped for appending

Thread[{data, lis[[All, 1]]}] // ((f \[Function] Flatten[f, 2]) /@ # & )

You mentioned concern about speed. Transpose seems to be about 10 times faster for appending then using Thread.

Speed test with 1 million random integers:

dset = {RandomInteger[10, 10^6], RandomInteger[10, 10^6]}

Thread  @ dset // Timing

Transpose  @ dset // Timing 

Thread returned 0.156001 seconds Transpose returned 0.015600 seconds

Agree with you not to use loop functions, they look awful to me and Mathematica stands for elegance....hi,hi,hi (PS: for f [Function] use the keys f Escape fn Escape)

$\endgroup$
2
  • $\begingroup$ Sorry some formatting issues when posting my answer. $\endgroup$
    – penguin77
    Mar 25, 2015 at 21:46
  • $\begingroup$ @bbgodfrey, thanks for your help in formattting, I am new and start getting into it. $\endgroup$
    – penguin77
    Mar 27, 2015 at 8:41

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.