Getting Intraday and Daily Forex Data

For the last couple of days I have been looking for a good source of historical forex data in OHLC format.
I found a few good articles but nothing really stood out for me.
So I decided to take it upon myself to write a small function to fetch historical data for most forex pairs.
The source of the data is at the following URL http://www.fxhistoricaldata.com/.
The original post was found here https://www.r-bloggers.com/intraday-backtest/ but it looks like the API has changed since then.

First create a function that fetches the data and converts it to an XTS object.

ForexData.R

fxhistoricaldata <- function
(
  Symbol,
  timeframe,
  download = FALSE
) 
{   
  
  # setup temp folder
  temp.folder <- paste(getwd(), 'temp', sep='/')
  dir.create(temp.folder, F)
  filename <- paste(temp.folder, '/',"fxhistoricaldata_",Symbol ,"_" ,timeframe,".csv", sep='')
  
  if(download) {
    downloadfile <- paste("http://api.fxhistoricaldata.com/v1/indicators?instruments=" ,Symbol ,"&expression=open,high,low,close&item_count=10000&format=csv&timeframe=" ,timeframe,sep='')
    download.file(downloadfile, filename,  mode = 'wb')
  }
    
    tempdf <- read.csv(filename)
    colnames(tempdf) <- c("Curr","Date","Open","High","Low","Close")
    tempdf <- tempdf[c("Date","Open","High","Low","Close")]
    tempdf$Date <- ymd_hms(tempdf$Date)
    out <-  xts(tempdf[,-1], order.by=tempdf[,1])
  
    return(out)
}

Then call the function from you main script.

main.r

require(quantmod)
require(IKTrading)
require(lubridate)
require(quantstrat)
require(PerformanceAnalytics)
require(candlesticks)

source("forexdata.R")

EURUSD <- fxhistoricaldata('EURUSD' ,'hour',download=T)
USDJPY <- fxhistoricaldata('USDJPY' ,'day',download=F)

chartSeries(EURUSD)
chartSeries(USDJPY)

Simple as that.
My next post I will do some backtesting on the data.

By |2017-02-27T12:24:28+00:00February 27th, 2017|Quantitative Analysis|