Skip to content

Instantly share code, notes, and snippets.

View timelyportfolio's full-sized avatar

timelyportfolio timelyportfolio

View GitHub Profile
@timelyportfolio
timelyportfolio / japan trade with korea.r
Created March 14, 2012 03:43
japan trade with korea.r
#further explore Japanese trade data by region
#website is http://www.customs.go.jp/toukei/suii/html/time_e.htm
#format is http://www.customs.go.jp/toukei/suii/html/data/d42ma001.csv
#and the filename increments from 001 to 008 for the geographic areas
require(quantmod)
numgeog <- 8 #there are really 9 but the last is "special area"
urls <- paste("http://www.customs.go.jp/toukei/suii/html/data/d42ma00",
@timelyportfolio
timelyportfolio / calendar heat of system entry.r
Created April 9, 2012 21:05
calendar heat of system entry
#quick example of d3.js style calendar heat map
#thanks so much Paul Bleicher, MD PhD who is Chief Medical Officer
# at Humedica, a next-generation clinical informatics company
# that provides novel business intelligence solutions
# to the health care and life science industries
#discussed in Revolution Analytics blog post
#http://blog.revolutionanalytics.com/2009/11/charting-time-series-as-calendar-heat-maps-in-r.html
con=url("http://blog.revolution-computing.com/downloads/calendarHeat.R")
source(con)
## title:First Attempt at a knitr Performance Report
## author:Timely Portfolio
require(PerformanceAnalytics)
require(xtable)
data(edhec)
table.CalendarReturns(edhec[,1:3])[13:15]
charts.PerformanceSummary(edhec[,1:3],
colorset=c("gray50","steelblue2","steelblue4"),
@timelyportfolio
timelyportfolio / performance summary.r
Created April 14, 2012 02:26
performance summary RNW and R
require(knitr)
knit2pdf("performance summary.rnw")
%% LyX 2.0.2 created this file. For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english,nohyper,noae]{tufte-handout}
\usepackage{helvet}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[unicode=true,pdfusetitle,
bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=1,
breaklinks=true,pdfborder={0 0 0},backref=false,colorlinks=false]
require(quantmod)
require(PerformanceAnalytics)
getSymbols("VFINX",from="1990-01-01",adjust=TRUE)
getSymbols("VBMFX",from="1990-01-01",adjust=TRUE)
perf <- na.omit(merge(monthlyReturn(VBMFX[,4]),monthlyReturn(VFINX[,4])))
colnames(perf) <- c("VBMFX","VFINX")
#get 8 month RSI; randomly picked 8; no optimization
rsi<- lag(merge(RSI(perf[,1],n=8),RSI(perf[,2],n=8)),k=1)
#allocate between vbmfx and vfinx based on highest RSI
require(quantmod)
require(PerformanceAnalytics)
getSymbols("VFINX",from="1990-01-01",adjust=TRUE)
getSymbols("VBMFX",from="1990-01-01",adjust=TRUE)
perf <- na.omit(merge(monthlyReturn(VBMFX[,4]),monthlyReturn(VFINX[,4])))
colnames(perf) <- c("VBMFX","VFINX")
#get 8 month RSI; randomly picked 8; no optimization
rsi<- lag(merge(RSI(perf[,1],n=8),RSI(perf[,2],n=8)),k=1)
#allocate between vbmfx and vfinx based on highest RSI
#analyze breakpoints with the R package bfast
#please read the paper
#Verbesselt J, Hyndman R, Newnham G, Culvenor D (2010)
#Detecting Trend and Seasonal Changes in Satellite Image Time Series.
#Remote Sensing of Environment, 114(1), 106–115.
#http://dx.doi.org/10.1016/j.rse.2009.08.014
require(bfast)
require(quantmod)
#analyze breakpoints in realtime with the R package bfast
#please read the paper
#Jan Verbesselt, Achim Zeileis, Martin Herold (2011). Near Real-Time Disturbance
#Detection in Terrestrial Ecosystems Using Satellite Image Time Series: Drought
#Detection in Somalia. Working Paper 2011-18. Working Papers in Economics and
#Statistics, Research Platform Empirical and Experimental Economics, Universitaet
#Innsbruck. URL http://EconPapers.RePEc.org/RePEc:inn:wpaper:2011-18
#http://scholar.google.com/scholar?cluster=9016488513865299942&hl=en&as_sdt=0,1
#install r-forge development version of bfast
@timelyportfolio
timelyportfolio / quantstrat to build on.r
Created April 30, 2012 01:41
quantstrat to build on.r
#thanks so much to the developers of quantstrat
#99% of this code comes from the demos in the quantstrat package
#now let's define our silly countupdown function
CUD <- function(price,n) {
#CUD takes the n-period sum of 1 (up days) and -1 (down days)
temp<-runSum(ifelse(ROC(price,1,type="discrete") > 0,1,-1),n)
colnames(temp) <- "CUD"
temp
}