CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. 66 % of retail investor accounts lose money when trading CFDs with this provider. You should consider whether you understand how CFDs work and whether you can afford to take the high risk of losing your money.

{DARWIN API} OAuth2 Connectivity in R

Hey all,

first post here. happy to be part of this exciting community!

I am trying to find an easy solution to download individual darwin data via R. Has anyone looked at this before? I post below my code.

I get Error message 401 - which i understand is an invalid authentication.

library(jsonlite)
library(httr)


APIkey <-'xxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxx'
API_header <- paste0('Authorization: Bearer ', APIkey)

## Link to get API key: https://www.darwinex.com/data/darwin-api
## link to get API request: https://api.darwinex.com/store/apis/info?name=DarwinInfoAPI&version=1.4&provider=admin#/default/get_products__productName__candles

## link to sample DARWIN: https://www.darwinex.com/darwin/THA.4.12/

product_name <- 'THA'
resolution <- '1m'
from <- '1559993918'
to <- '1562585961'

  get_DARWIN_hist <- function(product_name = product_name, resolution = resolution, from = from, to = to) {
    
    link <- paste0('https://api.darwinex.com/darwininfo/1.4/products/', product_name, '/candles?resolution=', resolution, '&from=', from, '&to=', to)
    get_prices <- GET(link_test,  add_headers(API_header) )
 
    return(get_prices$status_code)
   

}



get_DARWIN_hist()
2 Likes

Hi @alxbdi990,

Welcome to the Darwinex Community! :slight_smile:

The following video was released by @DarwinexLabs some time back for OAuth2 authentication in both Python and R.

You may find the accompanying source code on GitHub useful for the authentication side of your code.

For an example of getting candlesticks (in Python, but the URI construction is exactly the same in R), see this video and accompanying source code:

Hope this helps!

1 Like

Hi! thanks for the response!

quick question: I used as an example one of the top darwins at:
https://www.darwinex.com/darwin/THA.4.12/

but I can see you identify it as THA.4.12 in the URL. Does the API request ‘product_name’ require just THA or the former? i.e. are the tickers unique to that darwin?

i’ll look at the videos tomorrow. is there a slack community for this where we can chat or just this forum? (are you the same ‘integracore’ as in the robotwealth community?)

Many thanks!

2 Likes

Hi @alxbdi990,

  1. It is generally good practice to use the full ticker symbol of the DARWIN, e.g. THA.4.12 instead of THA.

  2. Each DARWIN has a unique ticker symbol, yes.

  3. As of today, yes - please visit Darwinex Collective - Slack Workspace for Algorithmic R&D for more information.

  4. Yes, same integracore as on RW Slack :wink:

Got it. Thanks for the tips. certainly needs more work but the below works…

I’ll leave the solution here.

**APIkey <-'Bearer xxxxxxx-xxxxxxx-xxxxxxx-xxxxxxx-xxxxxxx'**


get_DARWIN_hist <- function(product_name = "", resolution = "", from = "", to = "") {
  
  link <- paste0('https://api.darwinex.com/darwininfo/1.5/products/', product_name, '/candles?resolution=', resolution, '&from=', from, '&to=', to)
  get_prices <- GET(link,  add_headers(Authorization = APIkey ) )
  
  hist <-  fromJSON(content(get_prices, "text"))
  
  return(hist)
  
  
}


test <- get_DARWIN_hist(product_name = "THA", 
                resolution = "1d",
                from = "",
                to = "1562585961")


prices <- test$candles
1 Like

Also trying to get a list of all darwins now. I’me getting status 200 but no data coming through into ‘content’. are there any issues with this one on your side>?

Get list of all darwins

DARWIN_all <- function(status = “”, page = “”, per_page = “”) {

link <- paste0(‘https://api.darwinex.com/darwininfo/1.5/products?status=’, status, ‘&page=’, page, ‘&per_page=’, per_page)
get_data <- GET(link2, add_headers(Authorization = APIkey ) )

output <- fromJSON(content(get_data, “text”))

return(output)

}

get_data$status_code
get_data$headers
get_data$request

This is the status code message and API request

#############################################################

> get_data$status_code
   [1] 200
    > get_data$headers
$date
[1] "Tue, 09 Jul 2019 23:38:44 GMT"

$`content-type`
[1] "application/json;charset=UTF-8"

$`transfer-encoding`
[1] "chunked"

$connection
[1] "keep-alive"

$`access-control-allow-origin`
[1] "*"

$`access-control-allow-methods`
[1] "POST,GET"

$`access-control-allow-headers`
[1] "authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction"

attr(,"class")
[1] "insensitive" "list"       
> get_data$request
<request>
GET https://api.darwinex.com/darwininfo/1.5/products?status=ACTIVE&page=1&per_page=100000
Output: write_memory
Options:
* useragent: libcurl/7.59.0 r-curl/3.3 httr/1.4.0
* cainfo: C:/PROGRA~1/R/R-35~1.3/etc/curl-ca-bundle.crt
* httpget: TRUE
Headers:
* Accept: application/json, text/xml, application/xml, */*
* Authorization: Bearer xxxxxxx--xxxxxxxxxx---xxxxxxxxxxx
> 

It retrieves an empty list:

######################################################################

test <- DARWIN_all(status = 'ALL', page = 1, per_page = 50)

length(test$content)
[1] 0

Hello @alxbdi990,
I experienced some troubles too getting a clean table of darwins list in R. The point here I think is the conversion from the received format to a useful R’s format.

You are extracting the content and then converting from json, so I recommend you to look deep into the structure of that conversion pipeline and adjust it as required.

In my case, this is the only api function where I am not using GET(), but a system curl call, because of an easier conversion from the api output to a well structured data.table.

getToken = function(){
  return("xxxxxxxx-xxx-xxxx-xxxx-xxxxxxxxxxxx")
}

fromEPOCH = function(x){
    as.Date(as.POSIXct(x/1000,origin="1970-01-01",tz="UTC"))
}

getDWTable = function(){
  
  tmp <- system(paste0(
    "curl -k -X GET \"https://api.darwinex.com/darwininfo/1.3",
    "/products?status=ACTIVE&per_page=10000\"",
    " -H \"accept: application/json\" -H \"Authorization: Bearer ",
    getToken(),"\""),
    intern=TRUE)
  
  tmp <- rbindlist(rjson::fromJSON(tmp)$content)
  tmp$validationDate <- fromEPOCH(tmp$validationDate)
  tmp$migrationDate <- fromEPOCH(tmp$migrationDate)
  as.data.table(tmp)
  
}

head(getDWTable())
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0 100  591k    0  591k    0     0   643k      0 --:--:-- --:--:-- --:--:--  6420
   productName shortName status migrationDate validationDate resetDate currency
1:     AAE.4.6       AAE ACTIVE    1970-01-01     2017-04-01         0      USD
2:    AAJ.4.12       AAJ ACTIVE    1970-01-01     2017-04-02         0      USD
3:     AAN.4.2       AAN ACTIVE    1970-01-01     2017-04-02         0      EUR
4:     AAO.4.7       AAO ACTIVE    1970-01-01     2017-04-02         0      USD
5:    AAP.4.12       AAP ACTIVE    1970-01-01     2017-04-10         0      EUR
6:     AAY.4.5       AAY ACTIVE    1970-01-01     2017-04-02         0      USD