This example demonstrates how to use Eikon Data API with R on Jupyter Notebook. It uses the eikonapir package to retrieve data from Eikon and uses Plotly package to draw charts. It also uses the IRDisplay package to display news in HTML format.
To setup Jupyter Notebook environment for R or install Eikon Data API for R, please refer to this article.
library(eikonapir)
library(plotly)
library(IRdisplay)
To create an application key, please refer to Eikon Data API quick start guide.
set_app_id('your application key')
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.
The function returns a data frame with fields in columns and instruments as rows.
data_frame1 <- get_data(list("IBM", "GOOG.O", "MSFT.O"), list("TR.PriceClose", "TR.Volume", "TR.PriceLow"))
data_frame1
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.
The function returns a data frame with fields in columns and data points in rows.
data_frame2 <- get_data("IBM",
list("TR.OPENPRICE.Date","TR.OPENPRICE","TR.HIGHPRICE","TR.LOWPRICE","TR.CLOSEPRICE"),
list("Frq"="D","SDate"="0D","EDate"="-1AY"))
data_frame2
To create a chart, we need to convert the values in the Date column to date by using the mutate function.
data_frame2 <- data_frame2 %>%
mutate(Date=as.Date(Date, format="%Y-%m-%d"))
data_frame2
It calls the plot_ly function to create an OHLC chart with the Date, Open Price, Close Price, High Price, and Low Price columns.
OHLCChart1 <- data_frame2 %>%
plot_ly(x = ~Date, type="ohlc",
open = ~`Open Price`, close = ~`Close Price`,
high = ~`High Price`, low = ~`Low Price`) %>%
layout(title = "Basic OHLC Chart")
OHLCChart1
The following code calls the get_timeseries method to retrieve daily historical data of GOOG.O from 01 Jan 2019 to 30 Sep 2019.
data_frame3 = get_timeseries(list("GOOG.O"),list("*"),"2019-01-01T00:00:00","2019-09-30T00:00:00","daily")
data_frame3
In order to create a chart, the returned data frame will be modified:
- Changing the last column name from NA to RIC
- Converting the values in the TIMESTAMP column to date
colnames(data_frame3)[[8]] = "RIC"
data_frame3 <- data_frame3 %>%
mutate(TIMESTAMP=as.Date(TIMESTAMP, format="%Y-%m-%d"))
data_frame3
It calls the plot_ly function to create a candlestick chart with the TIMESTAMP, OPEN, CLOSE, HIGH, and LOW columns.
CandleStickChart <- data_frame3 %>%
plot_ly(x = ~TIMESTAMP, type="candlestick",
open = ~OPEN, close = ~CLOSE,
high = ~HIGH, low = ~LOW) %>%
layout(title = "Basic Candlestick Chart")
CandleStickChart
The following code calls the get_symbology method to convert RICS names to ISIN instrument names.
ISINList <- get_symbology(list("MSFT.O", "GOOG.O", "IBM.N"),
from_symbol_type="RIC",
to_symbol_type="ISIN")
ISINList
The following code calls the get_symbology method to convert ISIN instrument names to CUSIP instrument names.
CUSIPList <- get_symbology(list("US5949181045", "US02079K1079", "US4592001014"),
from_symbol_type="ISIN",
to_symbol_type="CUSIP")
CUSIPList
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.
headlines <- get_news_headlines("R:IBM.N IN ENGLISH")
for (row in 1:nrow(headlines))
{
display_html(paste("<h1>",headlines[row,"text"],"</h1>"))
story <- get_news_story(headlines[row, "storyId"])
display_html(story)
}