Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

API from marketstack in R

Based on document from https://marketstack.com/documentation

http://api.marketstack.com/v1/eod
    ? access_key = YOUR_ACCESS_KEY
    & symbols = AAPL
    & date_from = 2019-01-01
    & date_to = 2019-02-01

Specify a date in YYYY-MM-DD format. 
You can also specify an exact time in ISO-8601 date format, 
e.g. 2020-05-21T00:00:00+0000. 
Example: /eod/2020-01-01

I am able to get access from the stock market without add date parameter. It worked well

symbols <- c("FB", "GOOG", "AMZN")
access_key <- "my_access_key"
api_url <- paste0("http://api.marketstack.com/v1/eod?access_key=",
                  access_key, "&symbols=", paste(symbols, collapse = ","))

stock_dt <- fromJSON(api_url)

When I added date_from and date_to to url, it didn't understand those parameter

# this is not working
date <- "2019-01-06"
api_url <- paste0("http://api.marketstack.com/v1/eod/",date,"?access_key=",
                  access_key, "&symbols=", paste(symbols, collapse = ","))
stock_dt <- fromJSON(api_url)


# this is working
api_url <- paste0("http://api.marketstack.com/v1/eod/","2021-01-06T00:00:00+0000","?access_key=",
                  access_key, "&symbols=", paste(symbols, collapse = ","))
stock_dt <- fromJSON(api_url)


# this is not working
api_url <- paste0("http://api.marketstack.com/v1/eod?access_key=",access_key, "&symbols=",paste(symbols, collapse = ","), "&date_from=2021-01-06T00:00:00+0000","&date_to=2021-01-07T00:00:00+0000")
stocks_dt <- fromJSON(api_url)

Any suggestion?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The documentation under Historical Data suggests

https://api.marketstack.com/v1/eod
    ? access_key = YOUR_ACCESS_KEY
    & symbols = AAPL
    & date_from = 2019-01-01
    & date_to = 2019-02-01

which is different from what you tried.

So I just did the following:

Code

APIKEY <- "....my key here...."
query <- paste0("http://api.marketstack.com/v1/eod?access_key=", APIKEY,
                "&symbols=SPY&date_from=2021-01-01&date_to=2021-01-07")
library(RcppSimdJson)
res <- fload(query)

Output

> res
$pagination
$pagination$limit
[1] 100

$pagination$offset
[1] 0

$pagination$count
[1] 4

$pagination$total
[1] 4


$data
    open   high    low  close    volume adj_high adj_low adj_close adj_open adj_volume symbol exchange                     date
1 376.10 379.90 375.91 379.10  68766812   379.90  375.91    379.10   376.10   68766812    SPY     ARCX 2021-01-07T00:00:00+0000
2 369.71 376.98 369.12 373.55 107997675   376.98  369.12    373.55   369.71  107997675    SPY     ARCX 2021-01-06T00:00:00+0000
3 368.10 372.50 368.05 371.33  66426229   372.50  368.05    371.33   368.10   66426229    SPY     ARCX 2021-01-05T00:00:00+0000
4 375.31 375.45 364.82 368.79 110210810   375.45  364.82    368.79   375.31  110210810    SPY     ARCX 2021-01-04T00:00:00+0000

> 

and res$data is a data.frame as one would want.

Multiple tickers also work:

> query <- paste0("http://api.marketstack.com/v1/eod?access_key=", APIKEY, "&symbols=SPY,QQQ&date_from=2021-01-01&date_to=2021-01-07")
> res <- fload(query)
> res$data
    open   high    low  close    volume adj_high adj_low adj_close adj_open adj_volume symbol exchange                     date
1 310.28 315.84 310.25 314.98  30394826   315.84  310.25    314.98   310.28   30394826    QQQ     XNAS 2021-01-07T00:00:00+0000
2 376.10 379.90 375.91 379.10  68766812   379.90  375.91    379.10   376.10   68766812    SPY     ARCX 2021-01-07T00:00:00+0000
3 307.00 311.88 305.98 307.54  52809622   311.88  305.98    307.54   307.00   52809622    QQQ     XNAS 2021-01-06T00:00:00+0000
4 369.71 376.98 369.12 373.55 107997675   376.98  369.12    373.55   369.71  107997675    SPY     ARCX 2021-01-06T00:00:00+0000
5 308.29 312.14 308.29 311.86  29323409   312.14  308.29    311.86   308.29   29323409    QQQ     XNAS 2021-01-05T00:00:00+0000
6 368.10 372.50 368.05 371.33  66426229   372.50  368.05    371.33   368.10   66426229    SPY     ARCX 2021-01-05T00:00:00+0000
7 315.11 315.29 305.18 309.31  45305898   315.29  305.18    309.31   315.11   45305898    QQQ     XNAS 2021-01-04T00:00:00+0000
8 375.31 375.45 364.82 368.79 110210810   375.45  364.82    368.79   375.31  110210810    SPY     ARCX 2021-01-04T00:00:00+0000
> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...