Quantmod: Analyse Stock and Forex Data Using R

The quantmod library in R allows for streaming of financial data. This is quite useful when it comes to analysing financial markets and formulating quantitative trading strategies on the basis of such data. Let’s take a look at how quantmod can be used to analyse data for stocks and forex.
 
 

Use Quantmod to stream stock data

In this particular example, we use data downloaded on the stock ConocoPhillips (COP) as an illustration.

Disclaimer: The below examples are purely to illustrate the use of the quantmod library in R and various means in such the same could be utilized. Nothing presented here is to be regarded as investment advice. The reader alone bears all responsibility for the consequences of any financial action taken using the above information.

To do this, we download our data, generate a graph, and define our data using the procedures below:
 
1. We download data for COP by loading the Quantmod library and using the getSymbols function.
 

library("quantmod")
getSymbols("COP",src="yahoo", from = "2015-06-01", to = "2016-04-15") # from yahoo finance
barChart(COP)

 
2. We then generate a chart using the chartSeries command and add the indicators specified:
 

require(TTR)
chartSeries(COP.Adjusted) 
addMACD()
addATR()
addBBands()
addEMA(n=8, col="white")
addEMA(n=21, col="blue")
 
quantmod stock cop

 
3. We name our stock variable by setting it equal to the daily return for COP; defining our variable in this way is necessary to run subsequent statistical tests:
 

stock=dailyReturn(COP)

 
4. Now that our stock variable is loaded into R, we now have a time series that we can use for modelling purposes, e.g. ARIMA, incorporation into regression analysis, etc.

I already described in another post how ARIMA and Holt-Winters can be used to analyse stock returns, you can read it here.

Use Quantmod to stream forex data

When it comes to forex, one of my favourite is the momentum trading strategy. This is when an asset makes a sharp move in a particular direction, and due to the momentum is likely to continue moving in that direction.

Let’s take the example of the movements of the British pound (GBP) after Donald Trump had won the U.S. Presidential Election. From 11th November 2016, we see that the GBP had started appreciating sharply against the JPY. So, how could a trader have caught this trade just as it was unfolding?

An effective way of doing so in this instance would have been to conduct a strong-weak analysis, i.e. observe which currency among the majors was the strongest vs. the weakest over a certain period of time. For this purpose, we choose to compare returns over 2, 5, and 10 days.

Essentially, we want to find out which currency has been the strongest against the USD over the chosen time periods, and which one was the weakest. Therefore, we are calculating the returns for the following pairs: AUD/USD, CAD/USD, CHF/USD, EUR/USD, GBP/USD, USD/JPY, NZD/USD.

1. Load the quantmod library (or install if you have not done so already.

install.packages("quantmod")
library("quantmod")

2. Set the current date (or the date for which you wish to work backwards and conduct the analysis), as well as the time period d.

dateset="2016-11-11"
d=2

3. Calculate daily returns for the respective currency pairs.

AUD2d<-"AUD/USD"
time2<-as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
AUD2d <- getFX("AUD/USD", from=time1, to=time2)
AUD.data2d<-dailyReturn(get(AUD2d))
AUD.data2d

CAD2d<-"CAD/USD"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
CAD2d <- getFX("CAD/USD", from=time1, to=time2)
CAD.data2d<-dailyReturn(get(CAD2d))
CAD.data2d

CHF2d<-"CHF/USD"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
CHF2d <- getFX("CHF/USD", from=time1, to=time2)
CHF.data2d←dailyReturn(get(CHF2d))
CHF.data2d

EUR2d<-"EUR/USD"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
EUR2d <- getFX("EUR/USD", from=time1, to=time2)
EUR.data2d<-dailyReturn(get(EUR2d))
EUR.data2d

GBP2d<-"GBP/USD"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
GBP2d <- getFX("GBP/USD", from=time1, to=time2)
GBP.data2d<-dailyReturn(get(GBP2d))
GBP.data2d

JPY2d<-"USD/JPY"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
JPY2d <- getFX("USD/JPY", from=time1, to=time2)
JPY.data2d<-dailyReturn(get(JPY2d))
JPY.data2d

NZD2d<-"NZD/USD"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
NZD2d <- getFX("NZD/USD", from=time1, to=time2)
NZD.data2d<-dailyReturn(get(NZD2d))
NZD.data2d

4. Sum the returns of all pairs.

sum(AUD.data2d)
sum(CAD.data2d)
sum(CHF.data2d)
sum(EUR.data2d)
sum(GBP.data2d)
variable1=sum(JPY.data2d)
variable2=(1+variable1)
variable3=1/variable2
JPYreturn=1-variable3
paste(-JPYreturn)
sum(NZD.data2d)

5. Use the which.max and which.min formulas to identify the strongest and weakest currency pair.

TWODAYSTRONG<-c("sum(NZD.data2d)", "sum(GBP.data2d)", "-JPYreturn", "sum(EUR.data2d)", "sum(AUD.data2d)", "sum(CAD.data2d)", "sum(CHF.data2d)")[which.max(c(sum(NZD.data2d), sum(GBP.data2d), -JPYreturn, sum(EUR.data2d), sum(AUD.data2d), sum(CAD.data2d), sum(CHF.data2d)))]
TWODAYWEAK<-c("sum(NZD.data2d)", "sum(GBP.data2d)", "-JPYreturn", "sum(EUR.data2d)", "sum(AUD.data2d)", "sum(CAD.data2d)", "sum(CHF.data2d)")[which.min(c(sum(NZD.data2d), sum(GBP.data2d), -JPYreturn, sum(EUR.data2d), sum(AUD.data2d), sum(CAD.data2d), sum(CHF.data2d)))]

6. We are now repeating the process as above, only we are setting d = 5 (5 days).

d=5

AUD5d<-"AUD/USD"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
AUD5d <- getFX("AUD/USD", from=time1, to=time2)
AUD.data5d<-dailyReturn(get(AUD5d))
AUD.data5d

CAD5d<-"CAD/USD"
time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
CAD5d <- getFX("CAD/USD", from=time1, to=time2)
CAD.data5d<-dailyReturn(get(CAD5d))
CAD.data5d

Once you have calculated returns for all pairs when d=5, we now do the same for d=10.

7. We can now calculate which currencies were the strongest and weakest over 2, 5, and 10 day periods.

#2-DAY STRENGTH-WEAKNESS
TWODAYSTRONG
TWODAYWEAK

#5-DAY STRENGTH-WEAKNESS
FIVEDAYSTRONG
FIVEDAYWEAK

#10-DAY STRENGTH-WEAKNESS
TENDAYSTRONG
TENDAYWEAK

Results

> #2-DAY STRENGTH-WEAKNESS
> TWODAYSTRONG
[1] "sum(GBP.data2d)"
> TWODAYWEAK
[1] "-JPYreturn"

> #5-DAY STRENGTH-WEAKNESS
> FIVEDAYSTRONG
[1] "sum(GBP.data5d)"
> FIVEDAYWEAK
[1] "-JPYreturn"

> #10-DAY STRENGTH-WEAKNESS
> TENDAYSTRONG
[1] "sum(GBP.data10d)"
> TENDAYWEAK
[1] "-JPYreturn"

We see that consistently over the three time periods, GBP was consistently the strongest currency against the USD, while JPY was consistently the weakest. At the time, this served as a strong signal that the GBP was set to strengthen further against the JPY, and indeed had done so. Therefore, momentum trading can be a handy tool when it comes to technical analysis, and the R quantmod library can greatly help in automating this process.

R Code

forexmomentum.txt