1
$\begingroup$

So I am using the command FinancialData["name"] to get the live price for any given stock. The openings are very important in my studies that I am conducting and it seems there is a delay of 10 to 15 mins on the data (at least during opening period).

I have checked the Mathematica's sources given in here. What I wonder is that is it possible to change the sources manually? Suppose I set my source to Yahoo Stock so whenever I use the command I get the same data as Yahoo?

$\endgroup$
2
$\begingroup$

You can't--unfortunately--change where the financial data comes from (the exception is with Bloomberg data and the Finance Platform product), but you can easily grab data from Yahoo Finance.

Here's an example grabbing the name, ask, and bid price of three companies:

yahoostockquote[tickers_] := Module[
 {ql = If[ListQ@tickers, StringRiffle[tickers, "+"], tickers]},
 Import["http://finance.yahoo.com/d/quotes.csv?s=" <> ql <> "&f=nab"]]

yahoostockquote[{"AAPL","GOOG","MSFT"}]

{{"Apple Inc.", 128.78, 128.77}, {"Google Inc.", 537.11, 536.94}, {"Microsoft Corporation", 46.24, 46.23}}

We can expand this further for historical data by the following function:

yahoostockquote[ticker_?StringQ, fromdate_, todate_: Today] :=
 Module[{ffromdate = DateList[fromdate], ftodate = DateList[todate], querystring, return},
 querystring = "a=" <> ToString[ffromdate[[2]] - 1] <> "&b=" <> 
  ToString[ffromdate[[3]]] <> "&c=" <> ToString[ffromdate[[1]]] <> "&d=" <>
  ToString[ftodate[[2]] - 1] <> "&e=" <> 
  ToString[ftodate[[3]]] <> "&f=" <> ToString[ftodate[[1]]];
return = Import["http://ichart.finance.yahoo.com/table.csv?s=" <> ticker <> 
 "&" <> querystring <> "&g=d&ignore=.csv"];
return[[2 ;;, 1]] = 
  DateObject[{#, {"Year", "-", "Month", "-", "Day"}}, 
  DateFormat -> {"Month", "/", "Day", "/", "Year"}] & /@ 
return[[2 ;;, 1]];
return
]

yahoostockquote["AAPL", {2015, 1, 1}, {2015, 2, 1}]

enter image description here

$\endgroup$
  • $\begingroup$ When I run the code there is error! in the first section not even the second. are we sure it is right? I have error saying the url is not defined correctly! thanks $\endgroup$ – William Jun 6 '15 at 21:37
  • $\begingroup$ @William It works just fine for me. My guess is that you're not using version 10.1 so the function StringRiffle isn't yet available to you. Use StringJoin@Riffle[tickers, "+"] instead. $\endgroup$ – kale Jun 7 '15 at 3:31

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.