How To Convert MT4 Daily And 4 Hour CSV File Into R XTS Time Series?

This is a very important post that I am writing. It took me more than 2 weeks to figure out how to read MT4 csv files into R and then convert them into xts time series. I am writing this post to help those traders who want to use R to predict the market movement. You can ask what is R? R is a powerful statistical software that is open source and can be downloaded free. R is being increasingly used in developing trading indicators. I also plan to develop an R Forex Indicator and EA. So let’s get started.

First open your MT4 and goto Tools > History Center and download the EURUSD240.csv and EURUSD1440.csv. Use the following commands to read the EURUSD daily file into R.

quotes <- read.csv(“E:/MarketData/EURUSD1440.csv”, header=FALSE)

The header is FALSE because when you download the csv from MT4, it has no header. So we need to tell R that the first row is the data row and its not a header. If the first row is the header than you should put the header to TRUE so that R knows that the first row the column headers.

Now this is a data.frame file. Data.frame is the most basic R data structure. You can think of it as a matrix. The csv file contains the Date, Time, High, Low, Open, Close and Volume. You need to load the Quantmod R package:


Quantmod also contains the xts package. Now we can convert this data.frame into a time series object because the price data is infact a time series. Each OLHC price is being stamped with a particular date in case of daily csv. We use the following command to turn this data.frame into an xts time series object.

x <- as.xts(quotes[,-(1:2)], as.Date(paste(quotes[,1]),format=’%Y.%m.%d’))

This R command sounds complicated. So let’s discuss it. We are using the Date format %Y.%m.%d which tells R that this is daily data so that it considers the periodicity of this time series to be daily. We delete the time column as it is not needed in the daily data and is going to confuse R. Now we need to just name the columns with the following command.

colnames(x) <-c(‘Open’,’High’,’Low’,’Close’,’Volume’)

The first column is the index and is in the Date format %Y.%m.%d as said above. Now let’s try to read the EURUSD240.csv. This is the 4 Hour data.  We will read it as a data.frame with the same R command.

quotes <- read.csv(“E:/MarketData/EURUSD240.csv”, header=FALSE)

As above the header is FALSE because the first row is a data row. Now if we use the above R command that was used for the daily data, we will get an error. We need to convert the H4 data.frame into an xts time series using the POSIXct object as below:

x <- as.xts(quotes[,-(1:2)], as.POSIXct(paste(quotes[,1],quotes[,2]),format=’%Y.%m.%d %H:%M’))

In the above command, you can see we have used the POSIXct date format %Y.%m.%d %H:%M. Why? Because in intraday data hour and minutes are also used to tell us about the open and close of the intraday candles. If we had used the Date format %Y.%m.%d we would have got an error. So when we are dealing with intraday data we should always use the POSIXct time object when we coerce the data.frame into an xts time series. You can now draw the candlechart in R with the command:

candleChart(last(x, “2 days”), theme = “white”, TA = NULL)

EURUSD H4 Candle Chart

This is the very first step in using R. If you cannot upload the data correctly you cannot use the power of R and Quantmod in making the predictions. As said above my goal is to develop a forex indicator based on R that predicts that next 4 candles on any timeframe. So stay tuned as I write more posts and explain how to design and develop such a forex indicator. If you are getting bored, you can watch this video on the Million Dollar Trader Mentoring Program Caribbean Documentary. You can see how serious trading has become. You have to learn a lot to statistical analysis if you want to become a successful trader. You can also watch this video on the Million Dollar Trader Mentoring Program Thailand Documentary.