# Momentum Forex Trading Using The R Quantmod Library

We have already seen the basics of setting up quantmod in R to be able to obtain information on financial securities. The quantmod library is also able to obtain information on currencies, and R can be quite useful when it comes to formulating trading strategies.

Disclaimer: The above examples are purely to illustrate the use of the quantmod library in R. Nothing presented here should be regarded as investment advice. The reader alone bears all responsibility for the consequences of any financial action taken using the above information.

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.

# Code

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

time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")

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(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

time2←as.Date(dateset)
time1<-time2-as.difftime(d, unit="days")
```

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