## Monday, January 10, 2011

### Forex Trading with R : Part 1

I recently started learning R - probably something i should have done a long time ago - and since learning by doing is the best way to learn something i decided to  use  R to generate buy/see/hold signals for the EUR/USD Pair. For those that wish to use R for making Trading decisions, this series of posts is a short introduction with which one can pursue the subject further. By no means it is implied that this post's methodology is the one that  you should use  to trade :   different response variables, signal thresholds, technical indicators and classifiers than the ones presented here should be tried. Then, elaborate testing methods should be put to use to assess the  performance of  each classifier and the worth of your trading strategy.

1)  Packages : quantmod, nnet, e1071, tseries, randomforest
2) A file that contains OHLC Data. For this example EUR/USD Data are used. You can download an example file here. I downloaded the EUR/USD historical data here

I would also highly recommend getting Data Mining with R : A concise book that goes through an introduction of using R and then presents various case studies one of which is about Using R to predict variations of the S&P Index. The author also provides package "DMwR" that includes all necessary functionality for generating signals, extracting precision / recall metrics of generated models, performing Monte Carlo Estimates and evaluating trading strategies.

After downloading the file from step (2), place the file to a directory of your choice. Now copy and paste the following commands in R :

library(e1071)
library(nnet)
library(randomforest)
library(quantmod)
library(tseries)

Next we import the csv file from the directory that was originally saved  (change "YOUR_PATH" accordingly with the directory path you saved the csv file) :

#get data OHLC from csv file

Now, paste the following to R (change again YOUR_PATH accordingly) :

#convert date
stripday<-strptime(raw\$DATE,format="%Y%m%d")
fxdata<-data.frame(stripday,raw)

fxdata\$TIME<-NULL
fxdata\$TICKER<-NULL
fxdata\$DATE<-NULL
colnames(fxdata)<-c("Date","Open","Low","High","Close")

#write data to .csv
write.table(fxdata,"/YOUR_PATH/eurusd.csv",quote=FALSE,sep=",",row.names=FALSE)

##transform to an xts object

Now we define some technical indicators, the model to work on and a function that generates our trading signals  :

#setup Technical Indicators

myATR <- function(x) ATR(HLC(x))[,'atr']
mySMI <- function(x) SMI(HLC(x))[,'SMI']
myAroon <- function(x) aroon(x[,c('High','Low')])\$oscillator
myBB <- function(x) BBands(HLC(x))[,'pctB']
myChaikinVol<-function(x)Delt(chaikinVolatility(x[,c("High","Low")]))[,1]
myCLV <- function(x) EMA(CLV(HLC(x)))[,1]
myMACD <- function(x) MACD(Cl(x))[,2]
mySAR <- function(x) SAR(x[,c('High','Close')]) [,1]
myVolat <- function(x) volatility(OHLC(x),calc="garman")[,1]
myEMA10 <- function(x) EMA(Cl(x),n=10)[,1]
myEMA20 <- function(x) EMA(Cl(x),n=20)[,1]
myEMA30 <- function(x) EMA(Cl(x),n=30)[,1]
myEMA50 <- function(x) EMA(Cl(x),n=50)[,1]
myEMA60 <- function(x) EMA(Cl(x),n=60)[,1]

data.model <- specifyModel(Delt(Cl(EURUSD)) ~
myATR(EURUSD) + mySMI(EURUSD) + myADX(EURUSD) + myAroon(EURUSD) +
myBB(EURUSD) + myChaikinVol(EURUSD) + myCLV(EURUSD) +myEMA10(EURUSD) +myEMA20(EURUSD) +myEMA30(EURUSD) +myEMA50(EURUSD) + myEMA60(EURUSD) +
CMO(Cl(EURUSD)) + EMA(Delt(Cl(EURUSD))) +
myVolat(EURUSD) + myMACD(EURUSD) + RSI(Cl(EURUSD)) +
mySAR(EURUSD) + runMean(Cl(EURUSD)) + runSD(Cl(EURUSD)))

Tdata.train <- as.data.frame(modelData(data.model,
data.window=c('2008-01-01','2010-01-01')))

Tdata.eval <- na.omit(as.data.frame(modelData(data.model,
data.window=c('2010-01-02','2010-11-01'))))

# a very simple signal function
signals<-function(x) {

if(x>=-0.005&&x<=0.005) {result<-"hold"} else
if(x<-0.005) {result<-"sell"}

result
}

#create class vector that holds TRAINING buy,sell,hold signals

class<-sapply(Tdata.train\$Delt.Cl.EURUSD,signals)

#paste both to a new list that holds everything
traindata<-cbind(Tdata.train,class)

#remove Delt.Cl.EURUSD - not needed anymore.
traindata\$Delt.Cl.EURUSD<-NULL

#create class vector that  holds TESTING buy,sell,hold signals

class<-sapply(Tdata.eval\$Delt.Cl.EURUSD,signals)

#paste to a new list that holds everything
testdata<-cbind(Tdata.eval,class)
testdata\$Delt.Cl.EURUSD<-NULL

#get a summary of our traindata
summary(traindata)

The last command prints out some summary statistics of the traindata sample. Notice the 'class' attribute and the distribution of buy, hold and sell signals .

Now we are ready to apply some modeling techniques using traindata and testdata as the datasets to work with. Although i would suggest using also a third sample for validation (remember the "elaborate testing" discussed at the beginning), for this example we will keep things  simple.  More on the next post

Alexander Kharevich said...

Hi Themos! Thank you for the interesting post. I think you have a fundamental mistake in data model. You using current day prices Delt(Cl(EURUSD)) and in part 2 you training and predict current day prices based on current day indicators. In this version your system make very good predictions =) but if we use prices with lag to next day Next(Delt(Cl(EURUSD)) for predictions next day(perion) system has bad performance (~0.6).

skype: ninjaproger

Themos Kalafatis said...

Hi Alexander,

Thanks for your comment. I will have to look at my original code as i have not dealt with this for quite some time. It is interesting though because i remember i was not getting very good prediction metrics as you described - before the correction was made. Normally, very good results would tell me that either i have found a cash-making machine or something is wrong. Anyway i will have a look at it once i get the time as i am completely swamped with work at the moment.

Jacob Walker said...

Nicely presented information for FOREX Trading.

I like your blog because I am also an FOREX Analyst.