4
$\begingroup$

Say I have a toy package, something like:

BeginPackage["myPackage`"]
plotSomeData::usage = "plotFinancialData[symbol]... some usage language";
Begin["`Private`"]
plotSomeData[symbol_]:= DateListPlot[FinancialData[symbol, All]]
End[]
EndPackage[]

This should work fine, nothing particularly complicated here.

So now, I'd like to extend the FinancialData[] function embedded in the package to enable it to seamlessly access non-Wolfram curated data stored locally on my computer.

I could rewrite the package and set up some conditional code that would replace FinancialData[] with Import[], something like:

plotSomeData[symbol_] := DateListPlot[
  Which[
   symbol == "xyz", Import[pathAndFileName],
   symbol != "xyz", FinancialData[symbol, All]
   ]]

But, in my case, that would mean rewriting a bunch of perfectly good packges, so I'd like another solution. Some of the questions and answers such as:

What is a paclet?

UpValues TagSet and UpSet

have me wondering if another more elegant way exists to do this.

Could, for instance, one extend the paclet information for a set of curated data?

I've used TagSet before to do things like define how to calculate the CDF of a custom distribution, but I don't see a clear way to use it to enable a curated data function to recognize the location of a data symbol it doesn't already know.

SetOptions doesn't seem like the thing either.

UpVales and UpSet might do it, but I don't see it yet.

Ideally, I'd simply like to extend a curated data function to recognize the location of a data symbol it doesn't already know and thereby enable it to get the newly identified data.

Best if I can do this outside of the packages I already have.

Any ideas?

(The tags I have on this question could use some improvement, if anyone has some better ideas.)

$\endgroup$
9
  • $\begingroup$ You can't add rules to FinancialData, since it is Locked. As you mentioned yourself, UpValues can help. You can define a given symbol as sym/: FinancialData[sym,...]:=..., where you can insert on the r.h.s. the definition you want. $\endgroup$ Apr 17, 2013 at 0:17
  • $\begingroup$ @LeonidShifrin -- I think I understand. The r.h.s would become my Import[pathAndFileName]. That should enable me to do additional statements for other symbols that would correspond to their own data sets? $\endgroup$
    – Jagra
    Apr 17, 2013 at 2:03
  • $\begingroup$ Yes, that's right. $\endgroup$ Apr 17, 2013 at 11:19
  • $\begingroup$ @LeonidShifrin -- Certainly worthy of an accepted answer in my opinion. If you want to post it, you've got my vote. - Thx. $\endgroup$
    – Jagra
    Apr 17, 2013 at 16:09
  • $\begingroup$ No time for it at the moment, alas. Will do when I get some window, unless someone does that first. Also, feel free to post your own answer based on the hints I gave, and accept that. $\endgroup$ Apr 17, 2013 at 16:16

1 Answer 1

3
$\begingroup$

As @Mr.Wizard asked, "...it's time for an answer, don't you think?".

I forgot about this question, but after year it occurred to me to formulate an answer following @LeonidShifin suggestions:

First get some test data:

testData = FinancialData["GE", "Jan. 1, 2000"][[1 ;; 10]];
dataFile = "/Users/jagra/Data.test.csv";
Export[dataFile, testData, "CSV"];

Apply UpValues

sym /: FinancialData[sym] := Last[Import[dataFile]][[2]]
sym /: FinancialData[sym, All] := Import[dataFile]
sym /: FinancialData[sym, date_String] := Module[{data, pos},
  data = Import[dataFile];
  pos = Position[data, date];
  data[[Flatten[pos][[1]]]]]

FinancialData[sym]
FinancialData[sym, All]
FinancialData[sym, "{2000, 1, 4}"]


32.45

{{{2000, 1, 3},32.24},{{2000, 1, 4},30.95},{{2000, 1, 5},30.89},{{2000, 1, 6},31.31},{{2000, 1, 7},32.52},{{2000, 1, 10},32.5},{{2000, 1, 11},32.56},{{2000, 1, 12},32.67},{{2000, 1, 13},33.04},{{2000, 1, 14},32.45}}

{{2000, 1, 4},30.95}

This doesn't show all possible configurations for getting data (and even has a shortcut in using "{2000, 1, 4}") , but I think it demonstrates the flexibility of the approach.

$\endgroup$

Your Answer

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

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