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. 

First the resources needed to be downloaded and installed :

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 :


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
raw<- read.delim2("/YOUR_PATH/EURUSD.csv",header=TRUE,sep=",")

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

#convert date


#write data to .csv

##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']
myADX <- function(x) ADX(HLC(x))[,'ADX']
myAroon <- function(x) aroon(x[,c('High','Low')])$oscillator
myBB <- function(x) BBands(HLC(x))[,'pctB']
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)) ~
mySAR(EURUSD) + runMean(Cl(EURUSD)) + runSD(Cl(EURUSD)))

Tdata.train <-,

Tdata.eval <- na.omit(,

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

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


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


#paste both to a new list that holds everything

#remove Delt.Cl.EURUSD - not needed anymore.

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


#paste to a new list that holds everything

#get a summary of our 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.

Please see my Trading Strategies in my blog here-

FOREX Trading

Anas Ahmed said...

Dear author, I am still not getting what it is that you are calling 'R'? Is it any kind of Forex Signals or something else?

Themos Kalafatis said...

R is the statistical software with which this analysis is made : see

Harry L said...

Hi Themos, This looks interesting as I am familiar with R but not with trading. I downloded the EURUSD data from the link to repeat your code, but the data do not seem to be in the csv format. Could you please check it again?
"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."