Tuesday, November 20, 2012

Source of Forex historical intraday data

As mentioned in the last post, I am trying to find some historical forex intraday data, at least in hours, better in minutes or seconds.

I found a post listing 6 places to download forex historical intraday data. I select the follows as I feel they are  easier to use.

Fxhistoricaldata

Forex intraday data provided by Fxhistoricaldata is available in two different periods (hourly and daily) and for 17 FX pairs.

How to download quotes:

  • - Go to http://www.fxhistoricaldata.com/
  • - Click on one currency pair
  • - Click on "Hourly" to download 1-hour data
  • - Rename the downloaded file by adding the ".zip" extension
  • - Open the zipped file and extract the CSV data

I think, instead of using R to download daily forex data from Oanda, I prefer downloading data and do the analysis seperately. However, one thing I am worred is R's ability to work with big data which is an issue since long time ago -- several problems when R meets big data makes me stay farther from it.... 



Monday, November 19, 2012

Get ForEx data using quantmod R package

The first step of every analysis is getting enough data.

I am interested in the foreign exchange market and curious about the pattern about the exchange rate change; therefore, I try to find some convenient way to obtain the ForEx data.

Thanks to the "quantmod" package recently developed by some kind people, now I can download the forex data at least for some preliminary analysis, if not perfect data I hope to have.

The follows are what I have learnt from an example in the "quantmod" webiste.


# for example, I need to have forex data about USD again CAD price from 2004 to the current 

require(quantmod)


getSymbols("USD/CAD",src="oanda",from="2012-01-01")

forex12 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2011-01-01",to="2011-12-31")
forex11 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2010-01-01",to="2010-12-31")
forex10 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2009-01-01",to="2009-12-31")
forex09 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2008-01-01",to="2008-12-31")
forex08 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2007-01-01",to="2007-12-31")
forex07 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2006-01-01",to="2006-12-31")
forex06 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2005-01-01",to="2005-12-31")
forex05 <- USDCAD
getSymbols("USD/CAD",src="oanda",from="2004-01-01",to="2004-12-31")
forex04 <- USDCAD

forex <-rbind(forex04,forex05,forex06,forex07,forex08,forex09,forex10,forex11,forex12)


# make a rough plot to see the daily change

chartSeries(forex,theme = chartTheme("white"))



This downloading method works well, but I still feel the data is not enough to support some analysis, particular some short term trading strategy, so I really need to figure out if there is any way to have data in 5 mins or hours instead of on a daily average.


Monday, November 12, 2012

illustrate the crime trends across years

One day I was asked for help to illustrate and compare the crime trends for 4 communities in Chicago which are Beverly Hill, Washington Heights, Mount Greenwood and Morgan Park. The crime count data is from the Policy Department of Chicago. It gives all crimes happened in each community from year 2001 to 2010.

Personally, I feel it would not be hard to make a quick plot, but it worth putting some time to make this small task look nicer. Therefore, I decided to use ggplot2 and make the plot in R.

Hope someone who also wants to make similar plots like I did can eventually find my code and procedure useful. If anyone has other interesting ideas to make the plots nicer or more informative, please leave your comment :)



library(ggplot2) #import ggplot2 library
dat <- read.csv("community_crime.csv",header=T)
dat$community = factor(dat$community,
labels=c("Beverly Hill","Washington Heights","Mount Greenwood","Morgan Park")) 

X11(width = 18, height = 12) 

ggplot(dat, aes(x=year, y=any_1000, color=community)) + 
geom_point(shape=19,size=I(8),alpha = 0.3) +

scale_colour_hue(l=50) + # Use a slightly darker palette than normal

geom_smooth(method=lm,   # Add linear regression lines

se=FALSE,    # Don't add shaded confidence region

fullrange=T, size=1.4)+ # Extend regression lines

ylab("all crime count per 1000 population")+

xlab("Year")+

opts(title = expression("All Crime Count per 1000 population"))