├── DataStreamR
├── DataStreamRExample.ipynb
└── README.MD
├── EikonDataAPIR
├── EikonDataAPIRExample.ipynb
└── readme.md
├── README.md
├── RefinitivDataPlatformAPIR
├── RDPHistoricalRExample.ipynb
└── README.MD
├── Step1_InstallR.png
├── Step2_Python.png
├── Step2_PythonVersion.png
├── Step3_Jupyter.png
├── Step3_JupyterSupport.png
├── Step4_IRKernel1.png
├── Step4_IRKernel2.png
├── Step4_Jupyter.png
├── datastream.png
├── eikonapir.png
├── plotly.png
├── plotly_ex.png
└── rtools.png
/DataStreamR/DataStreamRExample.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# DataStream Web Service R Example\n",
8 | "\n",
9 | "This example demonstrates how to use [DataStream Web Service](https://developers.refinitiv.com/eikon-apis/datastream-web-service) with R on Jupyter Notebook. It uses the [DatastreamDSWS2R](https://github.com/CharlesCara/DatastreamDSWS2R/tree/master/R) package to retrieve data from DataStream Web Service and uses [Plotly](https://plot.ly/r/getting-started/) package to draw charts. \n",
10 | "\n",
11 | "To setup Jupyter Notebook environment for R or install DatastreamDSWS2R, please refer to this [article](https://developers.refinitiv.com/article/setup-jupyter-notebook-r)."
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "## The first step is loading the required libraries:\n",
19 | "\n",
20 | "- [DatastreamDSWS2R](https://github.com/CharlesCara/DatastreamDSWS2R): Functions and a R5 class to get data from the DSWS server\n",
21 | "- [xts](https://www.rdocumentation.org/packages/xts/versions/0.11-2): eXtensible Time Series for uniform handling of R's different time-based data classes \n",
22 | "- [dplyr](https://www.rdocumentation.org/packages/dplyr/versions/0.7.8): A fast, consistent tool for working with data frame like objects, both in memory and out of memory\n",
23 | "- [plotly](https://www.rdocumentation.org/packages/plotly/versions/4.9.0): Create interactive web graphics from 'ggplot2' graphs and/or a custom interface to the (MIT-licensed) JavaScript library 'plotly.js'\n"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "library(DatastreamDSWS2R)\n",
33 | "library(xts)\n",
34 | "library(dplyr)\n",
35 | "library(plotly)"
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | "## Next, set DataStream Web Service username and password\n",
43 | "\n",
44 | "There are two ways to set DataStream Web Service username and password.\n",
45 | "\n",
46 | "1. Set credentials in environment variables by adding to the **.Renviron** file the following lines\n",
47 | "\n",
48 | "```\n",
49 | "DatastreamUsername=YOURUSERNAME\n",
50 | "DatastreamPassword=YOURPASSWORD\n",
51 | "```\n",
52 | "\n",
53 | "2. Put credentials into the **options()** method which allows the user to set and examine a variety of global options. You can also add these lines to the **.RProfile** file.\n",
54 | "\n",
55 | "```\n",
56 | "options(Datastream.Username = \"YOURUSERNAME\")\n",
57 | "options(Datastream.Password = \"YOURPASSWORD\")\n",
58 | "```\n",
59 | "\n",
60 | "The below code calls the **options()** method to set the DataStream Web Service credentials. The below code can be skipped if the credentials have been set in the **.Renviron** or **.RProfile** file."
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {},
67 | "outputs": [],
68 | "source": [
69 | "options(Datastream.Username = \"YOURUSERNAME\")\n",
70 | "options(Datastream.Password = \"YOURPASSWORD\")"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "## Then, create an instance of the DataStream Web Service "
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": null,
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "mydsws <- dsws$new()"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {},
92 | "source": [
93 | "Now, the DataStream Web Service is ready to be used. Next, we will show several use cases for the DataStream Web Service.\n",
94 | "\n",
95 | "## The following examples show several use cases for the DataStream Web Service\n",
96 | "\n",
97 | "### 1. Retrieve static information for given instruments on a specific date\n",
98 | "\n",
99 | "The following code calls the **snapshotRequest** function to get the name (NAME), official closing price (P) and opening price (PO) of ABR, RIO, and WPP instruments on the last trading day. \n",
100 | "\n",
101 | "The function returns a data frame with fields in columns and instruments as rows."
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": null,
107 | "metadata": {},
108 | "outputs": [],
109 | "source": [
110 | "data1 <- mydsws$snapshotRequest(instrument = c(\"ABF\",\"RIO\",\"WPP\"),\n",
111 | " datatype = c(\"NAME\",\"P\",\"PO\"),\n",
112 | " requestDate =\"0D\")\n",
113 | "data1"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "### 2. Retrieve time series data and plot OHLC chart\n",
121 | "\n",
122 | "The following code calls the **timeSeriesListRequest** function to retrieve daily historical OPEN, HIGH, LOW, CLOSE fields from one year ago to the last trading day of IBM.\n",
123 | "\n",
124 | "The format is **\"ByDatatype\"** so the function returns xts data (eXtensible Time Series) with fields in columns and data points in rows."
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": null,
130 | "metadata": {},
131 | "outputs": [],
132 | "source": [
133 | "xtsOHLCData <- mydsws$timeSeriesListRequest(instrument = c(\"U:IBM\"),\n",
134 | " datatype = c(\"PO\", \"PH\",\"PL\",\"P\"),\n",
135 | " startDate = \"-1Y\",\n",
136 | " endDate = \"-0D\",\n",
137 | " frequency = \"D\",\n",
138 | " format=\"ByDatatype\")\n",
139 | "\n",
140 | "xtsOHLCData"
141 | ]
142 | },
143 | {
144 | "cell_type": "markdown",
145 | "metadata": {},
146 | "source": [
147 | "#### Change the column name to OPEN, HIGH, LOW, CLOSE\n",
148 | "\n",
149 | "The column names in the xts data are the instrument name so the **colnames()** function can be used to change the column names."
150 | ]
151 | },
152 | {
153 | "cell_type": "code",
154 | "execution_count": null,
155 | "metadata": {},
156 | "outputs": [],
157 | "source": [
158 | "colnames(xtsOHLCData)[[1]] <- \"OPEN\"\n",
159 | "colnames(xtsOHLCData)[[2]] <- \"HIGH\"\n",
160 | "colnames(xtsOHLCData)[[3]] <- \"LOW\"\n",
161 | "colnames(xtsOHLCData)[[4]] <- \"CLOSE\"\n",
162 | "xtsOHLCData"
163 | ]
164 | },
165 | {
166 | "cell_type": "markdown",
167 | "metadata": {},
168 | "source": [
169 | "#### Use the xts data to plot an OHLC chart\n",
170 | "\n",
171 | "Call the **plot_ly** function to plot an OHLC chart with the index (Date), OPEN, HIGH, LOW, and CLOSE columns."
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": null,
177 | "metadata": {},
178 | "outputs": [],
179 | "source": [
180 | "chart1 <- plot_ly(x = index(xtsOHLCData), \n",
181 | " type = \"ohlc\",\n",
182 | " open = coredata(xtsOHLCData)[,'OPEN'],\n",
183 | " high = coredata(xtsOHLCData)[,'HIGH'],\n",
184 | " low = coredata(xtsOHLCData)[,'LOW'],\n",
185 | " close = coredata(xtsOHLCData)[,'CLOSE']) %>% layout(title = \"Basic OHLC Chart\")\n",
186 | "chart1"
187 | ]
188 | },
189 | {
190 | "cell_type": "markdown",
191 | "metadata": {},
192 | "source": [
193 | "### 3. List request\n",
194 | "\n",
195 | "Datastream also supports Constituent Lists of instruments, e.g. LFTSE100, LS&PCOMP, LDAXINDX, LSTOKYOSE, etc. Only one list is permitted per request. The list instruments can be used with the **listRequest** and **timeSeriesListRequest** methods.\n",
196 | "\n",
197 | "\n",
198 | "#### Retrieve static information for a given list instrument on a specific date\n",
199 | "\n",
200 | "The following code calls the **listRequest** function with the **LS&PCOMP** which represents S&P 500 to get the name (NAME), mnemonic (MNEM), official closing price (P) and opening price (PO) of instruments in the **LS&PCOMP** on the last trading day. Mnemonic is a unique identification code, assigned by Datastream.\n",
201 | "\n",
202 | "The function returns a data frame with fields in columns and instruments as rows."
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": null,
208 | "metadata": {
209 | "scrolled": true
210 | },
211 | "outputs": [],
212 | "source": [
213 | "listDataFrame <- mydsws$listRequest(instrument = \"LS&PCOMP\",\n",
214 | " datatype = c(\"NAME\",\"MNEM\",\"P\",\"PO\"), \n",
215 | " requestDate=Sys.Date())\n",
216 | "listDataFrame"
217 | ]
218 | },
219 | {
220 | "cell_type": "markdown",
221 | "metadata": {},
222 | "source": [
223 | "#### Retrieve time series data for a given list instrument\n",
224 | "\n",
225 | "The following code calls the **timeSeriesListRequest** function with the **LS&PCOMP** which represents S&P 500 to get the daily official closing price (P) for the last thirty days.\n",
226 | "\n",
227 | "The function returns xts data (eXtensible Time Series) with items' data in columns and data points in rows. If the function shows the **Server error : Internal Server Error : Server error: (500) Internal Server Error**, please set the ChunkLimit to 20 by using the below code:\n",
228 | "```\n",
229 | "mydsws$chunkLimit <- 20L\n",
230 | "```"
231 | ]
232 | },
233 | {
234 | "cell_type": "code",
235 | "execution_count": null,
236 | "metadata": {},
237 | "outputs": [],
238 | "source": [
239 | "mydsws$chunkLimit <- 20L\n",
240 | "xtsData <- mydsws$timeSeriesListRequest(instrument = \"LS&PCOMP\",\n",
241 | " datatype = \"P\",\n",
242 | " startDate = \"-30D\",\n",
243 | " endDate = \"-0D\",\n",
244 | " frequency = \"D\")\n",
245 | "\n",
246 | "xtsData"
247 | ]
248 | },
249 | {
250 | "cell_type": "markdown",
251 | "metadata": {},
252 | "source": [
253 | "##### Change the column names\n",
254 | "\n",
255 | "The column names represent the datastream codes for instruments in the list. For readability, the following code changes the column names from the datastream codes to the mnemonic retrieved from the previous step."
256 | ]
257 | },
258 | {
259 | "cell_type": "code",
260 | "execution_count": null,
261 | "metadata": {},
262 | "outputs": [],
263 | "source": [
264 | "for(i in 1:ncol(xtsData)){\n",
265 | " colnames(xtsData)[[i]] <- listDataFrame$MNEM[i]\n",
266 | "}\n",
267 | "xtsData"
268 | ]
269 | },
270 | {
271 | "cell_type": "markdown",
272 | "metadata": {},
273 | "source": [
274 | "### 4. Expressions\n",
275 | "\n",
276 | "Expressions are Datastream functions which are statistical and display operators that allow you to calculate and view data in the way you want. Please refer to the [Datastream help page](http://product.datastream.com/Navigator/AdvanceHelpFiles/Functions/WebHelp/HFUNC.htm) for more information. \n",
277 | "\n",
278 | "#### Use an expression with the snapshotRequest method\n",
279 | "\n",
280 | "The **snapshotRequest** method accepts an expression through the **expression** parameter. \n",
281 | "\n",
282 | "The following code uses the Annualised growth rate, first and last values expression ([GRFL#](http://product.datastream.com/Navigator/AdvanceHelpFiles/Functions/WebHelp/Annualised_growth_rate_first_and_last_values_GRFL.htm)) to calculate the annualised growth rate between two dates. It calls the **snapshotRequest** method to get the annualised growth rate in the ABF, RIO, and WPP share prices for the previous quarter. \n"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": null,
288 | "metadata": {},
289 | "outputs": [],
290 | "source": [
291 | "myData <- mydsws$snapshotRequest(instrument = c(\"ABF\",\"RIO\",\"WPP\"), expression = \"GRFL#(XXXX,-1Q)\", requestDate = \"0D\")\n",
292 | "myData"
293 | ]
294 | },
295 | {
296 | "cell_type": "markdown",
297 | "metadata": {},
298 | "source": [
299 | "#### Use an expression with the timeSeriesListRequest method\n",
300 | "\n",
301 | "The following code uses the percentage change expression ([PCH#](http://product.datastream.com/Navigator/AdvanceHelpFiles/Functions/WebHelp/HFUNC.htm#Percentage_change_PCH.htm)) to calculate the percentage change for one day period. It requests the daily historical data for one month period of the ABF, RIO, and WPP instruments.\n",
302 | "\n",
303 | "The function returns xts data (eXtensible Time Series) with items' data in columns and data points in rows."
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": null,
309 | "metadata": {},
310 | "outputs": [],
311 | "source": [
312 | "xtsPCHData <- mydsws$timeSeriesListRequest(instrument = c(\"ABF\",\"RIO\",\"WPP\"),\n",
313 | "expression =\"PCH#(XXXX,1D)\", \n",
314 | " startDate = \"-1M\",\n",
315 | " endDate = \"-0D\",\n",
316 | " frequency = \"D\")\n",
317 | "\n",
318 | "xtsPCHData"
319 | ]
320 | },
321 | {
322 | "cell_type": "markdown",
323 | "metadata": {},
324 | "source": [
325 | "#### Use the xts data to plot a line chart\n",
326 | "\n",
327 | "The xts data contains time series data for three instruments. The following code calls the **plot_ly** function to create a line chart and then plot time series data for each instrument. "
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": null,
333 | "metadata": {},
334 | "outputs": [],
335 | "source": [
336 | "chart2 <- plot_ly()\n",
337 | "for(i in 1:ncol(xtsPCHData)){\n",
338 | " chart2 <- add_trace(chart2, \n",
339 | " x = index(xtsPCHData), \n",
340 | " y = coredata(xtsPCHData)[,i], \n",
341 | " name = colnames(xtsPCHData)[[i]], \n",
342 | " type = 'scatter', \n",
343 | " mode='lines') \n",
344 | "}\n",
345 | "chart2"
346 | ]
347 | },
348 | {
349 | "cell_type": "markdown",
350 | "metadata": {},
351 | "source": [
352 | "### 5. Symbology\n",
353 | "\n",
354 | "The **snapshotRequest** method can also be used to find instrument codes from other codes. \n",
355 | "\n",
356 | "The following example calls the **snapshotRequest** method to get instrument codes for these instruments:\n",
357 | "\n",
358 | "- **TH:BDMS** (Code - Local)\n",
359 | "- **US0231351067** (Code - ISIN)\n",
360 | "- **2005973** (Code - SEDOL)\n",
361 | "- **894371** (Code - Datastream)\n",
362 | "\n",
363 | "The data types used by the example are:\n",
364 | "\n",
365 | "|Data Type|Name|Description|\n",
366 | "|---------|----|-----------|\n",
367 | "|RIC|Reuters Instrument Code (RIC)|The Reuters Instrument Code|\n",
368 | "|ISIN|Code - Isin|ISIN (International Security Identification Number) is a code that uniquely identifies a security|\n",
369 | "|SECD|Code - Sedol|This is an identification code based on the code issued by the London Stock Exchange|\n",
370 | "|LOC|Code - Local|This is an identification code based on the official local exchange code|\n",
371 | "|DSCD|Code - Datastream|This is the unique six-digit identification code for every stock, allocated by Datastream|\n",
372 | "\n",
373 | "The function returns a data frame with fields in columns and instruments as rows."
374 | ]
375 | },
376 | {
377 | "cell_type": "code",
378 | "execution_count": null,
379 | "metadata": {},
380 | "outputs": [],
381 | "source": [
382 | "symbology <- mydsws$snapshotRequest(instrument = c(\"TH:BDMS\",\"US0231351067\",\"2005973\",\"894371\"),\n",
383 | " datatype = c(\"NAME\",\"RIC\",\"ISIN\", \"SECD\",\"LOC\",\"DSCD\"), \n",
384 | " requestDate=Sys.Date())\n",
385 | "symbology"
386 | ]
387 | },
388 | {
389 | "cell_type": "markdown",
390 | "metadata": {},
391 | "source": [
392 | "### 6. User Statistics\n",
393 | "\n",
394 | "The **snapshotRequest** method can also be used to retrieve monthly usage in terms of data points used per month by using **STATS** as instruments and **DS.USERSTATS** as data type. Only snapshot requests are supported and by default the current month’s usage stats are returned. Previous months’ data can be returned by simply adding a valid start date in request of any previous\n",
395 | "month.\n",
396 | "\n",
397 | "The following code requests the user statistics of the current month."
398 | ]
399 | },
400 | {
401 | "cell_type": "code",
402 | "execution_count": null,
403 | "metadata": {},
404 | "outputs": [],
405 | "source": [
406 | "myStat <- mydsws$snapshotRequest(instrument = \"STATS\", \n",
407 | " datatype = \"DS.USERSTATS\", \n",
408 | " requestDate = Sys.Date())\n",
409 | "myStat"
410 | ]
411 | },
412 | {
413 | "cell_type": "code",
414 | "execution_count": null,
415 | "metadata": {},
416 | "outputs": [],
417 | "source": []
418 | }
419 | ],
420 | "metadata": {
421 | "kernelspec": {
422 | "display_name": "R",
423 | "language": "R",
424 | "name": "ir"
425 | },
426 | "language_info": {
427 | "codemirror_mode": "r",
428 | "file_extension": ".r",
429 | "mimetype": "text/x-r-source",
430 | "name": "R",
431 | "pygments_lexer": "r",
432 | "version": "3.6.1"
433 | }
434 | },
435 | "nbformat": 4,
436 | "nbformat_minor": 2
437 | }
438 |
--------------------------------------------------------------------------------
/DataStreamR/README.MD:
--------------------------------------------------------------------------------
1 |
2 | # DataStream Web Service R Example
3 |
4 | This example demonstrates how to use [DataStream Web Service](https://developers.refinitiv.com/eikon-apis/datastream-web-service) with R on Jupyter Notebook. It uses the [DatastreamDSWS2R](https://github.com/CharlesCara/DatastreamDSWS2R/tree/master/R) package to retrieve data from DataStream Web Service and uses [Plotly](https://plot.ly/r/getting-started/) package to draw charts.
5 |
6 | To setup Jupyter Notebook environment for R or install DatastreamDSWS2R, please refer to this [article](https://developers.refinitiv.com/article/setup-jupyter-notebook-r).
7 |
8 | ## The first step is loading the required libraries:
9 |
10 | - [DatastreamDSWS2R](https://github.com/CharlesCara/DatastreamDSWS2R): Functions and a R5 class to get data from the DSWS server
11 | - [xts](https://www.rdocumentation.org/packages/xts/versions/0.11-2): eXtensible Time Series for uniform handling of R's different time-based data classes
12 | - [dplyr](https://www.rdocumentation.org/packages/dplyr/versions/0.7.8): A fast, consistent tool for working with data frame like objects, both in memory and out of memory
13 | - [plotly](https://www.rdocumentation.org/packages/plotly/versions/4.9.0): Create interactive web graphics from 'ggplot2' graphs and/or a custom interface to the (MIT-licensed) JavaScript library 'plotly.js'
14 |
15 |
16 |
17 | ```R
18 | library(DatastreamDSWS2R)
19 | library(xts)
20 | library(dplyr)
21 | library(plotly)
22 | ```
23 |
24 | ## Next, set DataStream Web Service username and password
25 |
26 | There are two ways to set DataStream Web Service username and password.
27 |
28 | 1. Set credentials in environment variables by adding to the **.Renviron** file the following lines
29 |
30 | ```
31 | DatastreamUsername=YOURUSERNAME
32 | DatastreamPassword=YOURPASSWORD
33 | ```
34 |
35 | 2. Put credentials into the **options()** method which allows the user to set and examine a variety of global options. You can also add these lines to the **.RProfile** file.
36 |
37 | ```
38 | options(Datastream.Username = "YOURUSERNAME")
39 | options(Datastream.Password = "YOURPASSWORD")
40 | ```
41 |
42 | The below code calls the **options()** method to set the DataStream Web Service credentials. The below code can be skipped if the credentials have been set in the **.Renviron** or **.RProfile** file.
43 |
44 |
45 | ```R
46 | options(Datastream.Username = "YOURUSERNAME")
47 | options(Datastream.Password = "YOURPASSWORD")
48 | ```
49 |
50 | ## Then, create an instance of the DataStream Web Service
51 |
52 |
53 | ```R
54 | mydsws <- dsws$new()
55 | ```
56 |
57 | Now, the DataStream Web Service is ready to be used. Next, we will show several use cases for the DataStream Web Service.
58 |
59 | ## The following examples show several use cases for the DataStream Web Service
60 |
61 | ### 1. Retrieve static information for given instruments on a specific date
62 |
63 | The following code calls the **snapshotRequest** function to get the name (NAME), official closing price (P) and opening price (PO) of ABR, RIO, and WPP instruments on the last trading day.
64 |
65 | The function returns a data frame with fields in columns and instruments as rows.
66 |
67 |
68 | ```R
69 | data1 <- mydsws$snapshotRequest(instrument = c("ABF","RIO","WPP"),
70 | datatype = c("NAME","P","PO"),
71 | requestDate ="0D")
72 | data1
73 | ```
74 |
75 | ### 2. Retrieve time series data and plot OHLC chart
76 |
77 | The following code calls the **timeSeriesListRequest** function to retrieve daily historical OPEN, HIGH, LOW, CLOSE fields from one year ago to the last trading day of IBM.
78 |
79 | The format is **"ByDatatype"** so the function returns xts data (eXtensible Time Series) with fields in columns and data points in rows.
80 |
81 |
82 | ```R
83 | xtsOHLCData <- mydsws$timeSeriesListRequest(instrument = c("U:IBM"),
84 | datatype = c("PO", "PH","PL","P"),
85 | startDate = "-1Y",
86 | endDate = "-0D",
87 | frequency = "D",
88 | format="ByDatatype")
89 |
90 | xtsOHLCData
91 | ```
92 |
93 | #### Change the column name to OPEN, HIGH, LOW, CLOSE
94 |
95 | The column names in the xts data are the instrument name so the **colnames()** function can be used to change the column names.
96 |
97 |
98 | ```R
99 | colnames(xtsOHLCData)[[1]] <- "OPEN"
100 | colnames(xtsOHLCData)[[2]] <- "HIGH"
101 | colnames(xtsOHLCData)[[3]] <- "LOW"
102 | colnames(xtsOHLCData)[[4]] <- "CLOSE"
103 | xtsOHLCData
104 | ```
105 |
106 | #### Use the xts data to plot an OHLC chart
107 |
108 | Call the **plot_ly** function to plot an OHLC chart with the index (Date), OPEN, HIGH, LOW, and CLOSE columns.
109 |
110 |
111 | ```R
112 | chart1 <- plot_ly(x = index(xtsOHLCData),
113 | type = "ohlc",
114 | open = coredata(xtsOHLCData)[,'OPEN'],
115 | high = coredata(xtsOHLCData)[,'HIGH'],
116 | low = coredata(xtsOHLCData)[,'LOW'],
117 | close = coredata(xtsOHLCData)[,'CLOSE']) %>% layout(title = "Basic OHLC Chart")
118 | chart1
119 | ```
120 |
121 | ### 3. List request
122 |
123 | Datastream also supports Constituent Lists of instruments, e.g. LFTSE100, LS&PCOMP, LDAXINDX, LSTOKYOSE, etc. Only one list is permitted per request. The list instruments can be used with the **listRequest** and **timeSeriesListRequest** methods.
124 |
125 |
126 | #### Retrieve static information for a given list instrument on a specific date
127 |
128 | The following code calls the **listRequest** function with the **LS&PCOMP** which represents S&P 500 to get the name (NAME), mnemonic (MNEM), official closing price (P) and opening price (PO) of instruments in the **LS&PCOMP** on the last trading day. Mnemonic is a unique identification code, assigned by Datastream.
129 |
130 | The function returns a data frame with fields in columns and instruments as rows.
131 |
132 |
133 | ```R
134 | listDataFrame <- mydsws$listRequest(instrument = "LS&PCOMP",
135 | datatype = c("NAME","MNEM","P","PO"),
136 | requestDate=Sys.Date())
137 | listDataFrame
138 | ```
139 |
140 | #### Retrieve time series data for a given list instrument
141 |
142 | The following code calls the **timeSeriesListRequest** function with the **LS&PCOMP** which represents S&P 500 to get the daily official closing price (P) for the last thirty days.
143 |
144 | The function returns xts data (eXtensible Time Series) with items' data in columns and data points in rows. If the function shows the **Server error : Internal Server Error : Server error: (500) Internal Server Error**, please set the ChunkLimit to 20 by using the below code:
145 | ```
146 | mydsws$chunkLimit <- 20L
147 | ```
148 |
149 |
150 | ```R
151 | mydsws$chunkLimit <- 20L
152 | xtsData <- mydsws$timeSeriesListRequest(instrument = "LS&PCOMP",
153 | datatype = "P",
154 | startDate = "-30D",
155 | endDate = "-0D",
156 | frequency = "D")
157 |
158 | xtsData
159 | ```
160 |
161 | ##### Change the column names
162 |
163 | The column names represent the datastream codes for instruments in the list. For readability, the following code changes the column names from the datastream codes to the mnemonic retrieved from the previous step.
164 |
165 |
166 | ```R
167 | for(i in 1:ncol(xtsData)){
168 | colnames(xtsData)[[i]] <- listDataFrame$MNEM[i]
169 | }
170 | xtsData
171 | ```
172 |
173 | ### 4. Expressions
174 |
175 | Expressions are Datastream functions which are statistical and display operators that allow you to calculate and view data in the way you want. Please refer to the [Datastream help page](http://product.datastream.com/Navigator/AdvanceHelpFiles/Functions/WebHelp/HFUNC.htm) for more information.
176 |
177 | #### Use an expression with the snapshotRequest method
178 |
179 | The **snapshotRequest** method accepts an expression through the **expression** parameter.
180 |
181 | The following code uses the Annualised growth rate, first and last values expression ([GRFL#](http://product.datastream.com/Navigator/AdvanceHelpFiles/Functions/WebHelp/Annualised_growth_rate_first_and_last_values_GRFL.htm)) to calculate the annualised growth rate between two dates. It calls the **snapshotRequest** method to get the annualised growth rate in the ABF, RIO, and WPP share prices for the previous quarter.
182 |
183 |
184 |
185 | ```R
186 | myData <- mydsws$snapshotRequest(instrument = c("ABF","RIO","WPP"), expression = "GRFL#(XXXX,-1Q)", requestDate = "0D")
187 | myData
188 | ```
189 |
190 | #### Use an expression with the timeSeriesListRequest method
191 |
192 | The following code uses the percentage change expression ([PCH#](http://product.datastream.com/Navigator/AdvanceHelpFiles/Functions/WebHelp/HFUNC.htm#Percentage_change_PCH.htm)) to calculate the percentage change for one day period. It requests the daily historical data for one month period of the ABF, RIO, and WPP instruments.
193 |
194 | The function returns xts data (eXtensible Time Series) with items' data in columns and data points in rows.
195 |
196 |
197 | ```R
198 | xtsPCHData <- mydsws$timeSeriesListRequest(instrument = c("ABF","RIO","WPP"),
199 | expression ="PCH#(XXXX,1D)",
200 | startDate = "-1M",
201 | endDate = "-0D",
202 | frequency = "D")
203 |
204 | xtsPCHData
205 | ```
206 |
207 | #### Use the xts data to plot a line chart
208 |
209 | The xts data contains time series data for three instruments. The following code calls the **plot_ly** function to create a line chart and then plot time series data for each instrument.
210 |
211 |
212 | ```R
213 | chart2 <- plot_ly()
214 | for(i in 1:ncol(xtsPCHData)){
215 | chart2 <- add_trace(chart2,
216 | x = index(xtsPCHData),
217 | y = coredata(xtsPCHData)[,i],
218 | name = colnames(xtsPCHData)[[i]],
219 | type = 'scatter',
220 | mode='lines')
221 | }
222 | chart2
223 | ```
224 |
225 | ### 5. Symbology
226 |
227 | The **snapshotRequest** method can also be used to find instrument codes from other codes.
228 |
229 | The following example calls the **snapshotRequest** method to get instrument codes for these instruments:
230 |
231 | - **TH:BDMS** (Code - Local)
232 | - **US0231351067** (Code - ISIN)
233 | - **2005973** (Code - SEDOL)
234 | - **894371** (Code - Datastream)
235 |
236 | The data types used by the example are:
237 |
238 | |Data Type|Name|Description|
239 | |---------|----|-----------|
240 | |RIC|Refinitiv Instrument Code (RIC)|The Refinitiv Instrument Code|
241 | |ISIN|Code - Isin|ISIN (International Security Identification Number) is a code that uniquely identifies a security|
242 | |SECD|Code - Sedol|This is an identification code based on the code issued by the London Stock Exchange|
243 | |LOC|Code - Local|This is an identification code based on the official local exchange code|
244 | |DSCD|Code - Datastream|This is the unique six-digit identification code for every stock, allocated by Datastream|
245 |
246 | The function returns a data frame with fields in columns and instruments as rows.
247 |
248 |
249 | ```R
250 | symbology <- mydsws$snapshotRequest(instrument = c("TH:BDMS","US0231351067","2005973","894371"),
251 | datatype = c("NAME","RIC","ISIN", "SECD","LOC","DSCD"),
252 | requestDate=Sys.Date())
253 | symbology
254 | ```
255 |
256 | ### 6. User Statistics
257 |
258 | The **snapshotRequest** method can also be used to retrieve monthly usage in terms of data points used per month by using **STATS** as instruments and **DS.USERSTATS** as data type. Only snapshot requests are supported and by default the current month’s usage stats are returned. Previous months’ data can be returned by simply adding a valid start date in request of any previous
259 | month.
260 |
261 | The following code requests the user statistics of the current month.
262 |
263 |
264 | ```R
265 | myStat <- mydsws$snapshotRequest(instrument = "STATS",
266 | datatype = "DS.USERSTATS",
267 | requestDate = Sys.Date())
268 | myStat
269 | ```
270 |
271 |
272 | ```R
273 |
274 | ```
275 |
--------------------------------------------------------------------------------
/EikonDataAPIR/EikonDataAPIRExample.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Eikon Data API R Example\n",
8 | "\n",
9 | "This example demonstrates how to use [Eikon Data API](https://developers.refinitiv.com/eikon-apis/eikon-data-api) with R on Jupyter Notebook. It uses the [eikonapir](https://github.com/ahmedmohamedali/eikonapir) package to retrieve data from Eikon and uses [Plotly](https://plot.ly/r/getting-started/) package to draw charts. It also uses the [IRDisplay](https://www.rdocumentation.org/packages/IRdisplay) package to display news in HTML format.\n",
10 | "\n",
11 | "To setup Jupyter Notebook environment for R or install Eikon Data API for R, please refer to this [article](https://developers.refinitiv.com/article/setup-jupyter-notebook-r)."
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "### The first step is loading the **eikonapir**, **plotly**, and **IRDisplay** packages."
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "library(eikonapir)\n",
28 | "library(plotly)\n",
29 | "library(IRdisplay)"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "### Next, call the **set_app_id** method with the application key. \n",
37 | "\n",
38 | "To create an application key, please refer to [Eikon Data API quick start guide](https://developers.refinitiv.com/eikon-apis/eikon-data-apis/quick-start)."
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "set_app_id('your application key')"
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {},
53 | "source": [
54 | "## 1. Use the **get_data** function to retrieve the latest data\n",
55 | "\n",
56 | "The following code calls the **get_data** function to retrieve the latest available close price, the volume of the latest trading day, and the low price for the latest trading day fields of IBM, GOOG.O, and MSFT.O instruments.\n",
57 | "\n",
58 | "The function returns a data frame with fields in columns and instruments as rows."
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {},
65 | "outputs": [],
66 | "source": [
67 | "data_frame1 <- get_data(list(\"IBM\", \"GOOG.O\", \"MSFT.O\"), list(\"TR.PriceClose\", \"TR.Volume\", \"TR.PriceLow\"))\n",
68 | "data_frame1"
69 | ]
70 | },
71 | {
72 | "cell_type": "markdown",
73 | "metadata": {},
74 | "source": [
75 | "## 2. Use the **get_data** function to retrieve the historical data and plot an OHLC chart\n",
76 | "\n",
77 | "The following code calls the **get_data** function to retrieve daily historical OPEN, HIGH, LOW, CLOSE fields from one year ago to the last trading day of IBM. \n",
78 | "\n",
79 | "The function returns a data frame with fields in columns and data points in rows."
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": null,
85 | "metadata": {},
86 | "outputs": [],
87 | "source": [
88 | "data_frame2 <- get_data(\"IBM\", \n",
89 | " list(\"TR.OPENPRICE.Date\",\"TR.OPENPRICE\",\"TR.HIGHPRICE\",\"TR.LOWPRICE\",\"TR.CLOSEPRICE\"),\n",
90 | " list(\"Frq\"=\"D\",\"SDate\"=\"0D\",\"EDate\"=\"-1AY\"))\n",
91 | "data_frame2"
92 | ]
93 | },
94 | {
95 | "cell_type": "markdown",
96 | "metadata": {},
97 | "source": [
98 | "### Modify the data frame by converting the values in the Date column to date. \n",
99 | "\n",
100 | "To create a chart, we need to convert the values in the Date column to date by using the **mutate** function.\n"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": null,
106 | "metadata": {},
107 | "outputs": [],
108 | "source": [
109 | "data_frame2 <- data_frame2 %>%\n",
110 | " mutate(Date=as.Date(Date, format=\"%Y-%m-%d\"))\n",
111 | "data_frame2"
112 | ]
113 | },
114 | {
115 | "cell_type": "markdown",
116 | "metadata": {},
117 | "source": [
118 | "### Use the data in the data frame to create an OHLC chart.\n",
119 | "\n",
120 | "It calls the **plot_ly** function to create an OHLC chart with the **Date**, **Open Price**, **Close Price**, **High Price**, and **Low Price** columns."
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": [
129 | "OHLCChart1 <- data_frame2 %>%\n",
130 | " plot_ly(x = ~Date, type=\"ohlc\",\n",
131 | " open = ~`Open Price`, close = ~`Close Price`,\n",
132 | " high = ~`High Price`, low = ~`Low Price`) %>%\n",
133 | " layout(title = \"Basic OHLC Chart\")"
134 | ]
135 | },
136 | {
137 | "cell_type": "markdown",
138 | "metadata": {},
139 | "source": [
140 | "### Display the OHLC chart"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": null,
146 | "metadata": {},
147 | "outputs": [],
148 | "source": [
149 | "OHLCChart1"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "## 3. Use the **get_timeseries** method to retrieve daily historical data\n",
157 | "\n",
158 | "The following code calls the **get_timeseries** method to retrieve daily historical data of GOOG.O from 01 Jan 2019 to 30 Sep 2019."
159 | ]
160 | },
161 | {
162 | "cell_type": "code",
163 | "execution_count": null,
164 | "metadata": {},
165 | "outputs": [],
166 | "source": [
167 | "data_frame3 = get_timeseries(list(\"GOOG.O\"),list(\"*\"),\"2019-01-01T00:00:00\",\"2019-09-30T00:00:00\",\"daily\")\n",
168 | "data_frame3"
169 | ]
170 | },
171 | {
172 | "cell_type": "markdown",
173 | "metadata": {},
174 | "source": [
175 | "### Modify the data frame\n",
176 | "\n",
177 | "In order to create a chart, the returned data frame will be modified:\n",
178 | "- Changing the last column name from NA to RIC\n",
179 | "- Converting the values in the TIMESTAMP column to date"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": null,
185 | "metadata": {},
186 | "outputs": [],
187 | "source": [
188 | "colnames(data_frame3)[[8]] = \"RIC\"\n",
189 | "data_frame3 <- data_frame3 %>%\n",
190 | " mutate(TIMESTAMP=as.Date(TIMESTAMP, format=\"%Y-%m-%d\"))\n",
191 | "data_frame3"
192 | ]
193 | },
194 | {
195 | "cell_type": "markdown",
196 | "metadata": {},
197 | "source": [
198 | "### Use the data in the data frame to create a candlestick chart\n",
199 | "\n",
200 | "It calls the **plot_ly** function to create a candlestick chart with the **TIMESTAMP**, **OPEN**, **CLOSE**, **HIGH**, and **LOW** columns."
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": null,
206 | "metadata": {},
207 | "outputs": [],
208 | "source": [
209 | "CandleStickChart <- data_frame3 %>%\n",
210 | " plot_ly(x = ~TIMESTAMP, type=\"candlestick\",\n",
211 | " open = ~OPEN, close = ~CLOSE,\n",
212 | " high = ~HIGH, low = ~LOW) %>%\n",
213 | " layout(title = \"Basic Candlestick Chart\")"
214 | ]
215 | },
216 | {
217 | "cell_type": "markdown",
218 | "metadata": {},
219 | "source": [
220 | "### Display the candlestick chart"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": null,
226 | "metadata": {},
227 | "outputs": [],
228 | "source": [
229 | "CandleStickChart"
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "## 4. Use the get_symbology function to convert instrument codes\n",
237 | "\n",
238 | "The following code calls the **get_symbology** method to convert RICS names to ISIN instrument names."
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": null,
244 | "metadata": {},
245 | "outputs": [],
246 | "source": [
247 | "ISINList <- get_symbology(list(\"MSFT.O\", \"GOOG.O\", \"IBM.N\"),\n",
248 | " from_symbol_type=\"RIC\", \n",
249 | " to_symbol_type=\"ISIN\")\n",
250 | "ISINList"
251 | ]
252 | },
253 | {
254 | "cell_type": "markdown",
255 | "metadata": {},
256 | "source": [
257 | "The following code calls the **get_symbology** method to convert ISIN instrument names to CUSIP instrument names."
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": null,
263 | "metadata": {},
264 | "outputs": [],
265 | "source": [
266 | "CUSIPList <- get_symbology(list(\"US5949181045\", \"US02079K1079\", \"US4592001014\"), \n",
267 | " from_symbol_type=\"ISIN\", \n",
268 | " to_symbol_type=\"CUSIP\")\n",
269 | "CUSIPList"
270 | ]
271 | },
272 | {
273 | "cell_type": "markdown",
274 | "metadata": {},
275 | "source": [
276 | "## 5. Use the get_news_headlines and get_news_story method to retrieve news\n",
277 | "\n",
278 | "The following code calls the **get_news_headlines** method to retrieve 10 news headlines about IBM.N in English. For each headline, it calls the **get_news_story** with the story ID to retrieve a news story. The news headlines and stories are displayed as HTML.\n"
279 | ]
280 | },
281 | {
282 | "cell_type": "code",
283 | "execution_count": null,
284 | "metadata": {},
285 | "outputs": [],
286 | "source": [
287 | "headlines <- get_news_headlines(\"R:IBM.N IN ENGLISH\")\n",
288 | "for (row in 1:nrow(headlines)) \n",
289 | "{ \n",
290 | " display_html(paste(\"
\",headlines[row,\"text\"],\"
\"))\n",
291 | " story <- get_news_story(headlines[row, \"storyId\"])\n",
292 | " display_html(story)\n",
293 | "}\n"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": null,
299 | "metadata": {},
300 | "outputs": [],
301 | "source": []
302 | }
303 | ],
304 | "metadata": {
305 | "kernelspec": {
306 | "display_name": "R",
307 | "language": "R",
308 | "name": "ir"
309 | },
310 | "language_info": {
311 | "codemirror_mode": "r",
312 | "file_extension": ".r",
313 | "mimetype": "text/x-r-source",
314 | "name": "R",
315 | "pygments_lexer": "r",
316 | "version": "3.6.1"
317 | }
318 | },
319 | "nbformat": 4,
320 | "nbformat_minor": 2
321 | }
322 |
--------------------------------------------------------------------------------
/EikonDataAPIR/readme.md:
--------------------------------------------------------------------------------
1 |
2 | # Eikon Data API R Example
3 |
4 | This example demonstrates how to use [Eikon Data API](https://developers.refinitiv.com/eikon-apis/eikon-data-api) with R on Jupyter Notebook. It uses the [eikonapir](https://github.com/ahmedmohamedali/eikonapir) package to retrieve data from Eikon and uses [Plotly](https://plot.ly/r/getting-started/) package to draw charts. It also uses the [IRDisplay](https://www.rdocumentation.org/packages/IRdisplay) package to display news in HTML format.
5 |
6 | To setup Jupyter Notebook environment for R or install Eikon Data API for R, please refer to this [article](https://developers.refinitiv.com/article/setup-jupyter-notebook-r).
7 |
8 | ### The first step is loading the **eikonapir**, **plotly**, and **IRDisplay** packages.
9 |
10 |
11 | ```R
12 | library(eikonapir)
13 | library(plotly)
14 | library(IRdisplay)
15 | ```
16 |
17 | ### Next, call the **set_app_id** method with the application key.
18 |
19 | To create an application key, please refer to [Eikon Data API quick start guide](https://developers.refinitiv.com/eikon-apis/eikon-data-apis/quick-start).
20 |
21 |
22 | ```R
23 | set_app_id('your application key')
24 | ```
25 |
26 | ## 1. Use the **get_data** function to retrieve the latest data
27 |
28 | The following code calls the **get_data** function to retrieve the latest available close price, the volume of the latest trading day, and the low price for the latest trading day fields of IBM, GOOG.O, and MSFT.O instruments.
29 |
30 | The function returns a data frame with fields in columns and instruments as rows.
31 |
32 |
33 | ```R
34 | data_frame1 <- get_data(list("IBM", "GOOG.O", "MSFT.O"), list("TR.PriceClose", "TR.Volume", "TR.PriceLow"))
35 | data_frame1
36 | ```
37 |
38 | ## 2. Use the **get_data** function to retrieve the historical data and plot an OHLC chart
39 |
40 | The following code calls the **get_data** function to retrieve daily historical OPEN, HIGH, LOW, CLOSE fields from one year ago to the last trading day of IBM.
41 |
42 | The function returns a data frame with fields in columns and data points in rows.
43 |
44 |
45 | ```R
46 | data_frame2 <- get_data("IBM",
47 | list("TR.OPENPRICE.Date","TR.OPENPRICE","TR.HIGHPRICE","TR.LOWPRICE","TR.CLOSEPRICE"),
48 | list("Frq"="D","SDate"="0D","EDate"="-1AY"))
49 | data_frame2
50 | ```
51 |
52 | ### Modify the data frame by converting the values in the Date column to date.
53 |
54 | To create a chart, we need to convert the values in the Date column to date by using the **mutate** function.
55 |
56 |
57 |
58 | ```R
59 | data_frame2 <- data_frame2 %>%
60 | mutate(Date=as.Date(Date, format="%Y-%m-%d"))
61 | data_frame2
62 | ```
63 |
64 | ### Use the data in the data frame to create an OHLC chart.
65 |
66 | It calls the **plot_ly** function to create an OHLC chart with the **Date**, **Open Price**, **Close Price**, **High Price**, and **Low Price** columns.
67 |
68 |
69 | ```R
70 | OHLCChart1 <- data_frame2 %>%
71 | plot_ly(x = ~Date, type="ohlc",
72 | open = ~`Open Price`, close = ~`Close Price`,
73 | high = ~`High Price`, low = ~`Low Price`) %>%
74 | layout(title = "Basic OHLC Chart")
75 | ```
76 |
77 | ### Display the OHLC chart
78 |
79 |
80 | ```R
81 | OHLCChart1
82 | ```
83 |
84 | ## 3. Use the **get_timeseries** method to retrieve daily historical data
85 |
86 | The following code calls the **get_timeseries** method to retrieve daily historical data of GOOG.O from 01 Jan 2019 to 30 Sep 2019.
87 |
88 |
89 | ```R
90 | data_frame3 = get_timeseries(list("GOOG.O"),list("*"),"2019-01-01T00:00:00","2019-09-30T00:00:00","daily")
91 | data_frame3
92 | ```
93 |
94 | ### Modify the data frame
95 |
96 | In order to create a chart, the returned data frame will be modified:
97 | - Changing the last column name from NA to RIC
98 | - Converting the values in the TIMESTAMP column to date
99 |
100 |
101 | ```R
102 | colnames(data_frame3)[[8]] = "RIC"
103 | data_frame3 <- data_frame3 %>%
104 | mutate(TIMESTAMP=as.Date(TIMESTAMP, format="%Y-%m-%d"))
105 | data_frame3
106 | ```
107 |
108 | ### Use the data in the data frame to create a candlestick chart
109 |
110 | It calls the **plot_ly** function to create a candlestick chart with the **TIMESTAMP**, **OPEN**, **CLOSE**, **HIGH**, and **LOW** columns.
111 |
112 |
113 | ```R
114 | CandleStickChart <- data_frame3 %>%
115 | plot_ly(x = ~TIMESTAMP, type="candlestick",
116 | open = ~OPEN, close = ~CLOSE,
117 | high = ~HIGH, low = ~LOW) %>%
118 | layout(title = "Basic Candlestick Chart")
119 | ```
120 |
121 | ### Display the candlestick chart
122 |
123 |
124 | ```R
125 | CandleStickChart
126 | ```
127 |
128 | ## 4. Use the get_symbology function to convert instrument codes
129 |
130 | The following code calls the **get_symbology** method to convert RICS names to ISIN instrument names.
131 |
132 |
133 | ```R
134 | ISINList <- get_symbology(list("MSFT.O", "GOOG.O", "IBM.N"),
135 | from_symbol_type="RIC",
136 | to_symbol_type="ISIN")
137 | ISINList
138 | ```
139 |
140 | The following code calls the **get_symbology** method to convert ISIN instrument names to CUSIP instrument names.
141 |
142 |
143 | ```R
144 | CUSIPList <- get_symbology(list("US5949181045", "US02079K1079", "US4592001014"),
145 | from_symbol_type="ISIN",
146 | to_symbol_type="CUSIP")
147 | CUSIPList
148 | ```
149 |
150 | ## 5. Use the get_news_headlines and get_news_story method to retrieve news
151 |
152 | The following code calls the **get_news_headlines** method to retrieve 10 news headlines about IBM.N in English. For each headline, it calls the **get_news_story** with the story ID to retrieve a news story. The news headlines and stories are displayed as HTML.
153 |
154 |
155 |
156 | ```R
157 | headlines <- get_news_headlines("R:IBM.N IN ENGLISH")
158 | for (row in 1:nrow(headlines))
159 | {
160 | display_html(paste("",headlines[row,"text"],"
"))
161 | story <- get_news_story(headlines[row, "storyId"])
162 | display_html(story)
163 | }
164 |
165 | ```
166 |
167 |
168 | ```R
169 |
170 | ```
171 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Setup Jupyter Notebook for R
2 |
3 | ## Overview
4 |
5 | R is an interpreted programming language for statistical computing and graphics supported by the [R Foundation](https://www.r-project.org/foundation/). It is widely used among statisticians and data miners for developing statistical software and data analysis.
6 |
7 | R is available as Free Software under the terms of the Free Software Foundation’s GNU General Public License in source code form. It compiles and runs on a wide variety of UNIX platforms and similar systems (including FreeBSD and Linux), Windows and macOS.
8 |
9 | Refinitiv's APIs, such as Eikon Data API, Datastream Web Service, DataScope Select, RTH, and Refinitiv Data Platform also support R. In this article, we will explain steps to:
10 | - Install R
11 | - Install Jupyter Notebook for R
12 | - Install Refinitiv's APIs for R, such as Eikon Data API, and Datastream Web Service
13 | - Install [Plotly](https://plot.ly/r/) which is R Open Source Graphing Library
14 |
15 | Moreover, in the end, there is a link to R examples that demonstrate how to use Refinitiv's APIs with Jupyter Notebook.
16 |
17 | For more information, please refer to https://developers.refinitiv.com/en/article-catalog/article/setup-jupyter-notebook-r.
18 |
19 |
--------------------------------------------------------------------------------
/RefinitivDataPlatformAPIR/RDPHistoricalRExample.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Refinitiv Data Platform Historical R Example\n",
8 | "\n",
9 | "Refinitiv Data Platform (RDP) is our cloud-enabled, open platform, that brings together content, analytics and proprietary, customer and third-party data distribution and management technology. It provides simple web-based API access to a broad range of content. \n",
10 | "\n",
11 | "This example demonstrates how to retrieve historical data from the Refinitiv Data Platform with R on Jupyter Notebook. It uses the [httr](https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html) package to send HTTP request messages and uses [Plotly](https://plot.ly/r/getting-started/) package to draw charts."
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "## The first step is loading the required libraries\n",
19 | "\n",
20 | "- [httr](https://cran.r-project.org/web/packages/httr/index.html): Tools for working with URLs and HTTP\n",
21 | "- [plotly](https://www.rdocumentation.org/packages/plotly/versions/4.9.0): Create interactive web graphics from 'ggplot2' graphs and/or a custom interface to the (MIT-licensed) JavaScript library 'plotly.js'\n",
22 | "- [zoo](https://cran.r-project.org/web/packages/zoo/index.html): The zoo package consists of the methods for totally ordered indexed observations. It aims at performing calculations containing irregular time series of numeric vectors, matrices & factors"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": [
31 | "library(httr)\n",
32 | "library(plotly)\n",
33 | "library(zoo) "
34 | ]
35 | },
36 | {
37 | "cell_type": "markdown",
38 | "metadata": {},
39 | "source": [
40 | "## Next, set URLs for the Refinitiv Data Platform services\n",
41 | "\n",
42 | "This example uses the following Refinitiv Data Platform services:\n",
43 | "\n",
44 | "- **/auth/oauth2/v1/token**: This service allows clients to get a token for password grant\n",
45 | "- **/data/historical-pricing/v1**: This service is used to retrieve Intraday and Interday time-series historical pricing data\n",
46 | "\n"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": null,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "REFINITIV_API_URL <- \"https://api.refinitiv.com/\"\n",
56 | "OAUTH2_TOKEN_URL <- paste(REFINITIV_API_URL, \"auth/oauth2/v1/token\", sep=\"\")\n",
57 | "HISTORICAL_PRICING_URL <- paste(REFINITIV_API_URL, \"data/historical-pricing/v1\", sep=\"\")"
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {},
63 | "source": [
64 | "## Then, set the Refinitiv Data Platform credential\n",
65 | "\n",
66 | "The Refinitiv Data Platform credential include:\n",
67 | "\n",
68 | "- **USERNAME**: The resource owner username (typically email)\n",
69 | "- **PASSWORD**: The resource owner password\n",
70 | "- **CLIENTID**: (Aka Application ID, aka AppKey.) A unique ID defined for an application making the request. Users can generate/manage their application ID's [here](https://emea1.apps.cp.thomsonreuters.com/apps/AppkeyGenerator)\n",
71 | "\n"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": null,
77 | "metadata": {},
78 | "outputs": [],
79 | "source": [
80 | "USERNAME <- \"\"\n",
81 | "PASSWORD <- \"\"\n",
82 | "CLIENTID <- \"\""
83 | ]
84 | },
85 | {
86 | "cell_type": "markdown",
87 | "metadata": {},
88 | "source": [
89 | "Instead of setting the Refinitiv Data Platform credential in the code, you can set credential information in the **.Renviron** file. Then, use the **Sys.getenv** method to retrieve the credential information. \n",
90 | "\n",
91 | "For example, the **.Renviron** file contains the following environment variables:\n",
92 | "\n",
93 | "```\n",
94 | "USERNAME=\n",
95 | "PASSWORD=\n",
96 | "CLIENTID=\n",
97 | "```\n",
98 | "Then, use the following code to set variables.\n",
99 | "\n",
100 | "```\n",
101 | "Credentials <-Sys.getenv(c(\"USERNAME\", \"PASSWORD\", \"CLIENTID\"))\n",
102 | "USERNAME <- Credentials[\"USERNAME\"]\n",
103 | "PASSWORD <- Credentials[\"PASSWORD\"]\n",
104 | "CLIENTID <- Credentials[\"CLIENTID\"]\n",
105 | "```\n",
106 | "\n",
107 | "If the HTTP requests must be sent through a proxy, the following code can be used to set a proxy.\n",
108 | "\n",
109 | "```\n",
110 | "set_config(use_proxy(url=\"\",port=))\n",
111 | "```\n",
112 | "\n",
113 | "Now, it is ready to use the Refinitiv Data Platform services. Next, we will show how to retrieve historical data from the Refinitiv Data Platform services.\n"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "## Retrieve Intraday and Interday time-series historical pricing data\n",
121 | "\n",
122 | "The following section will show how to use the **auth/oauth2** and **data/historical-pricing** services.\n",
123 | "\n",
124 | "## 1. Login\n",
125 | "\n",
126 | "Login is the first step for all EDP API requests. \n",
127 | "\n",
128 | "### Construct the body of the HTTP request message\n",
129 | "\n",
130 | "To login, the HTTP post message must be sent to the **auth/oauth2/v1/token** service. The body of the HTTP request message contains the following parameters.\n",
131 | "\n",
132 | "- **grant_type**: Supported values \"password\" and \"refresh_token\"\n",
133 | "- **username**: The resource owner username (typically email)\n",
134 | "- **password**: The resource owner password\n",
135 | "- **scope**: The scope of the access request\n",
136 | "- **takeExclusiveSignOnControl**: OPTIONAL. This is a Boolean that will allow the API Caller to create a session if the number of concurrent sessions has been reached\n",
137 | "- **client_id**: A unique ID defined for an application making the request. Users can generate/manage their application ID's [here](https://emea1.apps.cp.thomsonreuters.com/apps/AppkeyGenerator)\n",
138 | "\n",
139 | "For other parameters, please refer to the [API Documents](http://api.refinitiv.com/).\n",
140 | "\n",
141 | "The following code creates a list that contains those parameter names and values."
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "metadata": {},
148 | "outputs": [],
149 | "source": [
150 | "RequestTokenBody <- list(grant_type=\"password\",\n",
151 | " username=USERNAME,\n",
152 | " password=PASSWORD,\n",
153 | " scope=\"trapi\",\n",
154 | " takeExclusiveSignOnControl= \"True\",\n",
155 | " client_id=CLIENTID\n",
156 | " )"
157 | ]
158 | },
159 | {
160 | "cell_type": "markdown",
161 | "metadata": {},
162 | "source": [
163 | "### Send the HTTP post message to retrieve tokens\n",
164 | "\n",
165 | "The following code calls the **httr::post** method to send the HTTP post message which contains the login information in the message's body to the **auth/oauth2/v1/token** service. The login information is encoded with a URL encoded form (application/x-www-form-urlencoded) in the message's body. The HTTP post message also contains the **Authorization** HTTP header with the client id as its value. \n",
166 | "\n",
167 | "After receiving the response, it calls the **stop_for_status** function which converts HTTP errors to R errors or warnings. If the request was successful, it prints the response on the screen. Otherwise, it will stop."
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": null,
173 | "metadata": {},
174 | "outputs": [],
175 | "source": [
176 | "RequestTokenResponse <- httr::POST(OAUTH2_TOKEN_URL,\n",
177 | " add_headers(Authorization = CLIENTID), \n",
178 | " body = RequestTokenBody,\n",
179 | " encode = \"form\")\n",
180 | "stop_for_status(RequestTokenResponse)\n",
181 | "RequestTokenResponse"
182 | ]
183 | },
184 | {
185 | "cell_type": "markdown",
186 | "metadata": {},
187 | "source": [
188 | "If the response status is not 200, please verify the URL or login information.\n",
189 | "\n",
190 | "### Get an access token\n",
191 | "\n",
192 | "If the response status is 200, the HTTP response will contain an access token. This access token will be used in subsequent requests. \n",
193 | "\n",
194 | "The data is in JSON format so it calls the **httr::content** method to extract the JSON content from the HTTP response. Then, it saves an access token into the **ACCESS_TOKEN** variable so it can be used in the subsequent requests."
195 | ]
196 | },
197 | {
198 | "cell_type": "code",
199 | "execution_count": null,
200 | "metadata": {},
201 | "outputs": [],
202 | "source": [
203 | "TokenContent <- httr::content(RequestTokenResponse, \"parsed\", \"application/json\", encoding=\"UTF-8\")\n",
204 | "ACCESS_TOKEN <- TokenContent$access_token\n",
205 | "ACCESS_TOKEN"
206 | ]
207 | },
208 | {
209 | "cell_type": "markdown",
210 | "metadata": {},
211 | "source": [
212 | "After getting the access token, we can use this token to call other Refinitiv Data Platform services.\n",
213 | "\n",
214 | "## 2. Retrieve Interday Historical Pricing Summaries Data\n",
215 | "\n",
216 | "\n",
217 | "The historical pricing service can be used to retrieve Interday historical pricing summaries data through the **/views/interday-summaries** URL. \n",
218 | "\n",
219 | "### Send a request\n",
220 | "\n",
221 | "\n",
222 | "Interday Historical Pricing Summaries Data uses the HTTP GET request. The HTTP message has the **Authorization** HTTP header with the token type (bearer) and access token as its value. The requested instrument must be added at the end of the URL and other request parameters are in the query string.\n",
223 | "\n",
224 | "\n",
225 | "The following code calls the **httr::GET** method to request the daily historical pricing data (OPEN, HIGH, LOW, and CLOSE) of IBM.N for one year period with the following parameters:\n",
226 | "\n",
227 | "- **start**: The start date and timestamp of the query is in ISO8601 with UTC only e.g 2018-12-24T09:00:00.000000000Z\n",
228 | "- **end**: The end date and timestamp of the query is in ISO8601 with UTC only e.g 2018-12-24T09:00:00.000000000Z\n",
229 | "- **fields**: The comma-separated list of fields that are to be returned in the response\n",
230 | "- **interval**: The consolidation interval in ISO8601. The support intervals are PT1M, PT5M, PT10M, PT30M, PT60M, and PT1H\n",
231 | "\n",
232 | "The request parameters are created by using a list. For other parameters, please refer to the [API Documents](http://api.refinitiv.com/).\n",
233 | "\n",
234 | "After receiving the response, it calls the **stop_for_status** to verify the error. If the request was successful, it prints the response on the screen. Otherwise, it will stop."
235 | ]
236 | },
237 | {
238 | "cell_type": "code",
239 | "execution_count": null,
240 | "metadata": {},
241 | "outputs": [],
242 | "source": [
243 | "INTERDAY_SUMMARY_URL <- paste(HISTORICAL_PRICING_URL, \"/views/interday-summaries/\", sep=\"\")\n",
244 | "\n",
245 | "HistoricalResponse <- httr::GET(paste(INTERDAY_SUMMARY_URL, \"IBM.N\", sep=\"\"),\n",
246 | " add_headers(Authorization = paste(TokenContent[[\"token_type\"]], ACCESS_TOKEN)),\n",
247 | " query=list(start=Sys.Date()-365,\n",
248 | " end=Sys.Date(),\n",
249 | " fields=\"OPEN_PRC,HIGH_1,LOW_1,TRDPRC_1\",\n",
250 | " interval=\"P1D\")\n",
251 | " ) \n",
252 | "stop_for_status(HistoricalResponse)\n",
253 | "HistoricalResponse \n"
254 | ]
255 | },
256 | {
257 | "cell_type": "markdown",
258 | "metadata": {},
259 | "source": [
260 | "If the response status is 401, the access token may be expired. Please re-run the first step (**1. Login**) to get a new access token.\n",
261 | "\n",
262 | "\n",
263 | "### Extract the JSON content\n",
264 | "\n",
265 | "If the response status is 200, the HTTP response will contain the daily historical data in JSON tabular format.\n",
266 | "\n",
267 | "The following code calls the **httr::content** method to extract the JSON content from the HTTP response and then print it on the screen."
268 | ]
269 | },
270 | {
271 | "cell_type": "code",
272 | "execution_count": null,
273 | "metadata": {},
274 | "outputs": [],
275 | "source": [
276 | "HistoricalContent <- httr::content(HistoricalResponse, \"parsed\", \"application/json\", encoding=\"UTF-8\")\n",
277 | "HistoricalContent"
278 | ]
279 | },
280 | {
281 | "cell_type": "markdown",
282 | "metadata": {},
283 | "source": [
284 | "### Create a data frame from the JSON content\n",
285 | "\n",
286 | "With R, it is easier to display or manipulate the data in the data frame. \n",
287 | "\n",
288 | "The following code creates a function called **JsonTabularToDataFrame** which accepts the JSON tabular content and returns a data frame. Next, it calls this function with the JSON content returned by the previous step in order to create the data frame. Then, it prints the data frame on the screen."
289 | ]
290 | },
291 | {
292 | "cell_type": "code",
293 | "execution_count": null,
294 | "metadata": {},
295 | "outputs": [],
296 | "source": [
297 | "JsonTabularToDataFrame <- function(jsonObj){\n",
298 | " temp_df <- NULL\n",
299 | "\n",
300 | " for(i in c(1:length(jsonObj[[1]]$headers))){\n",
301 | " tempData <- c()\n",
302 | " for(j in c(1:length(jsonObj[[1]]$data))){ \n",
303 | " tempData <- c(tempData,jsonObj[[1]]$data[[j]][[i]]) \n",
304 | " }\n",
305 | " if (is.null(temp_df)){\n",
306 | " temp_df = data.frame(tempColName = tempData)\n",
307 | " names(temp_df)[names(temp_df) == \"tempColName\"] <- jsonObj[[1]]$headers[[i]]$name\n",
308 | " }else{\n",
309 | " temp_df[jsonObj[[1]]$headers[[i]]$name] <- tempData\n",
310 | " }\n",
311 | " }\n",
312 | " return(temp_df)\n",
313 | "}\n",
314 | "\n",
315 | "df1 <- JsonTabularToDataFrame(HistoricalContent)\n",
316 | "df1"
317 | ]
318 | },
319 | {
320 | "cell_type": "markdown",
321 | "metadata": {},
322 | "source": [
323 | "### Calculate the simple moving average\n",
324 | "\n",
325 | "This section uses the last prices (TRDPRC_1) in the data frame to calculate the 30-day simple moving average. It uses the **rollmean** function in the **[zoo](https://cran.r-project.org/web/packages/zoo/index.html)** package. The **rollmean** is a generic function for computing rolling means of ordered observations. \n",
326 | "\n",
327 | "Then, the 30-day simple moving average values are added into the data frame as a new column (**sma30**)."
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": null,
333 | "metadata": {},
334 | "outputs": [],
335 | "source": [
336 | "df1 <- df1 %>% \n",
337 | "mutate(sma30 = rollmean(TRDPRC_1, k=30, fill=NA, align=\"left\"))\n",
338 | "df1"
339 | ]
340 | },
341 | {
342 | "cell_type": "markdown",
343 | "metadata": {},
344 | "source": [
345 | "### Plot a chart\n",
346 | "\n",
347 | "The following code calls the **plot_ly** function to plot a candlestick chart for the DATE, OPEN_PRC, HIGH_1, LOW_1, and TRDPRC_1 columns. It also plots a line chart for the **sma30** column."
348 | ]
349 | },
350 | {
351 | "cell_type": "code",
352 | "execution_count": null,
353 | "metadata": {},
354 | "outputs": [],
355 | "source": [
356 | "chart1 <- df1 %>%\n",
357 | " plot_ly(x = ~DATE, type=\"candlestick\",\n",
358 | " open = ~OPEN_PRC, close = ~TRDPRC_1,\n",
359 | " high = ~HIGH_1, low = ~LOW_1, name=\"OHLC\") %>%\n",
360 | " add_lines(x = ~DATE, y = ~sma30, line = list(color = 'black', width = 0.75), inherit = F, name=\"SMA30D\") %>%\n",
361 | " layout(title = \"Basic Candlestick Chart\")\n",
362 | "chart1\n"
363 | ]
364 | },
365 | {
366 | "cell_type": "markdown",
367 | "metadata": {},
368 | "source": [
369 | "## 2. Retrieve the Time & Sales data\n",
370 | "\n",
371 | "The historical pricing service can be used to retrieve time series pricing events data (i.e. trades, quotes, and corrections) through the **/views/events** URL. \n",
372 | "\n",
373 | "### Send a request\n",
374 | "\n",
375 | "Time series pricing events use the HTTP GET request. The HTTP message has the **Authorization** HTTP header with the token type (bearer) and access token as its value. The requested instrument must be added at the end of the URL and other request parameters are in the query string.\n",
376 | "\n",
377 | "\n",
378 | "The following code calls the **httr::GET** method to request the Times & Sales data of IBM.N with the following parameters:\n",
379 | "\n",
380 | "- **eventTypes**: The list of market events (comma delimited) supports the values of trade, quote, and correction for event types\n",
381 | "- **end**: The end date and timestamp of the query is in ISO8601 with UTC only e.g 2018-12-24T09:00:00.000000000Z\n",
382 | "- **fields**: The comma-separated list of fields that are to be returned in the response\n",
383 | "- **count**: The maximum number of data returned\n",
384 | "\n",
385 | "The request parameters are created by using a list. For other parameters, please refer to the [API Documents](http://api.refinitiv.com/).\n",
386 | "\n",
387 | "After receiving the response, it calls the **stop_for_status** to verify the error. If the request was successful, it prints the response on the screen. Otherwise, it will stop."
388 | ]
389 | },
390 | {
391 | "cell_type": "code",
392 | "execution_count": null,
393 | "metadata": {},
394 | "outputs": [],
395 | "source": [
396 | "PRICING_EVENTS_URL <- paste(HISTORICAL_PRICING_URL, \"/views/events/\", sep=\"\")\n",
397 | "\n",
398 | "EventsResponse <- httr::GET(paste(PRICING_EVENTS_URL, \"IBM.N\", sep=\"\"),\n",
399 | " add_headers(Authorization = paste(TokenContent[[\"token_type\"]], ACCESS_TOKEN)),\n",
400 | " query=list(eventTypes=\"trade\",\n",
401 | " fields=\"TRDPRC_1\",\n",
402 | " count=100)\n",
403 | " ) \n",
404 | "stop_for_status(EventsResponse)\n",
405 | "EventsResponse \n"
406 | ]
407 | },
408 | {
409 | "cell_type": "markdown",
410 | "metadata": {},
411 | "source": [
412 | "If the response stat is 401, the access token may be expired. Please re-run the first step (**1. Login**) to get a new access token.\n",
413 | "\n",
414 | "### Extract the JSON content\n",
415 | "\n",
416 | "If the response status is 200, the HTTP response will contain the Times & Sales data in JSON tabular format.\n",
417 | "\n",
418 | "The following code calls the **httr::content** method to extract the JSON content from the HTTP response and then print it on the screen."
419 | ]
420 | },
421 | {
422 | "cell_type": "code",
423 | "execution_count": null,
424 | "metadata": {},
425 | "outputs": [],
426 | "source": [
427 | "EventsPricingContent <- httr::content(EventsResponse, \"parsed\", \"application/json\", encoding=\"UTF-8\")\n",
428 | "EventsPricingContent"
429 | ]
430 | },
431 | {
432 | "cell_type": "markdown",
433 | "metadata": {},
434 | "source": [
435 | "### Create a data frame from the JSON content\n",
436 | "\n",
437 | "With R, it is easier to display or manipulate the data in the data frame. \n",
438 | "\n",
439 | "The following code calls the **JsonTabularToDataFrame** method to create a data frame from the retrieved JSON tabular data. Then, it prints the data frame on the screen."
440 | ]
441 | },
442 | {
443 | "cell_type": "code",
444 | "execution_count": null,
445 | "metadata": {},
446 | "outputs": [],
447 | "source": [
448 | "df2 <- JsonTabularToDataFrame(EventsPricingContent)\n",
449 | "df2"
450 | ]
451 | },
452 | {
453 | "cell_type": "markdown",
454 | "metadata": {},
455 | "source": [
456 | "### Plot a chart\n",
457 | "\n",
458 | "The following code calls the **plot_ly** function to plot a line chart from the Time & Sales data."
459 | ]
460 | },
461 | {
462 | "cell_type": "code",
463 | "execution_count": null,
464 | "metadata": {},
465 | "outputs": [],
466 | "source": [
467 | "chart2 <- df2 %>%\n",
468 | " plot_ly(x = ~DATE_TIME, type=\"scatter\",\n",
469 | " mode=\"lines\",\n",
470 | " y = ~TRDPRC_1,\n",
471 | " name=\"Trade\") %>%\n",
472 | " layout(title = \"Trade\")\n",
473 | "chart2\n"
474 | ]
475 | },
476 | {
477 | "cell_type": "markdown",
478 | "metadata": {},
479 | "source": [
480 | "The historical pricing service can also be used to retrieve Intraday historical pricing summaries data and a single record of time series pricing events. Please refer to the [API Documents](http://api.refinitiv.com/) for more information."
481 | ]
482 | }
483 | ],
484 | "metadata": {
485 | "kernelspec": {
486 | "display_name": "R",
487 | "language": "R",
488 | "name": "ir"
489 | },
490 | "language_info": {
491 | "codemirror_mode": "r",
492 | "file_extension": ".r",
493 | "mimetype": "text/x-r-source",
494 | "name": "R",
495 | "pygments_lexer": "r",
496 | "version": "3.6.1"
497 | }
498 | },
499 | "nbformat": 4,
500 | "nbformat_minor": 2
501 | }
502 |
--------------------------------------------------------------------------------
/RefinitivDataPlatformAPIR/README.MD:
--------------------------------------------------------------------------------
1 |
2 | # Refinitiv Data Platform Historical R Example
3 |
4 | Refinitiv Data Platform (RDP) is our cloud-enabled, open platform, that brings together content, analytics and proprietary, customer and third-party data distribution and management technology. It provides simple web-based API access to a broad range of content.
5 |
6 | This example demonstrates how to retrieve historical data from the Refinitiv Data Platform with R on Jupyter Notebook. It uses the [httr](https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html) package to send HTTP request messages and uses [Plotly](https://plot.ly/r/getting-started/) package to draw charts.
7 |
8 | ## The first step is loading the required libraries
9 |
10 | - [httr](https://cran.r-project.org/web/packages/httr/index.html): Tools for working with URLs and HTTP
11 | - [plotly](https://www.rdocumentation.org/packages/plotly/versions/4.9.0): Create interactive web graphics from 'ggplot2' graphs and/or a custom interface to the (MIT-licensed) JavaScript library 'plotly.js'
12 | - [zoo](https://cran.r-project.org/web/packages/zoo/index.html): The zoo package consists of the methods for totally ordered indexed observations. It aims at performing calculations containing irregular time series of numeric vectors, matrices & factors
13 |
14 |
15 | ```R
16 | library(httr)
17 | library(plotly)
18 | library(zoo)
19 | ```
20 |
21 | ## Next, set URLs for the Refinitiv Data Platform services
22 |
23 | This example uses the following Refinitiv Data Platform services:
24 |
25 | - **/auth/oauth2/v1/token**: This service allows clients to get a token for password grant
26 | - **/data/historical-pricing/v1**: This service is used to retrieve Intraday and Interday time-series historical pricing data
27 |
28 |
29 |
30 |
31 | ```R
32 | REFINITIV_API_URL <- "https://api.refinitiv.com/"
33 | OAUTH2_TOKEN_URL <- paste(REFINITIV_API_URL, "auth/oauth2/v1/token", sep="")
34 | HISTORICAL_PRICING_URL <- paste(REFINITIV_API_URL, "data/historical-pricing/v1", sep="")
35 | ```
36 |
37 | ## Then, set the Refinitiv Data Platform credential
38 |
39 | The Refinitiv Data Platform credential include:
40 |
41 | - **USERNAME**: The resource owner username (typically email)
42 | - **PASSWORD**: The resource owner password
43 | - **CLIENTID**: (Aka Application ID, aka AppKey.) A unique ID defined for an application making the request. Users can generate/manage their application ID's [here](https://emea1.apps.cp.thomsonreuters.com/apps/AppkeyGenerator)
44 |
45 |
46 |
47 |
48 | ```R
49 | USERNAME <- ""
50 | PASSWORD <- ""
51 | CLIENTID <- ""
52 | ```
53 |
54 | Instead of setting the Refinitiv Data Platform credential in the code, you can set credential information in the **.Renviron** file. Then, use the **Sys.getenv** method to retrieve the credential information.
55 |
56 | For example, the **.Renviron** file contains the following environment variables:
57 |
58 | ```
59 | USERNAME=
60 | PASSWORD=
61 | CLIENTID=
62 | ```
63 | Then, use the following code to set variables.
64 |
65 | ```
66 | Credentials <-Sys.getenv(c("USERNAME", "PASSWORD", "CLIENTID"))
67 | USERNAME <- Credentials["USERNAME"]
68 | PASSWORD <- Credentials["PASSWORD"]
69 | CLIENTID <- Credentials["CLIENTID"]
70 | ```
71 |
72 | If the HTTP requests must be sent through a proxy, the following code can be used to set a proxy.
73 |
74 | ```
75 | set_config(use_proxy(url="",port=))
76 | ```
77 |
78 | Now, it is ready to use the Elektron Data Platform services. Next, we will show how to retrieve historical data from the Elektron Data Platform services.
79 |
80 |
81 | ## Retrieve Intraday and Interday time-series historical pricing data
82 |
83 | The following section will show how to use the **auth/oauth2** and **data/historical-pricing** services.
84 |
85 | ## 1. Login
86 |
87 | Login is the first step for all EDP API requests.
88 |
89 | ### Construct the body of the HTTP request message
90 |
91 | To login, the HTTP post message must be sent to the **auth/oauth2/v1/token** service. The body of the HTTP request message contains the following parameters.
92 |
93 | - **grant_type**: Supported values "password" and "refresh_token"
94 | - **username**: The resource owner username (typically email)
95 | - **password**: The resource owner password
96 | - **scope**: The scope of the access request
97 | - **takeExclusiveSignOnControl**: OPTIONAL. This is a Boolean that will allow the API Caller to create a session if the number of concurrent sessions has been reached
98 | - **client_id**: A unique ID defined for an application making the request. Users can generate/manage their application ID's [here](https://emea1.apps.cp.thomsonreuters.com/apps/AppkeyGenerator)
99 |
100 | For other parameters, please refer to the [API Documents](http://api.refinitiv.com/).
101 |
102 | The following code creates a list that contains those parameter names and values.
103 |
104 |
105 | ```R
106 | RequestTokenBody <- list(grant_type="password",
107 | username=USERNAME,
108 | password=PASSWORD,
109 | scope="trapi",
110 | takeExclusiveSignOnControl= "True",
111 | client_id=CLIENTID
112 | )
113 | ```
114 |
115 | ### Send the HTTP post message to retrieve tokens
116 |
117 | The following code calls the **httr::post** method to send the HTTP post message which contains the login information in the message's body to the **auth/oauth2/v1/token** service. The login information is encoded with a URL encoded form (application/x-www-form-urlencoded) in the message's body. The HTTP post message also contains the **Authorization** HTTP header with the client id as its value.
118 |
119 | After receiving the response, it calls the **stop_for_status** function which converts HTTP errors to R errors or warnings. If the request was successful, it prints the response on the screen. Otherwise, it will stop.
120 |
121 |
122 | ```R
123 | RequestTokenResponse <- httr::POST(OAUTH2_TOKEN_URL,
124 | add_headers(Authorization = CLIENTID),
125 | body = RequestTokenBody,
126 | encode = "form")
127 | stop_for_status(RequestTokenResponse)
128 | RequestTokenResponse
129 | ```
130 |
131 | If the response status is not 200, please verify the URL or login information.
132 |
133 | ### Get an access token
134 |
135 | If the response status is 200, the HTTP response will contain an access token. This access token will be used in subsequent requests.
136 |
137 | The data is in JSON format so it calls the **httr::content** method to extract the JSON content from the HTTP response. Then, it saves an access token into the **ACCESS_TOKEN** variable so it can be used in the subsequent requests.
138 |
139 |
140 | ```R
141 | TokenContent <- httr::content(RequestTokenResponse, "parsed", "application/json", encoding="UTF-8")
142 | ACCESS_TOKEN <- TokenContent$access_token
143 | ACCESS_TOKEN
144 | ```
145 |
146 | After getting the access token, we can use this token to call other Elektron Data Platform services.
147 |
148 | ## 2. Retrieve Interday Historical Pricing Summaries Data
149 |
150 |
151 | The historical pricing service can be used to retrieve Interday historical pricing summaries data through the **/views/interday-summaries** URL.
152 |
153 | ### Send a request
154 |
155 |
156 | Interday Historical Pricing Summaries Data uses the HTTP GET request. The HTTP message has the **Authorization** HTTP header with the token type (bearer) and access token as its value. The requested instrument must be added at the end of the URL and other request parameters are in the query string.
157 |
158 |
159 | The following code calls the **httr::GET** method to request the daily historical pricing data (OPEN, HIGH, LOW, and CLOSE) of IBM.N for one year period with the following parameters:
160 |
161 | - **start**: The start date and timestamp of the query is in ISO8601 with UTC only e.g 2018-12-24T09:00:00.000000000Z
162 | - **end**: The end date and timestamp of the query is in ISO8601 with UTC only e.g 2018-12-24T09:00:00.000000000Z
163 | - **fields**: The comma-separated list of fields that are to be returned in the response
164 | - **interval**: The consolidation interval in ISO8601. The support intervals are PT1M, PT5M, PT10M, PT30M, PT60M, and PT1H
165 |
166 | The request parameters are created by using a list. For other parameters, please refer to the [API Documents](http://api.refinitiv.com/).
167 |
168 | After receiving the response, it calls the **stop_for_status** to verify the error. If the request was successful, it prints the response on the screen. Otherwise, it will stop.
169 |
170 |
171 | ```R
172 | INTERDAY_SUMMARY_URL <- paste(HISTORICAL_PRICING_URL, "/views/interday-summaries/", sep="")
173 |
174 | HistoricalResponse <- httr::GET(paste(INTERDAY_SUMMARY_URL, "IBM.N", sep=""),
175 | add_headers(Authorization = paste(TokenContent[["token_type"]], ACCESS_TOKEN)),
176 | query=list(start=Sys.Date()-365,
177 | end=Sys.Date(),
178 | fields="OPEN_PRC,HIGH_1,LOW_1,TRDPRC_1",
179 | interval="P1D")
180 | )
181 | stop_for_status(HistoricalResponse)
182 | HistoricalResponse
183 |
184 | ```
185 |
186 | If the response status is 401, the access token may be expired. Please re-run the first step (**1. Login**) to get a new access token.
187 |
188 |
189 | ### Extract the JSON content
190 |
191 | If the response status is 200, the HTTP response will contain the daily historical data in JSON tabular format.
192 |
193 | The following code calls the **httr::content** method to extract the JSON content from the HTTP response and then print it on the screen.
194 |
195 |
196 | ```R
197 | HistoricalContent <- httr::content(HistoricalResponse, "parsed", "application/json", encoding="UTF-8")
198 | HistoricalContent
199 | ```
200 |
201 | ### Create a data frame from the JSON content
202 |
203 | With R, it is easier to display or manipulate the data in the data frame.
204 |
205 | The following code creates a function called **JsonTabularToDataFrame** which accepts the JSON tabular content and returns a data frame. Next, it calls this function with the JSON content returned by the previous step in order to create the data frame. Then, it prints the data frame on the screen.
206 |
207 |
208 | ```R
209 | JsonTabularToDataFrame <- function(jsonObj){
210 | temp_df <- NULL
211 |
212 | for(i in c(1:length(jsonObj[[1]]$headers))){
213 | tempData <- c()
214 | for(j in c(1:length(jsonObj[[1]]$data))){
215 | tempData <- c(tempData,jsonObj[[1]]$data[[j]][[i]])
216 | }
217 | if (is.null(temp_df)){
218 | temp_df = data.frame(tempColName = tempData)
219 | names(temp_df)[names(temp_df) == "tempColName"] <- jsonObj[[1]]$headers[[i]]$name
220 | }else{
221 | temp_df[jsonObj[[1]]$headers[[i]]$name] <- tempData
222 | }
223 | }
224 | return(temp_df)
225 | }
226 |
227 | df1 <- JsonTabularToDataFrame(HistoricalContent)
228 | df1
229 | ```
230 |
231 | ### Calculate the simple moving average
232 |
233 | This section uses the last prices (TRDPRC_1) in the data frame to calculate the 30-day simple moving average. It uses the **rollmean** function in the **[zoo](https://cran.r-project.org/web/packages/zoo/index.html)** package. The **rollmean** is a generic function for computing rolling means of ordered observations.
234 |
235 | Then, the 30-day simple moving average values are added into the data frame as a new column (**sma30**).
236 |
237 |
238 | ```R
239 | df1 <- df1 %>%
240 | mutate(sma30 = rollmean(TRDPRC_1, k=30, fill=NA, align="left"))
241 | df1
242 | ```
243 |
244 | ### Plot a chart
245 |
246 | The following code calls the **plot_ly** function to plot a candlestick chart for the DATE, OPEN_PRC, HIGH_1, LOW_1, and TRDPRC_1 columns. It also plots a line chart for the **sma30** column.
247 |
248 |
249 | ```R
250 | chart1 <- df1 %>%
251 | plot_ly(x = ~DATE, type="candlestick",
252 | open = ~OPEN_PRC, close = ~TRDPRC_1,
253 | high = ~HIGH_1, low = ~LOW_1, name="OHLC") %>%
254 | add_lines(x = ~DATE, y = ~sma30, line = list(color = 'black', width = 0.75), inherit = F, name="SMA30D") %>%
255 | layout(title = "Basic Candlestick Chart")
256 | chart1
257 |
258 | ```
259 |
260 | ## 2. Retrieve the Time & Sales data
261 |
262 | The historical pricing service can be used to retrieve time series pricing events data (i.e. trades, quotes, and corrections) through the **/views/events** URL.
263 |
264 | ### Send a request
265 |
266 | Time series pricing events use the HTTP GET request. The HTTP message has the **Authorization** HTTP header with the token type (bearer) and access token as its value. The requested instrument must be added at the end of the URL and other request parameters are in the query string.
267 |
268 |
269 | The following code calls the **httr::GET** method to request the Times & Sales data of IBM.N with the following parameters:
270 |
271 | - **eventTypes**: The list of market events (comma delimited) supports the values of trade, quote, and correction for event types
272 | - **end**: The end date and timestamp of the query is in ISO8601 with UTC only e.g 2018-12-24T09:00:00.000000000Z
273 | - **fields**: The comma-separated list of fields that are to be returned in the response
274 | - **count**: The maximum number of data returned
275 |
276 | The request parameters are created by using a list. For other parameters, please refer to the [API Documents](http://api.refinitiv.com/).
277 |
278 | After receiving the response, it calls the **stop_for_status** to verify the error. If the request was successful, it prints the response on the screen. Otherwise, it will stop.
279 |
280 |
281 | ```R
282 | PRICING_EVENTS_URL <- paste(HISTORICAL_PRICING_URL, "/views/events/", sep="")
283 |
284 | EventsResponse <- httr::GET(paste(PRICING_EVENTS_URL, "IBM.N", sep=""),
285 | add_headers(Authorization = paste(TokenContent[["token_type"]], ACCESS_TOKEN)),
286 | query=list(eventTypes="trade",
287 | fields="TRDPRC_1",
288 | count=100)
289 | )
290 | stop_for_status(EventsResponse)
291 | EventsResponse
292 |
293 | ```
294 |
295 | If the response stat is 401, the access token may be expired. Please re-run the first step (**1. Login**) to get a new access token.
296 |
297 | ### Extract the JSON content
298 |
299 | If the response status is 200, the HTTP response will contain the Times & Sales data in JSON tabular format.
300 |
301 | The following code calls the **httr::content** method to extract the JSON content from the HTTP response and then print it on the screen.
302 |
303 |
304 | ```R
305 | EventsPricingContent <- httr::content(EventsResponse, "parsed", "application/json", encoding="UTF-8")
306 | EventsPricingContent
307 | ```
308 |
309 | ### Create a data frame from the JSON content
310 |
311 | With R, it is easier to display or manipulate the data in the data frame.
312 |
313 | The following code calls the **JsonTabularToDataFrame** method to create a data frame from the retrieved JSON tabular data. Then, it prints the data frame on the screen.
314 |
315 |
316 | ```R
317 | df2 <- JsonTabularToDataFrame(EventsPricingContent)
318 | df2
319 | ```
320 |
321 | ### Plot a chart
322 |
323 | The following code calls the **plot_ly** function to plot a line chart from the Time & Sales data.
324 |
325 |
326 | ```R
327 | chart2 <- df2 %>%
328 | plot_ly(x = ~DATE_TIME, type="scatter",
329 | mode="lines",
330 | y = ~TRDPRC_1,
331 | name="Trade") %>%
332 | layout(title = "Trade")
333 | chart2
334 |
335 | ```
336 |
337 | The historical pricing service can also be used to retrieve Intraday historical pricing summaries data and a single record of time series pricing events. Please refer to the [API Documents](http://api.refinitiv.com/) for more information.
338 |
--------------------------------------------------------------------------------
/Step1_InstallR.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step1_InstallR.png
--------------------------------------------------------------------------------
/Step2_Python.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step2_Python.png
--------------------------------------------------------------------------------
/Step2_PythonVersion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step2_PythonVersion.png
--------------------------------------------------------------------------------
/Step3_Jupyter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step3_Jupyter.png
--------------------------------------------------------------------------------
/Step3_JupyterSupport.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step3_JupyterSupport.png
--------------------------------------------------------------------------------
/Step4_IRKernel1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step4_IRKernel1.png
--------------------------------------------------------------------------------
/Step4_IRKernel2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step4_IRKernel2.png
--------------------------------------------------------------------------------
/Step4_Jupyter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/Step4_Jupyter.png
--------------------------------------------------------------------------------
/datastream.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/datastream.png
--------------------------------------------------------------------------------
/eikonapir.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/eikonapir.png
--------------------------------------------------------------------------------
/plotly.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/plotly.png
--------------------------------------------------------------------------------
/plotly_ex.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/plotly_ex.png
--------------------------------------------------------------------------------
/rtools.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LSEG-API-Samples/Example.RefinitivAPIs.R.Jupyter/HEAD/rtools.png
--------------------------------------------------------------------------------