1
$\begingroup$

I tried to modify an old notebook to show a spreadsheet style output in a table. I am not getting any errors when it runs, so I think the data I am gathering and the new variables I added, t and y, are there correctly. I removed the colored aspect to try and simplify things. I get lost when I try to add data to the list2 variable. Can anyone help me get this to work properly?

stock = {"AAPL", "FB", "BAC", "TWTR", "IBM"};
stocks = {"AAPL", "FB", "BAC", "TWTR", "IBM"};
fields = {"Close", "5ema", "5ema[1]"};
tablefillnew[stock_] :=
 (list = {data = 
       FinancialData[#, "Close", {{DatePlus[-60], DatePlus[-1]}}]; 
      FinancialData[#, "Close"], 
      ema = FinancialIndicator["ExponentialMovingAverage", 5] #;
      t = Take[ema, {1}]; y = Take[ema {2}];} & /@ stock; 
  list2 = Table[{colored[[ii]], list[[ii, 2]]}, {ii, 1, Length[list]}];
  TableForm[list2, TableHeadings -> {stocks, fields}
   ])
tablefillnew[stock]
$\endgroup$
4
$\begingroup$

There are a lot of syntax errors and general messiness in this code. My proxy server won't let me download FinancialData at the moment, so I can't test this completely, but several things you should note:

  • intermediate variables should localised using Module.
  • syntax error: y = Take[ema {2}] should be y = Take[ema, {2}] or just ema[[2]].
  • FinancialIndicator["ExponentialMovingAverage", 5] #; doesn't work that way - it works inside things like TradingChart. You are better off just using the built-in ExponentialMovingAverage, after stripping off the dates.
  • it's no wonder you are confused with all the definitions inside definitions. Try making your code a bit less nested.
  • there seems to have been unnecessarily doubled braces in the definition of data
  • without knowing what coloured did, it is hard to work out what to replace it with.

Here is a simplified version that should work. Notice that I have made fields a parameter so you can change your preferred functions. I have used pattern matching to check that the data you pass to the function are what you expect (lists of strings). I have used Part ([[]]) instead of Take to simplify getting the bits of the exponential moving average. And instead of building up the table row by row using Table, I just use Transpose.

tablefillnew[stocklist:{__String}, fields:{__String}] :=
 Module[{data, ema, closeprices},
  data = FinancialData[#, "Close", {DatePlus[-60], DatePlus[-1]}] & /@ stocklist;
  closeprices = FinancialData[#, "Close"] & /@ stocklist; 
(* latest close not close for previous day:  they could be different *)
  ema = ExponentialMovingAverage[#[[All, 2]], 5] & /@ data;
  TableForm[Transpose[{closeprices, ema[[All, 1]], ema[[All, 2]]}], 
   TableHeadings -> {stocklist, fields}]] 
$\endgroup$
  • $\begingroup$ Hi Verbeia Thank you very much for your reply! Your rewrite is much clearer as to what the code is doing. :) I added the issues to a list called stocklist and can't seem to get any output from it. The colored variable was just coloring blue or green based on whether the price was above or below $20. But it hurt the clarity of the code too much. $\endgroup$ – ChrisF Dec 4 '14 at 12:21
  • $\begingroup$ stocklist = {"AAPL", "FB", "BAC", "TWTR", "IBM"}; fields = {"Close", "5ema", "5ema[1]"}; tablefillnew[stocklist : {__String}, fields : {__String}] := Module[{data, ema, closeprices}, data = FinancialData[#, "Close", {{DatePlus[-60], DatePlus[-1]}}] & /@ stocklist; closeprices = FinancialData[#, "Close"] & /@ stocklist;(latest close not close for previous day: they could be different) ema = ExponentialMovingAverage[#, 5] & /@ data; TableForm[Transpose[{closeprices, ema[[All, 1]], ema[[All, 2]]}], TableHeadings -> {stocklist, fields}]] $\endgroup$ – ChrisF Dec 4 '14 at 12:23

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.