Automatic Retrieval of Stock Pricing

Has anyone used DT to pull stock prices from the web?
Ideally, I would pull prices for a specified number of companies at the same time on a daily basis and build a table of data over weeks or months.

Thx!

Tthat would require API calls to a specific service - something not built into DEVONthink, so this isn’t possible out-of-the-box.

Though it might be feasible, I’m not sure if DT is the best tool for this. You can write a small script that downloads a web page regularly. The page is then stored in the Downloads group in Inbox (at least it was here). You can then, if you’re a really tough person, go and parse the HTML file to extract the information. Apple describes this online. This is probably slow and error prone because it depends on the structure of the website.

In my opinion, a better solution would be to download JSON data (provided that there’s a site offering them), parsing them (probably not with AppleScript, though) and then pass the data on to DT. This might be possible with e.g. Keyboard Maestro, but it certainly requires some work. There’s a JSON service out there: https://www.alphavantage.co/

1 Like

This should be doable by using the foundation framework in the script.

Does the foundation framework offer a JSON parser?

See NSJSONSerialization, e.g. Zotero to DT script (WIP) - ways to improve?

Thanks again. Interesting that it seems in fact to be easy, whereas even people much more knowledgeable in AppleScript then I am suggest incredibly complicated approaches
I’ll have a look into it.

Wouldn’t it be more useful if you imported data into a spreadsheet program (Numbers, Pages, Excel, etc.) for you to do number crunching and graphing (which I presume is your goal).

You could store/copy the spreadsheets into DEVONthink, if that useful.

Lots of ideas for consideration searching Internet for “download stock prices to excel”.

Just a thought.

1 Like

Sure. But everybody knows how to do that. We need a challenge :wink:

1 Like

Can’t disagree with the challenge aspect!

I’m seeking a way for DEVONthink to do my dishes, preferably lauched by a scheduled AppleScript. Any takers?

After some tinkering and with a lot of help from @cgrunenberg, I came up with this script to get the stock prices of the last (?) hundred days. It relies on this API and retrieves only the values for IBM. Adopting that for other stocks and adding the results to a DT sheet is left as an excercise to the reader :wink:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set TheURLStr to "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo"

set JSONData to getUrlSource(TheURLStr)
property NSJSONSerialization : a reference to current application's NSJSONSerialization
property NSData : a reference to current application's NSData
property NSArray : a reference to current application's NSArray

set theJSONDictionary to NSJSONSerialization's JSONObjectWithData:JSONData options:0 |error|:(missing value)
set timeSeries to theJSONDictionary's objectForKey:"Time Series (Daily)"

set prices to {} -- list of records for the prices, _not_ sorted
set theDates to timeSeries's allKeys() as list -- list of all dates in timeSeries

repeat with d in theDates
	set t to (timeSeries's objectForKey:d)
	set p to (t's objectForKey:"4. close")
	set end of prices to {date:d as string, price:p as string}
end repeat

-- prices is now a list of records containing the daily closing prices

on getUrlSource(urlStr)
	set theURL to current application's class "NSURL"'s URLWithString:urlStr
	set theData to current application's NSData's dataWithContentsOfURL:theURL
	return theData
end getUrlSource

2 Likes