Monday, April 1, 2013

Extract Trough and Peak Dates in xts object

x = as.xts(1:30,Sys.Date()+1:30) 

# extract smallest value date
( time.trough <- time(x)[ x == min(x) ] )

# extract largest value date

( time.peak <- time(x)[ x == max(x) ] )

# extract particular value date

( time.one <- time(x)[ x == 1 ] )




Friday, March 29, 2013

Opera Browser, Copy with Formatting



Opera Browser:

How to copy HTML by selecting elements on a web page and clicking the right mouse button.

N.B. You need to enable HTML formatting.

Step 1. Duplicate the menu:

Settings > Preferences > Advanced > Toolbars > Menu Setup > "Opera Standard" --> Duplicate

You will find the duplicated menu setup here: c:\users\username\AppData\Roaming\Opera\Opera\menu

The files name usually is "standard menu (1).ini".

Step 2. Add a menu item:

Edit the menu(1).ini file.

Got to:

[Hotclick Popup Menu]

Copy-paste the lines below:

--------------------
Item, "Copy with formating"=Go to page, "javascript:function sS() {var s = window.getSelection();if (s.getRangeAt && s.rangeCount) {return s.getRangeAt(0);}return null;}function rS(range) {if (range) {var s = window.getSelection();s.removeAllRanges();s.addRange(range);}}",,"Copying with formatting" & Go to page, "javascript:void(tmp=sS());void(document.designMode='on')" & Delay, 300 & Go to page, "javascript:void(rS(tmp))" & Delay, 300 & Copy & Delay, 300 & Go to page, "javascript:void(document.designMode='off')""


Thursday, March 21, 2013

R Tip - Loops and Names


# http://stats.stackexchange.com/questions/10838/produce-a-list-of-variable-name-in-a-for-loop-then-assign-values-to-them

for(i in 1:3){
assign(paste("a", i, sep = ""), i)
}

x <- as.list(rnorm(10000))
names(x) <- paste("a", 1:length(x), sep = "")
list2env(x , envir = .GlobalEnv)


# http://stackoverflow.com/questions/10369187/use-object-name-in-a-loop

a <- list(A = data.frame(a=runif(2), b=runif(2)),
B = data.frame(a=runif(2), b=runif(2)),
C = data.frame(a=runif(2), b=runif(2)))


# Loop over indices (faster, more cumbersome)
ns <- names(a)
for(i in seq_along(a)) {
v <- a[[i]] # extract value
n <- ns[[i]] # extract name
cat(n, ": \n")
str(v)
}

# Loop over names (easy but slower)
for(n in names(a)) {
v <- a[[n]] # extract value
cat(n, ": \n")
str(v)
}

Monday, March 11, 2013

Setting up a compiler with Maple 17 - Windows7x64

Detailed instructions on setting up a compiler with Maple 17 - Windows7x64.


Downloading Visual Studio Express 2012

Important: Visual Studio Express 2012 is supported only on Windows 7 SP1 and Windows 8. Users with other versions of Windows should install the full .NET Framework 4 with Visual Studio Express 2010.

To install Microsoft Visual Studio Express 2012 for Windows Desktop, do the following:

Go to http://www.microsoft.com/visualstudio/eng/downloads.

Expand the Visual Studio Express 2012 for Windows Desktop section.

Select your download language.

Click Install Now.

 Note: After installation, you must register Visual Studio Express 2012 within 30 days.

 Note that you do not need to install the SDK separately when installing VS 2012. 

You should no longer need to run the setup command in Maple itself. If the compiler is installed it should automatically be detected at startup. If it doesn't get detected, try setting it up with: Compiler:-Setup();

Friday, March 8, 2013

cross-platform parallel download of yahoo data

### Define directories
if(.Platform$OS.type == "windows") {
currentdir <- "c:/R/sp500"
} else {
currentdir <- "~/R/sp500"
}
setwd(currentdir)

### Download S&P 500 Data to R
### Download stock prices of companies included in S&P 500 index.
### Download from finance.yahoo
### sp500.csv contains a list of nearly all 500 companies
### loop to download all the data for every company in the list.


### Parallel implementation
### package doMC not available on Windows

library(quantmod)
library(tseries)
library(timeDate)
library(foreach)
symbols = read.csv("sp500.csv", header = FALSE, stringsAsFactors = FALSE)
nStocks = length(symbols[,1])
dateStart = "1999-12-31"


# doSNOW library also available on LINUX
# efficiency untested

if(.Platform$OS.type == "windows") {

# WINDOWS

library(doSNOW)
cl <- makeCluster(8) # number of CPU cores to be used
registerDoSNOW(cl)
z <- foreach(i = 1:nStocks, .combine = merge.xts) %dopar%
{
cat("Downloading ", i, " out of ", nStocks , "\n")
x <- try(get.hist.quote(instrument = symbols[i,],
start = dateStart,
quote = "AdjClose",
retclass = "zoo",
quiet = TRUE),
TRUE)
colnames(x) <- symbols[i,1]
x <- as.xts(x)
### as.xts(x) is more efficient
}
save(x,file="sp500.RData")
stopCluster(cl)
registerDoSNOW()

} else {

# LINUX

library(doMC)
ncores <- getDoParWorkers() # query number of cores
registerDoMC(cores=ncores) # number of CPU cores to be used
z <- foreach(i = 1:nStocks, .combine = merge.xts) %dopar%
{
cat("Downloading ", i, " out of ", nStocks , "\n")

x <- try(get.hist.quote(instrument = symbols[i,],
start = dateStart,
quote = "AdjClose",
retclass = "zoo",
quiet = TRUE),
TRUE)
colnames(x) <- symbols[i,1]
x <- as.xts(x)
### as.xts(x) is more efficient
}
save(x,file="sp500linux.RData")
registerDoMC()

}

cross-platform parallel download of yahoo data

Monday, October 8, 2012

pdf viewer

I was looking for a super simple pdf viewer.

My current favourite pdf viewer is PDF-XChange viewer, but it's not super
light and I was looking for an alternative.

I wanted to associate the viewer with TeXStudio, my currently favourite
LaTeX editor. TeXStudio has an internal viewer that's pretty good, but I
was looking for something I might like a little more.

The search took a while.

I tried Foxit, Sumatra: too heavy.

mupdf: cannot be configured.

evince: buggy

At last I found what I was looking for: pdflite

Niiiiiiiiiiiiiiiiice!

http://www.pdflite.com/reader