Financial data in R - FOREX and Inflation - PriceR

Financial data in R - FOREX and Inflation - PriceR

August 14, 2022

The PriceR Package

What is PriceR about?

While Operating with financial data, we might want to incorporate information about floating exchange rate between FIAT currencies or historical data about the inflation on a specific country.

PriceR is a package available in R that will collect such information for us in a very simple way.

You can find the source code at:

Using PriceR Package

Installing PriceR in RStudio

You can install PriceR in R with the following command:

# CRAN (stable)
install.packages("priceR")

Using PriceR in R together with plotly

Query Historical FIAT Exchange Rates:

You can use PriceR to query historical exchange rates in R the following way:

exc_rate <- historical_exchange_rates("EUR", to = "USD",
                                    start_date = "2010-01-01",
                                    end_date = "2022-08-30")


colnames(exc_rate)[which(names(exc_rate) == "date")] <- "ref_date"
colnames(exc_rate)[2] <- "ratio"

Then, just get some help from plotly to create one interactive visualization:

plot_ly(exc_rate, type = 'scatter', mode = 'lines')%>%
      add_trace(x = ~ref_date, y = ~ratio, showlegend = FALSE) %>%
                    layout(title = '<b>USD to EUR<b>', xaxis = list(title = 'Date'), 
                           yaxis = list(title = 'Exchange Rate'),
                           legend = list(title=list(text='<b> Date </b>')))

Query Historical FIAT Exchange Rates:

We can query historical inflation data in R the following way:

Year <- 2000:2021
nominal_prices <- rep(1,length(Year))

df_inflation <- data.frame(Year, nominal_prices)
df_inflation$in_202x_dollars_factor <- adjust_for_inflation(nominal_prices, Year, "US", to_date = 2021) #PriceR


df_inflation <- mutate(df_inflation, inflation_level = ( lag(in_202x_dollars_factor)/in_202x_dollars_factor -1)*100 )

NOTE: Data for a recently ended year might take some time to be ready in the package

plot_ly(df_inflation, type = 'scatter', mode = 'lines') %>%
      add_trace(x = ~Year, y = ~in_202x_dollars_factor, name="Inflation factor to Y2021") %>%
                    layout(title = '<b>US Historical Inflation<b>', xaxis = list(title = 'Year'), 
                           yaxis = list(title = 'a'),
                           legend = list(title=list(text='<b> Legend </b>')))  %>%
      add_trace(x = ~Year, y = ~inflation_level, name="Inflation Yearly") %>%
                    layout(title = '<b>US Historical Inflation<b>', xaxis = list(title = 'Year'), 
                          yaxis = list(title = '(%)'), 
                          legend = list(title=list(text='<b> Legend </b>')))

Project Example using PriceR

I found PriceR really useful and have used it during the creation of this Shiny project: